notify_spec.rb 30.4 KB
Newer Older
1
require 'spec_helper'
R
Robert Speicher 已提交
2
require 'email_spec'
3 4 5 6

describe Notify do
  include EmailSpec::Helpers
  include EmailSpec::Matchers
D
Dmitriy Zaporozhets 已提交
7
  include RepoHelpers
8

9 10
  new_user_address = 'newguy@example.com'

11 12 13
  let(:gitlab_sender_display_name) { Gitlab.config.gitlab.email_display_name }
  let(:gitlab_sender) { Gitlab.config.gitlab.email_from }
  let(:gitlab_sender_reply_to) { Gitlab.config.gitlab.email_reply_to }
14
  let(:recipient) { create(:user, email: 'recipient@example.com') }
D
Dmitriy Zaporozhets 已提交
15
  let(:project) { create(:project) }
16
  let(:build) { create(:ci_build) }
17

18
  before(:each) do
19
    ActionMailer::Base.deliveries.clear
20 21 22 23
    email = recipient.emails.create(email: "notifications@example.com")
    recipient.update_attribute(:notification_email, email.email)
  end

R
Robb Kidd 已提交
24 25
  shared_examples 'a multiple recipients email' do
    it 'is sent to the given recipient' do
26
      is_expected.to deliver_to recipient.notification_email
R
Robb Kidd 已提交
27 28 29
    end
  end

30 31 32
  shared_examples 'an email sent from GitLab' do
    it 'is sent from GitLab' do
      sender = subject.header[:from].addrs[0]
33
      expect(sender.display_name).to eq(gitlab_sender_display_name)
34
      expect(sender.address).to eq(gitlab_sender)
35
    end
36 37 38 39 40

    it 'has a Reply-To address' do
      reply_to = subject.header[:reply_to].addresses
      expect(reply_to).to eq([gitlab_sender_reply_to])
    end
41 42
  end

43 44
  shared_examples 'an email starting a new thread' do |message_id_prefix|
    it 'has a discussion identifier' do
45 46
      is_expected.to have_header 'Message-ID',  /<#{message_id_prefix}(.*)@#{Gitlab.config.gitlab.host}>/
      is_expected.to have_header 'X-GitLab-Project', /#{project.name}/
47 48 49 50 51
    end
  end

  shared_examples 'an answer to an existing thread' do |thread_id_prefix|
    it 'has a subject that begins with Re: ' do
52
      is_expected.to have_subject /^Re: /
53 54 55
    end

    it 'has headers that reference an existing thread' do
56
      is_expected.to have_header 'Message-ID',  /<(.*)@#{Gitlab.config.gitlab.host}>/
57 58 59
      is_expected.to have_header 'References',  /<#{thread_id_prefix}(.*)@#{Gitlab.config.gitlab.host}>/
      is_expected.to have_header 'In-Reply-To', /<#{thread_id_prefix}(.*)@#{Gitlab.config.gitlab.host}>/
      is_expected.to have_header 'X-GitLab-Project', /#{project.name}/
60 61 62
    end
  end

63
  shared_examples 'a new user email' do |user_email, site_path|
64
    it 'is sent to the new user' do
65
      is_expected.to deliver_to user_email
66 67 68
    end

    it 'has the correct subject' do
69
      is_expected.to have_subject /^Account was created for you$/i
70 71 72
    end

    it 'contains the new user\'s login name' do
73
      is_expected.to have_body_text /#{user_email}/
74 75
    end

76 77 78 79 80
    it 'includes a link to the site' do
      is_expected.to have_body_text /#{site_path}/
    end
  end

81 82 83 84 85 86 87 88
  shared_examples 'it should have Gmail Actions links' do
    it { is_expected.to have_body_text /ViewAction/ }
  end

  shared_examples 'it should not have Gmail Actions links' do
    it { is_expected.to_not have_body_text /ViewAction/ }
  end

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  shared_examples 'it should show Gmail Actions View Issue link' do
    it_behaves_like 'it should have Gmail Actions links'

    it { is_expected.to have_body_text /View Issue/ }
  end

  shared_examples 'it should show Gmail Actions View Merge request link' do
    it_behaves_like 'it should have Gmail Actions links'

    it { is_expected.to have_body_text /View Merge request/ }
  end

  shared_examples 'it should show Gmail Actions View Commit link' do
    it_behaves_like 'it should have Gmail Actions links'

    it { is_expected.to have_body_text /View Commit/ }
  end

