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

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

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

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

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

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

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

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

R
Robb Kidd 已提交
58
  describe 'for new users, the email' do
59
    let(:example_site_path) { root_path }
D
Dmitriy Zaporozhets 已提交
60
    let(:new_user) { create(:user, email: 'newguy@example.com', created_by_id: 1) }
61

62 63
    token = 'kETLwRaayvigPq_x3SNM'

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

66 67
    it_behaves_like 'an email sent from GitLab'

68
    it 'is sent to the new user' do
69
      is_expected.to deliver_to new_user.email
70 71 72
    end

    it 'has the correct subject' do
73
      is_expected.to have_subject /^Account was created for you$/i
74 75 76
    end

    it 'contains the new user\'s login name' do
77
      is_expected.to have_body_text /#{new_user.email}/
78 79
    end

M
Marin Jankovski 已提交
80
    it 'contains the password text' do
81
      is_expected.to have_body_text /Click here to set your password/
M
Marin Jankovski 已提交
82 83 84
    end

    it 'includes a link for user to set password' do
85
      params = "reset_password_token=#{token}"
86
      is_expected.to have_body_text(
87 88
        %r{http://localhost(:\d+)?/users/password/edit\?#{params}}
      )
89 90 91
    end

    it 'includes a link to the site' do
92
      is_expected.to have_body_text /#{example_site_path}/
93 94 95
    end
  end

M
Marin Jankovski 已提交
96 97 98 99 100

  describe 'for users that signed up, the email' do
    let(:example_site_path) { root_path }
    let(:new_user) { create(:user, email: 'newguy@example.com', password: "securePassword") }

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

103 104
    it_behaves_like 'an email sent from GitLab'

M
Marin Jankovski 已提交
105
    it 'is sent to the new user' do
106
      is_expected.to deliver_to new_user.email
M
Marin Jankovski 已提交
107 108 109
    end

    it 'has the correct subject' do
110
      is_expected.to have_subject /^Account was created for you$/i
M
Marin Jankovski 已提交
111 112 113
    end

    it 'contains the new user\'s login name' do
114
      is_expected.to have_body_text /#{new_user.email}/
M
Marin Jankovski 已提交
115 116 117
    end

    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

    it 'includes a link to the site' do
122
      is_expected.to have_body_text /#{example_site_path}/
M
Marin Jankovski 已提交
123 124 125
    end
  end

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

    subject { Notify.new_ssh_key_email(key.id) }

131 132
    it_behaves_like 'an email sent from GitLab'

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

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

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

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

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

    subject { Notify.new_email_email(email.id) }

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

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

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

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

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

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

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

R
Robb Kidd 已提交
190
      context 'for issues' do
191 192
        let(:issue) { create(:issue, author: current_user, assignee: assignee, project: project) }
        let(:issue_with_description) { create(:issue, author: current_user, assignee: assignee, project: project, description: Faker::Lorem.sentence) }
193

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

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

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

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

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

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

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

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

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

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

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

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

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

250 251
          it_behaves_like 'an answer to an existing thread', 'issue'

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

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

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

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

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

R
Robb Kidd 已提交
275 276 277
      end

      context 'for merge requests' do
278
        let(:merge_author) { create(:user) }
279
        let(:merge_request) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project) }
280
        let(:merge_request_with_description) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project, description: Faker::Lorem.sentence) }
R
Robb Kidd 已提交
281 282

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

402 403 404 405 406
    describe 'project was moved' do
      let(:project) { create(:project) }
      let(:user) { create(:user) }
      subject { Notify.project_was_moved_email(project.id, user.id) }

407 408
      it_behaves_like 'an email sent from GitLab'

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

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

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

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

      it_behaves_like 'an email sent from GitLab'

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

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

447
      before :each do
448
        allow(Note).to receive(:find).with(note.id).and_return(note)
449 450
      end

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

R
Robb Kidd 已提交
458
        it 'is sent to the given recipient' do
459
          is_expected.to deliver_to recipient.notification_email
R
Robb Kidd 已提交
460 461 462
        end

        it 'contains the message from the note' do
463
          is_expected.to have_body_text /#{note.note}/
R
Robb Kidd 已提交
464 465 466 467
        end
      end

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

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

D
Dmitriy Zaporozhets 已提交
472
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
473 474

        it_behaves_like 'a note email'
475
        it_behaves_like 'an answer to an existing thread', 'commits'
R
Robb Kidd 已提交
476 477

        it 'has the correct subject' do
478
          is_expected.to have_subject /#{commit.title} \(#{commit.short_id}\)/
