summaryrefslogtreecommitdiff
path: root/spec/services/issuable/bulk_update_service_spec.rb
blob: befa65ec0f80f6268120048c30bd351fe6f143a7 (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
require 'spec_helper'

describe Issuable::BulkUpdateService do
  let(:user)    { create(:user) }
  let(:project) { create(:project, namespace: user.namespace) }

  def bulk_update(issuables, extra_params = {})
    bulk_update_params = extra_params
      .reverse_merge(issuable_ids: Array(issuables).map(&:id).join(','))

    type = Array(issuables).first.model_name.param_key
    Issuable::BulkUpdateService.new(project, user, bulk_update_params).execute(type)
  end

  describe 'close issues' do
    let(:issues) { create_list(:issue, 2, project: project) }

    it 'succeeds and returns the correct number of issues updated' do
      result = bulk_update(issues, state_event: 'close')

      expect(result[:success]).to be_truthy
      expect(result[:count]).to eq(issues.count)
    end

    it 'closes all the issues passed' do
      bulk_update(issues, state_event: 'close')

      expect(project.issues.opened).to be_empty
      expect(project.issues.closed).not_to be_empty
    end
  end

  describe 'reopen issues' do
    let(:issues) { create_list(:closed_issue, 2, project: project) }

    it 'succeeds and returns the correct number of issues updated' do
      result = bulk_update(issues, state_event: 'reopen')

      expect(result[:success]).to be_truthy
      expect(result[:count]).to eq(issues.count)
    end

    it 'reopens all the issues passed' do
      bulk_update(issues, state_event: 'reopen')

      expect(project.issues.closed).to be_empty
      expect(project.issues.opened).not_to be_empty
    end
  end

  describe 'updating merge request assignee' do
    let(:merge_request) { create(:merge_request, target_project: project, source_project: project, assignee: user) }

    context 'when the new assignee ID is a valid user' do
      it 'succeeds' do
        new_assignee = create(:user)
        project.team << [new_assignee, :developer]

        result = bulk_update(merge_request, assignee_id: new_assignee.id)

        expect(result[:success]).to be_truthy
        expect(result[:count]).to eq(1)
      end

      it 'updates the assignee to the user ID passed' do
        assignee = create(:user)
        project.team << [assignee, :developer]

        expect { bulk_update(merge_request, assignee_id: assignee.id) }
          .to change { merge_request.reload.assignee }.from(user).to(assignee)
      end
    end

    context "when the new assignee ID is #{IssuableFinder::NONE}" do
      it 'unassigns the issues' do
        expect { bulk_update(merge_request, assignee_id: IssuableFinder::NONE) }
          .to change { merge_request.reload.assignee }.to(nil)
      end
    end

    context 'when the new assignee ID is not present' do
      it 'does not unassign' do
        expect { bulk_update(merge_request, assignee_id: nil) }
          .not_to change { merge_request.reload.assignee }
      end
    end
  end

  describe 'updating issue assignee' do
    let(:issue) { create(:issue, project: project, assignees: [user]) }

    context 'when the new assignee ID is a valid user' do
      it 'succeeds' do
        new_assignee = create(:user)
        project.team << [new_assignee, :developer]

        result = bulk_update(issue, assignee_ids: [new_assignee.id])

        expect(result[:success]).to be_truthy
        expect(result[:count]).to eq(1)
      end

      it 'updates the assignee to the user ID passed' do
        assignee = create(:user)
        project.team << [assignee, :developer]
        expect { bulk_update(issue, assignee_ids: [assignee.id]) }
          .to change { issue.reload.assignees.first }.from(user).to(assignee)
      end
    end

    context "when the new assignee ID is #{IssuableFinder::NONE}" do
      it "unassigns the issues" do
        expect { bulk_update(issue, assignee_ids: [IssuableFinder::NONE.to_s]) }
          .to change { issue.reload.assignees.count }.from(1).to(0)
      end
    end

    context 'when the new assignee ID is not present' do
      it 'does not unassign' do
        expect { bulk_update(issue, assignee_ids: []) }
          .not_to change{ issue.reload.assignees }
      end
    end
  end

  describe 'updating milestones' do
    let(:issue)     { create(:issue, project: project) }
    let(:milestone) { create(:milestone, project: project) }

    it 'succeeds' do
      result = bulk_update(issue, milestone_id: milestone.id)

      expect(result[:success]).to be_truthy
      expect(result[:count]).to eq(1)
    end

    it 'updates the issue milestone' do
      expect { bulk_update(issue, milestone_id: milestone.id) }
        .to change { issue.reload.milestone }.from(nil).to(milestone)
    end
  end

  describe 'updating labels' do
    def create_issue_with_labels(labels)
      create(:labeled_issue, project: project, labels: labels)
    end

    let(:bug) { create(:label, project: project) }
    let(:regression) { create(:label, project: project) }
    let(:merge_requests) { create(:label, project: project) }

    let(:issue_all_labels) { create_issue_with_labels([bug, regression, merge_requests]) }
    let(:issue_bug_and_regression) { create_issue_with_labels([bug, regression]) }
    let(:issue_bug_and_merge_requests) { create_issue_with_labels([bug, merge_requests]) }
    let(:issue_no_labels) { create(:issue, project: project) }
    let(:issues) { [issue_all_labels, issue_bug_and_regression, issue_bug_and_merge_requests, issue_no_labels] }

    let(:labels) { [] }
    let(:add_labels) { [] }
    let(:remove_labels) { [] }

    let(:bulk_update_params) do
      {
        label_ids:        labels.map(&:id),
        add_label_ids:    add_labels.map(&:id),
        remove_label_ids: remove_labels.map(&:id)
      }
    end

    before do
      bulk_update(issues, bulk_update_params)
    end

    context 'when label_ids are passed' do
      let(:issues) { [issue_all_labels, issue_no_labels] }
      let(:labels) { [bug, regression] }

      it 'updates the labels of all issues passed to the labels passed' do
        expect(issues.map(&:reload).map(&:label_ids)).to all(match_array(labels.map(&:id)))
      end

      it 'does not update issues not passed in' do
        expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
      end

      context 'when those label IDs are empty' do
        let(:labels) { [] }

        it 'updates the issues passed to have no labels' do
          expect(issues.map(&:reload).map(&:label_ids)).to all(be_empty)
        end
      end
    end

    context 'when add_label_ids are passed' do
      let(:issues) { [issue_all_labels, issue_bug_and_merge_requests, issue_no_labels] }
      let(:add_labels) { [bug, regression, merge_requests] }

      it 'adds those label IDs to all issues passed' do
        expect(issues.map(&:reload).map(&:label_ids)).to all(include(*add_labels.map(&:id)))
      end

      it 'does not update issues not passed in' do
        expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
      end
    end

    context 'when remove_label_ids are passed' do
      let(:issues) { [issue_all_labels, issue_bug_and_merge_requests, issue_no_labels] }
      let(:remove_labels) { [bug, regression, merge_requests] }

      it 'removes those label IDs from all issues passed' do
        expect(issues.map(&:reload).map(&:label_ids)).to all(be_empty)
      end

      it 'does not update issues not passed in' do
        expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
      end
    end

    context 'when add_label_ids and remove_label_ids are passed' do
      let(:issues) { [issue_all_labels, issue_bug_and_merge_requests, issue_no_labels] }
      let(:add_labels) { [bug] }
      let(:remove_labels) { [merge_requests] }

      it 'adds the label IDs to all issues passed' do
        expect(issues.map(&:reload).map(&:label_ids)).to all(include(bug.id))
      end

      it 'removes the label IDs from all issues passed' do
        expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(merge_requests.id)
      end

      it 'does not update issues not passed in' do
        expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
      end
    end

    context 'when add_label_ids and label_ids are passed' do
      let(:issues) { [issue_all_labels, issue_bug_and_regression, issue_bug_and_merge_requests] }
      let(:labels) { [merge_requests] }
      let(:add_labels) { [regression] }

      it 'adds the label IDs to all issues passed' do
        expect(issues.map(&:reload).map(&:label_ids)).to all(include(regression.id))
      end

      it 'ignores the label IDs parameter' do
        expect(issues.map(&:reload).map(&:label_ids)).to all(include(bug.id))
      end

      it 'does not update issues not passed in' do
        expect(issue_no_labels.label_ids).to be_empty
      end
    end

    context 'when remove_label_ids and label_ids are passed' do
      let(:issues) { [issue_no_labels, issue_bug_and_regression] }
      let(:labels) { [merge_requests] }
      let(:remove_labels) { [regression] }

      it 'removes the label IDs from all issues passed' do
        expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(regression.id)
      end

      it 'ignores the label IDs parameter' do
        expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(merge_requests.id)
      end

      it 'does not update issues not passed in' do
        expect(issue_all_labels.label_ids).to contain_exactly(bug.id, regression.id, merge_requests.id)
      end
    end

    context 'when add_label_ids, remove_label_ids, and label_ids are passed' do
      let(:issues) { [issue_bug_and_merge_requests, issue_no_labels] }
      let(:labels) { [regression] }
      let(:add_labels) { [bug] }
      let(:remove_labels) { [merge_requests] }

      it 'adds the label IDs to all issues passed' do
        expect(issues.map(&:reload).map(&:label_ids)).to all(include(bug.id))
      end

      it 'removes the label IDs from all issues passed' do
        expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(merge_requests.id)
      end

      it 'ignores the label IDs parameter' do
        expect(issues.map(&:reload).map(&:label_ids).flatten).not_to include(regression.id)
      end

      it 'does not update issues not passed in' do
        expect(issue_bug_and_regression.label_ids).to contain_exactly(bug.id, regression.id)
      end
    end
  end

  describe 'subscribe to issues' do
    let(:issues) { create_list(:issue, 2, project: project) }

    it 'subscribes the given user' do
      bulk_update(issues, subscription_event: 'subscribe')

      expect(issues).to all(be_subscribed(user, project))
    end
  end

  describe 'unsubscribe from issues' do
    let(:issues) do
      create_list(:closed_issue, 2, project: project) do |issue|
        issue.subscriptions.create(user: user, project: project, subscribed: true)
      end
    end

    it 'unsubscribes the given user' do
      bulk_update(issues, subscription_event: 'unsubscribe')

      issues.each do |issue|
        expect(issue).not_to be_subscribed(user, project)
      end
    end
  end
end