notify_spec.rb 27.2 KB
Newer Older
1
require 'spec_helper'
R
Robert Speicher 已提交
2
require 'email_spec'
3
require 'mailers/shared/notify'
4 5 6 7

describe Notify do
  include EmailSpec::Helpers
  include EmailSpec::Matchers
D
Dmitriy Zaporozhets 已提交
8
  include RepoHelpers
9

10
  include_context 'gitlab email notification'
11

R
Robb Kidd 已提交
12 13
  context 'for a project' do
    describe 'items that are assignable, the email' do
14
      let(:current_user) { create(:user, email: "current@email.com") }
15 16
      let(:assignee) { create(:user, email: 'assignee@example.com') }
      let(:previous_assignee) { create(:user, name: 'Previous Assignee') }
17

R
Robb Kidd 已提交
18
      shared_examples 'an assignee email' do
19 20
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
21 22
          expect(sender.display_name).to eq(current_user.name)
          expect(sender.address).to eq(gitlab_sender)
23 24
        end

R
Robb Kidd 已提交
25
        it 'is sent to the assignee' do
26
          is_expected.to deliver_to assignee.email
R
Robb Kidd 已提交
27 28
        end
      end
29

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

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

R
Robb Kidd 已提交
37
          it_behaves_like 'an assignee email'
38
          it_behaves_like 'an email starting a new thread', 'issue'
39
          it_behaves_like 'it should show Gmail Actions View Issue link'
40
          it_behaves_like 'an unsubscribeable thread'
41

R
Robb Kidd 已提交
42
          it 'has the correct subject' do
43
            is_expected.to have_subject /#{project.name} \| #{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
44
          end
45

R
Robb Kidd 已提交
46
          it 'contains a link to the new issue' do
V
Vinnie Okada 已提交
47
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
48
          end
49 50 51 52 53 54 55 56 57 58 59

          context 'when enabled email_author_in_body' do
            before do
              allow(current_application_settings).to receive(:email_author_in_body).and_return(true)
            end

            it 'contains a link to note author' do
              is_expected.to have_body_text issue.author_name
              is_expected.to have_body_text /wrote\:/
            end
          end
R
Robb Kidd 已提交
60
        end
61

62 63 64
        describe 'that are new with a description' do
          subject { Notify.new_issue_email(issue_with_description.assignee_id, issue_with_description.id) }

65 66
          it_behaves_like 'it should show Gmail Actions View Issue link'

67
          it 'contains the description' do
68
            is_expected.to have_body_text /#{issue_with_description.description}/
69 70 71
          end
        end

R
Robb Kidd 已提交
72
        describe 'that have been reassigned' do
V
Valery Sizov 已提交
73
          subject { Notify.reassigned_issue_email(recipient.id, issue.id, previous_assignee.id, current_user.id) }
R
Robb Kidd 已提交
74 75

          it_behaves_like 'a multiple recipients email'
76
          it_behaves_like 'an answer to an existing thread', 'issue'
77
          it_behaves_like 'it should show Gmail Actions View Issue link'
78
          it_behaves_like "an unsubscribeable thread"
R
Robb Kidd 已提交
79

80 81
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
82 83
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
84 85
          end

R
Robb Kidd 已提交
86
          it 'has the correct subject' do
87
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
88 89 90
          end

          it 'contains the name of the previous assignee' do
91
            is_expected.to have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
92 93 94
          end

          it 'contains the name of the new assignee' do
95
            is_expected.to have_body_text /#{assignee.name}/
R
Robb Kidd 已提交
96 97 98
          end

          it 'contains a link to the issue' do
V
Vinnie Okada 已提交
99
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
100 101
          end
        end
A
Alex Denisov 已提交
102 103 104

        describe 'status changed' do
          let(:status) { 'closed' }
V
Valery Sizov 已提交
105
          subject { Notify.issue_status_changed_email(recipient.id, issue.id, status, current_user.id) }
106

107
          it_behaves_like 'an answer to an existing thread', 'issue'
108
          it_behaves_like 'it should show Gmail Actions View Issue link'
109
          it_behaves_like 'an unsubscribeable thread'
