2022-08-27 11:52:29 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
RSpec.describe MemberRole, feature_category: :authentication_and_authorization do
|
2022-08-27 11:52:29 +05:30
|
|
|
describe 'associations' do
|
|
|
|
it { is_expected.to belong_to(:namespace) }
|
|
|
|
it { is_expected.to have_many(:members) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validation' do
|
2023-03-04 22:38:38 +05:30
|
|
|
subject(:member_role) { build(:member_role) }
|
2022-08-27 11:52:29 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it { is_expected.to validate_presence_of(:namespace) }
|
2022-08-27 11:52:29 +05:30
|
|
|
it { is_expected.to validate_presence_of(:base_access_level) }
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
context 'for attributes_locked_after_member_associated' do
|
|
|
|
context 'when assigned to member' do
|
|
|
|
it 'cannot be changed' do
|
|
|
|
member_role.save!
|
|
|
|
member_role.members << create(:project_member)
|
|
|
|
|
|
|
|
expect(member_role).not_to be_valid
|
|
|
|
expect(member_role.errors.messages[:base]).to include(
|
|
|
|
s_("MemberRole|cannot be changed because it is already assigned to a user. "\
|
|
|
|
"Please create a new Member Role instead")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not assigned to member' do
|
|
|
|
it 'can be changed' do
|
|
|
|
expect(member_role).to be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
context 'when for namespace' do
|
2022-11-25 23:54:43 +05:30
|
|
|
let_it_be(:root_group) { create(:group) }
|
|
|
|
|
|
|
|
context 'when namespace is a subgroup' do
|
|
|
|
it 'is invalid' do
|
|
|
|
subgroup = create(:group, parent: root_group)
|
2023-03-04 22:38:38 +05:30
|
|
|
member_role.namespace = subgroup
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(member_role).to be_invalid
|
|
|
|
expect(member_role.errors[:namespace]).to include(
|
|
|
|
s_("MemberRole|must be top-level namespace")
|
|
|
|
)
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when namespace is a root group' do
|
|
|
|
it 'is valid' do
|
2023-03-04 22:38:38 +05:30
|
|
|
member_role.namespace = root_group
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(member_role).to be_valid
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when namespace is not present' do
|
|
|
|
it 'is invalid with a different error message' do
|
2023-03-04 22:38:38 +05:30
|
|
|
member_role.namespace = nil
|
|
|
|
|
|
|
|
expect(member_role).to be_invalid
|
|
|
|
expect(member_role.errors[:namespace]).to include(_("can't be blank"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when namespace is outside hierarchy of member' do
|
|
|
|
it 'creates a validation error' do
|
|
|
|
member_role.save!
|
|
|
|
member_role.namespace = create(:group)
|
2022-11-25 23:54:43 +05:30
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(member_role).not_to be_valid
|
|
|
|
expect(member_role.errors[:namespace]).to include(s_("MemberRole|can't be changed"))
|
2022-11-25 23:54:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
describe 'callbacks' do
|
|
|
|
context 'for preventing deletion after member is associated' do
|
|
|
|
let_it_be(:member_role) { create(:member_role) }
|
|
|
|
|
|
|
|
subject(:destroy_member_role) { member_role.destroy } # rubocop: disable Rails/SaveBang
|
|
|
|
|
|
|
|
it 'allows deletion without any member associated' do
|
|
|
|
expect(destroy_member_role).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevent deletion when member is associated' do
|
|
|
|
create(:group_member, { group: member_role.namespace,
|
|
|
|
access_level: Gitlab::Access::DEVELOPER,
|
|
|
|
member_role: member_role })
|
|
|
|
member_role.members.reload
|
|
|
|
|
|
|
|
expect(destroy_member_role).to be_falsey
|
|
|
|
expect(member_role.errors.messages[:base])
|
|
|
|
.to(
|
|
|
|
include(s_("MemberRole|cannot be deleted because it is already assigned to a user. "\
|
|
|
|
"Please disassociate the member role from all users before deletion."))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
end
|