notify_spec.rb 30.4 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
      is_expected.to have_header 'Message-ID',  /<(.*)@#{Gitlab.config.gitlab.host}>/
56 57 58
      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}/
59 60 61
    end
  end

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

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

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

75 76 77 78 79
    it 'includes a link to the site' do
      is_expected.to have_body_text /#{site_path}/
    end
  end

80 81 82 83 84 85 86 87
  shared_examples 'it should have Gmail Actions links' do
    it { is_expected.to have_body_text /ViewAction/ }
  end

  shared_examples 'it should not have Gmail Actions links' do
    it { is_expected.to_not have_body_text /ViewAction/ }
  end

88 89 90 91 92 93 94 95 96 97
  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
98
    it_behaves_like 'it should not have Gmail Actions links'
99

M
Marin Jankovski 已提交
100
    it 'contains the password text' do
101
      is_expected.to have_body_text /Click here to set your password/
M
Marin Jankovski 已提交
102 103 104
    end

    it 'includes a link for user to set password' do
105
      params = "reset_password_token=#{token}"
106
      is_expected.to have_body_text(
107 108
        %r{http://localhost(:\d+)?/users/password/edit\?#{params}}
      )
109 110
    end

111 112 113
    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)
114
      is_expected.to have_body_text(/\?user_email=.*%40.*/)
115
    end
116 117
  end

M
Marin Jankovski 已提交
118 119 120

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

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

125
    it_behaves_like 'an email sent from GitLab'
126
    it_behaves_like 'a new user email', new_user_address
127
    it_behaves_like 'it should not have Gmail Actions links'
M
Marin Jankovski 已提交
128 129

    it 'should not contain the new user\'s password' do
130
      is_expected.not_to have_body_text /password/
M
Marin Jankovski 已提交
131 132 133
    end
  end

134 135 136 137 138
  describe 'user added ssh key' do
    let(:key) { create(:personal_key) }

    subject { Notify.new_ssh_key_email(key.id) }

139
    it_behaves_like 'an email sent from GitLab'
140
    it_behaves_like 'it should not have Gmail Actions links'
141

142
    it 'is sent to the new user' do
143
      is_expected.to deliver_to key.user.email
144 145 146
    end

    it 'has the correct subject' do
147
      is_expected.to have_subject /^SSH key was added to your account$/i
148 149 150
    end

    it 'contains the new ssh key title' do
151
      is_expected.to have_body_text /#{key.title}/
152 153 154
    end

    it 'includes a link to ssh keys page' do
155
      is_expected.to have_body_text /#{profile_keys_path}/
156 157 158
    end
  end

159 160 161 162 163
  describe 'user added email' do
    let(:email) { create(:email) }

    subject { Notify.new_email_email(email.id) }

164 165
    it_behaves_like 'it should not have Gmail Actions links'

166
    it 'is sent to the new user' do
167
      is_expected.to deliver_to email.user.email
168 169 170
    end

    it 'has the correct subject' do
171
      is_expected.to have_subject /^Email was added to your account$/i
172 173 174
    end

    it 'contains the new email address' do
175
      is_expected.to have_body_text /#{email.email}/
176 177 178
    end

    it 'includes a link to emails page' do
179
      is_expected.to have_body_text /#{profile_emails_path}/
180 181 182
    end
  end

R
Robb Kidd 已提交
183 184
  context 'for a project' do
    describe 'items that are assignable, the email' do
185
      let(:current_user) { create(:user, email: "current@email.com") }
186 187
      let(:assignee) { create(:user, email: 'assignee@example.com') }
      let(:previous_assignee) { create(:user, name: 'Previous Assignee') }
188

R
Robb Kidd 已提交
189
      shared_examples 'an assignee email' do
190 191
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
192 193
          expect(sender.display_name).to eq(current_user.name)
          expect(sender.address).to eq(gitlab_sender)
194 195
        end

R
Robb Kidd 已提交
196
        it 'is sent to the assignee' do
197
          is_expected.to deliver_to assignee.email
R
Robb Kidd 已提交
198 199
        end
      end
200

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

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

