notify_spec.rb 26.5 KB
Newer Older
1 2 3 4 5
require 'spec_helper'

describe Notify do
  include EmailSpec::Helpers
  include EmailSpec::Matchers
D
Dmitriy Zaporozhets 已提交
6
  include RepoHelpers
7

8
  let(:gitlab_sender_display_name) { Gitlab.config.gitlab.email_display_name }
9
  let(:gitlab_sender) { Gitlab.config.gitlab.email_from }
10
  let(:recipient) { create(:user, email: 'recipient@example.com') }
D
Dmitriy Zaporozhets 已提交
11
  let(:project) { create(:project) }
12

13 14 15 16 17
  before(:each) do
    email = recipient.emails.create(email: "notifications@example.com")
    recipient.update_attribute(:notification_email, email.email)
  end

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

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

32 33
  shared_examples 'an email starting a new thread' do |message_id_prefix|
    it 'has a discussion identifier' do
34 35
      is_expected.to have_header 'Message-ID',  /<#{message_id_prefix}(.*)@#{Gitlab.config.gitlab.host}>/
      is_expected.to have_header 'X-GitLab-Project', /#{project.name}/
36 37 38 39 40
    end
  end

  shared_examples 'an answer to an existing thread' do |thread_id_prefix|
    it 'has a subject that begins with Re: ' do
41
      is_expected.to have_subject /^Re: /
42 43 44
    end

    it 'has headers that reference an existing thread' do
45 46 47
      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}/
48 49 50
    end
  end

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

55 56
    token = 'kETLwRaayvigPq_x3SNM'

57
    subject { Notify.new_user_email(new_user.id, token) }
58

59 60
    it_behaves_like 'an email sent from GitLab'

61
    it 'is sent to the new user' do
62
      is_expected.to deliver_to new_user.email
63 64 65
    end

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

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

M
Marin Jankovski 已提交
73
    it 'contains the password text' do
74
      is_expected.to have_body_text /Click here to set your password/
M
Marin Jankovski 已提交
75 76 77
    end

    it 'includes a link for user to set password' do
78
      params = "reset_password_token=#{token}"
