notify_spec.rb 29.7 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 98 99 100 101 102 103 104 105
  shared_examples 'it should show Gmail Actions View Issue link' do
    it_behaves_like 'it should have Gmail Actions links'

    it { is_expected.to have_body_text /View Issue/ }
  end

  shared_examples 'it should show Gmail Actions View Merge request link' do
    it_behaves_like 'it should have Gmail Actions links'

    it { is_expected.to have_body_text /View Merge request/ }
  end

  shared_examples 'it should show Gmail Actions View Commit link' do
    it_behaves_like 'it should have Gmail Actions links'

    it { is_expected.to have_body_text /View Commit/ }
  end

106 107 108 109 110 111 112 113 114 115
  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
116
    it_behaves_like 'it should not have Gmail Actions links'
117

M
Marin Jankovski 已提交
118
    it 'contains the password text' do
119
      is_expected.to have_body_text /Click here to set your password/
M
Marin Jankovski 已提交
120 121 122
    end

    it 'includes a link for user to set password' do
123
      params = "reset_password_token=#{token}"
124
      is_expected.to have_body_text(
125 126
        %r{http://localhost(:\d+)?/users/password/edit\?#{params}}
      )
127 128
    end

129 130 131
    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)
132
      is_expected.to have_body_text(/\?user_email=.*%40.*/)
133
    end
134 135
  end

M
Marin Jankovski 已提交
136 137 138

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

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

143
    it_behaves_like 'an email sent from GitLab'
144
    it_behaves_like 'a new user email', new_user_address
145
    it_behaves_like 'it should not have Gmail Actions links'
M
Marin Jankovski 已提交
146 147

    it 'should not contain the new user\'s password' do
148
      is_expected.not_to have_body_text /password/
M
Marin Jankovski 已提交
149 150 151
    end
  end

152 153 154 155 156
  describe 'user added ssh key' do
    let(:key) { create(:personal_key) }

    subject { Notify.new_ssh_key_email(key.id) }

157
    it_behaves_like 'an email sent from GitLab'
158
    it_behaves_like 'it should not have Gmail Actions links'
159

160
    it 'is sent to the new user' do
161
      is_expected.to deliver_to key.user.email
162 163 164
    end

    it 'has the correct subject' do
165
      is_expected.to have_subject /^SSH key was added to your account$/i
166 167 168
    end

    it 'contains the new ssh key title' do
169
      is_expected.to have_body_text /#{key.title}/
170 171 172
    end

    it 'includes a link to ssh keys page' do
173
      is_expected.to have_body_text /#{profile_keys_path}/
174 175 176
    end
  end

177 178 179 180 181
  describe 'user added email' do
    let(:email) { create(:email) }

    subject { Notify.new_email_email(email.id) }

182 183
    it_behaves_like 'it should not have Gmail Actions links'

184
    it 'is sent to the new user' do
185
      is_expected.to deliver_to email.user.email
186 187 188
    end

    it 'has the correct subject' do
189
      is_expected.to have_subject /^Email was added to your account$/i
190 191 192
    end

    it 'contains the new email address' do
193
      is_expected.to have_body_text /#{email.email}/
194 195 196
    end

    it 'includes a link to emails page' do
197
      is_expected.to have_body_text /#{profile_emails_path}/
198 199 200
    end
  end

R
Robb Kidd 已提交
201 202
  context 'for a project' do
    describe 'items that are assignable, the email' do
203
      let(:current_user) { create(:user, email: "current@email.com") }
204 205
      let(:assignee) { create(:user, email: 'assignee@example.com') }
      let(:previous_assignee) { create(:user, name: 'Previous Assignee') }
206

R
Robb Kidd 已提交
207
      shared_examples 'an assignee email' do
208 209
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
210 211
          expect(sender.display_name).to eq(current_user.name)
          expect(sender.address).to eq(gitlab_sender)
212 213
        end

R
Robb Kidd 已提交
214
        it 'is sent to the assignee' do
215
          is_expected.to deliver_to assignee.email
R
Robb Kidd 已提交
216 217
        end
      end
218

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

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

R
Robb Kidd 已提交
226
          it_behaves_like 'an assignee email'
227
          it_behaves_like 'an email starting a new thread', 'issue'
228
          it_behaves_like 'it should show Gmail Actions View Issue link'
229

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

R
Robb Kidd 已提交
234
          it 'contains a link to the new issue' do
V
Vinnie Okada 已提交
235
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
236 237
          end
        end
238

239 240 241
        describe 'that are new with a description' do
          subject { Notify.new_issue_email(issue_with_description.assignee_id, issue_with_description.id) }

