-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathform_submission_confirmation_mailer_spec.rb
More file actions
294 lines (237 loc) · 12.3 KB
/
form_submission_confirmation_mailer_spec.rb
File metadata and controls
294 lines (237 loc) · 12.3 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
require "rails_helper"
describe FormSubmissionConfirmationMailer, type: :mailer do
let(:submission_locale) { :en }
let(:mail) do
described_class.send_confirmation_email(what_happens_next_markdown:,
support_contact_details:,
notify_response_id: "for-my-ref",
confirmation_email_address:,
mailer_options:,
submission_locale:)
end
let(:mailer_options) do
FormSubmissionService::MailerOptions.new(title:,
is_preview:,
timestamp: submission_timestamp,
submission_reference:,
payment_url:)
end
let(:title) { "Form 1" }
let(:what_happens_next_markdown) { "Please wait for a response" }
let(:support_contact_details) { OpenStruct.new(phone: "0203 222 2222", email: nil, support_url: nil, support_url_text: nil, call_charges_url: "https://www.gov.uk/call-charges") }
let(:is_preview) { false }
let(:confirmation_email_address) { "testing@gov.uk" }
let(:submission_timestamp) { Time.zone.now }
let(:submission_reference) { Faker::Alphanumeric.alphanumeric(number: 8).upcase }
let(:payment_url) { nil }
context "when form filler wants an form submission confirmation email" do
before do
Settings.govuk_notify.form_filler_confirmation_email_template_id = "123456"
Settings.govuk_notify.form_filler_confirmation_email_welsh_template_id = "7891011"
end
context "when submission_locale is :en" do
let(:submission_locale) { :en }
it "uses the English language template" do
expect(mail.govuk_notify_template).to eq("123456")
end
end
context "when submission_locale is :cy" do
let(:submission_locale) { :cy }
it "uses the bilingual template" do
expect(mail.govuk_notify_template).to eq("7891011")
end
end
it "sends an email to the form filler's email address" do
expect(mail.to).to eq(["testing@gov.uk"])
end
it "includes the form title" do
expect(mail.govuk_notify_personalisation[:title]).to eq("Form 1")
end
it "includes the forms what happens next" do
expect(mail.govuk_notify_personalisation[:what_happens_next_text]).to eq("Please wait for a response")
end
context "when what_happens_next_markdown_cy is provided" do
let(:mail) do
described_class.send_confirmation_email(what_happens_next_markdown:,
support_contact_details:,
notify_response_id: "for-my-ref",
confirmation_email_address:,
mailer_options:,
submission_locale:,
what_happens_next_markdown_cy: "Arhoswch am ymateb")
end
it "includes the Welsh what happens next" do
expect(mail.govuk_notify_personalisation[:what_happens_next_text_cy]).to eq("Arhoswch am ymateb")
end
end
context "when what_happens_next_markdown_cy is not provided" do
it "falls back to the English what happens next" do
expect(mail.govuk_notify_personalisation[:what_happens_next_text_cy]).to eq("Please wait for a response")
end
end
it "includes the forms support contact details" do
expect(mail.govuk_notify_personalisation[:support_contact_details]).to eq("0203 222 2222\n\n[Find out about call charges](https://www.gov.uk/call-charges)")
end
context "when support_contact_details_cy is not provided" do
it "falls back to support_contact_details formatted with Welsh locale" do
expect(mail.govuk_notify_personalisation[:support_contact_details_cy]).to eq("0203 222 2222\n\n[Darganfyddwch am gostau galwadau](https://www.gov.uk/call-charges)")
end
end
context "when support_contact_details_cy is provided" do
let(:welsh_support) { OpenStruct.new(phone: "0291 111 1111", email: nil, url: nil, url_text: nil, call_charges_url: "https://www.gov.uk/call-charges") }
let(:mail) do
described_class.send_confirmation_email(what_happens_next_markdown:,
support_contact_details:,
support_contact_details_cy: welsh_support,
notify_response_id: "for-my-ref",
confirmation_email_address:,
mailer_options:,
submission_locale:)
end
it "uses the Welsh support details formatted with Welsh locale" do
expect(mail.govuk_notify_personalisation[:support_contact_details_cy]).to eq("0291 111 1111\n\n[Darganfyddwch am gostau galwadau](https://www.gov.uk/call-charges)")
end
end
context "when what happens next is missing" do
let(:what_happens_next_markdown) { nil }
it "uses placeholder text" do
expect(mail.govuk_notify_personalisation[:what_happens_next_text]).to eq(I18n.t("mailer.submission_confirmation.default_what_happens_next"))
end
end
context "when what happens next is blank" do
let(:what_happens_next_markdown) { "" }
it "uses placeholder text" do
expect(mail.govuk_notify_personalisation[:what_happens_next_text]).to eq(I18n.t("mailer.submission_confirmation.default_what_happens_next"))
end
end
context "when support contact details are missing" do
let(:support_contact_details) { OpenStruct.new(phone: nil, email: nil, url: nil, url_text: nil, call_charges_url: "https://www.gov.uk/call-charges") }
it "uses placeholder text" do
expect(mail.govuk_notify_personalisation[:support_contact_details]).to eq(I18n.t("mailer.submission_confirmation.default_support_contact_details"))
end
end
context "when support contact details are blank" do
let(:support_contact_details) { OpenStruct.new(phone: "", email: "", url: "", url_text: "", call_charges_url: "https://www.gov.uk/call-charges") }
it "uses placeholder text" do
expect(mail.govuk_notify_personalisation[:support_contact_details]).to eq(I18n.t("mailer.submission_confirmation.default_support_contact_details"))
end
end
it "includes an email reference (mostly used to retrieve specific email in notify for e2e tests)" do
expect(mail.govuk_notify_reference).to eq("for-my-ref")
end
it "does include an email-reply-to" do
Settings.govuk_notify.form_submission_email_reply_to_id = "send-this-to-me@gov.uk"
expect(mail.govuk_notify_email_reply_to).to eq("send-this-to-me@gov.uk")
end
context "when a payment url is in" do
let(:payment_url) { "https://www.gov.uk/payments/test-service/pay-for-licence?reference=#{submission_reference}" }
it "sets the boolean for the payment content to 'yes'" do
expect(mail.govuk_notify_personalisation[:include_payment_link]).to eq("yes")
end
it "sets the payment_link" do
expect(mail.govuk_notify_personalisation[:payment_link]).to eq(payment_url)
end
end
context "when a payment link is not set" do
let(:payment_url) { nil }
it "sets the boolean for the payment content to 'no'" do
expect(mail.govuk_notify_personalisation[:include_payment_link]).to eq("no")
end
it "sets the payment link personalisation to an empty string" do
expect(mail.govuk_notify_personalisation[:payment_link]).to eq("")
end
end
describe "submission date/time" do
context "with a time in BST" do
let(:timestamp) { Time.utc(2022, 9, 14, 8, 0o0, 0o0) }
it "includes the time user submitted the form" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_time]).to eq("9:00am")
end
end
it "includes the date user submitted the form in English" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 September 2022")
end
end
it "includes the date user submitted the form in Welsh" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_date_cy]).to eq("14 Medi 2022")
end
end
end
context "with a time in GMT" do
let(:timestamp) { Time.utc(2022, 12, 14, 13, 0o0, 0o0) }
it "includes the time user submitted the form" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_time]).to eq("1:00pm")
end
end
it "includes the date user submitted the form" do
travel_to timestamp do
expect(mail.govuk_notify_personalisation[:submission_date]).to eq("14 December 2022")
end
end
end
end
context "when the submission is from preview mode" do
let(:is_preview) { true }
it "uses the preview personalisation" do
expect(mail.govuk_notify_personalisation[:test]).to eq("yes")
end
end
end
describe "#format_support_details" do
let(:mailer) { described_class.new }
context "with phone number only" do
let(:support_contact_details) { OpenStruct.new(phone: "0203 222 2222", email: nil, url: nil, url_text: nil, call_charges_url: "https://www.gov.uk/call-charges") }
it "formats phone number with call charges link" do
result = mailer.format_support_details(support_contact_details)
expect(result).to eq("0203 222 2222\n\n[Find out about call charges](https://www.gov.uk/call-charges)")
end
end
context "with email only" do
let(:support_contact_details) { OpenStruct.new(phone: nil, email: "help@example.gov.uk", url: nil, url_text: nil, call_charges_url: "https://www.gov.uk/call-charges") }
it "formats email as a mailto link" do
result = mailer.format_support_details(support_contact_details)
expect(result).to eq("[help@example.gov.uk](mailto:help@example.gov.uk)")
end
end
context "with support URL only" do
let(:support_contact_details) { OpenStruct.new(phone: nil, email: nil, url: "https://example.gov.uk/help", url_text: "Get help", call_charges_url: "https://www.gov.uk/call-charges") }
it "formats support URL as a link" do
result = mailer.format_support_details(support_contact_details)
expect(result).to eq("[Get help](https://example.gov.uk/help)")
end
end
context "with all support details" do
let(:support_contact_details) { OpenStruct.new(phone: "0203 222 2222", email: "help@example.gov.uk", url: "https://example.gov.uk/help", url_text: "Get help", call_charges_url: "https://www.gov.uk/call-charges") }
it "formats all details with proper separation" do
result = mailer.format_support_details(support_contact_details)
expected = "0203 222 2222\n\n[Find out about call charges](https://www.gov.uk/call-charges)\n\n[help@example.gov.uk](mailto:help@example.gov.uk)\n\n[Get help](https://example.gov.uk/help)"
expect(result).to eq(expected)
end
end
context "with no support details" do
let(:support_contact_details) { OpenStruct.new(phone: nil, email: nil, url: nil, url_text: nil, call_charges_url: "https://www.gov.uk/call-charges") }
it "returns empty string" do
result = mailer.format_support_details(support_contact_details)
expect(result).to eq("")
end
end
context "with phone number that has extra whitespace" do
let(:support_contact_details) { OpenStruct.new(phone: " 0203 222 2222\n\n ", email: nil, url: nil, url_text: nil, call_charges_url: "https://www.gov.uk/call-charges") }
it "normalizes whitespace" do
result = mailer.format_support_details(support_contact_details)
expect(result).to eq("0203 222 2222\n\n[Find out about call charges](https://www.gov.uk/call-charges)")
end
end
end
private
def submission_timezone
Rails.configuration.x.submission.time_zone || "UTC"
end
def submission_timestamp
Time.use_zone(submission_timezone) { Time.zone.now }
end
end