notification_service_spec.rb 15.3 KB
Newer Older
1 2 3 4
require 'spec_helper'

describe NotificationService do
  let(:notification) { NotificationService.new }
D
Dmitriy Zaporozhets 已提交
5

6 7
  describe 'Keys' do
    describe :new_key do
D
Dmitriy Zaporozhets 已提交
8
      let!(:key) { create(:personal_key) }
9

10
      it { expect(notification.new_key(key)).to be_truthy }
11 12

      it 'should sent email to key owner' do
13
        expect(Notify).to receive(:new_ssh_key_email).with(key.id)
14 15 16 17 18
        notification.new_key(key)
      end
    end
  end

19 20
  describe 'Email' do
    describe :new_email do
D
Dmitriy Zaporozhets 已提交
21
      let!(:email) { create(:email) }
22

23
      it { expect(notification.new_email(email)).to be_truthy }
24 25

      it 'should send email to email owner' do
26
        expect(Notify).to receive(:new_email_email).with(email.id)
27 28 29 30 31
        notification.new_email(email)
      end
    end
  end

32
  describe 'Notes' do
33
    context 'issue note' do
D
Douwe Maan 已提交
34 35
      let(:project) { create(:empty_project, :public) }
      let(:issue) { create(:issue, project: project, assignee: create(:user)) }
36
      let(:mentioned_issue) { create(:issue, assignee: issue.assignee) }
37
      let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: '@mention referenced') }
38

39 40
      before do
        build_team(note.project)
41 42
      end

43 44
      describe :new_note do
        it do
V
tests  
Valery Sizov 已提交
45 46
          add_users_with_subscription(note.project, issue)

47 48 49
          should_email(@u_watcher.id)
          should_email(note.noteable.author_id)
          should_email(note.noteable.assignee_id)
50
          should_email(@u_mentioned.id)
V
tests  
Valery Sizov 已提交
51
          should_email(@subscriber.id)
52 53 54
          should_not_email(note.author_id)
          should_not_email(@u_participating.id)
          should_not_email(@u_disabled.id)
V
tests  
Valery Sizov 已提交
55 56
          should_not_email(@unsubscriber.id)

57 58 59
          notification.new_note(note)
        end

60
        it 'filters out "mentioned in" notes' do
61
          mentioned_note = Note.create_cross_reference_note(mentioned_issue, issue, issue.author)
62

63
          expect(Notify).not_to receive(:note_issue_email)
64 65
          notification.new_note(mentioned_note)
        end
M
Marin Jankovski 已提交
66 67 68 69
      end

      describe 'new note on issue in project that belongs to a group' do
        let(:group) { create(:group) }
70

M
Marin Jankovski 已提交
71 72
        before do
          note.project.namespace_id = group.id
73
          note.project.group.add_user(@u_watcher, GroupMember::MASTER)
M
Marin Jankovski 已提交
74
          note.project.save
75
          user_project = note.project.project_members.find_by_user_id(@u_watcher.id)
M
Marin Jankovski 已提交
76 77
          user_project.notification_level = Notification::N_PARTICIPATING
          user_project.save
78 79 80
          group_member = note.project.group.group_members.find_by_user_id(@u_watcher.id)
          group_member.notification_level = Notification::N_GLOBAL
          group_member.save
81 82
        end

M
Marin Jankovski 已提交
83 84 85 86 87 88 89 90 91
        it do
          should_email(note.noteable.author_id)
          should_email(note.noteable.assignee_id)
          should_email(@u_mentioned.id)
          should_not_email(@u_watcher.id)
          should_not_email(note.author_id)
          should_not_email(@u_participating.id)
          should_not_email(@u_disabled.id)
          notification.new_note(note)
92
        end
93
      end
M
Marin Jankovski 已提交
94 95

      def should_email(user_id)
96
        expect(Notify).to receive(:note_issue_email).with(user_id, note.id)
M
Marin Jankovski 已提交
97 98 99
      end

      def should_not_email(user_id)
100
        expect(Notify).not_to receive(:note_issue_email).with(user_id, note.id)
M
Marin Jankovski 已提交
101
      end
102
    end
103

104
    context 'issue note mention' do
