diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-19 10:33:21 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-05-19 10:33:21 +0300 |
commit | 36a59d088eca61b834191dacea009677a96c052f (patch) | |
tree | e4f33972dab5d8ef79e3944a9f403035fceea43f /spec/support/shared_examples/models/member_shared_examples.rb | |
parent | a1761f15ec2cae7c7f7bbda39a75494add0dfd6f (diff) |
Add latest changes from gitlab-org/gitlab@15-0-stable-eev15.0.0-rc42
Diffstat (limited to 'spec/support/shared_examples/models/member_shared_examples.rb')
-rw-r--r-- | spec/support/shared_examples/models/member_shared_examples.rb | 403 |
1 files changed, 200 insertions, 203 deletions
diff --git a/spec/support/shared_examples/models/member_shared_examples.rb b/spec/support/shared_examples/models/member_shared_examples.rb index a329a6dca91..e293d10964b 100644 --- a/spec/support/shared_examples/models/member_shared_examples.rb +++ b/spec/support/shared_examples/models/member_shared_examples.rb @@ -77,312 +77,309 @@ RSpec.shared_examples '#valid_level_roles' do |entity_name| end RSpec.shared_examples_for "member creation" do - let_it_be(:user) { create(:user) } let_it_be(:admin) { create(:admin) } - describe '#execute' do - it 'returns a Member object', :aggregate_failures do - member = described_class.new(source, user, :maintainer).execute - - expect(member).to be_a member_type - expect(member).to be_persisted - end + it 'returns a Member object', :aggregate_failures do + member = described_class.new(source, user, :maintainer).execute - context 'when adding a project_bot' do - let_it_be(:project_bot) { create(:user, :project_bot) } - - before_all do - source.add_owner(user) - end + expect(member).to be_a member_type + expect(member).to be_persisted + end - context 'when project_bot is already a member' do - before do - source.add_developer(project_bot) - end + context 'when adding a project_bot' do + let_it_be(:project_bot) { create(:user, :project_bot) } - it 'does not update the member' do - member = described_class.new(source, project_bot, :maintainer, current_user: user).execute + before_all do + source.add_owner(user) + end - expect(source.users.reload).to include(project_bot) - expect(member).to be_persisted - expect(member.access_level).to eq(Gitlab::Access::DEVELOPER) - expect(member.errors.full_messages).to include(/not authorized to update member/) - end + context 'when project_bot is already a member' do + before do + source.add_developer(project_bot) end - context 'when project_bot is not already a member' do - it 'adds the member' do - member = described_class.new(source, project_bot, :maintainer, current_user: user).execute + it 'does not update the member' do + member = described_class.new(source, project_bot, :maintainer, current_user: user).execute - expect(source.users.reload).to include(project_bot) - expect(member).to be_persisted - end + expect(source.users.reload).to include(project_bot) + expect(member).to be_persisted + expect(member.access_level).to eq(Gitlab::Access::DEVELOPER) + expect(member.errors.full_messages).to include(/not authorized to update member/) end end - context 'when admin mode is enabled', :enable_admin_mode, :aggregate_failures do - it 'sets members.created_by to the given admin current_user' do - member = described_class.new(source, user, :maintainer, current_user: admin).execute + context 'when project_bot is not already a member' do + it 'adds the member' do + member = described_class.new(source, project_bot, :maintainer, current_user: user).execute + expect(source.users.reload).to include(project_bot) expect(member).to be_persisted - expect(source.users.reload).to include(user) - expect(member.created_by).to eq(admin) end end + end - context 'when admin mode is disabled' do - it 'rejects setting members.created_by to the given admin current_user', :aggregate_failures do - member = described_class.new(source, user, :maintainer, current_user: admin).execute + context 'when admin mode is enabled', :enable_admin_mode, :aggregate_failures do + it 'sets members.created_by to the given admin current_user' do + member = described_class.new(source, user, :maintainer, current_user: admin).execute - expect(member).not_to be_persisted - expect(source.users.reload).not_to include(user) - expect(member.errors.full_messages).to include(/not authorized to create member/) - end + expect(member).to be_persisted + expect(source.users.reload).to include(user) + expect(member.created_by).to eq(admin) end + end - it 'sets members.expires_at to the given expires_at' do - member = described_class.new(source, user, :maintainer, expires_at: Date.new(2016, 9, 22)).execute + context 'when admin mode is disabled' do + it 'rejects setting members.created_by to the given admin current_user', :aggregate_failures do + member = described_class.new(source, user, :maintainer, current_user: admin).execute - expect(member.expires_at).to eq(Date.new(2016, 9, 22)) + expect(member).not_to be_persisted + expect(source.users.reload).not_to include(user) + expect(member.errors.full_messages).to include(/not authorized to create member/) end + end - described_class.access_levels.each do |sym_key, int_access_level| - it "accepts the :#{sym_key} symbol as access level", :aggregate_failures do - expect(source.users).not_to include(user) + it 'sets members.expires_at to the given expires_at' do + member = described_class.new(source, user, :maintainer, expires_at: Date.new(2016, 9, 22)).execute - member = described_class.new(source, user.id, sym_key).execute + expect(member.expires_at).to eq(Date.new(2016, 9, 22)) + end - expect(member.access_level).to eq(int_access_level) - expect(source.users.reload).to include(user) - end + described_class.access_levels.each do |sym_key, int_access_level| + it "accepts the :#{sym_key} symbol as access level", :aggregate_failures do + expect(source.users).not_to include(user) + + member = described_class.new(source, user.id, sym_key).execute - it "accepts the #{int_access_level} integer as access level", :aggregate_failures do + expect(member.access_level).to eq(int_access_level) + expect(source.users.reload).to include(user) + end + + it "accepts the #{int_access_level} integer as access level", :aggregate_failures do + expect(source.users).not_to include(user) + + member = described_class.new(source, user.id, int_access_level).execute + + expect(member.access_level).to eq(int_access_level) + expect(source.users.reload).to include(user) + end + end + + context 'with no current_user' do + context 'when called with a known user id' do + it 'adds the user as a member' do expect(source.users).not_to include(user) - member = described_class.new(source, user.id, int_access_level).execute + described_class.new(source, user.id, :maintainer).execute - expect(member.access_level).to eq(int_access_level) expect(source.users.reload).to include(user) end end - context 'with no current_user' do - context 'when called with a known user id' do - it 'adds the user as a member' do - expect(source.users).not_to include(user) + context 'when called with an unknown user id' do + it 'does not add the user as a member' do + expect(source.users).not_to include(user) - described_class.new(source, user.id, :maintainer).execute + described_class.new(source, non_existing_record_id, :maintainer).execute - expect(source.users.reload).to include(user) - end + expect(source.users.reload).not_to include(user) end + end - context 'when called with an unknown user id' do - it 'does not add the user as a member' do - expect(source.users).not_to include(user) + context 'when called with a user object' do + it 'adds the user as a member' do + expect(source.users).not_to include(user) - described_class.new(source, non_existing_record_id, :maintainer).execute + described_class.new(source, user, :maintainer).execute - expect(source.users.reload).not_to include(user) - end + expect(source.users.reload).to include(user) + end + end + + context 'when called with a requester user object' do + before do + source.request_access(user) end - context 'when called with a user object' do - it 'adds the user as a member' do - expect(source.users).not_to include(user) + it 'adds the requester as a member', :aggregate_failures do + expect(source.users).not_to include(user) + expect(source.requesters.exists?(user_id: user)).to be_truthy + expect do described_class.new(source, user, :maintainer).execute + end.to raise_error(Gitlab::Access::AccessDeniedError) - expect(source.users.reload).to include(user) - end + expect(source.users.reload).not_to include(user) + expect(source.requesters.reload.exists?(user_id: user)).to be_truthy end + end - context 'when called with a requester user object' do - before do - source.request_access(user) - end - - it 'adds the requester as a member', :aggregate_failures do - expect(source.users).not_to include(user) - expect(source.requesters.exists?(user_id: user)).to be_truthy + context 'when called with a known user email' do + it 'adds the user as a member' do + expect(source.users).not_to include(user) - expect do - described_class.new(source, user, :maintainer).execute - end.to raise_error(Gitlab::Access::AccessDeniedError) + described_class.new(source, user.email, :maintainer).execute - expect(source.users.reload).not_to include(user) - expect(source.requesters.reload.exists?(user_id: user)).to be_truthy - end + expect(source.users.reload).to include(user) end + end - context 'when called with a known user email' do - it 'adds the user as a member' do - expect(source.users).not_to include(user) + context 'when called with an unknown user email' do + it 'creates an invited member' do + expect(source.users).not_to include(user) - described_class.new(source, user.email, :maintainer).execute + described_class.new(source, 'user@example.com', :maintainer).execute - expect(source.users.reload).to include(user) - end + expect(source.members.invite.pluck(:invite_email)).to include('user@example.com') end + end - context 'when called with an unknown user email' do - it 'creates an invited member' do - expect(source.users).not_to include(user) + context 'when called with an unknown user email starting with a number' do + it 'creates an invited member', :aggregate_failures do + email_starting_with_number = "#{user.id}_email@example.com" - described_class.new(source, 'user@example.com', :maintainer).execute + described_class.new(source, email_starting_with_number, :maintainer).execute - expect(source.members.invite.pluck(:invite_email)).to include('user@example.com') - end + expect(source.members.invite.pluck(:invite_email)).to include(email_starting_with_number) + expect(source.users.reload).not_to include(user) end + end + end - context 'when called with an unknown user email starting with a number' do - it 'creates an invited member', :aggregate_failures do - email_starting_with_number = "#{user.id}_email@example.com" + context 'when current_user can update member', :enable_admin_mode do + it 'creates the member' do + expect(source.users).not_to include(user) - described_class.new(source, email_starting_with_number, :maintainer).execute + described_class.new(source, user, :maintainer, current_user: admin).execute - expect(source.members.invite.pluck(:invite_email)).to include(email_starting_with_number) - expect(source.users.reload).not_to include(user) - end - end + expect(source.users.reload).to include(user) end - context 'when current_user can update member', :enable_admin_mode do - it 'creates the member' do + context 'when called with a requester user object' do + before do + source.request_access(user) + end + + it 'adds the requester as a member', :aggregate_failures do expect(source.users).not_to include(user) + expect(source.requesters.exists?(user_id: user)).to be_truthy described_class.new(source, user, :maintainer, current_user: admin).execute expect(source.users.reload).to include(user) + expect(source.requesters.reload.exists?(user_id: user)).to be_falsy end + end + end - context 'when called with a requester user object' do - before do - source.request_access(user) - end + context 'when current_user cannot update member' do + it 'does not create the member', :aggregate_failures do + expect(source.users).not_to include(user) - it 'adds the requester as a member', :aggregate_failures do - expect(source.users).not_to include(user) - expect(source.requesters.exists?(user_id: user)).to be_truthy + member = described_class.new(source, user, :maintainer, current_user: user).execute - described_class.new(source, user, :maintainer, current_user: admin).execute + expect(source.users.reload).not_to include(user) + expect(member).not_to be_persisted + end - expect(source.users.reload).to include(user) - expect(source.requesters.reload.exists?(user_id: user)).to be_falsy - end + context 'when called with a requester user object' do + before do + source.request_access(user) end - end - context 'when current_user cannot update member' do - it 'does not create the member', :aggregate_failures do + it 'does not destroy the requester', :aggregate_failures do expect(source.users).not_to include(user) + expect(source.requesters.exists?(user_id: user)).to be_truthy - member = described_class.new(source, user, :maintainer, current_user: user).execute + described_class.new(source, user, :maintainer, current_user: user).execute expect(source.users.reload).not_to include(user) - expect(member).not_to be_persisted + expect(source.requesters.exists?(user_id: user)).to be_truthy end + end + end - context 'when called with a requester user object' do - before do - source.request_access(user) - end + context 'when member already exists' do + before do + source.add_user(user, :developer) + end - it 'does not destroy the requester', :aggregate_failures do - expect(source.users).not_to include(user) - expect(source.requesters.exists?(user_id: user)).to be_truthy + context 'with no current_user' do + it 'updates the member' do + expect(source.users).to include(user) - described_class.new(source, user, :maintainer, current_user: user).execute + described_class.new(source, user, :maintainer).execute - expect(source.users.reload).not_to include(user) - expect(source.requesters.exists?(user_id: user)).to be_truthy - end + expect(source.members.find_by(user_id: user).access_level).to eq(Gitlab::Access::MAINTAINER) end end - context 'when member already exists' do - before do - source.add_user(user, :developer) - end - - context 'with no current_user' do - it 'updates the member' do - expect(source.users).to include(user) + context 'when current_user can update member', :enable_admin_mode do + it 'updates the member' do + expect(source.users).to include(user) - described_class.new(source, user, :maintainer).execute + described_class.new(source, user, :maintainer, current_user: admin).execute - expect(source.members.find_by(user_id: user).access_level).to eq(Gitlab::Access::MAINTAINER) - end + expect(source.members.find_by(user_id: user).access_level).to eq(Gitlab::Access::MAINTAINER) end + end - context 'when current_user can update member', :enable_admin_mode do - it 'updates the member' do - expect(source.users).to include(user) + context 'when current_user cannot update member' do + it 'does not update the member' do + expect(source.users).to include(user) - described_class.new(source, user, :maintainer, current_user: admin).execute + described_class.new(source, user, :maintainer, current_user: user).execute - expect(source.members.find_by(user_id: user).access_level).to eq(Gitlab::Access::MAINTAINER) - end + expect(source.members.find_by(user_id: user).access_level).to eq(Gitlab::Access::DEVELOPER) end + end + end - context 'when current_user cannot update member' do - it 'does not update the member' do - expect(source.users).to include(user) + context 'when `tasks_to_be_done` and `tasks_project_id` are passed' do + let(:task_project) { source.is_a?(Group) ? create(:project, group: source) : source } - described_class.new(source, user, :maintainer, current_user: user).execute + it 'creates a member_task with the correct attributes', :aggregate_failures do + described_class.new(source, user, :developer, tasks_to_be_done: %w(ci code), tasks_project_id: task_project.id).execute - expect(source.members.find_by(user_id: user).access_level).to eq(Gitlab::Access::DEVELOPER) - end - end - end + member = source.members.last - context 'when `tasks_to_be_done` and `tasks_project_id` are passed' do - let(:task_project) { source.is_a?(Group) ? create(:project, group: source) : source } + expect(member.tasks_to_be_done).to match_array([:ci, :code]) + expect(member.member_task.project).to eq(task_project) + end - it 'creates a member_task with the correct attributes', :aggregate_failures do - described_class.new(source, user, :developer, tasks_to_be_done: %w(ci code), tasks_project_id: task_project.id).execute + context 'with an already existing member' do + before do + source.add_user(user, :developer) + end - member = source.members.last + it 'does not update tasks to be done if tasks already exist', :aggregate_failures do + member = source.members.find_by(user_id: user.id) + create(:member_task, member: member, project: task_project, tasks_to_be_done: %w(code ci)) - expect(member.tasks_to_be_done).to match_array([:ci, :code]) + expect do + described_class.new(source, + user, + :developer, + tasks_to_be_done: %w(issues), + tasks_project_id: task_project.id).execute + end.not_to change(MemberTask, :count) + + member.reset + expect(member.tasks_to_be_done).to match_array([:code, :ci]) expect(member.member_task.project).to eq(task_project) end - context 'with an already existing member' do - before do - source.add_user(user, :developer) - end - - it 'does not update tasks to be done if tasks already exist', :aggregate_failures do - member = source.members.find_by(user_id: user.id) - create(:member_task, member: member, project: task_project, tasks_to_be_done: %w(code ci)) - - expect do - described_class.new(source, - user, - :developer, - tasks_to_be_done: %w(issues), - tasks_project_id: task_project.id).execute - end.not_to change(MemberTask, :count) - - member.reset - expect(member.tasks_to_be_done).to match_array([:code, :ci]) - expect(member.member_task.project).to eq(task_project) - end - - it 'adds tasks to be done if they do not exist', :aggregate_failures do - expect do - described_class.new(source, - user, - :developer, - tasks_to_be_done: %w(issues), - tasks_project_id: task_project.id).execute - end.to change(MemberTask, :count).by(1) - - member = source.members.find_by(user_id: user.id) - expect(member.tasks_to_be_done).to match_array([:issues]) - expect(member.member_task.project).to eq(task_project) - end + it 'adds tasks to be done if they do not exist', :aggregate_failures do + expect do + described_class.new(source, + user, + :developer, + tasks_to_be_done: %w(issues), + tasks_project_id: task_project.id).execute + end.to change(MemberTask, :count).by(1) + + member = source.members.find_by(user_id: user.id) + expect(member.tasks_to_be_done).to match_array([:issues]) + expect(member.member_task.project).to eq(task_project) end end end |