debian-mirror-gitlab/spec/models/project_authorization_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
3.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +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 ProjectAuthorization do
2022-05-07 20:08:51 +05:30
describe 'unique user, project authorizations' do
let_it_be(:user) { create(:user) }
let_it_be(:project_1) { create(:project) }
let!(:project_auth) do
create(
:project_authorization,
user: user,
project: project_1,
access_level: Gitlab::Access::DEVELOPER
)
end
context 'with duplicate user and project authorization' do
subject { project_auth.dup }
it { is_expected.to be_invalid }
context 'after validation' do
before do
subject.valid?
end
it 'contains duplicate error' do
expect(subject.errors[:user]).to include('has already been taken')
end
end
end
context 'with multiple access levels for the same user and project' do
subject do
project_auth.dup.tap do |auth|
auth.access_level = Gitlab::Access::MAINTAINER
end
end
it { is_expected.to be_invalid }
context 'after validation' do
before do
subject.valid?
end
it 'contains duplicate error' do
expect(subject.errors[:user]).to include('has already been taken')
end
end
end
end
2022-01-26 12:08:38 +05:30
describe 'relations' do
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:project) }
end
2017-08-17 22:00:37 +05:30
2022-01-26 12:08:38 +05:30
describe 'validations' do
it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_presence_of(:user) }
it { is_expected.to validate_presence_of(:access_level) }
it { is_expected.to validate_inclusion_of(:access_level).in_array(Gitlab::Access.all_values) }
end
2017-08-17 22:00:37 +05:30
2022-01-26 12:08:38 +05:30
describe '.insert_all' do
let_it_be(:user) { create(:user) }
let_it_be(:project_1) { create(:project) }
let_it_be(:project_2) { create(:project) }
let_it_be(:project_3) { create(:project) }
2017-08-17 22:00:37 +05:30
2022-01-26 12:08:38 +05:30
it 'skips duplicates and inserts the remaining rows without error' do
create(:project_authorization, user: user, project: project_1, access_level: Gitlab::Access::MAINTAINER)
attributes = [
{ user_id: user.id, project_id: project_1.id, access_level: Gitlab::Access::MAINTAINER },
{ user_id: user.id, project_id: project_2.id, access_level: Gitlab::Access::MAINTAINER },
{ user_id: user.id, project_id: project_3.id, access_level: Gitlab::Access::MAINTAINER }
]
2017-08-17 22:00:37 +05:30
2022-01-26 12:08:38 +05:30
described_class.insert_all(attributes)
expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values))
2017-08-17 22:00:37 +05:30
end
2022-01-26 12:08:38 +05:30
end
2021-12-11 22:18:48 +05:30
2022-01-26 12:08:38 +05:30
describe '.insert_all_in_batches' do
let_it_be(:user) { create(:user) }
let_it_be(:project_1) { create(:project) }
let_it_be(:project_2) { create(:project) }
let_it_be(:project_3) { create(:project) }
2021-12-11 22:18:48 +05:30
2022-01-26 12:08:38 +05:30
let(:per_batch_size) { 2 }
it 'inserts the rows in batches, as per the `per_batch` size' do
attributes = [
{ user_id: user.id, project_id: project_1.id, access_level: Gitlab::Access::MAINTAINER },
{ user_id: user.id, project_id: project_2.id, access_level: Gitlab::Access::MAINTAINER },
{ user_id: user.id, project_id: project_3.id, access_level: Gitlab::Access::MAINTAINER }
2021-12-11 22:18:48 +05:30
]
2022-01-26 12:08:38 +05:30
expect(described_class).to receive(:insert_all).twice.and_call_original
described_class.insert_all_in_batches(attributes, per_batch_size)
2021-12-11 22:18:48 +05:30
2022-01-26 12:08:38 +05:30
expect(user.project_authorizations.pluck(:user_id, :project_id, :access_level)).to match_array(attributes.map(&:values))
2021-12-11 22:18:48 +05:30
end
2017-08-17 22:00:37 +05:30
end
end