110

111 112
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
113 114
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
115 116
          end

A
Alex Denisov 已提交
117
          it 'has the correct subject' do
118
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/i
A
Alex Denisov 已提交
119 120 121
          end

          it 'contains the new status' do
122
            is_expected.to have_body_text /#{status}/i
A
Alex Denisov 已提交
123 124 125
          end

          it 'contains the user name' do
126
            is_expected.to have_body_text /#{current_user.name}/i
A
Alex Denisov 已提交
127 128 129
          end

          it 'contains a link to the issue' do
V
Vinnie Okada 已提交
130
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
A
Alex Denisov 已提交
131
          end
132
        end
R
Robb Kidd 已提交
133 134 135
      end

      context 'for merge requests' do
136
        let(:merge_author) { create(:user) }
137
        let(:merge_request) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project) }
R
Robert Speicher 已提交
138
        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 已提交
139 140

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

          it_behaves_like 'an assignee email'
144
          it_behaves_like 'an email starting a new thread', 'merge_request'
145
          it_behaves_like 'it should show Gmail Actions View Merge request link'
146
          it_behaves_like "an unsubscribeable thread"
R
Robb Kidd 已提交
147 148

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

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

          it 'contains the source branch for the merge request' do
157
            is_expected.to have_body_text /#{merge_request.source_branch}/
R
Robb Kidd 已提交
158 159 160
          end

          it 'contains the target branch for the merge request' do
161
            is_expected.to have_body_text /#{merge_request.target_branch}/
R
Robb Kidd 已提交
162
          end
P
Philip Blatter 已提交
163 164

          it 'has the correct message-id set' do
165
            is_expected.to have_header 'Message-ID', "<merge_request_#{merge_request.id}@#{Gitlab.config.gitlab.host}>"
P
Philip Blatter 已提交
166
          end
167 168 169 170 171 172 173 174 175 176 177

          context 'when enabled email_author_in_body' do
            before do
              allow(current_application_settings).to receive(:email_author_in_body).and_return(true)
            end

            it 'contains a link to note author' do
              is_expected.to have_body_text merge_request.author_name
              is_expected.to have_body_text /wrote\:/
            end
          end
R
Robb Kidd 已提交
178 179
        end

180 181 182
        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) }

183
          it_behaves_like 'it should show Gmail Actions View Merge request link'
184
          it_behaves_like "an unsubscribeable thread"
185

186
          it 'contains the description' do
187
            is_expected.to have_body_text /#{merge_request_with_description.description}/
188 189 190
          end
        end

R
Robb Kidd 已提交
191
        describe 'that are reassigned' do
192
          subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id, current_user.id) }
R
Robb Kidd 已提交
193 194

          it_behaves_like 'a multiple recipients email'
195
          it_behaves_like 'an answer to an existing thread', 'merge_request'
196
          it_behaves_like 'it should show Gmail Actions View Merge request link'
197
          it_behaves_like "an unsubscribeable thread"
R
Robb Kidd 已提交
198

199 200
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
201 202
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
203 204
          end

R
Robb Kidd 已提交
205
          it 'has the correct subject' do
206
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/
R
Robb Kidd 已提交
207 208 209
          end

          it 'contains the name of the previous assignee' do
210
            is_expected.to have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
211 212 213
          end

          it 'contains the name of the new assignee' do
214
            is_expected.to have_body_text /#{assignee.name}/
R
Robb Kidd 已提交
215 216 217
          end

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

222 223
        describe 'status changed' do
          let(:status) { 'reopened' }
V
Valery Sizov 已提交
224
          subject { Notify.merge_request_status_email(recipient.id, merge_request.id, status, current_user.id) }
225 226

          it_behaves_like 'an answer to an existing thread', 'merge_request'
227
          it_behaves_like 'it should show Gmail Actions View Merge request link'
228
          it_behaves_like "an unsubscribeable thread"
229 230 231

          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
232 233
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
234 235 236
          end

          it 'has the correct subject' do
237
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/i
238 239 240
          end

          it 'contains the new status' do
241
            is_expected.to have_body_text /#{status}/i
