File: create_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (759 lines) | stat: -rw-r--r-- 24,333 bytes parent folder | download
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Create a work item', feature_category: :team_planning do
  include GraphqlHelpers

  let_it_be(:group) { create(:group) }
  let_it_be(:project) { create(:project, group: group) }
  let_it_be(:developer) { create(:user, developer_of: group) }

  let(:input) do
    {
      'title' => 'new title',
      'description' => 'new description',
      'confidential' => true,
      'workItemTypeId' => WorkItems::Type.default_by_type(:task).to_gid.to_s
    }
  end

  let(:fields) { nil }
  let(:mutation_response) { graphql_mutation_response(:work_item_create) }
  let(:current_user) { developer }

  RSpec.shared_examples 'creates work item' do
    it 'creates the work item' do
      expect { post_graphql_mutation(mutation, current_user: current_user) }
        .to change { WorkItem.count }.by(1)

      created_work_item = WorkItem.last
      expect(response).to have_gitlab_http_status(:success)
      expect(created_work_item).to be_confidential
      expect(created_work_item.work_item_type.base_type).to eq('task')
      expect(mutation_response['workItem']).to include(
        input.except('workItemTypeId').merge(
          'id' => created_work_item.to_gid.to_s,
          'workItemType' => hash_including('name' => 'Task')
        )
      )
    end

    context 'when input is invalid' do
      let(:input) { { 'title' => '', 'workItemTypeId' => WorkItems::Type.default_by_type(:task).to_gid.to_s } }

      it 'does not create and returns validation errors' do
        expect do
          post_graphql_mutation(mutation, current_user: current_user)
        end.to not_change(WorkItem, :count)

        expect(graphql_mutation_response(:work_item_create)['errors']).to contain_exactly("Title can't be blank")
      end
    end

    it_behaves_like 'has spam protection' do
      let(:mutation_class) { ::Mutations::WorkItems::Create }
    end

    context 'with description widget input' do
      let(:input) do
        {
          title: 'title',
          workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
          descriptionWidget: { description: 'some description' }
        }
      end

      let(:widgets_response) { mutation_response['workItem']['widgets'] }
      let(:fields) do
        <<~FIELDS
        workItem {
          widgets {
            type
            ... on WorkItemWidgetDescription {
              description
              lastEditedAt
              lastEditedBy {
                id
              }
            }
          }
        }
        errors
        FIELDS
      end

      it 'sets the description but does not set last_edited_at and last_edited_by' do
        post_graphql_mutation(mutation, current_user: current_user)

        expect(response).to have_gitlab_http_status(:success)
        expect(widgets_response).to include(
          {
            'type' => 'DESCRIPTION',
            'description' => 'some description',
            'lastEditedAt' => nil,
            'lastEditedBy' => nil
          }
        )
      end
    end

    context 'with hierarchy widget input' do
      let(:widgets_response) { mutation_response['workItem']['widgets'] }
      let(:fields) do
        <<~FIELDS
        workItem {
          widgets {
            type
            ... on WorkItemWidgetHierarchy {
              parent {
                id
              }
              children {
                edges {
                  node {
                    id
                  }
                }
              }
            }
          }
        }
        errors
        FIELDS
      end

      context 'when setting parent' do
        let_it_be(:parent) { create(:work_item, **container_params) }

        let(:input) do
          {
            title: 'item1',
            workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
            hierarchyWidget: { 'parentId' => parent.to_gid.to_s }
          }
        end

        it 'updates the work item parent' do
          post_graphql_mutation(mutation, current_user: current_user)

          expect(response).to have_gitlab_http_status(:success)
          expect(widgets_response).to include(
            {
              'children' => { 'edges' => [] },
              'parent' => { 'id' => parent.to_gid.to_s },
              'type' => 'HIERARCHY'
            }
          )
        end

        context 'when parent work item type is invalid' do
          let_it_be(:parent) { create(:work_item, :task, **container_params) }

          it 'returns error' do
            post_graphql_mutation(mutation, current_user: current_user)

            expect(mutation_response['errors'])
              .to contain_exactly(/cannot be added: it's not allowed to add this type of parent item/)
            expect(mutation_response['workItem']).to be_nil
          end
        end

        context 'when parent work item is not found' do
          let_it_be(:parent) { build_stubbed(:work_item, id: non_existing_record_id) }

          it 'returns a top level error' do
            post_graphql_mutation(mutation, current_user: current_user)

            expect(graphql_errors.first['message']).to include('No object found for `parentId')
          end
        end

        context 'when adjacent is already in place' do
          let_it_be(:adjacent) { create(:work_item, :task, **container_params) }

          let(:work_item) { WorkItem.last }

          let(:input) do
            {
              title: 'item1',
              workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
              hierarchyWidget: { 'parentId' => parent.to_gid.to_s }
            }
          end

          before_all do
            create(:parent_link, work_item_parent: parent, work_item: adjacent, relative_position: 0)
          end

          it 'creates work item and sets the relative position to be BEFORE adjacent' do
            expect { post_graphql_mutation(mutation, current_user: current_user) }
              .to change { WorkItem.count }.by(1)

            expect(response).to have_gitlab_http_status(:success)
            expect(widgets_response).to include(
              {
                'children' => { 'edges' => [] },
                'parent' => { 'id' => parent.to_gid.to_s },
                'type' => 'HIERARCHY'
              }
            )
            expect(work_item.parent_link.relative_position).to be < adjacent.parent_link.relative_position
          end
        end
      end

      context 'when unsupported widget input is sent' do
        let(:input) do
          {
            'title' => 'new title',
            'description' => 'new description',
            'workItemTypeId' => WorkItems::Type.default_by_type(:test_case).to_gid.to_s,
            'hierarchyWidget' => {}
          }
        end

        it_behaves_like 'a mutation that returns top-level errors',
          errors: ['Following widget keys are not supported by Test Case type: [:hierarchy_widget]']
      end
    end

    context 'with milestone widget input' do
      let(:widgets_response) { mutation_response['workItem']['widgets'] }
      let(:fields) do
        <<~FIELDS
        workItem {
          widgets {
            type
            ... on WorkItemWidgetMilestone {
              milestone {
                id
              }
            }
          }
        }
        errors
        FIELDS
      end

      context 'when setting milestone on work item creation' do
        let_it_be(:project_milestone) { create(:milestone, project: project) }
        let_it_be(:group_milestone) { create(:milestone, group: group) }

        let(:input) do
          {
            title: 'some WI',
            workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
            milestoneWidget: { 'milestoneId' => milestone.to_gid.to_s }
          }
        end

        shared_examples "work item's milestone is set" do
          it "sets the work item's milestone" do
            expect { post_graphql_mutation(mutation, current_user: current_user) }
              .to change { WorkItem.count }.by(1)

            expect(response).to have_gitlab_http_status(:success)
            expect(widgets_response).to include(
              {
                'type' => 'MILESTONE',
                'milestone' => { 'id' => milestone.to_gid.to_s }
              }
            )
          end
        end

        context 'when assigning a project milestone' do
          before do
            group_work_item = container_params[:namespace].present?
            skip('cannot set a project level milestone to a group level work item') if group_work_item
          end

          it_behaves_like "work item's milestone is set" do
            let(:milestone) { project_milestone }
          end
        end

        context 'when assigning a group milestone' do
          it_behaves_like "work item's milestone is set" do
            let(:milestone) { group_milestone }
          end
        end
      end
    end

    context 'with assignee widget input' do
      let(:widgets_response) { mutation_response['workItem']['widgets'] }
      let(:fields) do
        <<~FIELDS
          workItem {
            widgets {
              type
              ... on WorkItemWidgetAssignees {
                assignees {
                  nodes {
                    id
                    username
                  }
                }
              }
            }
          }
          errors
        FIELDS
      end

      context 'when setting assignee on work item creation' do
        let_it_be(:assignee) { create(:user, developer_of: project) }

        let(:input) do
          {
            title: 'some WI',
            workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
            assigneesWidget: { 'assigneeIds' => assignee.to_gid.to_s }
          }
        end

        it "sets the work item's assignee" do
          expect { post_graphql_mutation(mutation, current_user: current_user) }
            .to change { WorkItem.count }.by(1)

          expect(response).to have_gitlab_http_status(:success)
          expect(widgets_response).to include(
            {
              'assignees' => { 'nodes' => [{ 'id' => assignee.to_gid.to_s, 'username' => assignee.username }] },
              'type' => 'ASSIGNEES'
            }
          )
        end
      end
    end

    context 'with labels widget input' do
      let(:widgets_response) { mutation_response['workItem']['widgets'] }
      let(:fields) do
        <<~FIELDS
          workItem {
            widgets {
              type
              ... on WorkItemWidgetLabels {
                labels {
                  nodes { id }
                }
              }
            }
          }
          errors
        FIELDS
      end

      context 'when setting labels on work item creation' do
        let_it_be(:label1) { create(:group_label, group: group) }
        let_it_be(:label2) { create(:group_label, group: group) }
        let(:label_ids) { [label1.to_gid.to_s, label2.to_gid.to_s] }

        let(:input) do
          {
            title: 'some WI',
            workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
            labelsWidget: { labelIds: label_ids }
          }
        end

        it "sets the work item's labels" do
          expect { post_graphql_mutation(mutation, current_user: current_user) }
            .to change { WorkItem.count }.by(1)

          expect(response).to have_gitlab_http_status(:success)
          expect(mutation_response['workItem']['widgets']).to include(
            'labels' => {
              'nodes' => containing_exactly(
                hash_including('id' => label_ids.first.to_s),
                hash_including('id' => label_ids.second.to_s)
              )
            },
            'type' => 'LABELS'
          )
        end
      end
    end

    context 'with linked items widget input' do
      let_it_be(:item1_global_id) { create(:work_item, :task, project: project).to_global_id.to_s }
      let_it_be(:item2_global_id) { create(:work_item, :task, project: project).to_global_id.to_s }

      let(:widgets_response) { mutation_response['workItem']['widgets'] }

      let(:fields) do
        <<~FIELDS
        workItem {
          widgets {
            type
            ... on WorkItemWidgetLinkedItems {
              linkedItems {
                nodes {
                  linkType
                  workItem { id }
                }
              }
            }
          }
        }
        errors
        FIELDS
      end

      let(:input) do
        {
          title: 'item1',
          workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
          linkedItemsWidget: { 'workItemsIds' => [item1_global_id, item2_global_id], 'linkType' => 'RELATED' }
        }
      end

      it 'creates work item with related items' do
        expect { post_graphql_mutation(mutation, current_user: current_user) }
          .to change { WorkItem.count }.by(1)
          .and change { WorkItems::RelatedWorkItemLink.count }.by(2)

        # We don't control the order in which links are created and we don't need to.
        # Because of that, we can't control the order of the returned linked items. But we do want to assert they are
        # ordered by `"issue_links"."id" DESC` when fetched from the API
        expected_ordered_linked_items = WorkItems::RelatedWorkItemLink.order(id: :desc).limit(2).map do |linked_item|
          { 'linkType' => 'relates_to', "workItem" => { "id" => linked_item.target.to_gid.to_s } }
        end

        expect(response).to have_gitlab_http_status(:success)
        expect(widgets_response).to include(
          {
            'linkedItems' => { 'nodes' => expected_ordered_linked_items },
            'type' => 'LINKED_ITEMS'
          }
        )
      end

      context 'when number of items exceeds maximum allowed' do
        before do
          stub_const('Types::WorkItems::Widgets::LinkedItemsCreateInputType::MAX_WORK_ITEMS', 1)
        end

        it_behaves_like 'a mutation that returns top-level errors',
          errors: [Types::WorkItems::Widgets::LinkedItemsCreateInputType::ERROR_MESSAGE]
      end

      context 'with invalid items' do
        let_it_be(:private_project) { create(:project, :private) }
        let_it_be(:item1_global_id) { create(:work_item, :task, project: private_project).to_global_id.to_s }
        let_it_be(:item2_global_id) { create(:work_item, :task, project: private_project).to_global_id.to_s }

        it 'creates the work item without linking items' do
          expect { post_graphql_mutation(mutation, current_user: current_user) }
            .to change { WorkItem.count }.by(1)
            .and not_change { WorkItems::RelatedWorkItemLink.count }

          expect(mutation_response['errors']).to contain_exactly(
            'No matching work item found. Make sure you are adding a valid ID and you have access to the item.'
          )
        end
      end
    end

    context 'with due and start date widget input', :freeze_time do
      let(:start_date) { Date.today }
      let(:due_date) { 1.week.from_now.to_date }
      let(:fields) do
        <<~FIELDS
          workItem {
            widgets {
              type
              ... on WorkItemWidgetStartAndDueDate {
                startDate
                dueDate
              }
              ... on WorkItemWidgetDescription {
                description
              }
            }
          }
          errors
        FIELDS
      end

      let(:input) do
        {
          'title' => 'new title',
          'description' => 'new description',
          'confidential' => true,
          'workItemTypeId' => WorkItems::Type.default_by_type(:task).to_gid.to_s,
          'startAndDueDateWidget' => {
            'startDate' => start_date.to_s,
            'dueDate' => due_date.to_s
          }
        }
      end

      it 'updates start and due date' do
        expect { post_graphql_mutation(mutation, current_user: current_user) }
          .to change { WorkItem.count }.by(1)

        expect(response).to have_gitlab_http_status(:success)
        expect(mutation_response['workItem']['widgets']).to include(
          {
            'startDate' => start_date.to_s,
            'dueDate' => due_date.to_s,
            'type' => 'START_AND_DUE_DATE'
          }
        )
      end
    end
  end

  context 'when the user is not allowed to create a work item' do
    let(:current_user) { create(:user) }
    let(:mutation) { graphql_mutation(:workItemCreate, input.merge('projectPath' => project.full_path), fields) }

    it_behaves_like 'a mutation that returns a top-level access error'
  end

  context 'when user has permissions to create a work item' do
    context 'when creating work items in a project' do
      context 'with projectPath' do
        let_it_be(:container_params) { { project: project } }
        let(:mutation) { graphql_mutation(:workItemCreate, input.merge('projectPath' => project.full_path), fields) }

        it_behaves_like 'creates work item'
      end

      context 'with namespacePath' do
        let_it_be(:container_params) { { project: project } }
        let(:mutation) { graphql_mutation(:workItemCreate, input.merge('namespacePath' => project.full_path), fields) }

        it_behaves_like 'creates work item'

        context 'when the namespace_level_work_items feature flag is disabled' do
          before do
            stub_feature_flags(namespace_level_work_items: false)
          end

          it_behaves_like 'creates work item'
        end
      end
    end

    context 'when creating work items in a group' do
      let_it_be(:container_params) { { namespace: group } }
      let(:mutation) { graphql_mutation(:workItemCreate, input.merge(namespacePath: group.full_path), fields) }

      it 'does not create the work item' do
        expect { post_graphql_mutation(mutation, current_user: current_user) }
          .not_to change { WorkItem.count }
      end

      it_behaves_like 'a mutation that returns top-level errors', errors: [
        "The resource that you are attempting to access does not exist or you don't have " \
          "permission to perform this action"
      ]
    end

    context 'when both projectPath and namespacePath are passed' do
      let_it_be(:container_params) { { project: project } }
      let(:mutation) do
        graphql_mutation(
          :workItemCreate,
          input.merge('projectPath' => project.full_path, 'namespacePath' => project.full_path),
          fields
        )
      end

      it_behaves_like 'a mutation that returns top-level errors', errors: [
        Mutations::WorkItems::Create::MUTUALLY_EXCLUSIVE_ARGUMENTS_ERROR
      ]
    end

    context 'when neither of projectPath nor namespacePath are passed' do
      let_it_be(:container_params) { { project: project } }
      let(:mutation) do
        graphql_mutation(
          :workItemCreate,
          input,
          fields
        )
      end

      it_behaves_like 'a mutation that returns top-level errors', errors: [
        Mutations::WorkItems::Create::MUTUALLY_EXCLUSIVE_ARGUMENTS_ERROR
      ]
    end
  end

  context 'with time tracking widget input' do
    shared_examples 'mutation creating work item with time tracking data' do
      it 'creates work item with time tracking data' do
        expect { post_graphql_mutation(mutation, current_user: current_user) }
          .to change { WorkItem.count }.by(1)

        expect(mutation_response['workItem']['widgets']).to include(
          'timeEstimate' => 12.hours.to_i,
          'totalTimeSpent' => 2.hours.to_i,
          'timelogs' => {
            'nodes' => [
              {
                'timeSpent' => 2.hours.to_i
              }
            ]
          },
          'type' => 'TIME_TRACKING'
        )

        expect(mutation_response['workItem']['widgets']).to include(
          'description' => 'some description',
          'type' => 'DESCRIPTION'
        )
      end
    end

    let(:mutation) { graphql_mutation(:workItemCreate, input.merge('namespacePath' => project.full_path), fields) }
    let(:fields) do
      <<~FIELDS
        workItem {
          widgets {
            ... on WorkItemWidgetTimeTracking {
              type
              timeEstimate
              totalTimeSpent
              timelogs {
                nodes {
                  timeSpent
                }
              }
            }
            ... on WorkItemWidgetDescription {
              type
              description
            }
          }
        }
        errors
      FIELDS
    end

    context 'when adding time estimate and time spent' do
      context 'with quick action' do
        let(:input) do
          {
            title: 'item1',
            workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
            'descriptionWidget' => { 'description' => "some description\n\n/estimate 12h\n/spend 2h" }
          }
        end

        it_behaves_like 'mutation creating work item with time tracking data'
      end

      context 'when work item belongs directly to the group' do
        let(:input) do
          {
            title: 'item1',
            workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
            descriptionWidget: { description: "some description\n\n/estimate 12h\n/spend 2h" },
            namespacePath: group.full_path
          }
        end

        it_behaves_like 'mutation creating work item with time tracking data'
      end
    end

    context 'when the work item type does not support time tracking widget' do
      let(:input) do
        {
          title: 'item1',
          workItemTypeId: WorkItems::Type.default_by_type(:task).to_gid.to_s,
          'descriptionWidget' => { 'description' => "some description\n\n/estimate 12h\n/spend 2h" }
        }
      end

      before do
        WorkItems::Type.default_by_type(:task).widget_definitions
          .find_by_widget_type(:time_tracking).update!(disabled: true)
      end

      it 'ignores the quick action' do
        expect { post_graphql_mutation(mutation, current_user: current_user) }
          .to change { WorkItem.count }.by(1)

        expect(mutation_response['workItem']['widgets']).not_to include('type' => 'TIME_TRACKING')
        expect(mutation_response['workItem']['widgets']).to include(
          'description' => "some description",
          'type' => 'DESCRIPTION'
        )
      end
    end
  end

  context 'with CRM contacts widget input' do
    let(:mutation) { graphql_mutation(:workItemCreate, input.merge('namespacePath' => project.full_path), fields) }
    let(:fields) do
      <<~FIELDS
        workItem {
          widgets {
            ... on WorkItemWidgetCrmContacts {
              type
              contacts {
                nodes {
                  id
                  firstName
                }
              }
            }
          }
        }
        errors
      FIELDS
    end

    let_it_be(:contact) { create(:contact, group: project.group) }

    shared_examples 'mutation setting work item contacts' do
      it 'creates work item with contact data' do
        expect { post_graphql_mutation(mutation, current_user: current_user) }
          .to change { WorkItem.count }.by(1)

        expect(mutation_response['workItem']['widgets']).to include(
          'contacts' => {
            'nodes' => [
              {
                'id' => expected_result[:id],
                'firstName' => expected_result[:first_name]
              }
            ]
          },
          'type' => 'CRM_CONTACTS'
        )
      end
    end

    context 'when setting the contacts' do
      context 'when mutating the work item' do
        let(:input) do
          {
            'title' => 'item1',
            'workItemTypeId' => WorkItems::Type.default_by_type(:issue).to_gid.to_s,
            'crmContactsWidget' => {
              'contactIds' => [global_id_of(contact)]
            }
          }
        end

        let(:expected_result) do
          {
            id: global_id_of(contact).to_s,
            first_name: contact.first_name
          }
        end

        it_behaves_like 'mutation setting work item contacts'
      end
    end
  end
end