notify_spec.rb 27.0 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

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

8 9
  new_user_address = 'newguy@example.com'

10 11 12
  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 }
13
  let(:recipient) { create(:user, email: 'recipient@example.com') }
D
Dmitriy Zaporozhets 已提交
14
  let(:project) { create(:project) }
15

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

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

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

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

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

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

    it 'has headers that reference an existing thread' do
54 55 56
      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}/
57 58 59
    end
  end

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

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

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

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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 已提交
89
    it 'contains the password text' do
90
      is_expected.to have_body_text /Click here to set your password/
M
Marin Jankovski 已提交
91 92 93
    end

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

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

M
Marin Jankovski 已提交
107 108 109

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

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

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

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

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

    subject { Notify.new_ssh_key_email(key.id) }

127 128
    it_behaves_like 'an email sent from GitLab'

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

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

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

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

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

    subject { Notify.new_email_email(email.id) }

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

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

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

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

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

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

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

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

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

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

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

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

205 206 207 208
        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
209
            is_expected.to have_body_text /#{issue_with_description.description}/
210 211 212
          end
        end

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

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

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

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

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

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

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

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

246 247
          it_behaves_like 'an answer to an existing thread', 'issue'

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

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

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

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

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

R
Robb Kidd 已提交
271 272 273
      end

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

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

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

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

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

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

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

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

305 306 307 308
        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
309
            is_expected.to have_body_text /#{merge_request_with_description.description}/
310 311 312
          end
        end

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

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

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

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

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

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

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

342 343 344 345 346 347 348 349
        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]
350 351
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
352 353 354
          end

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

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

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

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

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

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

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

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

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

398 399 400 401 402
    describe 'project was moved' do
      let(:project) { create(:project) }
      let(:user) { create(:user) }
      subject { Notify.project_was_moved_email(project.id, user.id) }

403 404
      it_behaves_like 'an email sent from GitLab'

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

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

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

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

      it_behaves_like 'an email sent from GitLab'

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

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

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

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

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

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

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

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

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

        it_behaves_like 'a note email'
469
        it_behaves_like 'an answer to an existing thread', 'commits'
R
Robb Kidd 已提交
470 471

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

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

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

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

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

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

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

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

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

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

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

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

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

    subject { Notify.group_access_granted_email(membership.id) }

527 528
    it_behaves_like 'an email sent from GitLab'

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

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

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

  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 }

553 554
    it_behaves_like 'an email sent from GitLab'

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

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

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

568 569 570 571 572
  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") }

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

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

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

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

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

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

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

    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

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

670
    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 已提交
671

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

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

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

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

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

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

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

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

      let(:send_from_committer_email) { true }

706 707 708 709 710
      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
711 712

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

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

        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
726 727
      end

728 729 730 731 732 733 734 735 736 737 738
      context "when the committer email domain is not completely within the GitLab domain" do

        before do
          user.update_attribute(:email, "user@something.company.com")
          user.confirm!
        end

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

        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
744 745 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")
          user.confirm!
        end
752 753 754 755 756

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

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

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

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

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

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

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

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

    it 'includes diffs' do
794
      is_expected.to have_body_text /def archive_formats_regex/
795 796 797
    end

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