notify_spec.rb 27.0 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 56 57
      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}/
58 59 60
    end
  end

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

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

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

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

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

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

M
Marin Jankovski 已提交
108 109 110

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

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

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

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

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

    subject { Notify.new_ssh_key_email(key.id) }

128 129
    it_behaves_like 'an email sent from GitLab'

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

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

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

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

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

    subject { Notify.new_email_email(email.id) }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

R
Robb Kidd 已提交
272 273 274
      end

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

404 405
      it_behaves_like 'an email sent from GitLab'

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

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

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

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

      it_behaves_like 'an email sent from GitLab'

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    subject { Notify.group_access_granted_email(membership.id) }

528 529
    it_behaves_like 'an email sent from GitLab'

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

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

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

  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 }

554 555
    it_behaves_like 'an email sent from GitLab'

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

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

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

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

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

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

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

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

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

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

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

    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

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

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

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

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

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

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

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

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

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

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

      let(:send_from_committer_email) { true }

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

        before do
714
          user.update_attribute(:email, "user@company.com")
715 716 717 718 719 720 721
          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
722 723 724 725 726

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

729 730 731 732 733 734 735 736 737 738 739
      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
740 741 742 743 744

        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
745 746 747 748 749 750 751 752
      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
753 754 755 756 757

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

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

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

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

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

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

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

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

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

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