summaryrefslogtreecommitdiff
path: root/spec/models/todo_spec.rb
blob: caa0a886abf35b8921d8cecbf72b9f40c0e7258f (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Todo do
  let(:issue) { create(:issue) }

  describe 'relationships' do
    it { is_expected.to belong_to(:author).class_name("User") }
    it { is_expected.to belong_to(:note) }
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:group) }
    it { is_expected.to belong_to(:target).touch(true) }
    it { is_expected.to belong_to(:user) }
  end

  describe 'respond to' do
    it { is_expected.to respond_to(:author_name) }
    it { is_expected.to respond_to(:author_email) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:action) }
    it { is_expected.to validate_presence_of(:target_type) }
    it { is_expected.to validate_presence_of(:user) }
    it { is_expected.to validate_presence_of(:author) }

    context 'for commits' do
      subject { described_class.new(target_type: 'Commit') }

      it { is_expected.to validate_presence_of(:commit_id) }
      it { is_expected.not_to validate_presence_of(:target_id) }
    end

    context 'for issuables' do
      subject { described_class.new(target: issue) }

      it { is_expected.to validate_presence_of(:target_id) }
      it { is_expected.not_to validate_presence_of(:commit_id) }
    end
  end

  describe '#body' do
    before do
      subject.target = build(:issue, title: 'Bugfix')
    end

    it 'returns target title when note is blank' do
      subject.note = nil

      expect(subject.body).to eq 'Bugfix'
    end

    it 'returns note when note is present' do
      subject.note = build(:note, note: 'quick fix')

      expect(subject.body).to eq 'quick fix'
    end
  end

  describe '#done' do
    it 'changes state to done' do
      todo = create(:todo, state: :pending)

      expect { todo.done }.to change(todo, :state).from('pending').to('done')
    end

    it 'does not raise error when is already done' do
      todo = create(:todo, state: :done)

      expect { todo.done }.not_to raise_error
    end
  end

  describe '#for_commit?' do
    it 'returns true when target is a commit' do
      subject.target_type = 'Commit'

      expect(subject.for_commit?).to eq true
    end

    it 'returns false when target is an issuable' do
      subject.target_type = 'Issue'

      expect(subject.for_commit?).to eq false
    end
  end

  describe '#for_design?' do
    it 'returns true when target is a Design' do
      subject.target_type = 'DesignManagement::Design'

      expect(subject.for_design?).to eq(true)
    end

    it 'returns false when target is not a Design' do
      subject.target_type = 'Issue'

      expect(subject.for_design?).to eq(false)
    end
  end

  describe '#for_alert?' do
    it 'returns true when target is a Alert' do
      subject.target_type = 'AlertManagement::Alert'

      expect(subject.for_alert?).to eq(true)
    end

    it 'returns false when target is not a Alert' do
      subject.target_type = 'Issue'

      expect(subject.for_alert?).to eq(false)
    end
  end

  describe '#target' do
    context 'for commits' do
      let(:project) { create(:project, :repository) }
      let(:commit) { project.commit }

      it 'returns an instance of Commit when exists' do
        subject.project = project
        subject.target_type = 'Commit'
        subject.commit_id = commit.id

        expect(subject.target).to be_a(Commit)
        expect(subject.target).to eq commit
      end

      it 'returns nil when does not exists' do
        subject.project = project
        subject.target_type = 'Commit'
        subject.commit_id = 'xxxx'

        expect(subject.target).to be_nil
      end
    end

    it 'returns the issuable for issuables' do
      subject.target_id = issue.id
      subject.target_type = issue.class.name

      expect(subject.target).to eq issue
    end
  end

  describe '#target_reference' do
    it 'returns commit full reference with short id' do
      project = create(:project, :repository)
      commit = project.commit

      subject.project = project
      subject.target_type = 'Commit'
      subject.commit_id = commit.id

      expect(subject.target_reference).to eq commit.reference_link_text(full: false)
    end

    it 'returns full reference for issuables' do
      subject.target = issue

      expect(subject.target_reference).to eq issue.to_reference(full: false)
    end
  end

  describe '#self_added?' do
    let(:user_1) { build(:user) }

    before do
      subject.user = user_1
    end

    it 'is true when the user is the author' do
      subject.author = user_1

      expect(subject).to be_self_added
    end

    it 'is false when the user is not the author' do
      subject.author = build(:user)

      expect(subject).not_to be_self_added
    end
  end

  describe '#done?' do
    let_it_be(:todo1) { create(:todo, state: :pending) }
    let_it_be(:todo2) { create(:todo, state: :done) }

    it 'returns true for todos with done state' do
      expect(todo2.done?).to be_truthy
    end

    it 'returns false for todos with state pending' do
      expect(todo1.done?).to be_falsey
    end
  end

  describe '#self_assigned?' do
    let(:user_1) { build(:user) }

    context 'when self_added' do
      before do
        subject.user = user_1
        subject.author = user_1
      end

      it 'returns true for ASSIGNED' do
        subject.action = Todo::ASSIGNED

        expect(subject).to be_self_assigned
      end

      it 'returns true for REVIEW_REQUESTED' do
        subject.action = Todo::REVIEW_REQUESTED

        expect(subject).to be_self_assigned
      end

      it 'returns false for other action' do
        subject.action = Todo::MENTIONED

        expect(subject).not_to be_self_assigned
      end
    end

    context 'when todo is not self_added' do
      before do
        subject.user = user_1
        subject.author = build(:user)
      end

      it 'returns false' do
        subject.action = Todo::ASSIGNED

        expect(subject).not_to be_self_assigned
      end
    end
  end

  describe '.for_action' do
    it 'returns the todos for a given action' do
      create(:todo, action: Todo::MENTIONED)

      todo = create(:todo, action: Todo::ASSIGNED)

      expect(described_class.for_action(Todo::ASSIGNED)).to eq([todo])
    end
  end

  describe '.for_author' do
    it 'returns the todos for a given author' do
      user1 = create(:user)
      user2 = create(:user)
      todo = create(:todo, author: user1)

      create(:todo, author: user2)

      expect(described_class.for_author(user1)).to eq([todo])
    end
  end

  describe '.for_project' do
    it 'returns the todos for a given project' do
      project1 = create(:project)
      project2 = create(:project)
      todo = create(:todo, project: project1)

      create(:todo, project: project2)

      expect(described_class.for_project(project1)).to eq([todo])
    end

    it 'returns the todos for many projects' do
      project1 = create(:project)
      project2 = create(:project)
      project3 = create(:project)

      todo1 = create(:todo, project: project1)
      todo2 = create(:todo, project: project2)
      create(:todo, project: project3)

      expect(described_class.for_project([project2, project1])).to contain_exactly(todo2, todo1)
    end
  end

  describe '.for_undeleted_projects' do
    let(:project1) { create(:project) }
    let(:project2) { create(:project) }
    let(:project3) { create(:project) }

    let!(:todo1) { create(:todo, project: project1) }
    let!(:todo2) { create(:todo, project: project2) }
    let!(:todo3) { create(:todo, project: project3) }

    it 'returns the todos for a given project' do
      expect(described_class.for_undeleted_projects).to contain_exactly(todo1, todo2, todo3)
    end

    context 'when todo belongs to deleted project' do
      let(:project2) { create(:project, pending_delete: true) }

      it 'excludes todos of deleted projects' do
        expect(described_class.for_undeleted_projects).to contain_exactly(todo1, todo3)
      end
    end
  end

  describe '.for_group' do
    it 'returns the todos for a given group' do
      group1 = create(:group)
      group2 = create(:group)
      todo = create(:todo, group: group1)

      create(:todo, group: group2)

      expect(described_class.for_group(group1)).to eq([todo])
    end
  end

  describe '.for_type' do
    it 'returns the todos for a given target type' do
      todo = create(:todo, target: create(:issue))

      create(:todo, target: create(:merge_request))

      expect(described_class.for_type(Issue.name)).to eq([todo])
    end
  end

  describe '.for_target' do
    it 'returns the todos for a given target' do
      todo = create(:todo, target: create(:issue))

      create(:todo, target: create(:merge_request))

      expect(described_class.for_type(Issue.name).for_target(todo.target))
        .to contain_exactly(todo)
    end
  end

  describe '.for_commit' do
    it 'returns the todos for a commit ID' do
      todo = create(:todo, commit_id: '123')

      create(:todo, commit_id: '456')

      expect(described_class.for_commit('123')).to eq([todo])
    end
  end

  describe '.for_group_ids_and_descendants' do
    it 'returns the todos for a group and its descendants' do
      parent_group = create(:group)
      child_group = create(:group, parent: parent_group)

      todo1 = create(:todo, group: parent_group)
      todo2 = create(:todo, group: child_group)
      todos = described_class.for_group_ids_and_descendants([parent_group.id])

      expect(todos).to contain_exactly(todo1, todo2)
    end
  end

  describe '.for_user' do
    it 'returns the expected todos' do
      user1 = create(:user)
      user2 = create(:user)

      todo1 = create(:todo, user: user1)
      todo2 = create(:todo, user: user1)
      create(:todo, user: user2)

      expect(described_class.for_user(user1)).to contain_exactly(todo1, todo2)
    end
  end

  describe '.group_by_user_id_and_state' do
    let_it_be(:user1) { create(:user) }
    let_it_be(:user2) { create(:user) }

    before do
      create(:todo, user: user1, state: :pending)
      create(:todo, user: user1, state: :pending)
      create(:todo, user: user1, state: :done)
      create(:todo, user: user2, state: :pending)
    end

    specify do
      expect(Todo.count_grouped_by_user_id_and_state).to eq({ [user1.id, "done"] => 1, [user1.id, "pending"] => 2, [user2.id, "pending"] => 1 })
    end
  end

  describe '.any_for_target?' do
    it 'returns true if there are todos for a given target' do
      todo = create(:todo)

      expect(described_class.any_for_target?(todo.target)).to eq(true)
    end

    it 'returns true if there is at least one todo for a given target with state pending' do
      issue = create(:issue)
      create(:todo, state: :done, target: issue)
      create(:todo, state: :pending, target: issue)

      expect(described_class.any_for_target?(issue)).to eq(true)
    end

    it 'returns false if there are only todos for a given target with state done while searching for pending' do
      issue = create(:issue)
      create(:todo, state: :done, target: issue)
      create(:todo, state: :done, target: issue)

      expect(described_class.any_for_target?(issue, :pending)).to eq(false)
    end

    it 'returns false if there are no todos for a given target' do
      issue = create(:issue)

      expect(described_class.any_for_target?(issue)).to eq(false)
    end
  end

  describe '.batch_update' do
    it 'updates the state of todos' do
      todo = create(:todo, :pending)
      ids = described_class.batch_update(state: :done)

      todo.reload

      expect(ids).to eq([todo.id])
      expect(todo.state).to eq('done')
    end

    it 'does not update todos that already have the given state' do
      create(:todo, :pending)

      expect(described_class.batch_update(state: :pending)).to be_empty
    end

    it 'updates updated_at' do
      create(:todo, :pending)

      travel_to(1.day.from_now) do
        expected_update_date = Time.current.utc

        ids = described_class.batch_update(state: :done)

        expect(Todo.where(id: ids).map(&:updated_at)).to all(be_like_time(expected_update_date))
      end
    end
  end

  describe '.distinct_user_ids' do
    subject { described_class.distinct_user_ids }

    let_it_be(:user1) { create(:user) }
    let_it_be(:user2) { create(:user) }
    let_it_be(:todo) { create(:todo, user: user1) }
    let_it_be(:todo) { create(:todo, user: user1) }
    let_it_be(:todo) { create(:todo, user: user2) }

    it { is_expected.to contain_exactly(user1.id, user2.id) }
  end
end