R
Robb Kidd 已提交
479 480 481
        end

        it 'contains a link to the commit' do
482
          is_expected.to have_body_text commit.short_id
R
Robb Kidd 已提交
483 484 485 486
        end
      end

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

491
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
492 493

        it_behaves_like 'a note email'
494
        it_behaves_like 'an answer to an existing thread', 'merge_request'
R
Robb Kidd 已提交
495 496

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

        it 'contains a link to the merge request note' do
501
          is_expected.to have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
502 503 504 505
        end
      end

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

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

        it_behaves_like 'a note email'
513
        it_behaves_like 'an answer to an existing thread', 'issue'
R
Robb Kidd 已提交
514 515

        it 'has the correct subject' do
516
          is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
517 518 519
        end

        it 'contains a link to the issue note' do
520
          is_expected.to have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
521 522
        end
      end
523 524
    end
  end
525 526 527 528

  describe 'group access changed' do
    let(:group) { create(:group) }
    let(:user) { create(:user) }
529
    let(:membership) { create(:group_member, group: group, user: user) }
530 531 532

    subject { Notify.group_access_granted_email(membership.id) }

533 534
    it_behaves_like 'an email sent from GitLab'

535
    it 'has the correct subject' do
536
      is_expected.to have_subject /Access to group was granted/
537 538 539
    end

    it 'contains name of project' do
540
      is_expected.to have_body_text /#{group.name}/
541 542 543
    end

    it 'contains new user role' do
544
      is_expected.to have_body_text /#{membership.human_access}/
545 546
    end
  end
547 548 549 550 551 552 553 554 555 556 557 558

  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 }

559 560
    it_behaves_like 'an email sent from GitLab'

561
    it 'is sent to the new user' do
562
      is_expected.to deliver_to 'new-email@mail.com'
563 564 565
    end

    it 'has the correct subject' do
566
      is_expected.to have_subject "Confirmation instructions"
567 568 569
    end

    it 'includes a link to the site' do
570
      is_expected.to have_body_text /#{example_site_path}/
571 572
    end
  end
D
Dmitriy Zaporozhets 已提交
573

574 575 576 577 578
  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") }

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

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

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

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

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

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

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

    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

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

676
    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 已提交
677

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

D
Dmitriy Zaporozhets 已提交
684
    it 'is sent to recipient' do
685
      is_expected.to deliver_to 'devs@company.name'
D
Dmitriy Zaporozhets 已提交
686 687 688
    end

    it 'has the correct subject' do
689
      is_expected.to have_subject /\[#{project.path_with_namespace}\]\[master\] #{commits.length} commits:/
D
Dmitriy Zaporozhets 已提交
690 691 692
    end

    it 'includes commits list' do
693
      is_expected.to have_body_text /Change some files/
D
Dmitriy Zaporozhets 已提交
694 695 696
    end

    it 'includes diffs' do
697
      is_expected.to have_body_text /def archive_formats_regex/
D
Dmitriy Zaporozhets 已提交
698
    end
699 700

    it 'contains a link to the diff' do
701
      is_expected.to have_body_text /#{diff_path}/
702
    end
703 704 705 706

    it 'doesn not contain the misleading footer' do
      is_expected.not_to have_body_text /you are a member of/
    end
707 708 709 710 711

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

      let(:send_from_committer_email) { true }

712 713 714 715 716
      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
717 718

        before do
719
          user.update_attribute(:email, "user@company.com")
720 721 722 723 724 725 726
          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
727 728 729 730 731

        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
732 733
      end

734 735 736 737 738 739 740 741 742 743 744
      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
745 746 747 748 749

        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
750 751 752 753 754 755 756 757
      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
758 759 760 761 762

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
763 764 765 766 767

        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
768 769
      end
    end
D
Dmitriy Zaporozhets 已提交
770
  end
771 772 773 774

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

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

    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
783 784
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
785 786 787
    end

    it 'is sent to recipient' do
788
      is_expected.to deliver_to 'devs@company.name'
789 790 791
    end

    it 'has the correct subject' do
792
      is_expected.to have_subject /#{commits.first.title}/
793 794 795
    end

    it 'includes commits list' do
796
      is_expected.to have_body_text /Change some files/
797 798 799
    end

    it 'includes diffs' do
800
      is_expected.to have_body_text /def archive_formats_regex/
801 802 803
    end

    it 'contains a link to the diff' do
804
      is_expected.to have_body_text /#{diff_path}/
805 806
    end
  end
807
end