79
      is_expected.to have_body_text(
80 81
        %r{http://localhost(:\d+)?/users/password/edit\?#{params}}
      )
82 83 84
    end

    it 'includes a link to the site' do
85
      is_expected.to have_body_text /#{example_site_path}/
86 87 88
    end
  end

M
Marin Jankovski 已提交
89 90 91 92 93

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

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

96 97
    it_behaves_like 'an email sent from GitLab'

M
Marin Jankovski 已提交
98
    it 'is sent to the new user' do
99
      is_expected.to deliver_to new_user.email
M
Marin Jankovski 已提交
100 101 102
    end

    it 'has the correct subject' do
103
      is_expected.to have_subject /^Account was created for you$/i
M
Marin Jankovski 已提交
104 105 106
    end

    it 'contains the new user\'s login name' do
107
      is_expected.to have_body_text /#{new_user.email}/
M
Marin Jankovski 已提交
108 109 110
    end

    it 'should not contain the new user\'s password' do
111
      is_expected.not_to have_body_text /password/
M
Marin Jankovski 已提交
112 113 114
    end

    it 'includes a link to the site' do
115
      is_expected.to have_body_text /#{example_site_path}/
M
Marin Jankovski 已提交
116 117 118
    end
  end

119 120 121 122 123
  describe 'user added ssh key' do
    let(:key) { create(:personal_key) }

    subject { Notify.new_ssh_key_email(key.id) }

124 125
    it_behaves_like 'an email sent from GitLab'

126
    it 'is sent to the new user' do
127
      is_expected.to deliver_to key.user.email
128 129 130
    end

    it 'has the correct subject' do
131
      is_expected.to have_subject /^SSH key was added to your account$/i
132 133 134
    end

    it 'contains the new ssh key title' do
135
      is_expected.to have_body_text /#{key.title}/
136 137 138
    end

    it 'includes a link to ssh keys page' do
139
      is_expected.to have_body_text /#{profile_keys_path}/
140 141 142
    end
  end

143 144 145 146 147 148
  describe 'user added email' do
    let(:email) { create(:email) }

    subject { Notify.new_email_email(email.id) }

    it 'is sent to the new user' do
149
      is_expected.to deliver_to email.user.email
150 151 152
    end

    it 'has the correct subject' do
153
      is_expected.to have_subject /^Email was added to your account$/i
154 155 156
    end

    it 'contains the new email address' do
157
      is_expected.to have_body_text /#{email.email}/
158 159 160
    end

    it 'includes a link to emails page' do
161
      is_expected.to have_body_text /#{profile_emails_path}/
162 163 164
    end
  end

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

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

R
Robb Kidd 已提交
178
        it 'is sent to the assignee' do
179
          is_expected.to deliver_to assignee.email
R
Robb Kidd 已提交
180 181
        end
      end
182

R
Robb Kidd 已提交
183
      context 'for issues' do
184 185
        let(:issue) { create(:issue, author: current_user, assignee: assignee, project: project) }
        let(:issue_with_description) { create(:issue, author: current_user, assignee: assignee, project: project, description: Faker::Lorem.sentence) }
186 187 188 189 190 191 192
        let(:issue_with_image) do
          create(:issue,
                 author: current_user,
                 assignee: assignee,
                 project: project,
                 description: "![test](#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/12345/test.jpg)")
        end
193

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

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

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

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

209 210 211 212
        describe 'that are new with a description' do
          subject { Notify.new_issue_email(issue_with_description.assignee_id, issue_with_description.id) }

          it 'contains the description' do
213
            is_expected.to have_body_text /#{issue_with_description.description}/
214 215 216
          end
        end

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        describe 'that contain images' do
          let(:png) { File.read("#{Rails.root}/spec/fixtures/dk.png") }
          let(:png_encoded) { Base64::encode64(png) }

          before :each do
            file_path = File.join(Rails.root, 'public', 'uploads', issue_with_image.project.path_with_namespace, '12345/test.jpg')
            allow(File).to receive(:file?).with(file_path).and_return(true)
            allow(File).to receive(:read).with(file_path).and_return(png)
          end

          subject { Notify.new_issue_email(issue_with_image.assignee_id, issue_with_image.id) }
          it 'replaces attached images with inline images' do
            is_expected.to have_body_text URI.encode(png_encoded)
          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'
R
Robb Kidd 已提交
238

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

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

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

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

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

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

266 267
          it_behaves_like 'an answer to an existing thread', 'issue'

268 269
          it 'is sent as the author' do
            sender = subject.header[:from].addrs[0]
270 271
            expect(sender.display_name).to eq(current_user.name)
            expect(sender.address).to eq(gitlab_sender)
272 273
          end

A
Alex Denisov 已提交
274
          it 'has the correct subject' do
275
            is_expected.to have_subject /#{issue.title} \(##{issue.iid}\)/i
A
Alex Denisov 已提交
276 277 278
          end

          it 'contains the new status' do
279
            is_expected.to have_body_text /#{status}/i
A
Alex Denisov 已提交
280 281 282
          end

          it 'contains the user name' do
283
            is_expected.to have_body_text /#{current_user.name}/i
A
Alex Denisov 已提交
284 285 286
          end

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

R
Robb Kidd 已提交
291 292 293
      end

      context 'for merge requests' do
294
        let(:merge_author) { create(:user) }
295
        let(:merge_request) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project) }
296
        let(:merge_request_with_description) { create(:merge_request, author: current_user, assignee: assignee, source_project: project, target_project: project, description: Faker::Lorem.sentence) }
297 298 299 300 301 302 303 304
        let(:merge_request_with_image) do
           create(:merge_request,
                  author: current_user,
                  assignee: assignee,
                  source_project: project,
                  target_project: project,
                  description: "![test](#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/12345/test.jpg)")
         end
R
Robb Kidd 已提交
305 306

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

          it_behaves_like 'an assignee email'
310
          it_behaves_like 'an email starting a new thread', 'merge_request'
R
Robb Kidd 已提交
311 312

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

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

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

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

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

333 334 335 336
        describe 'that are new with a description' do
          subject { Notify.new_merge_request_email(merge_request_with_description.assignee_id, merge_request_with_description.id) }

          it 'contains the description' do
337
            is_expected.to have_body_text /#{merge_request_with_description.description}/
338 339 340
          end
        end

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        describe 'that are new and contain contain images in the description' do
          let(:png) {File.read("#{Rails.root}/spec/fixtures/dk.png")}
          let(:png_encoded) { Base64::encode64(png) }

          before :each do
            file_path = File.join(Rails.root, 'public', 'uploads', merge_request_with_image.project.path_with_namespace, '/12345/test.jpg')
            allow(File).to receive(:file?).with(file_path).and_return(true)
            allow(File).to receive(:read).with(file_path).and_return(png)
          end

          subject { Notify.new_merge_request_email(merge_request_with_image.assignee_id, merge_request_with_image.id) }
          it 'replaces attached images with inline images' do
            is_expected.to have_body_text URI.encode(png_encoded)
          end
        end

