summaryrefslogtreecommitdiff
path: root/spec/services/authorized_project_update/project_recalculate_per_user_service_spec.rb
blob: 62862d0e5580547ad5f061de82738665f9196e96 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe AuthorizedProjectUpdate::ProjectRecalculatePerUserService, '#execute' do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { create(:user) }
  let_it_be(:another_user) { create(:user) }

  subject(:execute) { described_class.new(project, user).execute }

  it 'returns success' do
    expect(execute.success?).to eq(true)
  end

  context 'when there are no changes to be made' do
    it 'does not change authorizations' do
      expect { execute }.not_to(change { ProjectAuthorization.count })
    end
  end

  context 'when there are changes to be made' do
    context 'when addition is required' do
      before do
        project.add_developer(user)
        project.add_developer(another_user)
        project.project_authorizations.where(user: [user, another_user]).delete_all
      end

      it 'adds a new authorization record for the specific user' do
        expect { execute }.to(
          change { project.project_authorizations.where(user: user).count }
          .from(0).to(1)
        )
      end

      it 'does not add a new authorization record for the other user' do
        expect { execute }.not_to(
          change { project.project_authorizations.where(user: another_user).count }
        )
      end

      it 'adds a new authorization record with the correct access level for the specific user' do
        execute

        project_authorization = project.project_authorizations.where(
          user: user,
          access_level: Gitlab::Access::DEVELOPER
        )

        expect(project_authorization).to exist
      end
    end

    context 'when removal is required' do
      before do
        create(:project_authorization, user: user, project: project)
        create(:project_authorization, user: another_user, project: project)
      end

      it 'removes the authorization record for the specific user' do
        expect { execute }.to(
          change { project.project_authorizations.where(user: user).count }
          .from(1).to(0)
        )
      end

      it 'does not remove the authorization record for the other user' do
        expect { execute }.not_to(
          change { project.project_authorizations.where(user: another_user).count }
        )
      end
    end

    context 'when an update in access level is required' do
      before do
        project.add_developer(user)
        project.add_developer(another_user)
        project.project_authorizations.where(user: [user, another_user]).delete_all
        create(:project_authorization, project: project, user: user, access_level: Gitlab::Access::GUEST)
        create(:project_authorization, project: project, user: another_user, access_level: Gitlab::Access::GUEST)
      end

      it 'updates the authorization of the specific user to the correct access level' do
        expect { execute }.to(
          change { project.project_authorizations.find_by(user: user).access_level }
            .from(Gitlab::Access::GUEST).to(Gitlab::Access::DEVELOPER)
        )
      end

      it 'does not update the authorization of the other user to the correct access level' do
        expect { execute }.not_to(
          change { project.project_authorizations.find_by(user: another_user).access_level }
            .from(Gitlab::Access::GUEST)
        )
      end
    end
  end
end