Commit 5a4c56c3 authored by Douwe Maan's avatar Douwe Maan

Reduce method complexity in AutocompleteController

parent 347f1362
class AutocompleteController < ApplicationController class AutocompleteController < ApplicationController
skip_before_action :authenticate_user!, only: [:users] skip_before_action :authenticate_user!, only: [:users]
before_action :find_users, only: [:users]
def users def users
begin
@users =
if params[:project_id].present?
project = Project.find(params[:project_id])
if can?(current_user, :read_project, project)
project.team.users
end
elsif params[:group_id]
group = Group.find(params[:group_id])
if can?(current_user, :read_group, group)
group.users
end
elsif current_user
User.all
end
rescue ActiveRecord::RecordNotFound
if current_user
return render json: {}, status: 404
end
end
if @users.nil? && current_user.nil?
authenticate_user!
end
@users ||= User.none @users ||= User.none
@users = @users.search(params[:search]) if params[:search].present? @users = @users.search(params[:search]) if params[:search].present?
@users = @users.active @users = @users.active
...@@ -49,4 +23,25 @@ class AutocompleteController < ApplicationController ...@@ -49,4 +23,25 @@ class AutocompleteController < ApplicationController
@user = User.find(params[:id]) @user = User.find(params[:id])
render json: @user, only: [:name, :username, :id], methods: [:avatar_url] render json: @user, only: [:name, :username, :id], methods: [:avatar_url]
end end
private
def find_users
@users =
if params[:project_id].present?
project = Project.find(params[:project_id])
return render_404 unless can?(current_user, :read_project, project)
project.team.users
elsif params[:group_id].present?
group = Group.find(params[:group_id])
return render_404 unless can?(current_user, :read_group, group)
group.users
elsif current_user
User.all
else
User.none
end
end
end end
...@@ -114,7 +114,7 @@ describe AutocompleteController do ...@@ -114,7 +114,7 @@ describe AutocompleteController do
get(:users, project_id: project.id) get(:users, project_id: project.id)
end end
it { expect(response.status).to eq(302) } it { expect(response.status).to eq(404) }
end end
describe 'GET #users with unknown project' do describe 'GET #users with unknown project' do
...@@ -122,7 +122,7 @@ describe AutocompleteController do ...@@ -122,7 +122,7 @@ describe AutocompleteController do
get(:users, project_id: 'unknown') get(:users, project_id: 'unknown')
end end
it { expect(response.status).to eq(302) } it { expect(response.status).to eq(404) }
end end
describe 'GET #users with inaccessible group' do describe 'GET #users with inaccessible group' do
...@@ -131,7 +131,7 @@ describe AutocompleteController do ...@@ -131,7 +131,7 @@ describe AutocompleteController do
get(:users, group_id: user.namespace.id) get(:users, group_id: user.namespace.id)
end end
it { expect(response.status).to eq(302) } it { expect(response.status).to eq(404) }
end end
describe 'GET #users with no project' do describe 'GET #users with no project' do
...@@ -139,7 +139,8 @@ describe AutocompleteController do ...@@ -139,7 +139,8 @@ describe AutocompleteController do
get(:users) get(:users)
end end
it { expect(response.status).to eq(302) } it { expect(body).to be_kind_of(Array) }
it { expect(body.size).to eq 0 }
end end
end end
end end
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