242 243 244
          end

          it 'contains the user name' do
245
            is_expected.to have_body_text /#{current_user.name}/i
246 247 248
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
249
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
250 251 252
          end
        end

253 254 255 256
        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'
257
          it_behaves_like 'an answer to an existing thread', 'merge_request'
258
          it_behaves_like 'it should show Gmail Actions View Merge request link'
259
          it_behaves_like "an unsubscribeable thread"
260 261 262

          it 'is sent as the merge author' do
            sender = subject.header[:from].addrs[0]
263 264
            expect(sender.display_name).to eq(merge_author.name)
            expect(sender.address).to eq(gitlab_sender)
265 266 267
          end

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

271
          it 'contains the new status' do
272
            is_expected.to have_body_text /merged/i
273 274 275
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
276
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
277
          end
R
Robb Kidd 已提交
278 279
        end
      end
280 281
    end

282 283 284
    describe 'project was moved' do
      let(:project) { create(:project) }
      let(:user) { create(:user) }
285
      subject { Notify.project_was_moved_email(project.id, user.id, "gitlab/gitlab") }
286

287
      it_behaves_like 'an email sent from GitLab'
288
      it_behaves_like 'it should not have Gmail Actions links'
289
      it_behaves_like "a user cannot unsubscribe through footer link"
290

291
      it 'has the correct subject' do
292
        is_expected.to have_subject /Project was moved/
293 294 295
      end

      it 'contains name of project' do
296
        is_expected.to have_body_text /#{project.name_with_namespace}/
297 298 299
      end

      it 'contains new user role' do
300
        is_expected.to have_body_text /#{project.ssh_url_to_repo}/
301 302 303
      end
    end

304
    describe 'project access changed' do
305
      let(:project) { create(:project) }
306
      let(:user) { create(:user) }
307
      let(:project_member) { create(:project_member, project: project, user: user) }
308
      subject { Notify.project_access_granted_email(project_member.id) }
309 310

      it_behaves_like 'an email sent from GitLab'
311
      it_behaves_like 'it should not have Gmail Actions links'
312
      it_behaves_like "a user cannot unsubscribe through footer link"
313

314
      it 'has the correct subject' do
315
        is_expected.to have_subject /Access to project was granted/
316
      end
317

318
      it 'contains name of project' do
319
        is_expected.to have_body_text /#{project.name}/
320
      end
321

322
      it 'contains new user role' do
323
        is_expected.to have_body_text /#{project_member.human_access}/
324 325 326
      end
    end

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

331
      before :each do
332
        allow(Note).to receive(:find).with(note.id).and_return(note)
333 334
      end

R
Robb Kidd 已提交
335
      shared_examples 'a note email' do
336 337
        it_behaves_like 'it should have Gmail Actions links'

338 339
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
340 341
          expect(sender.display_name).to eq(note_author.name)
          expect(sender.address).to eq(gitlab_sender)
342 343
        end

R
Robb Kidd 已提交
344
        it 'is sent to the given recipient' do
345
          is_expected.to deliver_to recipient.notification_email
R
Robb Kidd 已提交
346 347 348
        end

        it 'contains the message from the note' do
349
          is_expected.to have_body_text /#{note.note}/
R
Robb Kidd 已提交
350
        end
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

        it 'not contains note author' do
          is_expected.not_to have_body_text /wrote\:/
        end

        context 'when enabled email_author_in_body' do
          before do
            allow(current_application_settings).to receive(:email_author_in_body).and_return(true)
          end

          it 'contains a link to note author' do
            is_expected.to have_body_text note.author_name
            is_expected.to have_body_text /wrote\:/
          end
        end
R
Robb Kidd 已提交
366 367 368
      end

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

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

D
Dmitriy Zaporozhets 已提交
373
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
374 375

        it_behaves_like 'a note email'
376
        it_behaves_like 'an answer to an existing thread', 'commit'
377
        it_behaves_like 'it should show Gmail Actions View Commit link'
378
        it_behaves_like "a user cannot unsubscribe through footer link"
R
Robb Kidd 已提交
379 380

        it 'has the correct subject' do