107 108 109 110 111 112 113 114 115 116
  describe 'for new users, the email' do
    let(:example_site_path) { root_path }
    let(:new_user) { create(:user, email: new_user_address, created_by_id: 1) }

    token = 'kETLwRaayvigPq_x3SNM'

    subject { Notify.new_user_email(new_user.id, token) }

    it_behaves_like 'an email sent from GitLab'
    it_behaves_like 'a new user email', new_user_address
117
    it_behaves_like 'it should not have Gmail Actions links'
118

M
Marin Jankovski 已提交
119
    it 'contains the password text' do
120
      is_expected.to have_body_text /Click here to set your password/
M
Marin Jankovski 已提交
121 122 123
    end

    it 'includes a link for user to set password' do
124
      params = "reset_password_token=#{token}"
125
      is_expected.to have_body_text(
126 127
        %r{http://localhost(:\d+)?/users/password/edit\?#{params}}
      )
128 129
    end

130 131 132
    it 'explains the reset link expiration' do
      is_expected.to have_body_text(/This link is valid for \d+ (hours?|days?)/)
      is_expected.to have_body_text(new_user_password_url)
133
      is_expected.to have_body_text(/\?user_email=.*%40.*/)
134
    end
135 136
  end

M
Marin Jankovski 已提交
137 138 139

  describe 'for users that signed up, the email' do
    let(:example_site_path) { root_path }
140
    let(:new_user) { create(:user, email: new_user_address, password: "securePassword") }
M
Marin Jankovski 已提交
141

142
    subject { Notify.new_user_email(new_user.id) }
M
Marin Jankovski 已提交
143

144
    it_behaves_like 'an email sent from GitLab'
145
    it_behaves_like 'a new user email', new_user_address
146
    it_behaves_like 'it should not have Gmail Actions links'
M
Marin Jankovski 已提交
147 148

    it 'should not contain the new user\'s password' do
149
      is_expected.not_to have_body_text /password/
M
Marin Jankovski 已提交
150 151 152
    end
  end

153 154 155 156 157
  describe 'user added ssh key' do
    let(:key) { create(:personal_key) }

    subject { Notify.new_ssh_key_email(key.id) }

158
    it_behaves_like 'an email sent from GitLab'
159
    it_behaves_like 'it should not have Gmail Actions links'
160

161
    it 'is sent to the new user' do
162
      is_expected.to deliver_to key.user.email
163 164 165
    end

    it 'has the correct subject' do
166
      is_expected.to have_subject /^SSH key was added to your account$/i
167 168 169
    end

    it 'contains the new ssh key title' do
170
      is_expected.to have_body_text /#{key.title}/
171 172 173
    end

    it 'includes a link to ssh keys page' do
174
      is_expected.to have_body_text /#{profile_keys_path}/
175 176 177
    end
  end

178 179 180 181 182
  describe 'user added email' do
    let(:email) { create(:email) }

    subject { Notify.new_email_email(email.id) }

183 184
    it_behaves_like 'it should not have Gmail Actions links'

185
    it 'is sent to the new user' do
186
      is_expected.to deliver_to email.user.email
187 188 189
    end

    it 'has the correct subject' do
190
      is_expected.to have_subject /^Email was added to your account$/i
191 192 193
    end

    it 'contains the new email address' do
194
      is_expected.to have_body_text /#{email.email}/
195 196 197
    end

    it 'includes a link to emails page' do
198
      is_expected.to have_body_text /#{profile_emails_path}/
199 200 201
    end
  end

R
Robb Kidd 已提交
202 203
  context 'for a project' do
    describe 'items that are assignable, the email' do
204
      let(:current_user) { create(:user, email: "current@email.com") }
205 206
      let(:assignee) { create(:user, email: 'assignee@example.com') }
      let(:previous_assignee) { create(:user, name: 'Previous Assignee') }
207

R
Robb Kidd 已提交
208
      shared_examples 'an assignee email' do
209 210
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
211 212
          expect(sender.display_name).to eq(current_user.name)
          expect(sender.address).to eq(gitlab_sender)
213 214
        end

R
Robb Kidd 已提交
215
        it 'is sent to the assignee' do
216
          is_expected.to deliver_to assignee.email
R
Robb Kidd 已提交
217 218
        end
      end
219

R
Robb Kidd 已提交
220
      context 'for issues' do
221
        let(:issue) { create(:issue, author: current_user, assignee: assignee, project: project) }
R
Robert Speicher 已提交
222
        let(:issue_with_description) { create(:issue, author: current_user, assignee: assignee, project: project, description: FFaker::Lorem.sentence) }
223

R
Robb Kidd 已提交
224
        describe 'that are new' do
225
          subject { Notify.new_issue_email(issue.assignee_id, issue.id) }
226

R
Robb Kidd 已提交
227
          it_behaves_like 'an assignee email'
228
          it_behaves_like 'an email starting a new thread', 'issue'
229
          it_behaves_like 'it should show Gmail Actions View Issue link'
230

R
Robb Kidd 已提交
231
          it 'has the correct subject' do
232
            is_expected.to have_subject /#{project.name} \| #{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
233
          end
234

R
Robb Kidd 已提交
235
          it 'contains a link to the new issue' do
V
Vinnie Okada 已提交
236
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
237 238
          end
        end
239

240 241 242
        describe 'that are new with a description' do
          subject { Notify.new_issue_email(issue_with_description.assignee_id, issue_with_description.id) }

243 244
          it_behaves_like 'it should show Gmail Actions View Issue link'

245
          it 'contains the description' do
246
            is_expected.to have_body_text /#{issue_with_description.description}/
247 248 249
          end
        end

R
Robb Kidd 已提交
250
        describe 'that have been reassigned' do
V
Valery Sizov 已提交
251
          subject { Notify.reassigned_issue_email(recipient.id, issue.id, previous_assignee.id, current_user.id) }
R
Robb Kidd 已提交
252 253

          it_behaves_like 'a multiple recipients email'
254
          it_behaves_like 'an answer to an existing thread', 'issue'
255
          it_behaves_like 'it should show Gmail Actions View Issue link'
R
Robb Kidd 已提交
256

257 258
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
259 260
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
261 262
          end

R
Robb Kidd 已提交
263
          it 'has the correct subject' do
264
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
265 266 267
          end

          it 'contains the name of the previous assignee' do
268
            is_expected.to have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
269 270 271
          end

          it 'contains the name of the new assignee' do
272
            is_expected.to have_body_text /#{assignee.name}/
R
Robb Kidd 已提交
273 274 275
          end

          it 'contains a link to the issue' do
V
Vinnie Okada 已提交
276
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
277 278
          end
        end
A
Alex Denisov 已提交
279 280 281

        describe 'status changed' do
          let(:status) { 'closed' }
V
Valery Sizov 已提交
282
          subject { Notify.issue_status_changed_email(recipient.id, issue.id, status, current_user.id) }
283

284
          it_behaves_like 'an answer to an existing thread', 'issue'
285
          it_behaves_like 'it should show Gmail Actions View Issue link'
286

287 288
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
289 290
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
291 292
          end

A
Alex Denisov 已提交
293
          it 'has the correct subject' do
294
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/i
A
Alex Denisov 已提交
295 296 297
          end

          it 'contains the new status' do
298
            is_expected.to have_body_text /#{status}/i
A
Alex Denisov 已提交
299 300 301
          end

          it 'contains the user name' do
302
            is_expected.to have_body_text /#{current_user.name}/i
A
Alex Denisov 已提交
303 304 305
          end

          it 'contains a link to the issue' do
V
Vinnie Okada 已提交
306
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
A
Alex Denisov 已提交
307
          end
308
        end
R
Robb Kidd 已提交
309 310 311
      end

      context 'for merge requests' do
312
        let(:merge_author) { create(:user) }
313
        let(:merge_request) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project) }