R
Robb Kidd 已提交
208
          it_behaves_like 'an assignee email'
209
          it_behaves_like 'an email starting a new thread', 'issue'
210
          it_behaves_like 'it should have Gmail Actions links'
211

R
Robb Kidd 已提交
212
          it 'has the correct subject' do
213
            is_expected.to have_subject /#{project.name} \| #{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
214
          end
215

R
Robb Kidd 已提交
216
          it 'contains a link to the new issue' do
V
Vinnie Okada 已提交
217
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
218
          end
219 220 221 222

          it 'Gmail Actions contain correct action name' do
            is_expected.to have_body_text /View Issue/
          end
R
Robb Kidd 已提交
223
        end
224

225 226 227 228
        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
229
            is_expected.to have_body_text /#{issue_with_description.description}/
230 231 232
          end
        end

R
Robb Kidd 已提交
233
        describe 'that have been reassigned' do
234
          subject { Notify.reassigned_issue_email(recipient.id, issue.id, previous_assignee.id, current_user) }
R
Robb Kidd 已提交
235 236

          it_behaves_like 'a multiple recipients email'
237
          it_behaves_like 'an answer to an existing thread', 'issue'
238
          it_behaves_like 'it should have Gmail Actions links'
R
Robb Kidd 已提交
239

240 241
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
242 243
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
244 245
          end

R
Robb Kidd 已提交
246
          it 'has the correct subject' do
247
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
248 249 250
          end

          it 'contains the name of the previous assignee' do
251
            is_expected.to have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
252 253 254
          end

          it 'contains the name of the new assignee' do
255
            is_expected.to have_body_text /#{assignee.name}/
R
Robb Kidd 已提交
256 257 258
          end

          it 'contains a link to the issue' do
V
Vinnie Okada 已提交
259
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
260
          end
261 262 263 264

          it 'Gmail Actions contain correct action name' do
            is_expected.to have_body_text /View Issue/
          end
R
Robb Kidd 已提交
265
        end
A
Alex Denisov 已提交
266 267 268 269

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

271
          it_behaves_like 'an answer to an existing thread', 'issue'
272
          it_behaves_like 'it should have Gmail Actions links'
273

274 275
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
276 277
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
278 279
          end

A
Alex Denisov 已提交
280
          it 'has the correct subject' do
281
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/i
A
Alex Denisov 已提交
282 283 284
          end

          it 'contains the new status' do
285
            is_expected.to have_body_text /#{status}/i
A
Alex Denisov 已提交
286 287 288
          end

          it 'contains the user name' do
289
            is_expected.to have_body_text /#{current_user.name}/i
A
Alex Denisov 已提交
290 291 292
          end

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

296 297 298 299
          it 'Gmail Actions contain correct action name' do
            is_expected.to have_body_text /View Issue/
          end
        end
R
Robb Kidd 已提交
300 301 302
      end

      context 'for merge requests' do
303
        let(:merge_author) { create(:user) }
304
        let(:merge_request) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project) }
R
Robert Speicher 已提交
305
        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 已提交
306 307

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

          it_behaves_like 'an assignee email'
311
          it_behaves_like 'an email starting a new thread', 'merge_request'
312
          it_behaves_like 'it should have Gmail Actions links'
R
Robb Kidd 已提交
313 314

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

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

          it 'contains the source branch for the merge request' do
323
            is_expected.to have_body_text /#{merge_request.source_branch}/
R
Robb Kidd 已提交
324 325 326
          end

          it 'contains the target branch for the merge request' do
327
            is_expected.to have_body_text /#{merge_request.target_branch}/
R
Robb Kidd 已提交
328
          end
P
Philip Blatter 已提交
329 330

          it 'has the correct message-id set' do
331
            is_expected.to have_header 'Message-ID', "<merge_request_#{merge_request.id}@#{Gitlab.config.gitlab.host}>"
P
Philip Blatter 已提交
332
          end
333 334 335 336

          it 'Gmail Actions contain correct action name' do
            is_expected.to have_body_text /View Merge request/
          end
R
Robb Kidd 已提交
337 338
        end

