summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/recalculate_project_authorizations_spec.rb
blob: ba87312e2bf85a554d78cf0bd96ad46def087c84 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::BackgroundMigration::RecalculateProjectAuthorizations, schema: 20200204113223 do
  let(:users_table) { table(:users) }
  let(:namespaces_table) { table(:namespaces) }
  let(:projects_table) { table(:projects) }
  let(:project_authorizations_table) { table(:project_authorizations) }
  let(:members_table) { table(:members) }
  let(:group_group_links) { table(:group_group_links) }
  let(:project_group_links) { table(:project_group_links) }

  let(:user) { users_table.create!(id: 1, email: 'user@example.com', projects_limit: 10) }
  let(:group) { namespaces_table.create!(type: 'Group', name: 'group', path: 'group') }

  subject { described_class.new.perform([user.id]) }

  context 'missing authorization' do
    context 'personal project' do
      before do
        user_namespace = namespaces_table.create!(owner_id: user.id, name: 'User', path: 'user')
        projects_table.create!(id: 1,
                               name: 'personal-project',
                               path: 'personal-project',
                               visibility_level: 0,
                               namespace_id: user_namespace.id)
      end

      it 'creates correct authorization' do
        expect { subject }.to change { project_authorizations_table.count }.from(0).to(1)
        expect(project_authorizations_table.all).to(
          match_array([have_attributes(user_id: 1, project_id: 1, access_level: 40)]))
      end
    end

    context 'group membership' do
      before do
        projects_table.create!(id: 1, name: 'group-project', path: 'group-project',
                               visibility_level: 0, namespace_id: group.id)
        members_table.create!(user_id: user.id, source_id: group.id, source_type: 'Namespace',
                              type: 'GroupMember', access_level: 20, notification_level: 3)
      end

      it 'creates correct authorization' do
        expect { subject }.to change { project_authorizations_table.count }.from(0).to(1)
        expect(project_authorizations_table.all).to(
          match_array([have_attributes(user_id: 1, project_id: 1, access_level: 20)]))
      end
    end

    context 'inherited group membership' do
      before do
        sub_group = namespaces_table.create!(type: 'Group', name: 'subgroup',
                                             path: 'subgroup', parent_id: group.id)
        projects_table.create!(id: 1, name: 'group-project', path: 'group-project',
                               visibility_level: 0, namespace_id: sub_group.id)
        members_table.create!(user_id: user.id, source_id: group.id, source_type: 'Namespace',
                              type: 'GroupMember', access_level: 20, notification_level: 3)
      end

      it 'creates correct authorization' do
        expect { subject }.to change { project_authorizations_table.count }.from(0).to(1)
        expect(project_authorizations_table.all).to(
          match_array([have_attributes(user_id: 1, project_id: 1, access_level: 20)]))
      end
    end

    context 'project membership' do
      before do
        project = projects_table.create!(id: 1, name: 'group-project', path: 'group-project',
                                         visibility_level: 0, namespace_id: group.id)
        members_table.create!(user_id: user.id, source_id: project.id, source_type: 'Project',
                              type: 'ProjectMember', access_level: 20, notification_level: 3)
      end

      it 'creates correct authorization' do
        expect { subject }.to change { project_authorizations_table.count }.from(0).to(1)
        expect(project_authorizations_table.all).to(
          match_array([have_attributes(user_id: 1, project_id: 1, access_level: 20)]))
      end
    end

    context 'shared group' do
      before do
        members_table.create!(user_id: user.id, source_id: group.id, source_type: 'Namespace',
                              type: 'GroupMember', access_level: 30, notification_level: 3)

        shared_group = namespaces_table.create!(type: 'Group', name: 'shared group',
                                                path: 'shared-group')
        projects_table.create!(id: 1, name: 'project', path: 'project', visibility_level: 0,
                               namespace_id: shared_group.id)

        group_group_links.create(shared_group_id: shared_group.id, shared_with_group_id: group.id,
                                 group_access: 20)
      end

      it 'creates correct authorization' do
        expect { subject }.to change { project_authorizations_table.count }.from(0).to(1)
        expect(project_authorizations_table.all).to(
          match_array([have_attributes(user_id: 1, project_id: 1, access_level: 20)]))
      end
    end

    context 'shared project' do
      before do
        members_table.create!(user_id: user.id, source_id: group.id, source_type: 'Namespace',
                              type: 'GroupMember', access_level: 30, notification_level: 3)

        another_group = namespaces_table.create!(type: 'Group', name: 'another group', path: 'another-group')
        shared_project = projects_table.create!(id: 1, name: 'shared project', path: 'shared-project',
                                                visibility_level: 0, namespace_id: another_group.id)

        project_group_links.create(project_id: shared_project.id, group_id: group.id, group_access: 20)
      end

      it 'creates correct authorization' do
        expect { subject }.to change { project_authorizations_table.count }.from(0).to(1)
        expect(project_authorizations_table.all).to(
          match_array([have_attributes(user_id: 1, project_id: 1, access_level: 20)]))
      end
    end
  end

  context 'unapproved access requests' do
    context 'group membership' do
      before do
        projects_table.create!(id: 1, name: 'group-project', path: 'group-project',
                               visibility_level: 0, namespace_id: group.id)
        members_table.create!(user_id: user.id, source_id: group.id, source_type: 'Namespace',
                              type: 'GroupMember', access_level: 20, requested_at: Time.now, notification_level: 3)
      end

      it 'does not create authorization' do
        expect { subject }.not_to change { project_authorizations_table.count }.from(0)
      end
    end

    context 'inherited group membership' do
      before do
        sub_group = namespaces_table.create!(type: 'Group', name: 'subgroup', path: 'subgroup',
                                             parent_id: group.id)
        projects_table.create!(id: 1, name: 'group-project', path: 'group-project',
                               visibility_level: 0, namespace_id: sub_group.id)
        members_table.create!(user_id: user.id, source_id: group.id, source_type: 'Namespace',
                              type: 'GroupMember', access_level: 20, requested_at: Time.now, notification_level: 3)
      end

      it 'does not create authorization' do
        expect { subject }.not_to change { project_authorizations_table.count }.from(0)
      end
    end

    context 'project membership' do
      before do
        project = projects_table.create!(id: 1, name: 'group-project', path: 'group-project',
                                         visibility_level: 0, namespace_id: group.id)
        members_table.create!(user_id: user.id, source_id: project.id, source_type: 'Project',
                              type: 'ProjectMember', access_level: 20, requested_at: Time.now, notification_level: 3)
      end

      it 'does not create authorization' do
        expect { subject }.not_to change { project_authorizations_table.count }.from(0)
      end
    end

    context 'shared group' do
      before do
        members_table.create!(user_id: user.id, source_id: group.id, source_type: 'Namespace',
                              type: 'GroupMember', access_level: 30, requested_at: Time.now, notification_level: 3)

        shared_group = namespaces_table.create!(type: 'Group', name: 'shared group',
                                                path: 'shared-group')
        projects_table.create!(id: 1, name: 'project', path: 'project', visibility_level: 0,
                               namespace_id: shared_group.id)

        group_group_links.create(shared_group_id: shared_group.id, shared_with_group_id: group.id,
                                 group_access: 20)
      end

      it 'does not create authorization' do
        expect { subject }.not_to change { project_authorizations_table.count }.from(0)
      end
    end

    context 'shared project' do
      before do
        members_table.create!(user_id: user.id, source_id: group.id, source_type: 'Namespace',
                              type: 'GroupMember', access_level: 30, requested_at: Time.now, notification_level: 3)

        another_group = namespaces_table.create!(type: 'Group', name: 'another group', path: 'another-group')
        shared_project = projects_table.create!(id: 1, name: 'shared project', path: 'shared-project',
                                                visibility_level: 0, namespace_id: another_group.id)

        project_group_links.create(project_id: shared_project.id, group_id: group.id, group_access: 20)
      end

      it 'does not create authorization' do
        expect { subject }.not_to change { project_authorizations_table.count }.from(0)
      end
    end
  end

  context 'incorrect authorization' do
    before do
      project = projects_table.create!(id: 1, name: 'group-project', path: 'group-project',
                                       visibility_level: 0, namespace_id: group.id)
      members_table.create!(user_id: user.id, source_id: group.id, source_type: 'Namespace',
                            type: 'GroupMember', access_level: 30, notification_level: 3)

      project_authorizations_table.create!(user_id: user.id, project_id: project.id,
                                           access_level: 10)
    end

    it 'fixes authorization' do
      expect { subject }.not_to change { project_authorizations_table.count }.from(1)
      expect(project_authorizations_table.all).to(
        match_array([have_attributes(user_id: 1, project_id: 1, access_level: 30)]))
    end
  end

  context 'unwanted authorization' do
    before do
      project = projects_table.create!(name: 'group-project', path: 'group-project',
                                       visibility_level: 0, namespace_id: group.id)

      project_authorizations_table.create!(user_id: user.id, project_id: project.id,
                                           access_level: 10)
    end

    it 'deletes authorization' do
      expect { subject }.to change { project_authorizations_table.count }.from(1).to(0)
    end
  end

  context 'deleted user' do
    it 'does not fail' do
      expect { described_class.new.perform([non_existing_record_id]) }.not_to raise_error
    end
  end
end