R
Robert Speicher 已提交
314
        let(:merge_request_with_description) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project, description: FFaker::Lorem.sentence) }
R
Robb Kidd 已提交
315 316

        describe 'that are new' do
317
          subject { Notify.new_merge_request_email(merge_request.assignee_id, merge_request.id) }
R
Robb Kidd 已提交
318 319

          it_behaves_like 'an assignee email'
320
          it_behaves_like 'an email starting a new thread', 'merge_request'
321
          it_behaves_like 'it should show Gmail Actions View Merge request link'
R
Robb Kidd 已提交
322 323

          it 'has the correct subject' do
324
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/
R
Robb Kidd 已提交
325 326 327
          end

          it 'contains a link to the new merge request' do
V
Vinnie Okada 已提交
328
            is_expected.to have_body_text /#{namespace_project_merge_request_path(project.namespace, project, merge_request)}/
R
Robb Kidd 已提交
329 330 331
          end

          it 'contains the source branch for the merge request' do
332
            is_expected.to have_body_text /#{merge_request.source_branch}/
R
Robb Kidd 已提交
333 334 335
          end

          it 'contains the target branch for the merge request' do
336
            is_expected.to have_body_text /#{merge_request.target_branch}/
R
Robb Kidd 已提交
337
          end
P
Philip Blatter 已提交
338 339

          it 'has the correct message-id set' do
340
            is_expected.to have_header 'Message-ID', "<merge_request_#{merge_request.id}@#{Gitlab.config.gitlab.host}>"
P
Philip Blatter 已提交
341
          end
R
Robb Kidd 已提交
342 343
        end

344 345 346
        describe 'that are new with a description' do
          subject { Notify.new_merge_request_email(merge_request_with_description.assignee_id, merge_request_with_description.id) }

347
          it_behaves_like 'it should show Gmail Actions View Merge request link'
348

349
          it 'contains the description' do
350
            is_expected.to have_body_text /#{merge_request_with_description.description}/
351 352 353
          end
        end

R
Robb Kidd 已提交
354
        describe 'that are reassigned' do
355
          subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id, current_user.id) }
R
Robb Kidd 已提交
356 357

          it_behaves_like 'a multiple recipients email'
358
          it_behaves_like 'an answer to an existing thread', 'merge_request'
359
          it_behaves_like 'it should show Gmail Actions View Merge request link'
R
Robb Kidd 已提交
360

361 362
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
363 364
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
365 366
          end

R
Robb Kidd 已提交
367
          it 'has the correct subject' do
368
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/
R
Robb Kidd 已提交
369 370 371
          end

          it 'contains the name of the previous assignee' do
372
            is_expected.to have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
373 374 375
          end

          it 'contains the name of the new assignee' do
376
            is_expected.to have_body_text /#{assignee.name}/
R
Robb Kidd 已提交
377 378 379
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
380
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
R
Robb Kidd 已提交
381
          end
382 383
        end

384 385
        describe 'status changed' do
          let(:status) { 'reopened' }
V
Valery Sizov 已提交
386
          subject { Notify.merge_request_status_email(recipient.id, merge_request.id, status, current_user.id) }
387 388

          it_behaves_like 'an answer to an existing thread', 'merge_request'
389
          it_behaves_like 'it should show Gmail Actions View Merge request link'
390 391 392

          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
393 394
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
395 396 397
          end

          it 'has the correct subject' do
398
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/i
399 400 401
          end

          it 'contains the new status' do
402
            is_expected.to have_body_text /#{status}/i
403 404 405
          end

          it 'contains the user name' do
406
            is_expected.to have_body_text /#{current_user.name}/i
407 408 409
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
410
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
411 412 413
          end
        end

414 415 416 417
        describe 'that are merged' do
          subject { Notify.merged_merge_request_email(recipient.id, merge_request.id, merge_author.id) }

          it_behaves_like 'a multiple recipients email'
418
          it_behaves_like 'an answer to an existing thread', 'merge_request'
419
          it_behaves_like 'it should show Gmail Actions View Merge request link'
420 421 422

          it 'is sent as the merge author' do
            sender = subject.header[:from].addrs[0]
423 424
            expect(sender.display_name).to eq(merge_author.name)
            expect(sender.address).to eq(gitlab_sender)
425 426 427
          end

          it 'has the correct subject' do
428
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/
429
          end
R
Robb Kidd 已提交
430

431
          it 'contains the new status' do
432
            is_expected.to have_body_text /merged/i
433 434 435
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
436
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
437
          end
R
Robb Kidd 已提交
438 439
        end
      end
440 441
    end

442 443 444
    describe 'project was moved' do
      let(:project) { create(:project) }
      let(:user) { create(:user) }
445
      subject { Notify.project_was_moved_email(project.id, user.id, "gitlab/gitlab") }
446

447
      it_behaves_like 'an email sent from GitLab'
448
      it_behaves_like 'it should not have Gmail Actions links'
449

450
      it 'has the correct subject' do
451
        is_expected.to have_subject /Project was moved/
452 453 454
      end

      it 'contains name of project' do
455
        is_expected.to have_body_text /#{project.name_with_namespace}/
456 457 458
      end

      it 'contains new user role' do