D
Douwe Maan 已提交
105 106
      let(:project) { create(:empty_project, :public) }
      let(:issue) { create(:issue, project: project, assignee: create(:user)) }
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      let(:mentioned_issue) { create(:issue, assignee: issue.assignee) }
      let(:note) { create(:note_on_issue, noteable: issue, project_id: issue.project_id, note: '@all mentioned') }

      before do
        build_team(note.project)
      end

      describe :new_note do
        it do
          # Notify all team members
          note.project.team.members.each do |member|
            # User with disabled notification should not be notified
            next if member.id == @u_disabled.id
            should_email(member.id)
          end
          should_email(note.noteable.author_id)
          should_email(note.noteable.assignee_id)

          should_not_email(note.author_id)
126
          should_not_email(@u_mentioned.id)
127 128 129 130 131 132
          should_not_email(@u_disabled.id)
          should_not_email(@u_not_mentioned.id)
          notification.new_note(note)
        end

        it 'filters out "mentioned in" notes' do
133
          mentioned_note = Note.create_cross_reference_note(mentioned_issue, issue, issue.author)
134

135
          expect(Notify).not_to receive(:note_issue_email)
136 137 138 139 140
          notification.new_note(mentioned_note)
        end
      end

      def should_email(user_id)
141
        expect(Notify).to receive(:note_issue_email).with(user_id, note.id)
142 143 144
      end

      def should_not_email(user_id)
145
        expect(Notify).not_to receive(:note_issue_email).with(user_id, note.id)
146 147 148
      end
    end

149
    context 'commit note' do
D
Douwe Maan 已提交
150 151
      let(:project) { create(:project, :public) }
      let(:note) { create(:note_on_commit, project: project) }
152 153 154

      before do
        build_team(note.project)
D
Douwe Maan 已提交
155
        allow_any_instance_of(Commit).to receive(:author).and_return(@u_committer)
156 157
      end

158 159
      describe :new_note do
        it do
160
          should_email(@u_committer.id, note)
161 162 163 164 165
          should_email(@u_watcher.id, note)
          should_not_email(@u_mentioned.id, note)
          should_not_email(note.author_id, note)
          should_not_email(@u_participating.id, note)
          should_not_email(@u_disabled.id, note)
166 167 168 169
          notification.new_note(note)
        end

        it do
170 171 172 173 174 175 176 177
          note.update_attribute(:note, '@mention referenced')
          should_email(@u_committer.id, note)
          should_email(@u_watcher.id, note)
          should_email(@u_mentioned.id, note)
          should_not_email(note.author_id, note)
          should_not_email(@u_participating.id, note)
          should_not_email(@u_disabled.id, note)
          notification.new_note(note)
178 179
        end

180 181 182 183 184 185
        it do
          @u_committer.update_attributes(notification_level: Notification::N_MENTION)
          should_not_email(@u_committer.id, note)
          notification.new_note(note)
        end

186
        def should_email(user_id, n)
187
          expect(Notify).to receive(:note_commit_email).with(user_id, n.id)
188 189
        end

190
        def should_not_email(user_id, n)
191
          expect(Notify).not_to receive(:note_commit_email).with(user_id, n.id)
192
        end
193 194 195 196
      end
    end
  end

197
  describe 'Issues' do
D
Douwe Maan 已提交
198 199
    let(:project) { create(:empty_project, :public) }
    let(:issue) { create :issue, project: project, assignee: create(:user), description: 'cc @participant' }
200

201 202
    before do
      build_team(issue.project)
V
tests  
Valery Sizov 已提交
203
      add_users_with_subscription(issue.project, issue)
204 205
    end

206
    describe :new_issue do
207 208 209
      it do
        should_email(issue.assignee_id)
        should_email(@u_watcher.id)
210
        should_email(@u_participant_mentioned.id)
211
        should_not_email(@u_mentioned.id)
212 213 214 215 216
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.new_issue(issue, @u_disabled)
      end

217 218 219 220 221 222
      it do
        issue.assignee.update_attributes(notification_level: Notification::N_MENTION)
        should_not_email(issue.assignee_id)
        notification.new_issue(issue, @u_disabled)
      end

223
      def should_email(user_id)
224
        expect(Notify).to receive(:new_issue_email).with(user_id, issue.id)
225 226 227
      end

      def should_not_email(user_id)
