summaryrefslogtreecommitdiff
path: root/spec/mailers/shared/notify.rb
blob: eafc5e5044b62c048619e7073288da9279572a72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
shared_context 'gitlab email notification' do
  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 }
  let(:recipient) { create(:user, email: 'recipient@example.com') }
  let(:project) { create(:project) }
  let(:new_user_address) { 'newguy@example.com' }

  before do
    ActionMailer::Base.deliveries.clear
    email = recipient.emails.create(email: "notifications@example.com")
    recipient.update_attribute(:notification_email, email.email)
  end
end

shared_examples 'a multiple recipients email' do
  it 'is sent to the given recipient' do
    is_expected.to deliver_to recipient.notification_email
  end
end

shared_examples 'an email sent from GitLab' do
  it 'is sent from GitLab' do
    sender = subject.header[:from].addrs[0]
    expect(sender.display_name).to eq(gitlab_sender_display_name)
    expect(sender.address).to eq(gitlab_sender)
  end

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

shared_examples 'an email that contains a header with author username' do
  it 'has X-GitLab-Author header containing author\'s username' do
    is_expected.to have_header 'X-GitLab-Author', user.username
  end
end

shared_examples 'an email with X-GitLab headers containing project details' do
  it 'has X-GitLab-Project* headers' do
    is_expected.to have_header 'X-GitLab-Project', /#{project.name}/
    is_expected.to have_header 'X-GitLab-Project-Id', /#{project.id}/
    is_expected.to have_header 'X-GitLab-Project-Path', /#{project.path_with_namespace}/
  end
end

shared_examples 'an email starting a new thread' do |message_id_prefix|
  include_examples 'an email with X-GitLab headers containing project details'

  it 'has a discussion identifier' do
    is_expected.to have_header 'Message-ID',  /<reply\+(.*)@#{Gitlab.config.gitlab.host}>/
    is_expected.to have_header 'References',  /<#{message_id_prefix}(.*)@#{Gitlab.config.gitlab.host}>/
  end
end

shared_examples 'an answer to an existing thread' do |thread_id_prefix|
  include_examples 'an email with X-GitLab headers containing project details'

  it 'has a subject that begins with Re: ' do
    is_expected.to have_subject /^Re: /
  end

  it 'has headers that reference an existing thread' do
    is_expected.to have_header 'Message-ID',  /<(.*)@#{Gitlab.config.gitlab.host}>/
    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}>/
  end
end

shared_examples 'a new user email' do
  it 'is sent to the new user' do
    is_expected.to deliver_to new_user_address
  end

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

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

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

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

shared_examples 'an unsubscribeable thread' do
  it { is_expected.to have_body_text /unsubscribe/ }
end

shared_examples 'a user cannot unsubscribe through footer link' do
  it { is_expected.not_to have_body_text /unsubscribe/ }
end

shared_examples 'an email with a labels subscriptions link in its footer' do
  it { is_expected.to have_body_text /label subscriptions/ }
end