459
        is_expected.to have_body_text /#{project.ssh_url_to_repo}/
460 461 462
      end
    end

463
    describe 'project access changed' do
464
      let(:project) { create(:project) }
465
      let(:user) { create(:user) }
466
      let(:project_member) { create(:project_member, project: project, user: user) }
467
      subject { Notify.project_access_granted_email(project_member.id) }
468 469

      it_behaves_like 'an email sent from GitLab'
470
      it_behaves_like 'it should not have Gmail Actions links'
471

472
      it 'has the correct subject' do
473
        is_expected.to have_subject /Access to project was granted/
474
      end
475

476
      it 'contains name of project' do
477
        is_expected.to have_body_text /#{project.name}/
478
      end
479

480
      it 'contains new user role' do
481
        is_expected.to have_body_text /#{project_member.human_access}/
482 483 484
      end
    end

R
Robb Kidd 已提交
485
    context 'items that are noteable, the email for a note' do
486 487
      let(:note_author) { create(:user, name: 'author_name') }
      let(:note) { create(:note, project: project, author: note_author) }
R
Robb Kidd 已提交
488

489
      before :each do
490
        allow(Note).to receive(:find).with(note.id).and_return(note)
491 492
      end

R
Robb Kidd 已提交
493
      shared_examples 'a note email' do
494 495
        it_behaves_like 'it should have Gmail Actions links'

496 497
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
498 499
          expect(sender.display_name).to eq(note_author.name)
          expect(sender.address).to eq(gitlab_sender)
500 501
        end

R
Robb Kidd 已提交
502
        it 'is sent to the given recipient' do
503
          is_expected.to deliver_to recipient.notification_email
R
Robb Kidd 已提交
504 505 506
        end

        it 'contains the message from the note' do
507
          is_expected.to have_body_text /#{note.note}/
R
Robb Kidd 已提交
508 509 510 511
        end
      end

      describe 'on a commit' do
512
        let(:commit) { project.commit }
D
Dmitriy Zaporozhets 已提交
513

514
        before(:each) { allow(note).to receive(:noteable).and_return(commit) }
R
Robb Kidd 已提交
515

D
Dmitriy Zaporozhets 已提交
516
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
517 518

        it_behaves_like 'a note email'
519
        it_behaves_like 'an answer to an existing thread', 'commit'
520
        it_behaves_like 'it should show Gmail Actions View Commit link'
R
Robb Kidd 已提交
521 522

        it 'has the correct subject' do
523
          is_expected.to have_subject /#{commit.title} \(#{commit.short_id}\)/
R
Robb Kidd 已提交
524 525 526
        end

        it 'contains a link to the commit' do
527
          is_expected.to have_body_text commit.short_id
R
Robb Kidd 已提交
528 529 530 531
        end
      end

      describe 'on a merge request' do
I
Izaak Alpert 已提交
532
        let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
V
Vinnie Okada 已提交
533
        let(:note_on_merge_request_path) { namespace_project_merge_request_path(project.namespace, project, merge_request, anchor: "note_#{note.id}") }
534
        before(:each) { allow(note).to receive(:noteable).and_return(merge_request) }
R
Robb Kidd 已提交
535

536
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
537 538

        it_behaves_like 'a note email'
539
        it_behaves_like 'an answer to an existing thread', 'merge_request'
540
        it_behaves_like 'it should show Gmail Actions View Merge request link'
R
Robb Kidd 已提交
541 542

        it 'has the correct subject' do
543
          is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/
R
Robb Kidd 已提交
544 545 546
        end

        it 'contains a link to the merge request note' do
547
          is_expected.to have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
548 549 550 551
        end
      end

      describe 'on an issue' do
552
        let(:issue) { create(:issue, project: project) }
V
Vinnie Okada 已提交
553
        let(:note_on_issue_path) { namespace_project_issue_path(project.namespace, project, issue, anchor: "note_#{note.id}") }
554
        before(:each) { allow(note).to receive(:noteable).and_return(issue) }
555 556

        subject { Notify.note_issue_email(recipient.id, note.id) }
R
Robb Kidd 已提交
557 558

        it_behaves_like 'a note email'
559
        it_behaves_like 'an answer to an existing thread', 'issue'
560
        it_behaves_like 'it should show Gmail Actions View Issue link'
R
Robb Kidd 已提交
561 562

        it 'has the correct subject' do
563
          is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
564 565 566
        end

        it 'contains a link to the issue note' do
567
          is_expected.to have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
568 569
        end
      end
570 571
    end
  end
572 573 574 575

  describe 'group access changed' do
    let(:group) { create(:group) }
    let(:user) { create(:user) }
576
    let(:membership) { create(:group_member, group: group, user: user) }
577 578 579

    subject { Notify.group_access_granted_email(membership.id) }

580
    it_behaves_like 'an email sent from GitLab'
581
    it_behaves_like 'it should not have Gmail Actions links'
582

583
    it 'has the correct subject' do
584
      is_expected.to have_subject /Access to group was granted/
585 586 587
    end

    it 'contains name of project' do
588
      is_expected.to have_body_text /#{group.name}/
589 590 591
    end

    it 'contains new user role' do
592
      is_expected.to have_body_text /#{membership.human_access}/
593 594
    end
  end
595 596 597 598 599 600

  describe 'confirmation if email changed' do
    let(:example_site_path) { root_path }
    let(:user) { create(:user, email: 'old-email@mail.com') }

    before do
V
Valery Sizov 已提交
601 602 603 604
      perform_enqueued_jobs do
        user.email = "new-email@mail.com"
        user.save
      end
605 606 607 608
    end

    subject { ActionMailer::Base.deliveries.last }

609 610
    it_behaves_like 'an email sent from GitLab'

611
    it 'is sent to the new user' do
612
      is_expected.to deliver_to 'new-email@mail.com'
613 614 615
    end

    it 'has the correct subject' do
616
      is_expected.to have_subject "Confirmation instructions"
617 618 619
    end

    it 'includes a link to the site' do
620
      is_expected.to have_body_text /#{example_site_path}/
621 622
    end
  end
D
Dmitriy Zaporozhets 已提交
623

624 625 626 627 628
  describe 'email on push for a created branch' do
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
    let(:tree_path) { namespace_project_tree_path(project.namespace, project, "master") }

629
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :create) }
630

631 632
    it_behaves_like 'it should not have Gmail Actions links'

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
    end

    it 'is sent to recipient' do
      is_expected.to deliver_to 'devs@company.name'
    end

    it 'has the correct subject' do
      is_expected.to have_subject /Pushed new branch master/
    end

    it 'contains a link to the branch' do
      is_expected.to have_body_text /#{tree_path}/
    end
  end

  describe 'email on push for a created tag' do
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
    let(:tree_path) { namespace_project_tree_path(project.namespace, project, "v1.0") }

657
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/tags/v1.0', action: :create) }
658

659 660
    it_behaves_like 'it should not have Gmail Actions links'

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
    end

    it 'is sent to recipient' do
      is_expected.to deliver_to 'devs@company.name'
    end

    it 'has the correct subject' do
      is_expected.to have_subject /Pushed new tag v1\.0/
    end

    it 'contains a link to the tag' do
      is_expected.to have_body_text /#{tree_path}/
    end
  end

  describe 'email on push for a deleted branch' do
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }

684
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :delete) }
685

686 687
    it_behaves_like 'it should not have Gmail Actions links'

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
    end

    it 'is sent to recipient' do
      is_expected.to deliver_to 'devs@company.name'
    end

    it 'has the correct subject' do
      is_expected.to have_subject /Deleted branch master/
    end
  end

  describe 'email on push for a deleted tag' do
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }

707
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/tags/v1.0', action: :delete) }
708

709 710
    it_behaves_like 'it should not have Gmail Actions links'

711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
    end

    it 'is sent to recipient' do
      is_expected.to deliver_to 'devs@company.name'
    end

    it 'has the correct subject' do
      is_expected.to have_subject /Deleted tag v1\.0/
    end
  end

726
  describe 'email on push with multiple commits' do
D
Dmitriy Zaporozhets 已提交
727 728
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
D
Dmitriy Zaporozhets 已提交
729
    let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) }
730 731
    let(:commits) { Commit.decorate(compare.commits, nil) }
    let(:diff_path) { namespace_project_compare_path(project.namespace, project, from: Commit.new(compare.base, project), to: Commit.new(compare.head, project)) }
732
    let(:send_from_committer_email) { false }
D
Dmitriy Zaporozhets 已提交
733

734
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :push, compare: compare, reverse_compare: false, send_from_committer_email: send_from_committer_email) }
D
Dmitriy Zaporozhets 已提交
735

736 737
    it_behaves_like 'it should not have Gmail Actions links'