R
Robb Kidd 已提交
357
        describe 'that are reassigned' do
358
          subject { Notify.reassigned_merge_request_email(recipient.id, merge_request.id, previous_assignee.id, current_user.id) }
R
Robb Kidd 已提交
359 360

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

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

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

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

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

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

386 387 388 389 390 391 392 393
        describe 'status changed' do
          let(:status) { 'reopened' }
          subject { Notify.merge_request_status_email(recipient.id, merge_request.id, status, current_user) }

          it_behaves_like 'an answer to an existing thread', 'merge_request'

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

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

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

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

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

415 416 417 418
        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'
419
          it_behaves_like 'an answer to an existing thread', 'merge_request'
420 421 422

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

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

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

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

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

447 448
      it_behaves_like 'an email sent from GitLab'

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 466 467 468 469 470
      let(:project_member) do
        create(:project_member,
               project: project,
               user: user)
      end

471
      subject { Notify.project_access_granted_email(project_member.id) }
472 473 474

      it_behaves_like 'an email sent from GitLab'

475
      it 'has the correct subject' do
476
        is_expected.to have_subject /Access to project was granted/
477 478
      end
      it 'contains name of project' do
479
        is_expected.to have_body_text /#{project.name}/
480 481
      end
      it 'contains new user role' do
482
        is_expected.to have_body_text /#{project_member.human_access}/
483 484 485
      end
    end

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

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

R
Robb Kidd 已提交
494
      shared_examples 'a note email' do
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
        end
      end

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
      describe 'on a commit that contains an image' do
        let(:commit) { project.repository.commit }
        let(:note_with_image) do
          create(:note,
                 project: project,
                 author: note_author,
                 note: "![test](#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/uploads/12345/test.jpg)")
        end

        let(:png) {File.read("#{Rails.root}/spec/fixtures/dk.png")}
        let(:png_encoded) { Base64::encode64(png) }

        before :each do
          file_path = File.join(Rails.root, 'public', 'uploads', note_with_image.project.path_with_namespace, '12345/test.jpg')
          allow(File).to receive(:file?).with(file_path).and_return(true)
          allow(File).to receive(:read).with(file_path).and_return(png)
          allow(Note).to receive(:find).with(note_with_image.id).and_return(note_with_image)
          allow(note_with_image).to receive(:noteable).and_return(commit)
        end

        subject { Notify.note_commit_email(recipient.id, note_with_image.id) }
        it 'replaces attached images with inline images' do
          is_expected.to have_body_text URI.encode(png_encoded)
        end
      end

R
Robb Kidd 已提交
536
      describe 'on a commit' do
537
        let(:commit) { project.repository.commit }
D
Dmitriy Zaporozhets 已提交
538

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

D
Dmitriy Zaporozhets 已提交
541
        subject { Notify.note_commit_email(recipient.id, note.id) }
R
Robb Kidd 已提交
542 543

        it_behaves_like 'a note email'
544
        it_behaves_like 'an answer to an existing thread', 'commits'
R
Robb Kidd 已提交
545 546

        it 'has the correct subject' do
547
          is_expected.to have_subject /#{commit.title} \(#{commit.short_id}\)/
R
Robb Kidd 已提交
548 549 550
        end

        it 'contains a link to the commit' do
551
          is_expected.to have_body_text commit.short_id
R
Robb Kidd 已提交
552 553 554 555
        end
      end

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

560
        subject { Notify.note_merge_request_email(recipient.id, note.id) }
R
Robb Kidd 已提交
561 562

        it_behaves_like 'a note email'
563
        it_behaves_like 'an answer to an existing thread', 'merge_request'
R
Robb Kidd 已提交
564 565

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

        it 'contains a link to the merge request note' do
570
          is_expected.to have_body_text /#{note_on_merge_request_path}/
R
Robb Kidd 已提交
571 572 573 574
        end
      end

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

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

        it_behaves_like 'a note email'
582
        it_behaves_like 'an answer to an existing thread', 'issue'
R
Robb Kidd 已提交
583 584

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

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

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

    subject { Notify.group_access_granted_email(membership.id) }

602 603
    it_behaves_like 'an email sent from GitLab'

604
    it 'has the correct subject' do
605
      is_expected.to have_subject /Access to group was granted/
