Commit f64bf654 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'username_not_exists_with_unique_suggestions' into 'master'

Add API for suggesting unique alternatives if username exists

See merge request gitlab-org/gitlab!19303
parents 91e1e5cb b2277c71
...@@ -16,7 +16,7 @@ class UsersController < ApplicationController ...@@ -16,7 +16,7 @@ class UsersController < ApplicationController
skip_before_action :authenticate_user! skip_before_action :authenticate_user!
prepend_before_action(only: [:show]) { authenticate_sessionless_user!(:rss) } prepend_before_action(only: [:show]) { authenticate_sessionless_user!(:rss) }
before_action :user, except: [:exists] before_action :user, except: [:exists, :suggests]
before_action :authorize_read_user_profile!, before_action :authorize_read_user_profile!,
only: [:calendar, :calendar_activities, :groups, :projects, :contributed_projects, :starred_projects, :snippets] only: [:calendar, :calendar_activities, :groups, :projects, :contributed_projects, :starred_projects, :snippets]
...@@ -114,6 +114,14 @@ class UsersController < ApplicationController ...@@ -114,6 +114,14 @@ class UsersController < ApplicationController
render json: { exists: !!Namespace.find_by_path_or_name(params[:username]) } render json: { exists: !!Namespace.find_by_path_or_name(params[:username]) }
end end
def suggests
namespace_path = params[:username]
exists = !!Namespace.find_by_path_or_name(namespace_path)
suggestions = exists ? [Namespace.clean_path(namespace_path)] : []
render json: { exists: exists, suggests: suggestions }
end
private private
def user def user
......
...@@ -55,6 +55,7 @@ scope(constraints: { username: Gitlab::PathRegex.root_namespace_route_regex }) d ...@@ -55,6 +55,7 @@ scope(constraints: { username: Gitlab::PathRegex.root_namespace_route_regex }) d
get :starred, as: :starred_projects get :starred, as: :starred_projects
get :snippets get :snippets
get :exists get :exists
get :suggests
get :activity get :activity
get '/', to: redirect('%{username}'), as: nil get '/', to: redirect('%{username}'), as: nil
end end
......
...@@ -348,6 +348,48 @@ describe UsersController do ...@@ -348,6 +348,48 @@ describe UsersController do
end end
end end
describe 'GET #suggests' do
context 'when user exists' do
it 'returns JSON indicating the user exists and a suggestion' do
get :suggests, params: { username: user.username }
expected_json = { exists: true, suggests: ["#{user.username}1"] }.to_json
expect(response.body).to eq(expected_json)
end
context 'when the casing is different' do
let(:user) { create(:user, username: 'CamelCaseUser') }
it 'returns JSON indicating the user exists and a suggestion' do
get :suggests, params: { username: user.username.downcase }
expected_json = { exists: true, suggests: ["#{user.username.downcase}1"] }.to_json
expect(response.body).to eq(expected_json)
end
end
end
context 'when the user does not exist' do
it 'returns JSON indicating the user does not exist' do
get :suggests, params: { username: 'foo' }
expected_json = { exists: false, suggests: [] }.to_json
expect(response.body).to eq(expected_json)
end
context 'when a user changed their username' do
let(:redirect_route) { user.namespace.redirect_routes.create(path: 'old-username') }
it 'returns JSON indicating a user by that username does not exist' do
get :suggests, params: { username: 'old-username' }
expected_json = { exists: false, suggests: [] }.to_json
expect(response.body).to eq(expected_json)
end
end
end
end
describe '#ensure_canonical_path' do describe '#ensure_canonical_path' do
before do before do
sign_in(user) sign_in(user)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment