notify_spec.rb 27.1 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

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

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

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

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

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

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

    it 'has headers that reference an existing thread' do
55
      is_expected.to have_header 'Message-ID',  /<(.*)@#{Gitlab.config.gitlab.host}>/
56 57 58
      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}/
59 60 61
    end
  end

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

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

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

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    it 'includes a link to the site' do
      is_expected.to have_body_text /#{site_path}/
    end
  end

  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

M
Marin Jankovski 已提交
91
    it 'contains the password text' do
92
      is_expected.to have_body_text /Click here to set your password/
M
Marin Jankovski 已提交
93 94 95
    end

    it 'includes a link for user to set password' do
96
      params = "reset_password_token=#{token}"
97
      is_expected.to have_body_text(
98 99
        %r{http://localhost(:\d+)?/users/password/edit\?#{params}}
      )
100 101
    end

102 103 104
    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)
105
      is_expected.to have_body_text(/\?user_email=.*%40.*/)
106
    end
107 108
  end

M
Marin Jankovski 已提交
109 110 111

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

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

116
    it_behaves_like 'an email sent from GitLab'
117
    it_behaves_like 'a new user email', new_user_address
M
Marin Jankovski 已提交
118 119

    it 'should not contain the new user\'s password' do
120
      is_expected.not_to have_body_text /password/
M
Marin Jankovski 已提交
121 122 123
    end
  end

124 125 126 127 128
  describe 'user added ssh key' do
    let(:key) { create(:personal_key) }

    subject { Notify.new_ssh_key_email(key.id) }

129 130
    it_behaves_like 'an email sent from GitLab'

131
    it 'is sent to the new user' do
132
      is_expected.to deliver_to key.user.email
133 134 135
    end

    it 'has the correct subject' do
136
      is_expected.to have_subject /^SSH key was added to your account$/i
137 138 139
    end

    it 'contains the new ssh key title' do
140
      is_expected.to have_body_text /#{key.title}/
141 142 143
    end

    it 'includes a link to ssh keys page' do
144
      is_expected.to have_body_text /#{profile_keys_path}/
145 146 147
    end
  end

148 149 150 151 152 153
  describe 'user added email' do
    let(:email) { create(:email) }

    subject { Notify.new_email_email(email.id) }

    it 'is sent to the new user' do
154
      is_expected.to deliver_to email.user.email
155 156 157
    end

    it 'has the correct subject' do
158
      is_expected.to have_subject /^Email was added to your account$/i
159 160 161
    end

    it 'contains the new email address' do
162
      is_expected.to have_body_text /#{email.email}/
163 164 165
    end

    it 'includes a link to emails page' do
166
      is_expected.to have_body_text /#{profile_emails_path}/
167 168 169
    end
  end

R
Robb Kidd 已提交
170 171
  context 'for a project' do
    describe 'items that are assignable, the email' do
172
      let(:current_user) { create(:user, email: "current@email.com") }
173 174
      let(:assignee) { create(:user, email: 'assignee@example.com') }
      let(:previous_assignee) { create(:user, name: 'Previous Assignee') }
175

R
Robb Kidd 已提交
176
      shared_examples 'an assignee email' do
177 178
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
179 180
          expect(sender.display_name).to eq(current_user.name)
          expect(sender.address).to eq(gitlab_sender)
181 182
        end

R
Robb Kidd 已提交
183
        it 'is sent to the assignee' do
184
          is_expected.to deliver_to assignee.email
R
Robb Kidd 已提交
185 186
        end
      end
187

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

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

R
Robb Kidd 已提交
195
          it_behaves_like 'an assignee email'
196
          it_behaves_like 'an email starting a new thread', 'issue'
197

R
Robb Kidd 已提交
198
          it 'has the correct subject' do
199
            is_expected.to have_subject /#{project.name} \| #{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
200
          end
201

R
Robb Kidd 已提交
202
          it 'contains a link to the new issue' do
V
Vinnie Okada 已提交
203
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
204 205
          end
        end
206

207 208 209 210
        describe 'that are new with a description' do
          subject { Notify.new_issue_email(issue_with_description.assignee_id, issue_with_description.id) }

          it 'contains the description' do
211
            is_expected.to have_body_text /#{issue_with_description.description}/
212 213 214
          end
        end

R
Robb Kidd 已提交
215
        describe 'that have been reassigned' do
216
          subject { Notify.reassigned_issue_email(recipient.id, issue.id, previous_assignee.id, current_user) }
R
Robb Kidd 已提交
217 218

          it_behaves_like 'a multiple recipients email'
219
          it_behaves_like 'an answer to an existing thread', 'issue'
R
Robb Kidd 已提交
220

221 222
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
223 224
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
225 226
          end

R
Robb Kidd 已提交
227
          it 'has the correct subject' do
228
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
229 230 231
          end

          it 'contains the name of the previous assignee' do
232
            is_expected.to have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
233 234 235
          end

          it 'contains the name of the new assignee' do
236
            is_expected.to have_body_text /#{assignee.name}/
R
Robb Kidd 已提交
237 238 239
          end

          it 'contains a link to the issue' do
V
Vinnie Okada 已提交
240
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
241 242
          end
        end
A
Alex Denisov 已提交
243 244 245 246

        describe 'status changed' do
          let(:status) { 'closed' }
          subject { Notify.issue_status_changed_email(recipient.id, issue.id, status, current_user) }
247

248 249
          it_behaves_like 'an answer to an existing thread', 'issue'

250 251
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
252 253
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
254 255
          end

A
Alex Denisov 已提交
256
          it 'has the correct subject' do
257
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/i
A
Alex Denisov 已提交
258 259 260
          end

          it 'contains the new status' do
261
            is_expected.to have_body_text /#{status}/i
A
Alex Denisov 已提交
262 263 264
          end

          it 'contains the user name' do
265
            is_expected.to have_body_text /#{current_user.name}/i
A
Alex Denisov 已提交
266 267 268
          end

          it 'contains a link to the issue' do
V
Vinnie Okada 已提交
269
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
A
Alex Denisov 已提交
270 271 272
          end
        end

R
Robb Kidd 已提交
273 274 275
      end

      context 'for merge requests' do
276
        let(:merge_author) { create(:user) }
277
        let(:merge_request) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project) }
R
Robert Speicher 已提交
278
        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 已提交
279 280

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

          it_behaves_like 'an assignee email'
284
          it_behaves_like 'an email starting a new thread', 'merge_request'
R
Robb Kidd 已提交
285 286

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

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

          it 'contains the source branch for the merge request' do
295
            is_expected.to have_body_text /#{merge_request.source_branch}/
R
Robb Kidd 已提交
296 297 298
          end

          it 'contains the target branch for the merge request' do
299
            is_expected.to have_body_text /#{merge_request.target_branch}/
R
Robb Kidd 已提交
300
          end
P
Philip Blatter 已提交
301 302

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

307 308 309 310
        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) }

          it 'contains the description' do
311
            is_expected.to have_body_text /#{merge_request_with_description.description}/
312 313 314
          end
        end

R
Robb Kidd 已提交
315
        describe 'that are reassigned' do
316
          subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id, current_user.id) }
R
Robb Kidd 已提交
317 318

          it_behaves_like 'a multiple recipients email'
319
          it_behaves_like 'an answer to an existing thread', 'merge_request'
R
Robb Kidd 已提交
320

321 322
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
323 324
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
325 326
          end

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

          it 'contains the name of the previous assignee' do
332
            is_expected.to have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
333 334 335
          end

          it 'contains the name of the new assignee' do
336
            is_expected.to have_body_text /#{assignee.name}/
R
Robb Kidd 已提交
337 338 339
          end

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

344 345 346 347 348 349 350 351
        describe 'status changed' do
          let(:status) { 'reopened' }
          subject { Notify.merge_request_status_email(recipient.id, merge_request.id, status, current_user) }

          it_behaves_like 'an answer to an existing thread', 'merge_request'

          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
352 353
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
354 355 356
          end

          it 'has the correct subject' do
357
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/i
358 359 360
          end

          it 'contains the new status' do
361
            is_expected.to have_body_text /#{status}/i
362 363 364
          end

          it 'contains the user name' do
365
            is_expected.to have_body_text /#{current_user.name}/i
366 367 368
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
369
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
370 371 372
          end
        end

373 374 375 376
        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'
377
          it_behaves_like 'an answer to an existing thread', 'merge_request'
378 379 380

          it 'is sent as the merge author' do
            sender = subject.header[:from].addrs[0]
381 382
            expect(sender.display_name).to eq(merge_author.name)
            expect(sender.address).to eq(gitlab_sender)
383 384 385
          end

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

389
          it 'contains the new status' do
390
            is_expected.to have_body_text /merged/i
391 392 393
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
394
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
395
          end
R
Robb Kidd 已提交
396 397
        end
      end
398 399
    end

400 401 402
    describe 'project was moved' do
      let(:project) { create(:project) }
      let(:user) { create(:user) }
403
      subject { Notify.project_was_moved_email(project.id, user.id, "gitlab/gitlab") }
404

405 406
      it_behaves_like 'an email sent from GitLab'

407
      it 'has the correct subject' do
408
        is_expected.to have_subject /Project was moved/
409 410 411
      end

      it 'contains name of project' do
412
        is_expected.to have_body_text /#{project.name_with_namespace}/
413 414 415
      end

      it 'contains new user role' do
416
        is_expected.to have_body_text /#{project.ssh_url_to_repo}/
417 418 419
      end
    end

420
    describe 'project access changed' do
421
      let(:project) { create(:project) }
422
      let(:user) { create(:user) }
423
      let(:project_member) { create(:project_member, project: project, user: user) }
424
      subject { Notify.project_access_granted_email(project_member.id) }
425 426 427

      it_behaves_like 'an email sent from GitLab'

428
      it 'has the correct subject' do
429
        is_expected.to have_subject /Access to project was granted/
430 431
      end
      it 'contains name of project' do
432
        is_expected.to have_body_text /#{project.name}/
433 434
      end
      it 'contains new user role' do
435
        is_expected.to have_body_text /#{project_member.human_access}/
436 437 438
      end
    end

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

443
      before :each do
444
        allow(Note).to receive(:find).with(note.id).and_return(note)
445 446
      end

R
Robb Kidd 已提交
447
      shared_examples 'a note email' do
448 449
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
450 451
          expect(sender.display_name).to eq(note_author.name)
          expect(sender.address).to eq(gitlab_sender)
452 453
        end

R
Robb Kidd 已提交
454
        it 'is sent to the given recipient' do
455
          is_expected.to deliver_to recipient.notification_email
R
Robb Kidd 已提交
456 457 458
        end

        it 'contains the message from the note' do
459
          is_expected.to have_body_text /#{note.note}/
R
Robb Kidd 已提交
460 461 462 463
        end
      end

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

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

D
Dmitriy Zaporozhets 已提交
468
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
469 470

        it_behaves_like 'a note email'
471
        it_behaves_like 'an answer to an existing thread', 'commit'
R
Robb Kidd 已提交
472 473

        it 'has the correct subject' do
474
          is_expected.to have_subject /#{commit.title} \(#{commit.short_id}\)/
R
Robb Kidd 已提交
475 476 477
        end

        it 'contains a link to the commit' do
478
          is_expected.to have_body_text commit.short_id
R
Robb Kidd 已提交
479 480 481 482
        end
      end

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

487
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
488 489

        it_behaves_like 'a note email'
490
        it_behaves_like 'an answer to an existing thread', 'merge_request'
R
Robb Kidd 已提交
491 492

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

        it 'contains a link to the merge request note' do
497
          is_expected.to have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
498 499 500 501
        end
      end

      describe 'on an issue' do
502
        let(:issue) { create(:issue, project: project) }
V
Vinnie Okada 已提交
503
        let(:note_on_issue_path) { namespace_project_issue_path(project.namespace, project, issue, anchor: "note_#{note.id}") }
504
        before(:each) { allow(note).to receive(:noteable).and_return(issue) }
505 506

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

        it_behaves_like 'a note email'
509
        it_behaves_like 'an answer to an existing thread', 'issue'
R
Robb Kidd 已提交
510 511

        it 'has the correct subject' do
512
          is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
513 514 515
        end

        it 'contains a link to the issue note' do
516
          is_expected.to have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
517 518
        end
      end
519 520
    end
  end
521 522 523 524

  describe 'group access changed' do
    let(:group) { create(:group) }
    let(:user) { create(:user) }
525
    let(:membership) { create(:group_member, group: group, user: user) }
526 527 528

    subject { Notify.group_access_granted_email(membership.id) }