381
          is_expected.to have_subject /#{commit.title} \(#{commit.short_id}\)/
R
Robb Kidd 已提交
382 383 384
        end

        it 'contains a link to the commit' do
385
          is_expected.to have_body_text commit.short_id
R
Robb Kidd 已提交
386 387 388 389
        end
      end

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

394
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
395 396

        it_behaves_like 'a note email'
397
        it_behaves_like 'an answer to an existing thread', 'merge_request'
398
        it_behaves_like 'it should show Gmail Actions View Merge request link'
399
        it_behaves_like 'an unsubscribeable thread'
R
Robb Kidd 已提交
400 401

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

        it 'contains a link to the merge request note' do
406
          is_expected.to have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
407 408 409 410
        end
      end

      describe 'on an issue' do
411
        let(:issue) { create(:issue, project: project) }
V
Vinnie Okada 已提交
412
        let(:note_on_issue_path) { namespace_project_issue_path(project.namespace, project, issue, anchor: "note_#{note.id}") }
413
        before(:each) { allow(note).to receive(:noteable).and_return(issue) }
414 415

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

        it_behaves_like 'a note email'
418
        it_behaves_like 'an answer to an existing thread', 'issue'
419
        it_behaves_like 'it should show Gmail Actions View Issue link'
420
        it_behaves_like 'an unsubscribeable thread'
R
Robb Kidd 已提交
421 422

        it 'has the correct subject' do
423
          is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
424 425 426
        end

        it 'contains a link to the issue note' do
427
          is_expected.to have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
428 429
        end
      end
430 431
    end
  end
432 433 434 435

  describe 'group access changed' do
    let(:group) { create(:group) }
    let(:user) { create(:user) }
436
    let(:membership) { create(:group_member, group: group, user: user) }
437 438 439

    subject { Notify.group_access_granted_email(membership.id) }

440
    it_behaves_like 'an email sent from GitLab'
441
    it_behaves_like 'it should not have Gmail Actions links'
442
    it_behaves_like "a user cannot unsubscribe through footer link"
443

444
    it 'has the correct subject' do
445
      is_expected.to have_subject /Access to group was granted/
446 447 448
    end

    it 'contains name of project' do
449
      is_expected.to have_body_text /#{group.name}/
450 451 452
    end

    it 'contains new user role' do
453
      is_expected.to have_body_text /#{membership.human_access}/
454 455
    end
  end
456 457 458 459 460 461

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

    before do
V
Valery Sizov 已提交
462 463 464 465
      perform_enqueued_jobs do
        user.email = "new-email@mail.com"
        user.save
      end
466 467 468 469
    end

    subject { ActionMailer::Base.deliveries.last }

470
    it_behaves_like 'an email sent from GitLab'
471
    it_behaves_like "a user cannot unsubscribe through footer link"
472

473
    it 'is sent to the new user' do
474
      is_expected.to deliver_to 'new-email@mail.com'
475 476 477
    end

    it 'has the correct subject' do
478
      is_expected.to have_subject "Confirmation instructions"
479 480 481
    end

    it 'includes a link to the site' do
482
      is_expected.to have_body_text /#{example_site_path}/
483 484
    end
  end
D
Dmitriy Zaporozhets 已提交
485

486 487 488 489 490
  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") }

491
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :create) }
492

493
    it_behaves_like 'it should not have Gmail Actions links'
494
    it_behaves_like "a user cannot unsubscribe through footer link"
495 496
    it_behaves_like 'an email with X-GitLab headers containing project details'
    it_behaves_like 'an email that contains a header with author username'
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
    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") }

522
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/tags/v1.0', action: :create) }
523

524
    it_behaves_like 'it should not have Gmail Actions links'
525
    it_behaves_like "a user cannot unsubscribe through footer link"
526 527
    it_behaves_like 'an email with X-GitLab headers containing project details'
    it_behaves_like 'an email that contains a header with author username'
528

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    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) }

552
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/heads/master', action: :delete) }
553

554
    it_behaves_like 'it should not have Gmail Actions links'
555
    it_behaves_like "a user cannot unsubscribe through footer link"