242 243
          it_behaves_like 'it should show Gmail Actions View Issue link'

244
          it 'contains the description' do
245
            is_expected.to have_body_text /#{issue_with_description.description}/
246 247 248
          end
        end

R
Robb Kidd 已提交
249
        describe 'that have been reassigned' do
250
          subject { Notify.reassigned_issue_email(recipient.id, issue.id, previous_assignee.id, current_user) }
R
Robb Kidd 已提交
251 252

          it_behaves_like 'a multiple recipients email'
253
          it_behaves_like 'an answer to an existing thread', 'issue'
254
          it_behaves_like 'it should show Gmail Actions View Issue link'
R
Robb Kidd 已提交
255

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

R
Robb Kidd 已提交
262
          it 'has the correct subject' do
263
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
264 265 266
          end

          it 'contains the name of the previous assignee' do
267
            is_expected.to have_body_text /#{previous_assignee.name}/
R
Robb Kidd 已提交
268 269 270
          end

          it 'contains the name of the new assignee' do
271
            is_expected.to have_body_text /#{assignee.name}/
R
Robb Kidd 已提交
272 273 274
          end

          it 'contains a link to the issue' do
V
Vinnie Okada 已提交
275
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
R
Robb Kidd 已提交
276 277
          end
        end
A
Alex Denisov 已提交
278 279 280 281

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

283
          it_behaves_like 'an answer to an existing thread', 'issue'
284
          it_behaves_like 'it should show Gmail Actions View Issue link'
285

286 287
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
288 289
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
290 291
          end

A
Alex Denisov 已提交
292
          it 'has the correct subject' do
293
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/i
A
Alex Denisov 已提交
294 295 296
          end

          it 'contains the new status' do
297
            is_expected.to have_body_text /#{status}/i
A
Alex Denisov 已提交
298 299 300
          end

          it 'contains the user name' do
301
            is_expected.to have_body_text /#{current_user.name}/i
A
Alex Denisov 已提交
302 303 304
          end

          it 'contains a link to the issue' do
V
Vinnie Okada 已提交
305
            is_expected.to have_body_text /#{namespace_project_issue_path project.namespace, project, issue}/
A
Alex Denisov 已提交
306
          end
307
        end
R
Robb Kidd 已提交
308 309 310
      end

      context 'for merge requests' do
311
        let(:merge_author) { create(:user) }
312
        let(:merge_request) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project) }
R
Robert Speicher 已提交
313
        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 已提交
314 315

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

          it_behaves_like 'an assignee email'
319
          it_behaves_like 'an email starting a new thread', 'merge_request'
320
          it_behaves_like 'it should show Gmail Actions View Merge request link'
R
Robb Kidd 已提交
321 322

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

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

          it 'contains the source branch for the merge request' do
331
            is_expected.to have_body_text /#{merge_request.source_branch}/
R
Robb Kidd 已提交
332 333 334
          end

          it 'contains the target branch for the merge request' do
335
            is_expected.to have_body_text /#{merge_request.target_branch}/
R
Robb Kidd 已提交
336
          end
P
Philip Blatter 已提交
337 338

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

343 344 345
        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) }

346
          it_behaves_like 'it should show Gmail Actions View Merge request link'
347

348
          it 'contains the description' do
349
            is_expected.to have_body_text /#{merge_request_with_description.description}/
350 351 352
          end
        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 show Gmail Actions View Merge request link'
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
        end

383 384 385 386 387
        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'
388
          it_behaves_like 'it should show Gmail Actions View Merge request link'
389 390 391

          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
392 393
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
394 395 396
          end

          it 'has the correct subject' do
397
            is_expected.to have_subject /#{merge_request.title} \(##{merge_request.iid}\)/i
398 399 400
          end

          it 'contains the new status' do
401
            is_expected.to have_body_text /#{status}/i
402 403 404
          end

          it 'contains the user name' do
405
            is_expected.to have_body_text /#{current_user.name}/i
406 407 408
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
409
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
410 411 412
          end
        end

413 414 415 416
        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'
417
          it_behaves_like 'an answer to an existing thread', 'merge_request'
418
          it_behaves_like 'it should show Gmail Actions View Merge request link'
419 420 421

          it 'is sent as the merge author' do
            sender = subject.header[:from].addrs[0]
422 423
            expect(sender.display_name).to eq(merge_author.name)
            expect(sender.address).to eq(gitlab_sender)
424 425 426
          end

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

430
          it 'contains the new status' do
431
            is_expected.to have_body_text /merged/i
432 433 434
          end

          it 'contains a link to the merge request' do
