summaryrefslogtreecommitdiff
path: root/spec/models/concerns/mentionable_spec.rb
blob: 516c0fd75bcbf8a4cb2291aa2901218cb03188d7 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mentionable do
  before do
    stub_const('Example', Class.new)
    Example.class_eval do
      include Mentionable

      attr_accessor :project, :message
      attr_mentionable :message

      def author
        nil
      end
    end
  end

  describe 'references' do
    let(:project) { create(:project) }
    let(:mentionable) { Example.new }

    it 'excludes Jira references' do
      allow(project).to receive_messages(jira_tracker?: true)

      mentionable.project = project
      mentionable.message = 'JIRA-123'
      expect(mentionable.referenced_mentionables).to be_empty
    end
  end

  describe '#any_mentionable_attributes_changed?' do
    message = Struct.new(:text)

    let(:mentionable) { Example.new }
    let(:changes) do
      msg = message.new('test')

      changes = {}
      changes[msg] = ['', 'some message']
      changes[:random_sym_key] = ['', 'some message']
      changes["random_string_key"] = ['', 'some message']
      changes
    end

    it 'returns true with key string' do
      changes["message"] = ['', 'some message']

      allow(mentionable).to receive(:saved_changes).and_return(changes)

      expect(mentionable.send(:any_mentionable_attributes_changed?)).to be true
    end

    it 'returns false with key symbol' do
      changes[:message] = ['', 'some message']
      allow(mentionable).to receive(:saved_changes).and_return(changes)

      expect(mentionable.send(:any_mentionable_attributes_changed?)).to be false
    end

    it 'returns false when no attr_mentionable keys' do
      allow(mentionable).to receive(:saved_changes).and_return(changes)

      expect(mentionable.send(:any_mentionable_attributes_changed?)).to be false
    end
  end
end

RSpec.describe Issue, "Mentionable" do
  describe '#mentioned_users' do
    let!(:user) { create(:user, username: 'stranger') }
    let!(:user2) { create(:user, username: 'john') }
    let!(:user3) { create(:user, username: 'jim') }
    let(:issue) { create(:issue, description: "#{user.to_reference} mentioned") }

    subject { issue.mentioned_users }

    it { expect(subject).to contain_exactly(user) }

    context 'when a note on personal snippet' do
      let!(:note) { create(:note_on_personal_snippet, note: "#{user.to_reference} mentioned #{user3.to_reference}") }

      subject { note.mentioned_users }

      it { expect(subject).to contain_exactly(user, user3) }
    end
  end

  describe '#referenced_mentionables' do
    context 'with an issue on a private project' do
      let(:project) { create(:project, :public) }
      let(:issue) { create(:issue, project: project) }
      let(:public_issue) { create(:issue, project: project) }
      let(:private_project) { create(:project, :private) }
      let(:private_issue) { create(:issue, project: private_project) }
      let(:user) { create(:user) }

      def referenced_issues(current_user)
        issue.title = "#{private_issue.to_reference(project)} and #{public_issue.to_reference}"
        issue.referenced_mentionables(current_user)
      end

      context 'when the current user can see the issue' do
        before do
          private_project.add_developer(user)
        end

        it 'includes the reference' do
          expect(referenced_issues(user)).to contain_exactly(private_issue, public_issue)
        end
      end

      context 'when the current user cannot see the issue' do
        it 'does not include the reference' do
          expect(referenced_issues(user)).to contain_exactly(public_issue)
        end
      end

      context 'when there is no current user' do
        it 'does not include the reference' do
          expect(referenced_issues(nil)).to contain_exactly(public_issue)
        end
      end
    end
  end

  describe '#create_cross_references!' do
    let(:project) { create(:project, :repository) }
    let(:author)  { build(:user) }
    let(:commit)  { project.commit }
    let(:commit2) { project.commit }

    let!(:issue) do
      create(:issue, project: project, description: "See #{commit.to_reference}")
    end

    it 'correctly removes already-mentioned Commits' do
      expect(SystemNoteService).not_to receive(:cross_reference)

      issue.create_cross_references!(author, [commit2])
    end
  end

  describe '#create_new_cross_references!' do
    let(:project) { create(:project) }
    let(:author)  { create(:author) }
    let(:issues)  { create_list(:issue, 2, project: project, author: author) }

    before do
      project.add_developer(author)
    end

    context 'before changes are persisted' do
      it 'ignores pre-existing references' do
        issue = create_issue(description: issues[0].to_reference)

        expect(SystemNoteService).not_to receive(:cross_reference)

        issue.description = 'New description'
        issue.create_new_cross_references!
      end

      it 'notifies new references' do
        issue = create_issue(description: issues[0].to_reference)

        expect(SystemNoteService).to receive(:cross_reference).with(issues[1], any_args)

        issue.description = issues[1].to_reference
        issue.create_new_cross_references!
      end
    end

    context 'after changes are persisted' do
      it 'ignores pre-existing references' do
        issue = create_issue(description: issues[0].to_reference)

        expect(SystemNoteService).not_to receive(:cross_reference)

        issue.update!(description: 'New description')
        issue.create_new_cross_references!
      end

      it 'notifies new references' do
        issue = create_issue(description: issues[0].to_reference)

        expect(SystemNoteService).to receive(:cross_reference).with(issues[1], any_args)

        issue.update!(description: issues[1].to_reference)
        issue.create_new_cross_references!
      end

      it 'notifies new references from project snippet note' do
        snippet = create(:snippet, project: project)
        note = create(:note, note: issues[0].to_reference, noteable: snippet, project: project, author: author)

        expect(SystemNoteService).to receive(:cross_reference).with(issues[1], any_args)

        note.update!(note: issues[1].to_reference)
        note.create_new_cross_references!
      end
    end

    def create_issue(description:)
      create(:issue, project: project, description: description, author: author)
    end
  end

  describe '#store_mentions!' do
    it_behaves_like 'mentions in description', :issue
    it_behaves_like 'mentions in notes', :issue do
      let(:note) { create(:note_on_issue) }
      let(:mentionable) { note.noteable }
    end
  end

  describe 'load mentions' do
    it_behaves_like 'load mentions from DB', :issue do
      let(:note) { create(:note_on_issue) }
      let(:mentionable) { note.noteable }
    end
  end
