debian-mirror-gitlab/spec/services/users/refresh_authorized_projects_service_spec.rb

167 lines
5.4 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Users::RefreshAuthorizedProjectsService do
2018-11-08 19:23:39 +05:30
include ExclusiveLeaseHelpers
2017-09-10 17:25:29 +05:30
# We're using let! here so that any expectations for the service class are not
# triggered twice.
let!(:project) { create(:project) }
2017-08-17 22:00:37 +05:30
let(:user) { project.namespace.owner }
let(:service) { described_class.new(user) }
2017-09-10 17:25:29 +05:30
describe '#execute', :clean_gitlab_redis_shared_state do
2017-08-17 22:00:37 +05:30
it 'refreshes the authorizations using a lease' do
2018-11-08 19:23:39 +05:30
lease_key = "refresh_authorized_projects:#{user.id}"
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
expect_to_obtain_exclusive_lease(lease_key, 'uuid')
expect_to_cancel_exclusive_lease(lease_key, 'uuid')
2017-08-17 22:00:37 +05:30
expect(service).to receive(:execute_without_lease)
service.execute
end
2020-02-15 18:32:13 +05:30
context 'callbacks' do
let(:callback) { double('callback') }
context 'incorrect_auth_found_callback callback' do
let(:user) { create(:user) }
let(:service) do
described_class.new(user,
incorrect_auth_found_callback: callback)
end
it 'is called' do
access_level = Gitlab::Access::DEVELOPER
create(:project_authorization, user: user, project: project, access_level: access_level)
expect(callback).to receive(:call).with(project.id, access_level).once
service.execute
end
end
context 'missing_auth_found_callback callback' do
let(:service) do
described_class.new(user,
missing_auth_found_callback: callback)
end
it 'is called' do
ProjectAuthorization.delete_all
expect(callback).to receive(:call).with(project.id, Gitlab::Access::MAINTAINER).once
service.execute
end
end
end
2017-08-17 22:00:37 +05:30
end
describe '#execute_without_lease' do
before do
user.project_authorizations.delete_all
end
it 'updates the authorized projects of the user' do
2017-09-10 17:25:29 +05:30
project2 = create(:project)
to_remove = user.project_authorizations
2018-11-18 11:00:15 +05:30
.create!(project: project2, access_level: Gitlab::Access::MAINTAINER)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expect(service).to receive(:update_authorizations)
2018-11-18 11:00:15 +05:30
.with([to_remove.project_id], [[user.id, project.id, Gitlab::Access::MAINTAINER]])
2017-08-17 22:00:37 +05:30
service.execute_without_lease
end
2020-10-24 23:57:45 +05:30
it 'removes duplicate entries' do
[Gitlab::Access::MAINTAINER, Gitlab::Access::REPORTER].each do |access_level|
user.project_authorizations.create!(project: project, access_level: access_level)
end
expect(service).to(
receive(:update_authorizations)
.with([project.id], [[user.id, project.id, Gitlab::Access::MAINTAINER]])
.and_call_original)
service.execute_without_lease
expect(user.project_authorizations.count).to eq(1)
project_authorization = ProjectAuthorization.where(
project_id: project.id,
user_id: user.id,
access_level: Gitlab::Access::MAINTAINER)
expect(project_authorization).to exist
end
2017-08-17 22:00:37 +05:30
it 'sets the access level of a project to the highest available level' do
2017-09-10 17:25:29 +05:30
user.project_authorizations.delete_all
to_remove = user.project_authorizations
.create!(project: project, access_level: Gitlab::Access::DEVELOPER)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
expect(service).to receive(:update_authorizations)
2018-11-18 11:00:15 +05:30
.with([to_remove.project_id], [[user.id, project.id, Gitlab::Access::MAINTAINER]])
2017-08-17 22:00:37 +05:30
service.execute_without_lease
end
it 'returns a User' do
expect(service.execute_without_lease).to be_an_instance_of(User)
end
end
describe '#update_authorizations' do
context 'when there are no rows to add and remove' do
it 'does not change authorizations' do
expect(user).not_to receive(:remove_project_authorizations)
expect(ProjectAuthorization).not_to receive(:insert_authorizations)
service.update_authorizations([], [])
end
end
it 'removes authorizations that should be removed' do
2017-09-10 17:25:29 +05:30
authorization = user.project_authorizations.find_by(project_id: project.id)
2017-08-17 22:00:37 +05:30
service.update_authorizations([authorization.project_id])
expect(user.project_authorizations).to be_empty
end
it 'inserts authorizations that should be added' do
2017-09-10 17:25:29 +05:30
user.project_authorizations.delete_all
2018-11-18 11:00:15 +05:30
service.update_authorizations([], [[user.id, project.id, Gitlab::Access::MAINTAINER]])
2017-08-17 22:00:37 +05:30
authorizations = user.project_authorizations
expect(authorizations.length).to eq(1)
expect(authorizations[0].user_id).to eq(user.id)
expect(authorizations[0].project_id).to eq(project.id)
2018-11-18 11:00:15 +05:30
expect(authorizations[0].access_level).to eq(Gitlab::Access::MAINTAINER)
2017-08-17 22:00:37 +05:30
end
2021-03-11 19:13:27 +05:30
it 'logs the details of the refresh' do
source = :foo
service = described_class.new(user, source: source)
user.project_authorizations.delete_all
expect(Gitlab::AppJsonLogger).to(
receive(:info)
.with(event: 'authorized_projects_refresh',
2021-04-17 20:07:23 +05:30
user_id: user.id,
2021-03-11 19:13:27 +05:30
'authorized_projects_refresh.source': source,
2021-04-17 20:07:23 +05:30
'authorized_projects_refresh.rows_deleted_count': 0,
'authorized_projects_refresh.rows_added_count': 1,
'authorized_projects_refresh.rows_deleted_slice': [],
'authorized_projects_refresh.rows_added_slice': [[user.id, project.id, Gitlab::Access::MAINTAINER]])
)
2021-03-11 19:13:27 +05:30
service.update_authorizations([], [[user.id, project.id, Gitlab::Access::MAINTAINER]])
end
2017-08-17 22:00:37 +05:30
end
end