V
Vinnie Okada 已提交
435
            is_expected.to have_body_text /#{namespace_project_merge_request_path project.namespace, project, merge_request}/
436
          end
R
Robb Kidd 已提交
437 438
        end
      end
439 440
    end

441 442 443
    describe 'project was moved' do
      let(:project) { create(:project) }
      let(:user) { create(:user) }
444
      subject { Notify.project_was_moved_email(project.id, user.id, "gitlab/gitlab") }
445

446
      it_behaves_like 'an email sent from GitLab'
447
      it_behaves_like 'it should not have Gmail Actions links'
448

449
      it 'has the correct subject' do
450
        is_expected.to have_subject /Project was moved/
451 452 453
      end

      it 'contains name of project' do
454
        is_expected.to have_body_text /#{project.name_with_namespace}/
455 456 457
      end

      it 'contains new user role' do
458
        is_expected.to have_body_text /#{project.ssh_url_to_repo}/
459 460 461
      end
    end

462
    describe 'project access changed' do
463
      let(:project) { create(:project) }
464
      let(:user) { create(:user) }
465
      let(:project_member) { create(:project_member, project: project, user: user) }
466
      subject { Notify.project_access_granted_email(project_member.id) }
467 468

      it_behaves_like 'an email sent from GitLab'
469
      it_behaves_like 'it should not have Gmail Actions links'
470

471
      it 'has the correct subject' do
472
        is_expected.to have_subject /Access to project was granted/
473
      end
474

475
      it 'contains name of project' do
476
        is_expected.to have_body_text /#{project.name}/
477
      end
478

479
      it 'contains new user role' do
480
        is_expected.to have_body_text /#{project_member.human_access}/
481 482 483
      end
    end

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

488
      before :each do
489
        allow(Note).to receive(:find).with(note.id).and_return(note)
490 491
      end

R
Robb Kidd 已提交
492
      shared_examples 'a note email' do
493 494
        it_behaves_like 'it should have Gmail Actions links'

495 496
        it 'is sent as the author' do
          sender = subject.header[:from].addrs[0]
497 498
          expect(sender.display_name).to eq(note_author.name)
          expect(sender.address).to eq(gitlab_sender)
499 500
        end

R
Robb Kidd 已提交
501
        it 'is sent to the given recipient' do
502
          is_expected.to deliver_to recipient.notification_email
R
Robb Kidd 已提交
503 504 505
        end

        it 'contains the message from the note' do
506
          is_expected.to have_body_text /#{note.note}/
R
Robb Kidd 已提交
507 508 509 510
        end
      end

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

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

D
Dmitriy Zaporozhets 已提交
515
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
516 517

        it_behaves_like 'a note email'
518
        it_behaves_like 'an answer to an existing thread', 'commit'
519
        it_behaves_like 'it should show Gmail Actions View Commit link'
R
Robb Kidd 已提交
520 521

        it 'has the correct subject' do
522
          is_expected.to have_subject /#{commit.title} \(#{commit.short_id}\)/
R
Robb Kidd 已提交
523 524 525
        end

        it 'contains a link to the commit' do
526
          is_expected.to have_body_text commit.short_id
R
Robb Kidd 已提交
527 528 529 530
        end
      end

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

535
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
536 537

        it_behaves_like 'a note email'
538
        it_behaves_like 'an answer to an existing thread', 'merge_request'
539
        it_behaves_like 'it should show Gmail Actions View Merge request link'
R
Robb Kidd 已提交
540 541

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

        it 'contains a link to the merge request note' do
546
          is_expected.to have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
547 548 549 550
        end
      end

      describe 'on an issue' do
551
        let(:issue) { create(:issue, project: project) }
V
Vinnie Okada 已提交
552
        let(:note_on_issue_path) { namespace_project_issue_path(project.namespace, project, issue, anchor: "note_#{note.id}") }
553
        before(:each) { allow(note).to receive(:noteable).and_return(issue) }
554 555

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

        it_behaves_like 'a note email'
558
        it_behaves_like 'an answer to an existing thread', 'issue'
559
        it_behaves_like 'it should show Gmail Actions View Issue link'
R
Robb Kidd 已提交
560 561

        it 'has the correct subject' do
562
          is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/
R
Robb Kidd 已提交
563 564 565
        end

        it 'contains a link to the issue note' do
566
          is_expected.to have_body_text /#{note_on_issue_path}/
R
Robb Kidd 已提交
567 568
        end
      end
569 570
    end
  end
571 572 573 574

  describe 'group access changed' do
    let(:group) { create(:group) }
    let(:user) { create(:user) }
575
    let(:membership) { create(:group_member, group: group, user: user) }