228
        expect(Notify).not_to receive(:new_issue_email).with(user_id, issue.id)
229 230 231 232
      end
    end

    describe :reassigned_issue do
233 234 235
      it 'should email new assignee' do
        should_email(issue.assignee_id)
        should_email(@u_watcher.id)
236
        should_email(@u_participant_mentioned.id)
V
tests  
Valery Sizov 已提交
237 238
        should_email(@subscriber.id)
        should_not_email(@unsubscriber.id)
239 240 241 242 243 244 245
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)

        notification.reassigned_issue(issue, @u_disabled)
      end

      def should_email(user_id)
246
        expect(Notify).to receive(:reassigned_issue_email).with(user_id, issue.id, nil, @u_disabled.id)
247 248 249
      end

      def should_not_email(user_id)
250
        expect(Notify).not_to receive(:reassigned_issue_email).with(user_id, issue.id, issue.assignee_id, @u_disabled.id)
251 252 253 254 255
      end
    end

    describe :close_issue do
      it 'should sent email to issue assignee and issue author' do
256 257 258
        should_email(issue.assignee_id)
        should_email(issue.author_id)
        should_email(@u_watcher.id)
259
        should_email(@u_participant_mentioned.id)
V
tests  
Valery Sizov 已提交
260 261
        should_email(@subscriber.id)
        should_not_email(@unsubscriber.id)
262 263 264 265 266 267 268
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)

        notification.close_issue(issue, @u_disabled)
      end

      def should_email(user_id)
269
        expect(Notify).to receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
270 271 272
      end

      def should_not_email(user_id)
273
        expect(Notify).not_to receive(:closed_issue_email).with(user_id, issue.id, @u_disabled.id)
274 275
      end
    end
276 277 278 279 280 281

    describe :reopen_issue do
      it 'should send email to issue assignee and issue author' do
        should_email(issue.assignee_id)
        should_email(issue.author_id)
        should_email(@u_watcher.id)
282
        should_email(@u_participant_mentioned.id)
V
tests  
Valery Sizov 已提交
283 284
        should_email(@subscriber.id)
        should_not_email(@unsubscriber.id)
285 286 287 288 289 290 291
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)

        notification.reopen_issue(issue, @u_disabled)
      end

      def should_email(user_id)
292
        expect(Notify).to receive(:issue_status_changed_email).with(user_id, issue.id, 'reopened', @u_disabled.id)
293 294 295
      end

      def should_not_email(user_id)
296
        expect(Notify).not_to receive(:issue_status_changed_email).with(user_id, issue.id, 'reopened', @u_disabled.id)
297 298
      end
    end
299
  end
300 301

  describe 'Merge Requests' do
D
Douwe Maan 已提交
302 303
    let(:project) { create(:project, :public) }
    let(:merge_request) { create :merge_request, source_project: project, assignee: create(:user) }
304

305
    before do
I
Izaak Alpert 已提交
306
      build_team(merge_request.target_project)
V
tests  
Valery Sizov 已提交
307
      add_users_with_subscription(merge_request.target_project, merge_request)
308 309
    end

310
    describe :new_merge_request do
311 312 313 314 315 316 317 318 319
      it do
        should_email(merge_request.assignee_id)
        should_email(@u_watcher.id)
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.new_merge_request(merge_request, @u_disabled)
      end

      def should_email(user_id)
320
        expect(Notify).to receive(:new_merge_request_email).with(user_id, merge_request.id)
321 322
      end

323
      def should_not_email(user_id)
324
        expect(Notify).not_to receive(:new_merge_request_email).with(user_id, merge_request.id)
325 326
      end
    end
327 328 329 330 331

    describe :reassigned_merge_request do
      it do
        should_email(merge_request.assignee_id)
        should_email(@u_watcher.id)
V
tests  
Valery Sizov 已提交
332 333
        should_email(@subscriber.id)
        should_not_email(@unsubscriber.id)
334 335 336 337 338 339
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.reassigned_merge_request(merge_request, merge_request.author)
      end

      def should_email(user_id)
340
        expect(Notify).to receive(:reassigned_merge_request_email).with(user_id, merge_request.id, nil, merge_request.author_id)
341 342 343
      end

      def should_not_email(user_id)
