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
|
|
|
|
end
|