File: assignees_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 (126 lines) | stat: -rw-r--r-- 3,443 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WorkItems::Callbacks::Assignees, :freeze_time, feature_category: :portfolio_management do
  let_it_be(:reporter) { create(:user) }
  let_it_be(:project) { create(:project, :private, reporters: reporter) }
  let_it_be(:new_assignee) { create(:user, guest_of: project) }

  let(:work_item) do
    create(:work_item, project: project, updated_at: 1.day.ago)
  end

  let(:current_user) { reporter }
  let(:params) { { assignee_ids: [new_assignee.id] } }
  let(:service) { described_class.new(issuable: work_item, current_user: current_user, params: params) }

  shared_examples 'assignee callback' do
    it 'updates the assignees' do
      assignees_callback

      expect(work_item.assignee_ids).to contain_exactly(new_assignee.id)
    end

    context 'when passing an empty array' do
      let(:params) { { assignee_ids: [] } }

      before do
        work_item.assignee_ids = [reporter.id]
      end

      it 'removes existing assignees' do
        assignees_callback

        expect(work_item.assignee_ids).to be_empty
      end
    end

    context 'when user does not have access' do
      let(:current_user) { create(:user) }

      it 'does not update the assignees' do
        assignees_callback

        expect(work_item.assignee_ids).to be_empty
      end
    end

    context 'when multiple assignees are given' do
      let(:params) { { assignee_ids: [new_assignee.id, reporter.id] } }

      context 'when work item allows multiple assignees' do
        before do
          allow(work_item).to receive(:allows_multiple_assignees?).and_return(true)
        end

        it 'sets all the given assignees' do
          assignees_callback

          expect(work_item.assignee_ids).to contain_exactly(new_assignee.id, reporter.id)
        end
      end

      context 'when work item does not allow multiple assignees' do
        before do
          allow(work_item).to receive(:allows_multiple_assignees?).and_return(false)
        end

        it 'only sets the first assignee' do
          assignees_callback

          expect(work_item.assignee_ids).to contain_exactly(new_assignee.id)
        end
      end
    end

    context 'when assignee does not have access to the work item' do
      let(:params) { { assignee_ids: [create(:user).id] } }

      it 'does not set the assignee' do
        assignees_callback

        expect(work_item.assignee_ids).to be_empty
      end
    end

    context 'when assignee ids are the same as the existing ones' do
      before do
        work_item.assignee_ids = [new_assignee.id]
      end

      it 'does not touch updated_at' do
        assignees_callback

        expect(work_item.assignee_ids).to contain_exactly(new_assignee.id)
      end
    end

    context 'when widget does not exist in new type' do
      let(:params) { {} }

      before do
        allow(service).to receive(:excluded_in_new_type?).and_return(true)
        work_item.assignee_ids = [new_assignee.id]
      end

      it "resets the work item's assignees" do
        assignees_callback

        expect(work_item.assignee_ids).to be_empty
      end
    end
  end

  describe '#before_create' do
    subject(:assignees_callback) { service.before_create }

    it_behaves_like 'assignee callback'
  end

  describe '#before_update' do
    subject(:assignees_callback) { service.before_update }

    it_behaves_like 'assignee callback'
  end
end