344
        expect(Notify).not_to receive(:reassigned_merge_request_email).with(user_id, merge_request.id, merge_request.assignee_id, merge_request.author_id)
345 346 347 348 349 350 351
      end
    end

    describe :closed_merge_request do
      it do
        should_email(merge_request.assignee_id)
        should_email(@u_watcher.id)
V
tests  
Valery Sizov 已提交
352 353
        should_email(@subscriber.id)
        should_not_email(@unsubscriber.id)
354 355
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
356
        notification.close_mr(merge_request, @u_disabled)
357 358 359
      end

      def should_email(user_id)
360
        expect(Notify).to receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
361 362 363
      end

      def should_not_email(user_id)
364
        expect(Notify).not_to receive(:closed_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
365 366 367 368 369 370 371
      end
    end

    describe :merged_merge_request do
      it do
        should_email(merge_request.assignee_id)
        should_email(@u_watcher.id)
V
tests  
Valery Sizov 已提交
372 373
        should_email(@subscriber.id)
        should_not_email(@unsubscriber.id)
374 375
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
376
        notification.merge_mr(merge_request, @u_disabled)
377 378 379
      end

      def should_email(user_id)
380
        expect(Notify).to receive(:merged_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
381 382 383
      end

      def should_not_email(user_id)
384
        expect(Notify).not_to receive(:merged_merge_request_email).with(user_id, merge_request.id, @u_disabled.id)
385 386
      end
    end
387 388 389 390 391

    describe :reopen_merge_request do
      it do
        should_email(merge_request.assignee_id)
        should_email(@u_watcher.id)
V
tests  
Valery Sizov 已提交
392 393
        should_email(@subscriber.id)
        should_not_email(@unsubscriber.id)
394 395 396 397 398 399
        should_not_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.reopen_mr(merge_request, @u_disabled)
      end

      def should_email(user_id)
400
        expect(Notify).to receive(:merge_request_status_email).with(user_id, merge_request.id, 'reopened', @u_disabled.id)
401 402 403
      end

      def should_not_email(user_id)
404
        expect(Notify).not_to receive(:merge_request_status_email).with(user_id, merge_request.id, 'reopened', @u_disabled.id)
405 406
      end
    end
407 408
  end

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
  describe 'Projects' do
    let(:project) { create :project }

    before do
      build_team(project)
    end

    describe :project_was_moved do
      it do
        should_email(@u_watcher.id)
        should_email(@u_participating.id)
        should_not_email(@u_disabled.id)
        notification.project_was_moved(project)
      end

      def should_email(user_id)
425
        expect(Notify).to receive(:project_was_moved_email).with(project.id, user_id)
426 427 428
      end

      def should_not_email(user_id)
429
        expect(Notify).not_to receive(:project_was_moved_email).with(project.id, user_id)
430 431 432 433
      end
    end
  end

434
  def build_team(project)
D
Dmitriy Zaporozhets 已提交
435 436
    @u_watcher = create(:user, notification_level: Notification::N_WATCH)
    @u_participating = create(:user, notification_level: Notification::N_PARTICIPATING)
437
    @u_participant_mentioned = create(:user, username: 'participant', notification_level: Notification::N_PARTICIPATING)
D
Dmitriy Zaporozhets 已提交
438
    @u_disabled = create(:user, notification_level: Notification::N_DISABLED)
439
    @u_mentioned = create(:user, username: 'mention', notification_level: Notification::N_MENTION)
440
    @u_committer = create(:user, username: 'committer')
441
    @u_not_mentioned = create(:user, username: 'regular', notification_level: Notification::N_PARTICIPATING)
442 443 444 445

    project.team << [@u_watcher, :master]
    project.team << [@u_participating, :master]
    project.team << [@u_disabled, :master]
446
    project.team << [@u_mentioned, :master]
447
    project.team << [@u_committer, :master]
448
  end
V
tests  
Valery Sizov 已提交
449 450 451 452 453 454 455 456 457 458 459

  def add_users_with_subscription(project, issuable)
    @subscriber = create :user
    @unsubscriber = create :user

    project.team << [@subscriber, :master]
    project.team << [@unsubscriber, :master]

    issuable.subscriptions.create(user: @subscriber, subscribed: true)
    issuable.subscriptions.create(user: @unsubscriber, subscribed: false)
  end
460
end