summaryrefslogtreecommitdiff
path: root/spec/models/deploy_token_spec.rb
blob: a2d4c046d46cef1a2ea567d94aac633d6f075603 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# frozen_string_literal: true

require 'spec_helper'

describe DeployToken do
  subject(:deploy_token) { create(:deploy_token) }

  it { is_expected.to have_many :project_deploy_tokens }
  it { is_expected.to have_many(:projects).through(:project_deploy_tokens) }
  it { is_expected.to have_many :group_deploy_tokens }
  it { is_expected.to have_many(:groups).through(:group_deploy_tokens) }

  it_behaves_like 'having unique enum values'

  describe 'validations' do
    let(:username_format_message) { "can contain only letters, digits, '_', '-', '+', and '.'" }

    it { is_expected.to validate_length_of(:username).is_at_most(255) }
    it { is_expected.to allow_value('GitLab+deploy_token-3.14').for(:username) }
    it { is_expected.not_to allow_value('<script>').for(:username).with_message(username_format_message) }
    it { is_expected.not_to allow_value('').for(:username).with_message(username_format_message) }
    it { is_expected.to validate_presence_of(:deploy_token_type) }
  end

  describe 'deploy_token_type validations' do
    context 'when a deploy token is associated to a group' do
      it 'does not allow setting a project to it' do
        group_token = create(:deploy_token, :group)
        group_token.projects << build(:project)

        expect(group_token).not_to be_valid
        expect(group_token.errors.full_messages).to include('Deploy token cannot have projects assigned')
      end
    end

    context 'when a deploy token is associated to a project' do
      it 'does not allow setting a group to it' do
        project_token = create(:deploy_token)
        project_token.groups << build(:group)

        expect(project_token).not_to be_valid
        expect(project_token.errors.full_messages).to include('Deploy token cannot have groups assigned')
      end
    end
  end

  describe '#ensure_token' do
    it 'ensures a token' do
      deploy_token.token = nil
      deploy_token.save

      expect(deploy_token.token).not_to be_empty
    end
  end

  describe '#ensure_at_least_one_scope' do
    context 'with at least one scope' do
      it 'is valid' do
        is_expected.to be_valid
      end
    end

    context 'with no scopes' do
      it 'is invalid' do
        deploy_token = build(:deploy_token, read_repository: false, read_registry: false, write_registry: false)

        expect(deploy_token).not_to be_valid
        expect(deploy_token.errors[:base].first).to eq("Scopes can't be blank")
      end
    end
  end

  describe '#scopes' do
    context 'with all the scopes' do
      it 'returns scopes assigned to DeployToken' do
        expect(deploy_token.scopes).to eq([:read_repository, :read_registry])
      end
    end

    context 'with only one scope' do
      it 'returns scopes assigned to DeployToken' do
        deploy_token = create(:deploy_token, read_registry: false, write_registry: false)
        expect(deploy_token.scopes).to eq([:read_repository])
      end
    end
  end

  describe '#revoke!' do
    it 'updates revoke attribute' do
      deploy_token.revoke!
      expect(deploy_token.revoked?).to be_truthy
    end
  end

  describe "#active?" do
    context "when it has been revoked" do
      it 'returns false' do
        deploy_token.revoke!
        expect(deploy_token.active?).to be_falsy
      end
    end

    context "when it hasn't been revoked and is not expired" do
      it 'returns true' do
        expect(deploy_token.active?).to be_truthy
      end
    end

    context "when it hasn't been revoked and is expired" do
      it 'returns true' do
        deploy_token.update_attribute(:expires_at, Date.today - 5.days)
        expect(deploy_token.active?).to be_falsy
      end
    end

    context "when it hasn't been revoked and has no expiry" do
      let(:deploy_token) { create(:deploy_token, expires_at: nil) }

      it 'returns true' do
        expect(deploy_token.active?).to be_truthy
      end
    end
  end

  describe '#username' do
    context 'persisted records' do
      it 'returns a default username if none is set' do
        expect(deploy_token.username).to eq("gitlab+deploy-token-#{deploy_token.id}")
      end

      it 'returns the username provided if one is set' do
        deploy_token = create(:deploy_token, username: 'deployer')

        expect(deploy_token.username).to eq('deployer')
      end
    end

    context 'new records' do
      it 'returns nil if no username is set' do
        deploy_token = build(:deploy_token)

        expect(deploy_token.username).to be_nil
      end

      it 'returns the username provided if one is set' do
        deploy_token = build(:deploy_token, username: 'deployer')

        expect(deploy_token.username).to eq('deployer')
      end
    end
  end

  describe '#holder' do
    subject { deploy_token.holder }

    context 'when the token is of project type' do
      it 'returns the relevant holder token' do
        expect(subject).to eq(deploy_token.project_deploy_tokens.first)
      end
    end

    context 'when the token is of group type' do
      let(:group) { create(:group) }
      let(:deploy_token) { create(:deploy_token, :group) }

      it 'returns the relevant holder token' do
        expect(subject).to eq(deploy_token.group_deploy_tokens.first)
      end
    end
  end

  describe '#has_access_to?' do
    let(:project) { create(:project) }

    subject { deploy_token.has_access_to?(project) }

    context 'when a project is not passed in' do
      let(:project) { nil }

      it { is_expected.to be_falsy }
    end

    context 'when a project is passed in' do
      context 'when deploy token is active and related to project' do
        let(:deploy_token) { create(:deploy_token, projects: [project]) }

        it { is_expected.to be_truthy }
      end

      context 'when deploy token is active but not related to project' do
        let(:deploy_token) { create(:deploy_token) }

        it { is_expected.to be_falsy }
      end

      context 'when deploy token is revoked and related to project' do
        let(:deploy_token) { create(:deploy_token, :revoked, projects: [project]) }

        it { is_expected.to be_falsy }
      end

      context 'when deploy token is revoked and not related to the project' do
        let(:deploy_token) { create(:deploy_token, :revoked) }

        it { is_expected.to be_falsy }
      end

      context 'and when the token is of group type' do
        let_it_be(:group) { create(:group) }
        let(:deploy_token) { create(:deploy_token, :group) }

        before do
          deploy_token.groups << group
        end

        context 'and the allow_group_deploy_token feature flag is turned off' do
          it 'is false' do
            stub_feature_flags(allow_group_deploy_token: false)

            is_expected.to be_falsy
          end
        end

        context 'and the allow_group_deploy_token feature flag is turned on' do
          before do
            stub_feature_flags(allow_group_deploy_token: true)
          end

          context 'and the passed-in project does not belong to any group' do
            it { is_expected.to be_falsy }
          end

          context 'and the passed-in project belongs to the token group' do
            it 'is true' do
              group.projects << project

              is_expected.to be_truthy
            end
          end

          context 'and the passed-in project belongs to a subgroup' do
            let(:child_group) { create(:group, parent_id: group.id) }
            let(:grandchild_group) { create(:group, parent_id: child_group.id) }

            before do
              grandchild_group.projects << project
            end

            context 'and the token group is an ancestor (grand-parent) of this group' do
              it { is_expected.to be_truthy }
            end

            context 'and the token group is not ancestor of this group' do
              let(:child2_group) { create(:group, parent_id: group.id) }

              it 'is false' do
                deploy_token.groups = [child2_group]

                is_expected.to be_falsey
              end
            end
          end

          context 'and the passed-in project does not belong to the token group' do
            it { is_expected.to be_falsy }
          end

          context 'and the project belongs to a group that is parent of the token group' do
            let(:super_group) { create(:group) }
            let(:deploy_token) { create(:deploy_token, :group) }
            let(:group) { create(:group, parent_id: super_group.id) }

            it 'is false' do
              super_group.projects << project

              is_expected.to be_falsey
            end
          end
        end
      end

      context 'and the token is of project type' do
        let(:deploy_token) { create(:deploy_token, projects: [project]) }

        context 'and the passed-in project is the same as the token project' do
          it { is_expected.to be_truthy }
        end

        context 'and the passed-in project is not the same as the token project' do
          subject { deploy_token.has_access_to?(create(:project)) }

          it { is_expected.to be_falsey }
        end
      end
    end
  end

  describe '#expires_at' do
    context 'when using Forever.date' do
      let(:deploy_token) { create(:deploy_token, expires_at: nil) }

      it 'returns nil' do
        expect(deploy_token.expires_at).to be_nil
      end
    end

    context 'when using a personalized date' do
      let(:expires_at) { Date.today + 5.months }
      let(:deploy_token) { create(:deploy_token, expires_at: expires_at) }

      it 'returns the personalized date' do
        expect(deploy_token.expires_at).to eq(expires_at)
      end
    end
  end

  describe '#expires_at=' do
    context 'when passing nil' do
      let(:deploy_token) { create(:deploy_token, expires_at: nil) }

      it 'assigns Forever.date' do
        expect(deploy_token.read_attribute(:expires_at)).to eq(Forever.date)
      end
    end

    context 'when passing a value' do
      let(:expires_at) { Date.today + 5.months }
      let(:deploy_token) { create(:deploy_token, expires_at: expires_at) }

      it 'respects the value' do
        expect(deploy_token.read_attribute(:expires_at)).to eq(expires_at)
      end
    end
  end

  describe '.gitlab_deploy_token' do
    let(:project) { create(:project ) }

    subject { project.deploy_tokens.gitlab_deploy_token }

    context 'with a gitlab deploy token associated' do
      it 'returns the gitlab deploy token' do
        deploy_token = create(:deploy_token, :gitlab_deploy_token, projects: [project])
        is_expected.to eq(deploy_token)
      end
    end

    context 'with no gitlab deploy token associated' do
      it 'returns nil' do
        is_expected.to be_nil
      end
    end
  end
end