529 530
    it_behaves_like 'an email sent from GitLab'

531
    it 'has the correct subject' do
532
      is_expected.to have_subject /Access to group was granted/
533 534 535
    end

    it 'contains name of project' do
536
      is_expected.to have_body_text /#{group.name}/
537 538 539
    end

    it 'contains new user role' do
540
      is_expected.to have_body_text /#{membership.human_access}/
541 542
    end
  end
543 544 545 546 547 548 549 550 551 552 553 554

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

    before do
      user.email = "new-email@mail.com"
      user.save
    end

    subject { ActionMailer::Base.deliveries.last }

555 556
    it_behaves_like 'an email sent from GitLab'

557
    it 'is sent to the new user' do
558
      is_expected.to deliver_to 'new-email@mail.com'
559 560 561
    end

    it 'has the correct subject' do
562
      is_expected.to have_subject "Confirmation instructions"
563 564 565
    end

    it 'includes a link to the site' do
566
      is_expected.to have_body_text /#{example_site_path}/
567 568
    end
  end
D
Dmitriy Zaporozhets 已提交
569

570 571 572 573 574
  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") }

575
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :create) }
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

    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") }

601
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/tags/v1.0', action: :create) }
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

    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) }

626
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :delete) }
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

    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) }

647
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/tags/v1.0', action: :delete) }
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

    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

664
  describe 'email on push with multiple commits' do
D
Dmitriy Zaporozhets 已提交
665 666
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
D
Dmitriy Zaporozhets 已提交
667
    let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) }
668 669
    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)) }
670
    let(:send_from_committer_email) { false }
D
Dmitriy Zaporozhets 已提交
671

672
    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 已提交
673

674 675
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
676 677
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
678 679
    end

D
Dmitriy Zaporozhets 已提交
680
    it 'is sent to recipient' do
681
      is_expected.to deliver_to 'devs@company.name'
D
Dmitriy Zaporozhets 已提交
682 683 684
    end

    it 'has the correct subject' do
685
      is_expected.to have_subject /\[#{project.path_with_namespace}\]\[master\] #{commits.length} commits:/
D
Dmitriy Zaporozhets 已提交
686 687 688
    end

    it 'includes commits list' do
689
      is_expected.to have_body_text /Change some files/
D
Dmitriy Zaporozhets 已提交
690 691 692
    end

    it 'includes diffs' do
693
      is_expected.to have_body_text /def archive_formats_regex/
D
Dmitriy Zaporozhets 已提交
694
    end
695 696

    it 'contains a link to the diff' do
697
      is_expected.to have_body_text /#{diff_path}/
698
    end
699 700 701 702

    it 'doesn not contain the misleading footer' do
      is_expected.not_to have_body_text /you are a member of/
    end
703 704 705 706 707

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

      let(:send_from_committer_email) { true }

708 709 710 711 712
      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
713 714

        before do
715
          user.update_attribute(:email, "user@company.com")
716
          user.confirm
717 718 719 720 721 722
        end

        it "is sent from the committer email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(user.email)
        end
723 724 725 726 727

        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
728 729
      end

730 731 732 733
      context "when the committer email domain is not completely within the GitLab domain" do

        before do
          user.update_attribute(:email, "user@something.company.com")
734
          user.confirm
735 736 737 738 739 740
        end

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
741 742 743 744 745

        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
746 747 748 749 750 751
      end

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

        before do
          user.update_attribute(:email, "user@mpany.com")
752
          user.confirm
753
        end
754 755 756 757 758

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
759 760 761 762 763

        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
764 765
      end
    end
D
Dmitriy Zaporozhets 已提交
766
  end
767 768 769 770

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

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

    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
779 780
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
781 782 783
    end

    it 'is sent to recipient' do
784
      is_expected.to deliver_to 'devs@company.name'
785 786 787
    end

    it 'has the correct subject' do
788
      is_expected.to have_subject /#{commits.first.title}/
789 790 791
    end

    it 'includes commits list' do
792
      is_expected.to have_body_text /Change some files/
793 794 795
    end

    it 'includes diffs' do
796
      is_expected.to have_body_text /def archive_formats_regex/
797 798 799
    end

    it 'contains a link to the diff' do
800
      is_expected.to have_body_text /#{diff_path}/
801 802
    end
  end
803
end