debian-mirror-gitlab/spec/controllers/projects/project_members_controller_spec.rb

404 lines
12 KiB
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
require('spec_helper')
describe Projects::ProjectMembersController do
2017-08-17 22:00:37 +05:30
let(:user) { create(:user) }
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, :public, :access_requestable) }
2017-08-17 22:00:37 +05:30
describe 'GET index' do
2017-09-10 17:25:29 +05:30
it 'should have the project_members address with a 200 status code' do
2019-02-15 15:39:39 +05:30
get :index, params: { namespace_id: project.namespace, project_id: project }
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(200)
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
end
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
describe 'POST create' do
let(:project_user) { create(:user) }
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
before do
sign_in(user)
end
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
context 'when user does not have enough rights' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(user)
2017-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
it 'returns 404' do
2019-02-15 15:39:39 +05:30
post :create, params: {
namespace_id: project.namespace,
project_id: project,
user_ids: project_user.id,
access_level: Gitlab::Access::GUEST
}
2016-06-02 11:05:42 +05:30
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
2017-08-17 22:00:37 +05:30
expect(project.users).not_to include project_user
2016-06-02 11:05:42 +05:30
end
end
2017-08-17 22:00:37 +05:30
context 'when user has enough rights' do
2017-09-10 17:25:29 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it 'adds user to members' do
2017-09-10 17:25:29 +05:30
expect_any_instance_of(Members::CreateService).to receive(:execute).and_return(status: :success)
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
post :create, params: {
namespace_id: project.namespace,
project_id: project,
user_ids: project_user.id,
access_level: Gitlab::Access::GUEST
}
2017-08-17 22:00:37 +05:30
expect(response).to set_flash.to 'Users were successfully added.'
2017-09-10 17:25:29 +05:30
expect(response).to redirect_to(project_project_members_path(project))
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
it 'adds no user to members' do
2017-09-10 17:25:29 +05:30
expect_any_instance_of(Members::CreateService).to receive(:execute).and_return(status: :failure, message: 'Message')
2017-08-17 22:00:37 +05:30
2019-02-15 15:39:39 +05:30
post :create, params: {
namespace_id: project.namespace,
project_id: project,
user_ids: '',
access_level: Gitlab::Access::GUEST
}
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expect(response).to set_flash.to 'Message'
expect(response).to redirect_to(project_project_members_path(project))
2017-08-17 22:00:37 +05:30
end
2016-06-02 11:05:42 +05:30
end
end
2018-03-17 18:26:18 +05:30
describe 'PUT update' do
let(:requester) { create(:project_member, :access_request, project: project) }
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2018-03-17 18:26:18 +05:30
sign_in(user)
end
Gitlab::Access.options.each do |label, value|
it "can change the access level to #{label}" do
2019-02-15 15:39:39 +05:30
put :update, params: {
project_member: { access_level: value },
namespace_id: project.namespace,
project_id: project,
id: requester
}, xhr: true
2018-03-17 18:26:18 +05:30
expect(requester.reload.human_access).to eq(label)
end
end
end
2017-08-17 22:00:37 +05:30
describe 'DELETE destroy' do
let(:member) { create(:project_member, :developer, project: project) }
2017-09-10 17:25:29 +05:30
before do
sign_in(user)
end
context 'when member is not found' do
it 'returns 404' do
2019-02-15 15:39:39 +05:30
delete :destroy, params: {
namespace_id: project.namespace,
project_id: project,
id: 42
}
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
end
end
context 'when member is found' do
context 'when user does not have enough rights' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(user)
2017-09-10 17:25:29 +05:30
end
it 'returns 404' do
2019-02-15 15:39:39 +05:30
delete :destroy, params: {
namespace_id: project.namespace,
project_id: project,
id: member
}
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
2017-08-17 22:00:37 +05:30
expect(project.members).to include member
end
end
context 'when user has enough rights' do
2017-09-10 17:25:29 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2017-09-10 17:25:29 +05:30
end
it '[HTML] removes user from members' do
2019-02-15 15:39:39 +05:30
delete :destroy, params: {
namespace_id: project.namespace,
project_id: project,
id: member
}
expect(response).to redirect_to(
2017-09-10 17:25:29 +05:30
project_project_members_path(project)
)
2017-08-17 22:00:37 +05:30
expect(project.members).not_to include member
end
it '[JS] removes user from members' do
2019-02-15 15:39:39 +05:30
delete :destroy, params: {
namespace_id: project.namespace,
project_id: project,
id: member
}, xhr: true
expect(response).to be_success
2017-08-17 22:00:37 +05:30
expect(project.members).not_to include member
end
end
end
end
2017-08-17 22:00:37 +05:30
describe 'DELETE leave' do
2017-09-10 17:25:29 +05:30
before do
sign_in(user)
end
context 'when member is not found' do
2016-11-03 12:29:30 +05:30
it 'returns 404' do
2019-02-15 15:39:39 +05:30
delete :leave, params: {
namespace_id: project.namespace,
project_id: project
}
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
end
end
context 'when member is found' do
context 'and is not an owner' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(user)
2017-09-10 17:25:29 +05:30
end
it 'removes user from members' do
2019-02-15 15:39:39 +05:30
delete :leave, params: {
namespace_id: project.namespace,
project_id: project
}
expect(response).to set_flash.to "You left the \"#{project.human_name}\" project."
expect(response).to redirect_to(dashboard_projects_path)
expect(project.users).not_to include user
end
end
context 'and is an owner' do
2017-09-10 17:25:29 +05:30
let(:project) { create(:project, namespace: user.namespace) }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2017-09-10 17:25:29 +05:30
end
2018-11-08 19:23:39 +05:30
it 'cannot remove themselves from the project' do
2019-02-15 15:39:39 +05:30
delete :leave, params: {
namespace_id: project.namespace,
project_id: project
}
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(403)
end
end
context 'and is a requester' do
2017-09-10 17:25:29 +05:30
before do
project.request_access(user)
end
it 'removes user from members' do
2019-02-15 15:39:39 +05:30
delete :leave, params: {
namespace_id: project.namespace,
project_id: project
}
expect(response).to set_flash.to 'Your access request to the project has been withdrawn.'
2017-09-10 17:25:29 +05:30
expect(response).to redirect_to(project_path(project))
2016-08-24 12:49:21 +05:30
expect(project.requesters).to be_empty
expect(project.users).not_to include user
end
end
end
end
2017-08-17 22:00:37 +05:30
describe 'POST request_access' do
2017-09-10 17:25:29 +05:30
before do
sign_in(user)
end
it 'creates a new ProjectMember that is not a team member' do
2019-02-15 15:39:39 +05:30
post :request_access, params: {
namespace_id: project.namespace,
project_id: project
}
expect(response).to set_flash.to 'Your request for access has been queued for review.'
expect(response).to redirect_to(
2017-09-10 17:25:29 +05:30
project_path(project)
)
2016-08-24 12:49:21 +05:30
expect(project.requesters.exists?(user_id: user)).to be_truthy
expect(project.users).not_to include user
end
end
2017-08-17 22:00:37 +05:30
describe 'POST approve' do
let(:member) { create(:project_member, :access_request, project: project) }
2017-09-10 17:25:29 +05:30
before do
sign_in(user)
end
context 'when member is not found' do
it 'returns 404' do
2019-02-15 15:39:39 +05:30
post :approve_access_request, params: {
namespace_id: project.namespace,
project_id: project,
id: 42
}
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
end
end
context 'when member is found' do
context 'when user does not have enough rights' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
project.add_developer(user)
2017-09-10 17:25:29 +05:30
end
it 'returns 404' do
2019-02-15 15:39:39 +05:30
post :approve_access_request, params: {
namespace_id: project.namespace,
project_id: project,
id: member
}
2018-03-17 18:26:18 +05:30
expect(response).to have_gitlab_http_status(404)
2017-08-17 22:00:37 +05:30
expect(project.members).not_to include member
end
end
context 'when user has enough rights' do
2017-09-10 17:25:29 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2017-09-10 17:25:29 +05:30
end
it 'adds user to members' do
2019-02-15 15:39:39 +05:30
post :approve_access_request, params: {
namespace_id: project.namespace,
project_id: project,
id: member
}
expect(response).to redirect_to(
2017-09-10 17:25:29 +05:30
project_project_members_path(project)
)
2017-08-17 22:00:37 +05:30
expect(project.members).to include member
end
end
end
end
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
describe 'POST apply_import' do
2017-09-10 17:25:29 +05:30
let(:another_project) { create(:project, :private) }
2017-08-17 22:00:37 +05:30
let(:member) { create(:user) }
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2018-03-17 18:26:18 +05:30
another_project.add_guest(member)
2017-08-17 22:00:37 +05:30
sign_in(user)
end
shared_context 'import applied' do
before do
2019-02-15 15:39:39 +05:30
post(:apply_import, params: {
namespace_id: project.namespace,
project_id: project,
source_project_id: another_project.id
})
2017-08-17 22:00:37 +05:30
end
end
context 'when user can access source project members' do
2017-09-10 17:25:29 +05:30
before do
2018-03-17 18:26:18 +05:30
another_project.add_guest(user)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
include_context 'import applied'
it 'imports source project members' do
expect(project.team_members).to include member
expect(response).to set_flash.to 'Successfully imported'
expect(response).to redirect_to(
2017-09-10 17:25:29 +05:30
project_project_members_path(project)
2017-08-17 22:00:37 +05:30
)
end
end
context 'when user is not member of a source project' do
include_context 'import applied'
it 'does not import team members' do
expect(project.team_members).not_to include member
end
it 'responds with not found' do
expect(response.status).to eq 404
end
end
end
2016-11-03 12:29:30 +05:30
describe 'POST create' do
let(:stranger) { create(:user) }
context 'when creating owner' do
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2016-11-03 12:29:30 +05:30
sign_in(user)
end
it 'does not create a member' do
expect do
2019-02-15 15:39:39 +05:30
post :create, params: {
user_ids: stranger.id,
namespace_id: project.namespace,
access_level: Member::OWNER,
project_id: project
}
2016-11-03 12:29:30 +05:30
end.to change { project.members.count }.by(0)
end
end
2018-11-18 11:00:15 +05:30
context 'when create maintainer' do
2016-11-03 12:29:30 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2016-11-03 12:29:30 +05:30
sign_in(user)
end
it 'creates a member' do
expect do
2019-02-15 15:39:39 +05:30
post :create, params: {
user_ids: stranger.id,
namespace_id: project.namespace,
access_level: Member::MAINTAINER,
project_id: project
}
2016-11-03 12:29:30 +05:30
end.to change { project.members.count }.by(1)
end
end
end
2016-06-02 11:05:42 +05:30
end