576 577 578

    subject { Notify.group_access_granted_email(membership.id) }

579
    it_behaves_like 'an email sent from GitLab'
580
    it_behaves_like 'it should not have Gmail Actions links'
581

582
    it 'has the correct subject' do
583
      is_expected.to have_subject /Access to group was granted/
584 585 586
    end

    it 'contains name of project' do
587
      is_expected.to have_body_text /#{group.name}/
588 589 590
    end

    it 'contains new user role' do
591
      is_expected.to have_body_text /#{membership.human_access}/
592 593
    end
  end
594 595 596 597 598 599 600 601 602 603 604 605

  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 }

606 607
    it_behaves_like 'an email sent from GitLab'

608
    it 'is sent to the new user' do
609
      is_expected.to deliver_to 'new-email@mail.com'
610 611 612
    end

    it 'has the correct subject' do
613
      is_expected.to have_subject "Confirmation instructions"
614 615 616
    end

    it 'includes a link to the site' do
617
      is_expected.to have_body_text /#{example_site_path}/
618 619
    end
  end
D
Dmitriy Zaporozhets 已提交
620

621 622 623 624 625
  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") }

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

628 629
    it_behaves_like 'it should not have Gmail Actions links'

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    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") }

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

656 657
    it_behaves_like 'it should not have Gmail Actions links'

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

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

683 684
    it_behaves_like 'it should not have Gmail Actions links'

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

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

706 707
    it_behaves_like 'it should not have Gmail Actions links'

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
    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

723
  describe 'email on push with multiple commits' do
D
Dmitriy Zaporozhets 已提交
724 725
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
D
Dmitriy Zaporozhets 已提交
726
    let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) }
727 728
    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)) }
729
    let(:send_from_committer_email) { false }
D
Dmitriy Zaporozhets 已提交
730

731
    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 已提交
732

733 734
    it_behaves_like 'it should not have Gmail Actions links'

735 736
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
737 738
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
739 740
    end

D
Dmitriy Zaporozhets 已提交
741
    it 'is sent to recipient' do
742
      is_expected.to deliver_to 'devs@company.name'
D
Dmitriy Zaporozhets 已提交
743 744 745
    end

    it 'has the correct subject' do
746
      is_expected.to have_subject /\[#{project.path_with_namespace}\]\[master\] #{commits.length} commits:/
D
Dmitriy Zaporozhets 已提交
747 748 749
    end

    it 'includes commits list' do
750
      is_expected.to have_body_text /Change some files/
D
Dmitriy Zaporozhets 已提交
751 752 753
    end

    it 'includes diffs' do
754
      is_expected.to have_body_text /def archive_formats_regex/
D
Dmitriy Zaporozhets 已提交
755
    end
756 757

    it 'contains a link to the diff' do
758
      is_expected.to have_body_text /#{diff_path}/
759
    end
760 761 762 763

    it 'doesn not contain the misleading footer' do
      is_expected.not_to have_body_text /you are a member of/
    end
764 765 766 767 768

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

      let(:send_from_committer_email) { true }

769 770 771 772 773
      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
774 775

        before do
776
          user.update_attribute(:email, "user@company.com")
777
          user.confirm
778 779 780 781 782 783
        end

        it "is sent from the committer email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(user.email)
        end
784 785 786 787 788

        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
789 790
      end

791 792 793 794
      context "when the committer email domain is not completely within the GitLab domain" do

        before do
          user.update_attribute(:email, "user@something.company.com")
795
          user.confirm
796 797 798 799 800 801
        end

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
802 803 804 805 806

        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
807 808 809 810 811 812
      end

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

        before do
          user.update_attribute(:email, "user@mpany.com")
813
          user.confirm
814
        end
815 816 817 818 819

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
820 821 822 823 824

        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
825 826
      end
    end
D
Dmitriy Zaporozhets 已提交
827
  end
828 829 830 831

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

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

838
    it_behaves_like 'it should show Gmail Actions View Commit link'
839

840 841
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
842 843
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
844 845 846
    end

    it 'is sent to recipient' do
847
      is_expected.to deliver_to 'devs@company.name'
848 849 850
    end

    it 'has the correct subject' do
851
      is_expected.to have_subject /#{commits.first.title}/
852 853 854
    end

    it 'includes commits list' do
855
      is_expected.to have_body_text /Change some files/
856 857 858
    end

    it 'includes diffs' do
859
      is_expected.to have_body_text /def archive_formats_regex/
860 861 862
    end

    it 'contains a link to the diff' do
863
      is_expected.to have_body_text /#{diff_path}/
864 865
    end
  end
866
end