Commit 51f30396 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'refactor_group_members' into 'master'

Implement CreateProjectMembers service to make controller thin

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/23566

See merge request !7170
parents 957308af 92167778
...@@ -25,18 +25,15 @@ class Projects::ProjectMembersController < Projects::ApplicationController ...@@ -25,18 +25,15 @@ class Projects::ProjectMembersController < Projects::ApplicationController
end end
def create def create
if params[:user_ids].blank? status = Members::CreateService.new(@project, current_user, params).execute
return redirect_to(namespace_project_project_members_path(@project.namespace, @project), alert: 'No users or groups specified.')
end
@project.team.add_users( redirect_url = namespace_project_project_members_path(@project.namespace, @project)
params[:user_ids].split(','),
params[:access_level],
expires_at: params[:expires_at],
current_user: current_user
)
redirect_to namespace_project_project_members_path(@project.namespace, @project), notice: 'Users were successfully added.' if status
redirect_to redirect_url, notice: 'Users were successfully added.'
else
redirect_to redirect_url, alert: 'No users or groups specified.'
end
end end
def update def update
......
module Members
class CreateService < BaseService
def execute
return false if params[:user_ids].blank?
project.team.add_users(
params[:user_ids].split(','),
params[:access_level],
expires_at: params[:expires_at],
current_user: current_user
)
true
end
end
end
...@@ -14,7 +14,6 @@ describe Projects::ProjectMembersController do ...@@ -14,7 +14,6 @@ describe Projects::ProjectMembersController do
end end
describe 'POST create' do describe 'POST create' do
context 'when users are added' do
let(:project_user) { create(:user) } let(:project_user) { create(:user) }
before { sign_in(user) } before { sign_in(user) }
...@@ -37,6 +36,8 @@ describe Projects::ProjectMembersController do ...@@ -37,6 +36,8 @@ describe Projects::ProjectMembersController do
before { project.team << [user, :master] } before { project.team << [user, :master] }
it 'adds user to members' do it 'adds user to members' do
expect_any_instance_of(Members::CreateService).to receive(:execute).and_return(true)
post :create, namespace_id: project.namespace, post :create, namespace_id: project.namespace,
project_id: project, project_id: project,
user_ids: project_user.id, user_ids: project_user.id,
...@@ -44,10 +45,11 @@ describe Projects::ProjectMembersController do ...@@ -44,10 +45,11 @@ describe Projects::ProjectMembersController do
expect(response).to set_flash.to 'Users were successfully added.' expect(response).to set_flash.to 'Users were successfully added.'
expect(response).to redirect_to(namespace_project_project_members_path(project.namespace, project)) expect(response).to redirect_to(namespace_project_project_members_path(project.namespace, project))
expect(project.users).to include project_user
end end
it 'adds no user to members' do it 'adds no user to members' do
expect_any_instance_of(Members::CreateService).to receive(:execute).and_return(false)
post :create, namespace_id: project.namespace, post :create, namespace_id: project.namespace,
project_id: project, project_id: project,
user_ids: '', user_ids: '',
...@@ -55,8 +57,6 @@ describe Projects::ProjectMembersController do ...@@ -55,8 +57,6 @@ describe Projects::ProjectMembersController do
expect(response).to set_flash.to 'No users or groups specified.' expect(response).to set_flash.to 'No users or groups specified.'
expect(response).to redirect_to(namespace_project_project_members_path(project.namespace, project)) expect(response).to redirect_to(namespace_project_project_members_path(project.namespace, project))
expect(project.users).not_to include project_user
end
end end
end end
end end
......
require 'spec_helper'
describe Members::CreateService, services: true do
let(:project) { create(:empty_project) }
let(:user) { create(:user) }
let(:project_user) { create(:user) }
before { project.team << [user, :master] }
it 'adds user to members' do
params = { user_ids: project_user.id.to_s, access_level: Gitlab::Access::GUEST }
result = described_class.new(project, user, params).execute
expect(result).to be_truthy
expect(project.users).to include project_user
end
it 'adds no user to members' do
params = { user_ids: '', access_level: Gitlab::Access::GUEST }
result = described_class.new(project, user, params).execute
expect(result).to be_falsey
expect(project.users).not_to include project_user
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