606 607 608
    end

    it 'contains name of project' do
609
      is_expected.to have_body_text /#{group.name}/
610 611 612
    end

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

  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 }

628 629
    it_behaves_like 'an email sent from GitLab'

630
    it 'is sent to the new user' do
631
      is_expected.to deliver_to 'new-email@mail.com'
632 633 634
    end

    it 'has the correct subject' do
635
      is_expected.to have_subject "Confirmation instructions"
636 637 638
    end

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

643
  describe 'email on push with multiple commits' do
D
Dmitriy Zaporozhets 已提交
644 645
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
D
Dmitriy Zaporozhets 已提交
646
    let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_image_commit.id, sample_commit.id) }
647
    let(:commits) { Commit.decorate(compare.commits) }
648
    let(:diff_path) { namespace_project_compare_path(project.namespace, project, from: Commit.new(compare.base), to: Commit.new(compare.head)) }
649
    let(:send_from_committer_email) { false }
D
Dmitriy Zaporozhets 已提交
650

651
    subject { Notify.repository_push_email(project.id, 'devs@company.name', user.id, 'master', compare, false, send_from_committer_email) }
D
Dmitriy Zaporozhets 已提交
652

653 654
    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
655 656
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
657 658
    end

D
Dmitriy Zaporozhets 已提交
659
    it 'is sent to recipient' do
660
      is_expected.to deliver_to 'devs@company.name'
D
Dmitriy Zaporozhets 已提交
661 662 663
    end

    it 'has the correct subject' do
664
      is_expected.to have_subject /\[#{project.path_with_namespace}\]\[master\] #{commits.length} commits:/
D
Dmitriy Zaporozhets 已提交
665 666 667
    end

    it 'includes commits list' do
668
      is_expected.to have_body_text /Change some files/
D
Dmitriy Zaporozhets 已提交
669 670 671
    end

    it 'includes diffs' do
672
      is_expected.to have_body_text /def archive_formats_regex/
D
Dmitriy Zaporozhets 已提交
673
    end
674 675

    it 'contains a link to the diff' do
676
      is_expected.to have_body_text /#{diff_path}/
677
    end
678 679 680 681

    it 'doesn not contain the misleading footer' do
      is_expected.not_to have_body_text /you are a member of/
    end
682 683 684 685 686

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

      let(:send_from_committer_email) { true }

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

        before do
694
          user.update_attribute(:email, "user@company.com")
695 696 697 698 699 700 701 702 703
          user.confirm!
        end

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

704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
      context "when the committer email domain is not completely within the GitLab domain" do

        before do
          user.update_attribute(:email, "user@something.company.com")
          user.confirm!
        end

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

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

        before do
          user.update_attribute(:email, "user@mpany.com")
          user.confirm!
        end
723 724 725 726 727 728 729

        it "is sent from the default email" do
          sender = subject.header[:from].addrs[0]
          expect(sender.address).to eq(gitlab_sender)
        end
      end
    end
D
Dmitriy Zaporozhets 已提交
730
  end
731 732 733 734

  describe 'email on push with a single commit' do
    let(:example_site_path) { root_path }
    let(:user) { create(:user) }
D
Dmitriy Zaporozhets 已提交
735
    let(:compare) { Gitlab::Git::Compare.new(project.repository.raw_repository, sample_commit.parent_id, sample_commit.id) }
736
    let(:commits) { Commit.decorate(compare.commits) }
V
Vinnie Okada 已提交
737
    let(:diff_path) { namespace_project_commit_path(project.namespace, project, commits.first) }
738 739 740 741 742

    subject { Notify.repository_push_email(project.id, 'devs@company.name', user.id, 'master', compare) }

    it 'is sent as the author' do
      sender = subject.header[:from].addrs[0]
743 744
      expect(sender.display_name).to eq(user.name)
      expect(sender.address).to eq(gitlab_sender)
745 746 747
    end

    it 'is sent to recipient' do
748
      is_expected.to deliver_to 'devs@company.name'
749 750 751
    end

    it 'has the correct subject' do
752
      is_expected.to have_subject /#{commits.first.title}/
753 754 755
    end

    it 'includes commits list' do
756
      is_expected.to have_body_text /Change some files/
757 758 759
    end

    it 'includes diffs' do
760
      is_expected.to have_body_text /def archive_formats_regex/
761 762 763
    end

    it 'contains a link to the diff' do
764
      is_expected.to have_body_text /#{diff_path}/
765 766
    end
  end
767
end