end

RSpec.describe Commit, 'Mentionable' do
  let(:project) { create(:project, :public, :repository) }
  let(:commit)  { project.commit }

  describe '#matches_cross_reference_regex?' do
    it "is false when message doesn't reference anything" do
      allow(commit.raw).to receive(:message).and_return "WIP: Do something"

      expect(commit.matches_cross_reference_regex?).to be_falsey
    end

    it 'is true if issue #number mentioned in title' do
      allow(commit.raw).to receive(:message).and_return "#1"

      expect(commit.matches_cross_reference_regex?).to be_truthy
    end

    it 'is true if references an MR' do
      allow(commit.raw).to receive(:message).and_return "See merge request !12"

      expect(commit.matches_cross_reference_regex?).to be_truthy
    end

    it 'is true if references a commit' do
      allow(commit.raw).to receive(:message).and_return "a1b2c3d4"

      expect(commit.matches_cross_reference_regex?).to be_truthy
    end

    it 'is true if issue referenced by url' do
      issue = create(:issue, project: project)

      allow(commit.raw).to receive(:message).and_return Gitlab::UrlBuilder.build(issue)

      expect(commit.matches_cross_reference_regex?).to be_truthy
    end

    context 'with external issue tracker' do
      let(:project) { create(:jira_project, :repository) }

      it 'is true if external issues referenced' do
        allow(commit.raw).to receive(:message).and_return 'JIRA-123'

        expect(commit.matches_cross_reference_regex?).to be_truthy
      end

      it 'is true if internal issues referenced' do
        allow(commit.raw).to receive(:message).and_return '#123'

        expect(commit.matches_cross_reference_regex?).to be_truthy
      end
    end
  end

  describe '#store_mentions!' do
    it_behaves_like 'mentions in notes', :commit do
      let(:note) { create(:note_on_commit) }
      let(:mentionable) { note.noteable }
    end
  end

  describe 'load mentions' do
    it_behaves_like 'load mentions from DB', :commit do
      let(:note) { create(:note_on_commit) }
      let(:mentionable) { note.noteable }
    end
  end
end

RSpec.describe MergeRequest, 'Mentionable' do
  describe '#store_mentions!' do
    it_behaves_like 'mentions in description', :merge_request
    it_behaves_like 'mentions in notes', :merge_request do
      let(:project) { create(:project) }
      let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
      let(:note) { create(:note_on_merge_request, noteable: merge_request, project: merge_request.project) }
      let(:mentionable) { note.noteable }
    end
  end

  describe 'load mentions' do
    it_behaves_like 'load mentions from DB', :merge_request do
      let(:project) { create(:project) }
      let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
      let(:note) { create(:note_on_merge_request, noteable: merge_request, project: merge_request.project) }
      let(:mentionable) { note.noteable }
    end
  end
end

RSpec.describe Snippet, 'Mentionable' do
  describe '#store_mentions!' do
    it_behaves_like 'mentions in description', :project_snippet
    it_behaves_like 'mentions in notes', :project_snippet do
      let(:note) { create(:note_on_project_snippet) }
      let(:mentionable) { note.noteable }
    end
  end

  describe 'load mentions' do
    it_behaves_like 'load mentions from DB', :project_snippet do
      let(:note) { create(:note_on_project_snippet) }
      let(:mentionable) { note.noteable }
    end
  end
end

RSpec.describe PersonalSnippet, 'Mentionable' do
  describe '#store_mentions!' do
    it_behaves_like 'mentions in description', :personal_snippet
    it_behaves_like 'mentions in notes', :personal_snippet do
      let(:note) { create(:note_on_personal_snippet) }
      let(:mentionable) { note.noteable }
    end
  end

  describe 'load mentions' do
    it_behaves_like 'load mentions from DB', :personal_snippet do
      let(:note) { create(:note_on_personal_snippet) }
      let(:mentionable) { note.noteable }
    end
  end
end

RSpec.describe DesignManagement::Design do
  describe '#store_mentions!' do
    it_behaves_like 'mentions in notes', :design do
      let(:note) { create(:diff_note_on_design) }
      let(:mentionable) { note.noteable }
    end
  end

  describe 'load mentions' do
    it_behaves_like 'load mentions from DB', :design do
      let(:note) { create(:diff_note_on_design) }
      let(:mentionable) { note.noteable }
    end
  end
end