summaryrefslogtreecommitdiff
path: root/spec/requests/api/labels_spec.rb
blob: fc674fca9b23e3ea4348e316a0f7340d1e43ed72 (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::Labels do
  def put_labels_api(route_type, user, spec_params, request_params = {})
    if route_type == :deprecated
      put api("/projects/#{project.id}/labels", user),
          params: request_params.merge(spec_params)
    else
      label_id = spec_params[:name] || spec_params[:label_id]

      put api("/projects/#{project.id}/labels/#{label_id}", user),
          params: request_params.merge(spec_params.except(:name, :id))
    end
  end

  let(:user) { create(:user) }
  let(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
  let!(:label1) { create(:label, description: 'the best label', title: 'label1', project: project) }
  let!(:priority_label) { create(:label, title: 'bug', project: project, priority: 3) }

  route_types = [:deprecated, :rest]

  shared_examples 'label update API' do
    route_types.each do |route_type|
      it "returns 200 if name is changed (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, new_name: 'New Label')

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['name']).to eq('New Label')
        expect(json_response['color']).to eq(label1.color)
      end

      it "returns 200 if colors is changed (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, color: '#FFFFFF')

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['name']).to eq(label1.name)
        expect(json_response['color']).to eq('#FFFFFF')
      end

      it "returns 200 if a priority is added (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, priority: 3)

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['name']).to eq(label1.name)
        expect(json_response['priority']).to eq(3)
      end

      it "returns 400 if no new parameters given (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params)

        expect(response).to have_gitlab_http_status(:bad_request)
        expect(json_response['error']).to eq('new_name, color, description, priority are missing, '\
                                             'at least one parameter must be provided')
      end

      it "returns 400 when color code is too short (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, color: '#FF')

        expect(response).to have_gitlab_http_status(:bad_request)
        expect(json_response['message']['color']).to eq(['must be a valid color code'])
      end

      it "returns 400 for too long color code (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, color: '#FFAAFFFF')

        expect(response).to have_gitlab_http_status(:bad_request)
        expect(json_response['message']['color']).to eq(['must be a valid color code'])
      end

      it "returns 400 for invalid priority (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, priority: 'foo')

        expect(response).to have_gitlab_http_status(:bad_request)
      end

      it "returns 200 if name and colors and description are changed (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, new_name: 'New Label', color: '#FFFFFF', description: 'test')

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['name']).to eq('New Label')
        expect(json_response['color']).to eq('#FFFFFF')
        expect(json_response['description']).to eq('test')
      end

      it "returns 400 for invalid name (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, new_name: ',', color: '#FFFFFF')

        expect(response).to have_gitlab_http_status(:bad_request)
        expect(json_response['message']['title']).to eq(['is invalid'])
      end

      it "returns 200 if description is changed (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, description: 'test')

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['id']).to eq(expected_response_label_id)
        expect(json_response['description']).to eq('test')
      end

      it "returns 200 if priority is changed (#{route_type} route)" do
        put_labels_api(route_type, user, spec_params, priority: 10)

        expect(response).to have_gitlab_http_status(:ok)
        expect(json_response['id']).to eq(expected_response_label_id)
        expect(json_response['priority']).to eq(10)
      end
    end

    it 'returns 200 if a priority is removed (deprecated route)' do
      label = find_by_spec_params(spec_params)

      expect(label).not_to be_nil

      label.priorities.create(project: label.project, priority: 1)
      label.save!

      request_params = {
        priority: nil
      }.merge(spec_params)

      put api("/projects/#{project.id}/labels", user),
          params: request_params

      expect(response).to have_gitlab_http_status(:ok)
      expect(json_response['id']).to eq(expected_response_label_id)
      expect(json_response['priority']).to be_nil
    end

    it 'returns 200 if a priority is removed (rest route)' do
      label = find_by_spec_params(spec_params)
      expect(label).not_to be_nil
      label_id = spec_params[:name] || spec_params[:label_id]

      label.priorities.create(project: label.project, priority: 1)
      label.save!

      request_params = {
        priority: nil
      }.merge(spec_params.except(:name, :id))

      put api("/projects/#{project.id}/labels/#{label_id}", user),
          params: request_params

      expect(response).to have_gitlab_http_status(:ok)
      expect(json_response['id']).to eq(expected_response_label_id)
      expect(json_response['priority']).to be_nil
    end

    def find_by_spec_params(params)
      if params.key?(:label_id)
        Label.find(params[:label_id])
      else
        Label.find_by(name: params[:name])
      end
    end
  end

  shared_examples 'label delete API' do
    it 'returns 204 for existing label (deprecated route)' do
      delete api("/projects/#{project.id}/labels", user), params: spec_params

      expect(response).to have_gitlab_http_status(:no_content)
    end

    it 'returns 204 for existing label (rest route)' do
      label_id = spec_params[:name] || spec_params[:label_id]
      delete api("/projects/#{project.id}/labels/#{label_id}", user), params: spec_params.except(:name, :label_id)

      expect(response).to have_gitlab_http_status(:no_content)
    end
  end

  before do
    project.add_maintainer(user)
  end

  describe 'GET /projects/:id/labels' do
    let(:group) { create(:group) }
    let!(:group_label) { create(:group_label, title: 'feature', group: group) }

    before do
      project.update!(group: group)
    end

    it 'returns all available labels to the project' do
      get api("/projects/#{project.id}/labels", user)

      expect(response).to have_gitlab_http_status(:ok)
      expect(response).to include_pagination_headers
      expect(json_response).to all(match_schema('public_api/v4/labels/project_label'))
      expect(json_response.size).to eq(3)
      expect(json_response.map { |l| l['name'] }).to match_array([group_label.name, priority_label.name, label1.name])
    end

    context 'when the with_counts parameter is set' do
      before do
        create(:labeled_issue, project: project, labels: [group_label], author: user)
        create(:labeled_issue, project: project, labels: [label1], author: user, state: :closed)
        create(:labeled_merge_request, labels: [priority_label], author: user, source_project: project )
      end

      it 'includes counts in the response' do
        get api("/projects/#{project.id}/labels", user), params: { with_counts: true }

        expect(response).to have_gitlab_http_status(:ok)
        expect(response).to include_pagination_headers
        expect(json_response).to all(match_schema('public_api/v4/labels/project_label_with_counts'))
        expect(json_response.size).to eq(3)
        expect(json_response.map { |l| l['name'] }).to match_array([group_label.name, priority_label.name, label1.name])

        label1_response = json_response.find { |l| l['name'] == label1.title }
        group_label_response = json_response.find { |l| l['name'] == group_label.title }
        priority_label_response = json_response.find { |l| l['name'] == priority_label.title }

        expect(label1_response).to include('open_issues_count' => 0,
                                           'closed_issues_count' => 1,
                                           'open_merge_requests_count' => 0,
                                           'name' => label1.name,
                                           'description' => 'the best label',
                                           'color' => a_string_matching(/^#\h{6}$/),
                                           'text_color' => a_string_matching(/^#\h{6}$/),
                                           'priority' => nil,
                                           'subscribed' => false,
                                           'is_project_label' => true)

        expect(group_label_response).to include('open_issues_count' => 1,
                                                'closed_issues_count' => 0,
                                                'open_merge_requests_count' => 0,
                                                'name' => group_label.name,
                                                'description' => nil,
                                                'color' => a_string_matching(/^#\h{6}$/),
                                                'text_color' => a_string_matching(/^#\h{6}$/),
                                                'priority' => nil,
                                                'subscribed' => false,
                                                'is_project_label' => false)

        expect(priority_label_response).to include('open_issues_count' => 0,
                                                   'closed_issues_count' => 0,
                                                   'open_merge_requests_count' => 1,
                                                   'name' => priority_label.name,
                                                   'description' => nil,
                                                   'color' => a_string_matching(/^#\h{6}$/),
                                                   'text_color' => a_string_matching(/^#\h{6}$/),
                                                   'priority' => 3,
                                                   'subscribed' => false,
                                                   'is_project_label' => true)
      end
    end

    context 'when the include_ancestor_groups parameter is not set' do
      let(:group) { create(:group) }
      let!(:group_label) { create(:group_label, title: 'feature', group: group) }
      let(:subgroup) { create(:group, parent: group) }
      let!(:subgroup_label) { create(:group_label, title: 'support', group: subgroup) }

      before do
        subgroup.add_owner(user)
        project.update!(group: subgroup)
      end

      it 'returns all available labels for the project, parent group and ancestor groups' do
        get api("/projects/#{project.id}/labels", user)

        expect(response).to have_gitlab_http_status(:ok)
        expect(response).to include_pagination_headers
        expect(json_response).to be_an Array
        expect(json_response).to all(match_schema('public_api/v4/labels/label'))
        expect(json_response.size).to eq(4)
        expect(json_response.map {|r| r['name'] }).to contain_exactly(group_label.name, subgroup_label.name, priority_label.name, label1.name)
      end
    end

    context 'when the include_ancestor_groups parameter is set to false' do
      let(:group) { create(:group) }
      let!(:group_label) { create(:group_label, title: 'feature', group: group) }
      let(:subgroup) { create(:group, parent: group) }
      let!(:subgroup_label) { create(:group_label, title: 'support', group: subgroup) }

      before do
        subgroup.add_owner(user)
        project.update!(group: subgroup)
      end

      it 'returns all available labels for the project and the parent group only' do
        get api("/projects/#{project.id}/labels", user), params: { include_ancestor_groups: false }

        expect(response).to have_gitlab_http_status(:ok)
        expect(response).to include_pagination_headers
        expect(json_response).to be_an Array
        expect(json_response).to all(match_schema('public_api/v4/labels/label'))
        expect(json_response.size).to eq(3)
        expect(json_response.map {|r| r['name'] }).to contain_exactly(subgroup_label.name, priority_label.name, label1.name)
      end
    end
  end

  describe 'POST /projects/:id/labels' do
    it 'returns created label when all params' do
      post api("/projects/#{project.id}/labels", user),
           params: {
             name: 'Foo',
             color: '#FFAABB',
             description: 'test',
             priority: 2
           }

      expect(response).to have_gitlab_http_status(:created)
      expect(json_response['name']).to eq('Foo')
      expect(json_response['color']).to eq('#FFAABB')
      expect(json_response['description']).to eq('test')
      expect(json_response['priority']).to eq(2)
    end

    it 'returns created label when only required params' do
      post api("/projects/#{project.id}/labels", user),
           params: {
             name: 'Foo & Bar',
             color: '#FFAABB'
           }

      expect(response).to have_gitlab_http_status(:created)
      expect(json_response['name']).to eq('Foo & Bar')
      expect(json_response['color']).to eq('#FFAABB')
      expect(json_response['description']).to be_nil
      expect(json_response['priority']).to be_nil
    end

    it 'creates a prioritized label' do
      post api("/projects/#{project.id}/labels", user),
           params: {
             name: 'Foo & Bar',
             color: '#FFAABB',
             priority: 3
           }

      expect(response).to have_gitlab_http_status(:created)
      expect(json_response['name']).to eq('Foo & Bar')
      expect(json_response['color']).to eq('#FFAABB')
      expect(json_response['description']).to be_nil
      expect(json_response['priority']).to eq(3)
    end

    it 'returns a 400 bad request if name not given' do
      post api("/projects/#{project.id}/labels", user), params: { color: '#FFAABB' }
      expect(response).to have_gitlab_http_status(:bad_request)
    end

    it 'returns a 400 bad request if color not given' do
      post api("/projects/#{project.id}/labels", user), params: { name: 'Foobar' }
      expect(response).to have_gitlab_http_status(:bad_request)
    end

    it 'returns 400 for invalid color' do
      post api("/projects/#{project.id}/labels", user),
           params: {
             name: 'Foo',
             color: '#FFAA'
           }
      expect(response).to have_gitlab_http_status(:bad_request)
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
    end

    it 'returns 400 for too long color code' do
      post api("/projects/#{project.id}/labels", user),
           params: {
             name: 'Foo',
             color: '#FFAAFFFF'
           }
      expect(response).to have_gitlab_http_status(:bad_request)
      expect(json_response['message']['color']).to eq(['must be a valid color code'])
    end

    it 'returns 400 for invalid name' do
      post api("/projects/#{project.id}/labels", user),
           params: {
             name: ',',
             color: '#FFAABB'
           }
      expect(response).to have_gitlab_http_status(:bad_request)
      expect(json_response['message']['title']).to eq(['is invalid'])
    end

    it 'returns 409 if label already exists in group' do
      group = create(:group)
      group_label = create(:group_label, group: group)
      project.update(group: group)

      post api("/projects/#{project.id}/labels", user),
           params: {
             name: group_label.name,
             color: '#FFAABB'
           }

      expect(response).to have_gitlab_http_status(:conflict)
      expect(json_response['message']).to eq('Label already exists')
    end

    it 'returns 400 for invalid priority' do
      post api("/projects/#{project.id}/labels", user),
           params: {
             name: 'Foo',
             color: '#FFAAFFFF',
             priority: 'foo'
           }

      expect(response).to have_gitlab_http_status(:bad_request)
    end

    it 'returns 409 if label already exists in project' do
      post api("/projects/#{project.id}/labels", user),
           params: {
             name: 'label1',
             color: '#FFAABB'
           }
      expect(response).to have_gitlab_http_status(:conflict)
      expect(json_response['message']).to eq('Label already exists')
    end
  end

  describe 'DELETE /projects/:id/labels' do
    it_behaves_like 'label delete API' do
      let(:spec_params) { { name: 'label1' } }
    end

    it_behaves_like 'label delete API' do
      let(:spec_params) { { label_id: label1.id } }
    end

    it 'returns 404 for non existing label' do
      delete api("/projects/#{project.id}/labels", user), params: { name: 'label2' }

      expect(response).to have_gitlab_http_status(:not_found)
      expect(json_response['message']).to eq('404 Label Not Found')
    end

    it 'returns 400 for wrong parameters' do
      delete api("/projects/#{project.id}/labels", user)

      expect(response).to have_gitlab_http_status(:bad_request)
    end

    it 'fails if label_id and name are given in params' do
      delete api("/projects/#{project.id}/labels", user),
          params: {
            label_id: label1.id,
            name: priority_label.name
          }

      expect(response).to have_gitlab_http_status(:bad_request)
    end

    it_behaves_like '412 response' do
      let(:request) { api("/projects/#{project.id}/labels", user) }
      let(:params) { { name: 'label1' } }
    end
  end

  describe 'PUT /projects/:id/labels' do
    context 'when using name' do
      it_behaves_like 'label update API' do
        let(:spec_params) { { name: 'label1' } }
        let(:expected_response_label_id) { label1.id }
      end
    end

    context 'when using label_id' do
      it_behaves_like 'label update API' do
        let(:spec_params) { { label_id: label1.id } }
        let(:expected_response_label_id) { label1.id }
      end
    end

    it 'returns 404 if label does not exist' do
      put api("/projects/#{project.id}/labels", user),
          params: {
            name: 'label2',
            new_name: 'label3'
          }

      expect(response).to have_gitlab_http_status(:not_found)
    end

    it 'returns 404 if label by id does not exist' do
      put api("/projects/#{project.id}/labels", user),
          params: {
            label_id: 0,
            new_name: 'label3'
          }

      expect(response).to have_gitlab_http_status(:not_found)
    end

    it 'returns 400 if no label name and id is given' do
      put api("/projects/#{project.id}/labels", user), params: { new_name: 'label2' }

      expect(response).to have_gitlab_http_status(:bad_request)
      expect(json_response['error']).to eq('label_id, name are missing, exactly one parameter must be provided')
    end

    it 'fails if label_id and name are given in params' do
      put api("/projects/#{project.id}/labels", user),
          params: {
            label_id: label1.id,
            name: priority_label.name,
            new_name: 'New Label'
          }

      expect(response).to have_gitlab_http_status(:bad_request)
    end
  end

  describe 'PUT /projects/:id/labels/promote' do
    let(:group) { create(:group) }

    before do
      group.add_owner(user)
      project.update!(group: group)
    end

    it 'returns 200 if label is promoted' do
      put api("/projects/#{project.id}/labels/promote", user), params: { name: label1.name }

      expect(response).to have_gitlab_http_status(:ok)
      expect(json_response['name']).to eq(label1.name)
      expect(json_response['color']).to eq(label1.color)
    end

    it 'returns 200 if group label already exists' do
      create(:group_label, title: label1.name, group: group)

      expect { put api("/projects/#{project.id}/labels/promote", user), params: { name: label1.name } }
        .to change(project.labels, :count).by(-1)
        .and change(group.labels, :count).by(0)

      expect(response).to have_gitlab_http_status(:ok)
    end

    it 'returns 403 if guest promotes label' do
      guest = create(:user)
      project.add_guest(guest)

      put api("/projects/#{project.id}/labels/promote", guest), params: { name: label1.name }

      expect(response).to have_gitlab_http_status(:forbidden)
    end

    it 'returns 404 if label does not exist' do
      put api("/projects/#{project.id}/labels/promote", user), params: { name: 'unknown' }

      expect(response).to have_gitlab_http_status(:not_found)
    end

    it 'returns 400 if no label name given' do
      put api("/projects/#{project.id}/labels/promote", user)

      expect(response).to have_gitlab_http_status(:bad_request)
      expect(json_response['error']).to eq('name is missing')
    end
  end

  describe "POST /projects/:id/labels/:label_id/subscribe" do
    context "when label_id is a label title" do
      it "subscribes to the label" do
        post api("/projects/#{project.id}/labels/#{label1.title}/subscribe", user)

        expect(response).to have_gitlab_http_status(:created)
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_truthy
      end
    end

    context "when label_id is a label ID" do
      it "subscribes to the label" do
        post api("/projects/#{project.id}/labels/#{label1.id}/subscribe", user)

        expect(response).to have_gitlab_http_status(:created)
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_truthy
      end
    end

    context "when user is already subscribed to label" do
      before do
        label1.subscribe(user, project)
      end

      it "returns 304" do
        post api("/projects/#{project.id}/labels/#{label1.id}/subscribe", user)

        expect(response).to have_gitlab_http_status(:not_modified)
      end
    end

    context "when label ID is not found" do
      it "returns 404 error" do
        post api("/projects/#{project.id}/labels/#{non_existing_record_id}/subscribe", user)

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe "POST /projects/:id/labels/:label_id/unsubscribe" do
    before do
      label1.subscribe(user, project)
    end

    context "when label_id is a label title" do
      it "unsubscribes from the label" do
        post api("/projects/#{project.id}/labels/#{label1.title}/unsubscribe", user)

        expect(response).to have_gitlab_http_status(:created)
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_falsey
      end
    end

    context "when label_id is a label ID" do
      it "unsubscribes from the label" do
        post api("/projects/#{project.id}/labels/#{label1.id}/unsubscribe", user)

        expect(response).to have_gitlab_http_status(:created)
        expect(json_response["name"]).to eq(label1.title)
        expect(json_response["subscribed"]).to be_falsey
      end
    end

    context "when user is already unsubscribed from label" do
      before do
        label1.unsubscribe(user, project)
      end

      it "returns 304" do
        post api("/projects/#{project.id}/labels/#{label1.id}/unsubscribe", user)

        expect(response).to have_gitlab_http_status(:not_modified)
      end
    end

    context "when label ID is not found" do
      it "returns 404 error" do
        post api("/projects/#{project.id}/labels/#{non_existing_record_id}/unsubscribe", user)

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end
end