debian-mirror-gitlab/spec/models/members/project_member_spec.rb

310 lines
10 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe ProjectMember do
describe 'associations' do
2017-08-17 22:00:37 +05:30
it { is_expected.to belong_to(:project).with_foreign_key(:source_id) }
end
describe 'validations' do
it { is_expected.to allow_value('Project').for(:source_type) }
it { is_expected.not_to allow_value('project').for(:source_type) }
2016-09-13 17:45:13 +05:30
it { is_expected.to validate_inclusion_of(:access_level).in_array(Gitlab::Access.values) }
end
2021-04-29 21:17:54 +05:30
describe 'delegations' do
it { is_expected.to delegate_method(:namespace_id).to(:project) }
end
2016-11-03 12:29:30 +05:30
describe '.access_level_roles' do
it 'returns Gitlab::Access.options' do
expect(described_class.access_level_roles).to eq(Gitlab::Access.options)
end
end
describe '#real_source_type' do
subject { create(:project_member).real_source_type }
it { is_expected.to eq 'Project' }
end
describe "#destroy" do
2018-11-18 11:00:15 +05:30
let(:owner) { create(:project_member, access_level: ProjectMember::MAINTAINER) }
let(:project) { owner.project }
2019-03-02 22:35:43 +05:30
let(:maintainer) { create(:project_member, project: project) }
2017-08-17 22:00:37 +05:30
it "creates an expired event when left due to expiry" do
2020-10-04 03:57:07 +05:30
expired = create(:project_member, project: project, expires_at: 1.day.from_now)
2021-06-08 01:23:25 +05:30
travel_to(2.days.from_now) { expired.destroy! }
2020-10-04 03:57:07 +05:30
2020-06-23 00:09:42 +05:30
expect(Event.recent.first).to be_expired_action
2017-08-17 22:00:37 +05:30
end
it "creates a left event when left due to leave" do
2021-06-08 01:23:25 +05:30
maintainer.destroy!
2020-06-23 00:09:42 +05:30
expect(Event.recent.first).to be_left_action
2017-08-17 22:00:37 +05:30
end
2021-09-04 01:27:46 +05:30
context 'for an orphaned member' do
let!(:orphaned_project_member) do
owner.tap { |member| member.update_column(:user_id, nil) }
end
it 'does not raise an error' do
expect { orphaned_project_member.destroy! }.not_to raise_error
end
end
end
2016-11-03 12:29:30 +05:30
describe '.import_team' do
2015-04-26 12:48:37 +05:30
before do
2017-09-10 17:25:29 +05:30
@project_1 = create(:project)
@project_2 = create(:project)
2015-04-26 12:48:37 +05:30
@user_1 = create :user
@user_2 = create :user
2018-03-17 18:26:18 +05:30
@project_1.add_developer(@user_1)
@project_2.add_reporter(@user_2)
2015-04-26 12:48:37 +05:30
@status = @project_2.team.import(@project_1)
end
it { expect(@status).to be_truthy }
describe 'project 2 should get user 1 as developer. user_2 should not be changed' do
it { expect(@project_2.users).to include(@user_1) }
it { expect(@project_2.users).to include(@user_2) }
2016-09-29 09:46:39 +05:30
it { expect(Ability.allowed?(@user_1, :create_project, @project_2)).to be_truthy }
it { expect(Ability.allowed?(@user_2, :read_project, @project_2)).to be_truthy }
2015-04-26 12:48:37 +05:30
end
describe 'project 1 should not be changed' do
it { expect(@project_1.users).to include(@user_1) }
it { expect(@project_1.users).not_to include(@user_2) }
end
end
2016-09-13 17:45:13 +05:30
describe '.add_users_to_projects' do
2016-11-03 12:29:30 +05:30
it 'adds the given users to the given projects' do
2017-09-10 17:25:29 +05:30
projects = create_list(:project, 2)
2016-11-03 12:29:30 +05:30
users = create_list(:user, 2)
2015-04-26 12:48:37 +05:30
2016-11-03 12:29:30 +05:30
described_class.add_users_to_projects(
2017-08-17 22:00:37 +05:30
[projects.first.id, projects.second.id],
2016-11-03 12:29:30 +05:30
[users.first.id, users.second],
2018-11-18 11:00:15 +05:30
described_class::MAINTAINER)
2015-04-26 12:48:37 +05:30
2016-11-03 12:29:30 +05:30
expect(projects.first.users).to include(users.first)
expect(projects.first.users).to include(users.second)
2015-04-26 12:48:37 +05:30
2016-11-03 12:29:30 +05:30
expect(projects.second.users).to include(users.first)
expect(projects.second.users).to include(users.second)
end
2015-04-26 12:48:37 +05:30
end
2016-08-24 12:49:21 +05:30
describe '.truncate_teams' do
2015-04-26 12:48:37 +05:30
before do
2017-09-10 17:25:29 +05:30
@project_1 = create(:project)
@project_2 = create(:project)
2015-04-26 12:48:37 +05:30
@user_1 = create :user
@user_2 = create :user
2018-03-17 18:26:18 +05:30
@project_1.add_developer(@user_1)
@project_2.add_reporter(@user_2)
2015-04-26 12:48:37 +05:30
2017-09-10 17:25:29 +05:30
described_class.truncate_teams([@project_1.id, @project_2.id])
2015-04-26 12:48:37 +05:30
end
it { expect(@project_1.users).to be_empty }
it { expect(@project_2.users).to be_empty }
end
2018-10-15 14:42:47 +05:30
it_behaves_like 'members notifications', :project
2019-02-15 15:39:39 +05:30
context 'access levels' do
context 'with parent group' do
it_behaves_like 'inherited access level as a member of entity' do
let(:entity) { create(:project, group: parent_entity) }
end
end
2019-10-12 21:52:04 +05:30
context 'with parent group and a subgroup' do
2019-02-15 15:39:39 +05:30
it_behaves_like 'inherited access level as a member of entity' do
let(:subgroup) { create(:group, parent: parent_entity) }
let(:entity) { create(:project, group: subgroup) }
end
end
end
2021-10-27 15:23:28 +05:30
context 'refreshing project_authorizations' do
let_it_be_with_refind(:project) { create(:project) }
let_it_be_with_refind(:user) { create(:user) }
let_it_be(:project_member) { create(:project_member, :guest, project: project, user: user) }
context 'when the source project of the project member is destroyed' do
it 'refreshes the authorization of user to the project in the group' do
expect { project.destroy! }.to change { user.can?(:guest_access, project) }.from(true).to(false)
end
it 'refreshes the authorization without calling AuthorizedProjectUpdate::ProjectRecalculatePerUserService' do
expect(AuthorizedProjectUpdate::ProjectRecalculatePerUserService).not_to receive(:new)
project.destroy!
end
end
context 'when the user of the project member is destroyed' do
it 'refreshes the authorization of user to the project in the group' do
expect(project.authorized_users).to include(user)
user.destroy!
expect(project.authorized_users).not_to include(user)
end
it 'refreshes the authorization without calling UserProjectAccessChangedService' do
expect(UserProjectAccessChangedService).not_to receive(:new)
user.destroy!
end
end
context 'when importing' do
it 'does not refresh' do
expect(AuthorizedProjectUpdate::ProjectRecalculatePerUserService).not_to receive(:new)
member = build(:project_member)
member.importing = true
member.save!
end
end
end
context 'authorization refresh on addition/updation/deletion' do
let_it_be(:project) { create(:project) }
let_it_be(:user) { create(:user) }
shared_examples_for 'calls AuthorizedProjectUpdate::ProjectRecalculatePerUserService to recalculate authorizations' do
it 'calls AuthorizedProjectUpdate::ProjectRecalculatePerUserService' do
expect_next_instance_of(AuthorizedProjectUpdate::ProjectRecalculatePerUserService, project, user) do |service|
expect(service).to receive(:execute)
end
action
end
end
shared_examples_for 'calls AuthorizedProjectUpdate::UserRefreshFromReplicaWorker with a delay to update project authorizations' do
it 'calls AuthorizedProjectUpdate::UserRefreshFromReplicaWorker' do
expect(AuthorizedProjectUpdate::UserRefreshFromReplicaWorker).to(
receive(:bulk_perform_in)
.with(1.hour,
[[user.id]],
batch_delay: 30.seconds, batch_size: 100)
)
action
end
end
context 'on create' do
let(:action) { project.add_user(user, Gitlab::Access::GUEST) }
it 'changes access level' do
expect { action }.to change { user.can?(:guest_access, project) }.from(false).to(true)
end
it_behaves_like 'calls AuthorizedProjectUpdate::ProjectRecalculatePerUserService to recalculate authorizations'
it_behaves_like 'calls AuthorizedProjectUpdate::UserRefreshFromReplicaWorker with a delay to update project authorizations'
end
context 'on update' do
let(:action) { project.members.find_by(user: user).update!(access_level: Gitlab::Access::DEVELOPER) }
before do
project.add_user(user, Gitlab::Access::GUEST)
end
it 'changes access level' do
expect { action }.to change { user.can?(:developer_access, project) }.from(false).to(true)
end
it_behaves_like 'calls AuthorizedProjectUpdate::ProjectRecalculatePerUserService to recalculate authorizations'
it_behaves_like 'calls AuthorizedProjectUpdate::UserRefreshFromReplicaWorker with a delay to update project authorizations'
end
context 'on destroy' do
let(:action) { project.members.find_by(user: user).destroy! }
before do
project.add_user(user, Gitlab::Access::GUEST)
end
it 'changes access level' do
expect { action }.to change { user.can?(:guest_access, project) }.from(true).to(false)
end
it_behaves_like 'calls AuthorizedProjectUpdate::ProjectRecalculatePerUserService to recalculate authorizations'
it_behaves_like 'calls AuthorizedProjectUpdate::UserRefreshFromReplicaWorker with a delay to update project authorizations'
end
context 'when the feature flag `specialized_service_for_project_member_auth_refresh` is disabled' do
before do
stub_feature_flags(specialized_service_for_project_member_auth_refresh: false)
end
shared_examples_for 'calls UserProjectAccessChangedService to recalculate authorizations' do
it 'calls UserProjectAccessChangedService' do
expect_next_instance_of(UserProjectAccessChangedService, user.id) do |service|
expect(service).to receive(:execute)
end
action
end
end
context 'on create' do
let(:action) { project.add_user(user, Gitlab::Access::GUEST) }
it 'changes access level' do
expect { action }.to change { user.can?(:guest_access, project) }.from(false).to(true)
end
it_behaves_like 'calls UserProjectAccessChangedService to recalculate authorizations'
end
context 'on update' do
let(:action) { project.members.find_by(user: user).update!(access_level: Gitlab::Access::DEVELOPER) }
before do
project.add_user(user, Gitlab::Access::GUEST)
end
it 'changes access level' do
expect { action }.to change { user.can?(:developer_access, project) }.from(false).to(true)
end
it_behaves_like 'calls UserProjectAccessChangedService to recalculate authorizations'
end
context 'on destroy' do
let(:action) { project.members.find_by(user: user).destroy! }
before do
project.add_user(user, Gitlab::Access::GUEST)
end
it 'changes access level' do
expect { action }.to change { user.can?(:guest_access, project) }.from(true).to(false)
end
it_behaves_like 'calls UserProjectAccessChangedService to recalculate authorizations'
end
end
end
2015-04-26 12:48:37 +05:30
end