556 557
    it_behaves_like 'an email with X-GitLab headers containing project details'
    it_behaves_like 'an email that contains a header with author username'
558

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    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) }

578
    subject { Notify.repository_push_email(project.id, 'devs@company.name', author_id: user.id, ref: 'refs/tags/v1.0', action: :delete) }
579

580
    it_behaves_like 'it should not have Gmail Actions links'
581
    it_behaves_like "a user cannot unsubscribe through footer link"
582 583
    it_behaves_like 'an email with X-GitLab headers containing project details'
    it_behaves_like 'an email that contains a header with author username'
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 /Deleted tag v1\.0/
    end
  end

600
  describe 'email on push with multiple commits' do
D
Dmitriy Zaporozhets 已提交
601 602
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
D
Dmitriy Zaporozhets 已提交
603
    let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) }
604 605
    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)) }
606
    let(:send_from_committer_email) { false }
D
Dmitriy Zaporozhets 已提交
607

608
    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 已提交
609

610
    it_behaves_like 'it should not have Gmail Actions links'
611
    it_behaves_like "a user cannot unsubscribe through footer link"
612 613
    it_behaves_like 'an email with X-GitLab headers containing project details'
    it_behaves_like 'an email that contains a header with author username'
614

615 616
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
617 618
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
619 620
    end

D
Dmitriy Zaporozhets 已提交
621
    it 'is sent to recipient' do
622
      is_expected.to deliver_to 'devs@company.name'
D
Dmitriy Zaporozhets 已提交
623 624 625
    end

    it 'has the correct subject' do
626
      is_expected.to have_subject /\[#{project.path_with_namespace}\]\[master\] #{commits.length} commits:/
D
Dmitriy Zaporozhets 已提交
627 628 629
    end

    it 'includes commits list' do
630
      is_expected.to have_body_text /Change some files/
D
Dmitriy Zaporozhets 已提交
631 632 633
    end

    it 'includes diffs' do
634
      is_expected.to have_body_text /def archive_formats_regex/
D
Dmitriy Zaporozhets 已提交
635
    end
636 637

    it 'contains a link to the diff' do
638
      is_expected.to have_body_text /#{diff_path}/
639
    end
640 641 642 643

    it 'doesn not contain the misleading footer' do
      is_expected.not_to have_body_text /you are a member of/
    end
644 645 646 647 648

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

      let(:send_from_committer_email) { true }

649 650 651 652 653
      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
654 655

        before do
656
          user.update_attribute(:email, "user@company.com")
657
          user.confirm
658 659 660 661 662 663
        end

        it "is sent from the committer email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(user.email)
        end
664 665 666 667 668

        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
669 670
      end

671 672 673 674
      context "when the committer email domain is not completely within the GitLab domain" do

        before do
          user.update_attribute(:email, "user@something.company.com")
675
          user.confirm
676 677 678 679 680 681
        end

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
682 683 684 685 686

        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
687 688 689 690 691 692
      end

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

        before do
          user.update_attribute(:email, "user@mpany.com")
693
          user.confirm
694
        end
695 696 697 698 699

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
700 701 702 703 704

        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
705 706
      end
    end
D
Dmitriy Zaporozhets 已提交
707
  end
708 709 710 711

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

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

718
    it_behaves_like 'it should show Gmail Actions View Commit link'
719
    it_behaves_like "a user cannot unsubscribe through footer link"
720 721
    it_behaves_like 'an email with X-GitLab headers containing project details'
    it_behaves_like 'an email that contains a header with author username'
722

723 724
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
725 726
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
727 728 729
    end

    it 'is sent to recipient' do
730
      is_expected.to deliver_to 'devs@company.name'
731 732 733
    end

    it 'has the correct subject' do
734
      is_expected.to have_subject /#{commits.first.title}/
735 736 737
    end

    it 'includes commits list' do
738
      is_expected.to have_body_text /Change some files/
739 740 741
    end

    it 'includes diffs' do
742
      is_expected.to have_body_text /def archive_formats_regex/
743 744 745
    end

    it 'contains a link to the diff' do
746
      is_expected.to have_body_text /#{diff_path}/
747 748
    end
  end
749

750
end