notify_spec.rb 27.1 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 422 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', 'commits'
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 717 718 719 720 721 722
          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
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 734 735 736 737 738 739 740
      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
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 752 753
      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
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