Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20210812013042_remove_duplicate_project_authorizations_spec.rb « migrations « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c88f94c6426d4071f9c9b1b692bb5865d1176f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

require 'spec_helper'
require_migration!('remove_duplicate_project_authorizations')

RSpec.describe RemoveDuplicateProjectAuthorizations, :migration do
  let(:users) { table(:users) }
  let(:namespaces) { table(:namespaces) }
  let(:projects) { table(:projects) }
  let(:project_authorizations) { table(:project_authorizations) }

  let!(:user_1) { users.create! email: 'user1@example.com', projects_limit: 0 }
  let!(:user_2) { users.create! email: 'user2@example.com', projects_limit: 0 }
  let!(:namespace_1) { namespaces.create! name: 'namespace 1', path: 'namespace1' }
  let!(:namespace_2) { namespaces.create! name: 'namespace 2', path: 'namespace2' }
  let!(:project_1) { projects.create! namespace_id: namespace_1.id }
  let!(:project_2) { projects.create! namespace_id: namespace_2.id }

  before do
    stub_const("#{described_class.name}::BATCH_SIZE", 2)
  end

  describe '#up' do
    subject { migrate! }

    context 'User with multiple projects' do
      before do
        project_authorizations.create! project_id: project_1.id, user_id: user_1.id, access_level: Gitlab::Access::DEVELOPER
        project_authorizations.create! project_id: project_2.id, user_id: user_1.id, access_level: Gitlab::Access::DEVELOPER
      end

      it { expect { subject }.not_to change { ProjectAuthorization.count } }
    end

    context 'Project with multiple users' do
      before do
        project_authorizations.create! project_id: project_1.id, user_id: user_1.id, access_level: Gitlab::Access::DEVELOPER
        project_authorizations.create! project_id: project_1.id, user_id: user_2.id, access_level: Gitlab::Access::DEVELOPER
      end

      it { expect { subject }.not_to change { ProjectAuthorization.count } }
    end

    context 'Same project and user but different access level' do
      before do
        project_authorizations.create! project_id: project_1.id, user_id: user_1.id, access_level: Gitlab::Access::DEVELOPER
        project_authorizations.create! project_id: project_1.id, user_id: user_1.id, access_level: Gitlab::Access::MAINTAINER
        project_authorizations.create! project_id: project_1.id, user_id: user_1.id, access_level: Gitlab::Access::REPORTER
      end

      it { expect { subject }.to change { ProjectAuthorization.count }.from(3).to(1) }

      it 'retains the highest access level' do
        subject

        all_records = ProjectAuthorization.all.to_a
        expect(all_records.count).to eq 1
        expect(all_records.first.access_level).to eq Gitlab::Access::MAINTAINER
      end
    end
  end
end