File: common_system_notes_service_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 (188 lines) | stat: -rw-r--r-- 6,555 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Issuable::CommonSystemNotesService, feature_category: :team_planning do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { create(:user) }

  let(:issuable) { create(:issue, project: project) }

  shared_examples 'system note for issuable date changes' do
    it 'does not call SystemNoteService if no dates are changed' do
      issuable.update!(title: 'new title')

      expect(SystemNoteService).not_to receive(:change_start_date_or_due_date)

      execute_notes_service
    end

    context 'and issuable is an Issue' do
      it 'creates a system note for due_date set' do
        issuable.update!(due_date: Date.today)

        expect { execute_notes_service }.to change { issuable.notes.count }.from(0).to(1)
        expect(issuable.notes.last.note).to match('changed due date to')
      end

      it 'creates a system note for start_date set' do
        issuable.update!(start_date: Date.today)

        expect { execute_notes_service }.to change { issuable.notes.count }.from(0).to(1)
        expect(issuable.notes.last.note).to match('changed start date to')
      end

      it 'creates a note when both start and due date are changed' do
        issuable.update!(start_date: Date.today, due_date: 1.week.from_now)

        expect { execute_notes_service }.to change { issuable.notes.count }.from(0).to(1)
        expect(issuable.notes.last.note).to match(/changed start date to.+and changed due date to/)
      end
    end

    context 'and issuable is a WorkItem' do
      let_it_be_with_reload(:issuable) { create(:work_item, :issue, project: project) }
      let(:dates_source) { create(:work_items_dates_source, work_item: issuable) }

      it 'creates a system note for due_date set' do
        dates_source.update!(due_date: Date.today)

        expect { execute_notes_service }.to change { issuable.notes.count }.from(0).to(1)
        expect(issuable.notes.last.note).to match('changed due date to')
      end

      it 'creates a system note for start_date set' do
        dates_source.update!(start_date: Date.today)

        expect { execute_notes_service }.to change { issuable.notes.count }.from(0).to(1)
        expect(issuable.notes.last.note).to match('changed start date to')
      end

      it 'creates a note when both start and due date are changed' do
        dates_source.update!(start_date: Date.today, due_date: 1.week.from_now)

        expect { execute_notes_service }.to change { issuable.notes.count }.from(0).to(1)
        expect(issuable.notes.last.note).to match(/changed start date to.+and changed due date to/)
      end
    end
  end

  context 'on issuable update' do
    it_behaves_like 'system note creation', { title: 'New title' }, 'changed title'
    it_behaves_like 'system note creation', { description: 'New description' }, 'changed the description'
    it_behaves_like 'system note creation', { discussion_locked: true }, 'locked the discussion in this issue'
    it_behaves_like 'system note creation', { time_estimate: 5 }, 'added time estimate of 5s'

    context 'when new label is added' do
      let(:label) { create(:label, project: project) }

      before do
        issuable.labels << label
        issuable.save!
      end

      it 'creates a resource label event' do
        described_class.new(project: project, current_user: user).execute(issuable, old_labels: [])
        event = issuable.reload.resource_label_events.last

        expect(event).not_to be_nil
        expect(event.label_id).to eq label.id
        expect(event.user_id).to eq user.id
      end
    end

    context 'with merge requests Draft note' do
      context 'and adding Draft note' do
        let(:issuable) { create(:merge_request, title: "merge request") }

        it_behaves_like 'system note creation',
          { title: "Draft: merge request" },
          'marked this merge request as **draft**'

        context 'and changing title' do
          before do
            issuable.update_attribute(:title, "Draft: changed title")
          end

          it_behaves_like 'draft notes creation', 'draft'
        end
      end

      context 'and removing Draft note' do
        let(:issuable) { create(:merge_request, title: "Draft: merge request") }

        it_behaves_like 'system note creation', { title: "merge request" }, 'marked this merge request as **ready**'

        context 'and changing title' do
          before do
            issuable.update_attribute(:title, "changed title")
          end

          it_behaves_like 'draft notes creation', 'ready'
        end
      end
    end

    context 'when changing dates' do
      it_behaves_like 'system note for issuable date changes' do
        subject(:execute_notes_service) do
          described_class
            .new(project: project, current_user: user)
            .execute(issuable)
        end
      end
    end
  end

  context 'on issuable create' do
    let(:issuable) { build(:issue, project: project) }

    subject(:execute_notes_service) do
      described_class
        .new(project: project, current_user: user)
        .execute(issuable, old_labels: [], is_update: false)
    end

    it 'does not create system note for title and description' do
      issuable.save!

      expect { subject }.not_to change { issuable.notes.count }
    end

    it 'creates a resource label event for labels added' do
      label = create(:label, project: project)

      issuable.labels << label
      issuable.save!

      expect { subject }.to change { issuable.resource_label_events.count }.from(0).to(1)

      event = issuable.reload.resource_label_events.last

      expect(event).not_to be_nil
      expect(event.label_id).to eq label.id
      expect(event.user_id).to eq user.id
    end

    context 'when changing milestones' do
      let_it_be(:milestone) { create(:milestone, project: project) }
      let_it_be(:issuable) { create(:issue, project: project, milestone: milestone) }

      it 'does not create a system note for milestone set' do
        expect { subject }.not_to change { issuable.notes.count }
      end

      it 'creates a milestone change event' do
        expect { subject }.to change { ResourceMilestoneEvent.count }.from(0).to(1)
      end
    end

    context 'when changing dates' do
      it_behaves_like 'system note for issuable date changes'
    end

    context 'when setting an estimae' do
      it_behaves_like 'system note creation', { time_estimate: 5 }, 'added time estimate of 5s', false
    end
  end
end