738 739
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
740 741
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
742 743
    end

D
Dmitriy Zaporozhets 已提交
744
    it 'is sent to recipient' do
745
      is_expected.to deliver_to 'devs@company.name'
D
Dmitriy Zaporozhets 已提交
746 747 748
    end

    it 'has the correct subject' do
749
      is_expected.to have_subject /\[#{project.path_with_namespace}\]\[master\] #{commits.length} commits:/
D
Dmitriy Zaporozhets 已提交
750 751 752
    end

    it 'includes commits list' do
753
      is_expected.to have_body_text /Change some files/
D
Dmitriy Zaporozhets 已提交
754 755 756
    end

    it 'includes diffs' do
757
      is_expected.to have_body_text /def archive_formats_regex/
D
Dmitriy Zaporozhets 已提交
758
    end
759 760

    it 'contains a link to the diff' do
761
      is_expected.to have_body_text /#{diff_path}/
762
    end
763 764 765 766

    it 'doesn not contain the misleading footer' do
      is_expected.not_to have_body_text /you are a member of/
    end
767 768 769 770 771

    context "when set to send from committer email if domain matches" do

      let(:send_from_committer_email) { true }

772 773 774 775 776
      before do
        allow(Gitlab.config.gitlab).to receive(:host).and_return("gitlab.corp.company.com")
      end

      context "when the committer email domain is within the GitLab domain" do
777 778

        before do
779
          user.update_attribute(:email, "user@company.com")
780
          user.confirm
781 782 783 784 785 786
        end

        it "is sent from the committer email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(user.email)
        end
787 788 789 790 791

        it "is set to reply to the committer email" do
          sender = subject.header[:reply_to].addrs[0]
          expect(sender.address).to eq(user.email)
        end
792 793
      end

794 795 796 797
      context "when the committer email domain is not completely within the GitLab domain" do

        before do
          user.update_attribute(:email, "user@something.company.com")
798
          user.confirm
799 800 801 802 803 804
        end

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
805 806 807 808 809

        it "is set to reply to the default email" do
          sender = subject.header[:reply_to].addrs[0]
          expect(sender.address).to eq(gitlab_sender_reply_to)
        end
810 811 812 813 814 815
      end

      context "when the committer email domain is outside the GitLab domain" do

        before do
          user.update_attribute(:email, "user@mpany.com")
816
          user.confirm
817
        end
818 819 820 821 822

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
823 824 825 826 827

        it "is set to reply to the default email" do
          sender = subject.header[:reply_to].addrs[0]
          expect(sender.address).to eq(gitlab_sender_reply_to)
        end
828 829
      end
    end
D
Dmitriy Zaporozhets 已提交
830
  end
831 832 833 834

  describe 'email on push with a single commit' do
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
D
Dmitriy Zaporozhets 已提交
835
    let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_commit.parent_id, sample_commit.id) }
836
    let(:commits) { Commit.decorate(compare.commits, nil) }
V
Vinnie Okada 已提交
837
    let(:diff_path) { namespace_project_commit_path(project.namespace, project, commits.first) }
838

839
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :push, compare: compare) }
840

841
    it_behaves_like 'it should show Gmail Actions View Commit link'
842

843 844
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
845 846
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
847 848 849
    end

    it 'is sent to recipient' do
850
      is_expected.to deliver_to 'devs@company.name'
851 852 853
    end

    it 'has the correct subject' do
854
      is_expected.to have_subject /#{commits.first.title}/
855 856 857
    end

    it 'includes commits list' do
858
      is_expected.to have_body_text /Change some files/
859 860 861
    end

    it 'includes diffs' do
862
      is_expected.to have_body_text /def archive_formats_regex/
863 864 865
    end

    it 'contains a link to the diff' do
866
      is_expected.to have_body_text /#{diff_path}/
867 868
    end
  end
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896

  describe 'build success' do
    before { build.success }

    subject { Notify.build_success_email(build.id, 'wow@example.com') }

    it 'has the correct subject' do
      should have_subject /Build success for/
    end

    it 'contains name of project' do
      should have_body_text build.project_name
    end
  end

  describe 'build fail' do
    before { build.drop }

    subject { Notify.build_fail_email(build.id, 'wow@example.com') }

    it 'has the correct subject' do
      should have_subject /Build failed for/
    end

    it 'contains name of project' do
      should have_body_text build.project_name
    end
  end
897
end