339 340 341
        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) }

342 343
          it_behaves_like 'it should have Gmail Actions links'

344
          it 'contains the description' do
345
            is_expected.to have_body_text /#{merge_request_with_description.description}/
346
          end
347 348 349 350

          it 'Gmail Actions contain correct action name' do
            is_expected.to have_body_text /View Merge request/
          end
351 352
        end

R
Robb Kidd 已提交
353
        describe 'that are reassigned' do
354
          subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id, current_user.id) }
R
Robb Kidd 已提交
355 356

          it_behaves_like 'a multiple recipients email'
357
          it_behaves_like 'an answer to an existing thread', 'merge_request'
358
          it_behaves_like 'it should have Gmail Actions links'
R
Robb Kidd 已提交
359

360 361
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
362 363
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
364 365
          end

R
Robb Kidd 已提交
366
          it 'has the correct subject' do
367
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/
R
Robb Kidd 已提交
368 369 370
          end

          it 'contains the name of the previous assignee' do
371
            is_expected.to have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
372 373 374
          end

          it 'contains the name of the new assignee' do
375
            is_expected.to have_body_text /#{assignee.name}/
R
Robb Kidd 已提交
376 377 378
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
379
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
R
Robb Kidd 已提交
380
          end
381 382 383 384

          it 'Gmail Actions contain correct action name' do
            is_expected.to have_body_text /View Merge request/
          end
385 386
        end

387 388 389 390 391
        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'
392 393
          it_behaves_like 'it should have Gmail Actions links'

394 395 396

          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
397 398
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
399 400 401
          end

          it 'has the correct subject' do
402
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/i
403 404 405
          end

          it 'contains the new status' do
406
            is_expected.to have_body_text /#{status}/i
407 408 409
          end

          it 'contains the user name' do
410
            is_expected.to have_body_text /#{current_user.name}/i
411 412 413
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
414
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
415
          end
416 417 418 419

          it 'Gmail Actions contain correct action name' do
            is_expected.to have_body_text /View Merge request/
          end
420 421
        end

422 423 424 425
        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'
426
          it_behaves_like 'an answer to an existing thread', 'merge_request'
427
          it_behaves_like 'it should have Gmail Actions links'
428 429 430

          it 'is sent as the merge author' do
            sender = subject.header[:from].addrs[0]
431 432
            expect(sender.display_name).to eq(merge_author.name)
            expect(sender.address).to eq(gitlab_sender)
433 434 435
          end

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

439
          it 'contains the new status' do
440
            is_expected.to have_body_text /merged/i
441 442 443
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
444
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
445
          end
446 447 448 449

          it 'Gmail Actions contain correct action name' do
            is_expected.to have_body_text /View Merge request/
          end
R
Robb Kidd 已提交
450 451
        end
      end
452 453
    end

454 455 456
    describe 'project was moved' do
      let(:project) { create(:project) }
      let(:user) { create(:user) }
457
      subject { Notify.project_was_moved_email(project.id, user.id, "gitlab/gitlab") }
458

459
      it_behaves_like 'an email sent from GitLab'
460
      it_behaves_like 'it should not have Gmail Actions links'
461

462
      it 'has the correct subject' do
463
        is_expected.to have_subject /Project was moved/
464 465 466
      end

      it 'contains name of project' do
467
        is_expected.to have_body_text /#{project.name_with_namespace}/
468 469 470
      end

      it 'contains new user role' do
471
        is_expected.to have_body_text /#{project.ssh_url_to_repo}/
472 473 474
      end
    end

475
    describe 'project access changed' do
476
      let(:project) { create(:project) }
477
      let(:user) { create(:user) }
478
      let(:project_member) { create(:project_member, project: project, user: user) }
479
      subject { Notify.project_access_granted_email(project_member.id) }
480 481

      it_behaves_like 'an email sent from GitLab'
482
      it_behaves_like 'it should not have Gmail Actions links'
483

484
      it 'has the correct subject' do
485
        is_expected.to have_subject /Access to project was granted/
486
      end
487

488
      it 'contains name of project' do
489
        is_expected.to have_body_text /#{project.name}/
490
      end
491

492
      it 'contains new user role' do
493
        is_expected.to have_body_text /#{project_member.human_access}/
494 495 496
      end
    end

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

501
      before :each do
502
        allow(Note).to receive(:find).with(note.id).and_return(note)
503 504
      end

R
Robb Kidd 已提交
505
      shared_examples 'a note email' do
506 507
        it_behaves_like 'it should have Gmail Actions links'

508 509
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
510 511
          expect(sender.display_name).to eq(note_author.name)
          expect(sender.address).to eq(gitlab_sender)
512 513
        end

R
Robb Kidd 已提交
514
        it 'is sent to the given recipient' do
515
          is_expected.to deliver_to recipient.notification_email
R
Robb Kidd 已提交
516 517 518
        end

        it 'contains the message from the note' do
519
          is_expected.to have_body_text /#{note.note}/
R
Robb Kidd 已提交
520 521 522 523
        end
      end

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

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

D
Dmitriy Zaporozhets 已提交
528
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
529 530

        it_behaves_like 'a note email'
531
        it_behaves_like 'an answer to an existing thread', 'commit'
532
        it_behaves_like 'it should have Gmail Actions links'
R
Robb Kidd 已提交
533 534

        it 'has the correct subject' do
535
          is_expected.to have_subject /#{commit.title} \(#{commit.short_id}\)/
R
Robb Kidd 已提交
536 537 538
        end

        it 'contains a link to the commit' do
539
          is_expected.to have_body_text commit.short_id
R
Robb Kidd 已提交
540
        end
541 542 543 544

        it 'Gmail Actions contain correct action name' do
          is_expected.to have_body_text /View Commit/
        end
R
Robb Kidd 已提交
545 546 547
      end

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

552
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
553 554

        it_behaves_like 'a note email'
555
        it_behaves_like 'an answer to an existing thread', 'merge_request'
556
        it_behaves_like 'it should have Gmail Actions links'
R
Robb Kidd 已提交
557 558

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

        it 'contains a link to the merge request note' do
563
          is_expected.to have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
564
        end
565 566 567 568

        it 'Gmail Actions contain correct action name' do
          is_expected.to have_body_text /View Merge request/
        end
R
Robb Kidd 已提交
569 570 571
      end

      describe 'on an issue' do
572
        let(:issue) { create(:issue, project: project) }
V
Vinnie Okada 已提交
573
        let(:note_on_issue_path) { namespace_project_issue_path(project.namespace, project, issue, anchor: "note_#{note.id}") }
574
        before(:each) { allow(note).to receive(:noteable).and_return(issue) }
575 576

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

        it_behaves_like 'a note email'
579
        it_behaves_like 'an answer to an existing thread', 'issue'
580
        it_behaves_like 'it should have Gmail Actions links'
R
Robb Kidd 已提交
581 582

        it 'has the correct subject' do
583
          is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
584 585 586
        end

        it 'contains a link to the issue note' do
587
          is_expected.to have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
588
        end
589 590 591 592

        it 'Gmail Actions contain correct action name' do
          is_expected.to have_body_text /View Issue/
        end
R
Robb Kidd 已提交
593
      end
594 595
    end
  end
596 597 598 599

  describe 'group access changed' do
    let(:group) { create(:group) }
    let(:user) { create(:user) }
600
    let(:membership) { create(:group_member, group: group, user: user) }
601 602 603

    subject { Notify.group_access_granted_email(membership.id) }

604
    it_behaves_like 'an email sent from GitLab'
605
    it_behaves_like 'it should not have Gmail Actions links'
606

607
    it 'has the correct subject' do
608
      is_expected.to have_subject /Access to group was granted/
609 610 611
    end

    it 'contains name of project' do
612
      is_expected.to have_body_text /#{group.name}/
613 614 615
    end

    it 'contains new user role' do
616
      is_expected.to have_body_text /#{membership.human_access}/
617 618
    end
  end
619 620 621 622 623 624 625 626 627 628 629 630

  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 }

631 632
    it_behaves_like 'an email sent from GitLab'

633
    it 'is sent to the new user' do
634
      is_expected.to deliver_to 'new-email@mail.com'
635 636 637
    end

    it 'has the correct subject' do
638
      is_expected.to have_subject "Confirmation instructions"
639 640 641
    end

    it 'includes a link to the site' do
642
      is_expected.to have_body_text /#{example_site_path}/
643 644
    end
  end
D
Dmitriy Zaporozhets 已提交
645

646 647 648 649 650
  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") }

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

653 654
    it_behaves_like 'it should not have Gmail Actions links'

655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    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") }

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

681 682
    it_behaves_like 'it should not have Gmail Actions links'

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
    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) }

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

708 709
    it_behaves_like 'it should not have Gmail Actions links'

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    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) }

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

731 732
    it_behaves_like 'it should not have Gmail Actions links'

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

748
  describe 'email on push with multiple commits' do
D
Dmitriy Zaporozhets 已提交
749 750
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
D
Dmitriy Zaporozhets 已提交
751
    let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) }
752 753
    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)) }
754
    let(:send_from_committer_email) { false }
D
Dmitriy Zaporozhets 已提交
755

756
    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 已提交
757

758 759
    it_behaves_like 'it should not have Gmail Actions links'

760 761
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
762 763
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
764 765
    end

D
Dmitriy Zaporozhets 已提交
766
    it 'is sent to recipient' do
767
      is_expected.to deliver_to 'devs@company.name'
D
Dmitriy Zaporozhets 已提交
768 769 770
    end

    it 'has the correct subject' do
771
      is_expected.to have_subject /\[#{project.path_with_namespace}\]\[master\] #{commits.length} commits:/
D
Dmitriy Zaporozhets 已提交
772 773 774
    end

    it 'includes commits list' do
775
      is_expected.to have_body_text /Change some files/
D
Dmitriy Zaporozhets 已提交
776 777 778
    end

    it 'includes diffs' do
779
      is_expected.to have_body_text /def archive_formats_regex/
D
Dmitriy Zaporozhets 已提交
780
    end
781 782

    it 'contains a link to the diff' do
783
      is_expected.to have_body_text /#{diff_path}/
784
    end
785 786 787 788

    it 'doesn not contain the misleading footer' do
      is_expected.not_to have_body_text /you are a member of/
    end
789 790 791 792 793

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

      let(:send_from_committer_email) { true }

794 795 796 797 798
      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
799 800

        before do
801
          user.update_attribute(:email, "user@company.com")
802
          user.confirm
803 804 805 806 807 808
        end

        it "is sent from the committer email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(user.email)
        end
809 810 811 812 813

        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
814 815
      end

816 817 818 819
      context "when the committer email domain is not completely within the GitLab domain" do

        before do
          user.update_attribute(:email, "user@something.company.com")
820
          user.confirm
821 822 823 824 825 826
        end

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
827 828 829 830 831

        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
832 833 834 835 836 837
      end

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

        before do
          user.update_attribute(:email, "user@mpany.com")
838
          user.confirm
839
        end
840 841 842 843 844

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
845 846 847 848 849

        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
850 851
      end
    end
D
Dmitriy Zaporozhets 已提交
852
  end
853 854 855 856

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

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

863 864
    it_behaves_like 'it should have Gmail Actions links'

865 866
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
867 868
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
869 870 871
    end

    it 'is sent to recipient' do
872
      is_expected.to deliver_to 'devs@company.name'
873 874 875
    end

    it 'has the correct subject' do
876
      is_expected.to have_subject /#{commits.first.title}/
877 878 879
    end

    it 'includes commits list' do
880
      is_expected.to have_body_text /Change some files/
881 882 883
    end

    it 'includes diffs' do
884
      is_expected.to have_body_text /def archive_formats_regex/
885 886 887
    end

    it 'contains a link to the diff' do
888
      is_expected.to have_body_text /#{diff_path}/
889
    end
890 891 892 893

    it 'Gmail Actions contain correct action name' do
      is_expected.to have_body_text /View Commit/
    end
894
  end
895
end