-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcheck_your_answers_controller_spec.rb
More file actions
713 lines (597 loc) · 21.5 KB
/
check_your_answers_controller_spec.rb
File metadata and controls
713 lines (597 loc) · 21.5 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
require "rails_helper"
RSpec.describe Forms::CheckYourAnswersController, type: :request do
include Capybara::RSpecMatchers
let(:timestamp_of_request) { Time.utc(2022, 12, 14, 10, 0o0, 0o0) }
let(:form_data) do
build(:v2_form_document, :with_support,
id: 2,
start_page: 1,
privacy_policy_url: "http://www.example.gov.uk/privacy_policy",
what_happens_next_markdown: "Good things come to those that wait",
declaration_text: "agree to the declaration",
steps: steps_data,
support_phone: "0203 222 2222",
support_email: "help@example.gov.uk",
support_url: "https://example.gov.uk/help",
support_url_text: "Get help",
submission_email:)
end
let(:email_confirmation_input) do
{ send_confirmation: "send_email",
confirmation_email_address: Faker::Internet.email,
confirmation_email_reference: }
end
let(:submission_email) { Faker::Internet.email(domain: "example.gov.uk") }
let(:store) do
{
answers: {
"2" => {
"1" => {
"date_year" => "2000",
"date_month" => "1",
"date_day" => "1",
},
"2" => {
"date_year" => "2023",
"date_month" => "6",
"date_day" => "9",
},
},
},
}
end
let(:steps_data) do
[
{
id: 1,
position: 1,
next_step_id: 2,
type: "question_page",
data: {
answer_type: "date",
is_optional: nil,
question_text: "Question one",
},
},
{
id: 2,
position: 2,
type: "question_page",
data: {
answer_type: "date",
is_optional: nil,
question_text: "Question two",
},
},
]
end
let(:req_headers) { { "Accept" => "application/json" } }
let(:api_url_suffix) { "/live" }
let(:mode) { "form" }
let(:frozen_time) { Time.zone.local(2023, 3, 13, 9, 47, 57) }
let(:repeat_form_submission) { false }
let(:reference) { Faker::Alphanumeric.alphanumeric(number: 8).upcase }
let(:confirmation_email_id) { "2222" }
let(:confirmation_email_reference) { "confirmation-email-ref" }
let(:output) { StringIO.new }
let(:logger) { ActiveSupport::Logger.new(output) }
before do
# Intercept the request logs so we can do assertions on them
allow(Lograge).to receive(:logger).and_return(logger)
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v2/forms/2#{api_url_suffix}", req_headers, form_data.to_json, 200
end
allow(Flow::Context).to receive(:new).and_wrap_original do |original_method, *args|
context_spy = original_method.call(form: args[0][:form], store:)
allow(context_spy).to receive(:form_submitted?).and_return(repeat_form_submission)
context_spy
end
allow(ReferenceNumberService).to receive(:generate).and_return(reference)
end
describe "#show" do
shared_examples "for notification references" do
prepend_before do
allow(EmailConfirmationInput).to receive(:new).and_wrap_original do |original_method, *args|
double = original_method.call(*args)
allow(double).to receive_messages(confirmation_email_reference:)
double
end
end
it "includes a notification reference for the confirmation email" do
expect(response.body).to include confirmation_email_reference
end
end
shared_examples "for redirecting if the form is incomplete" do
context "without any questions answered" do
let(:store) do
{
answers: {},
}
end
it "redirects to first incomplete page of form" do
get check_your_answers_path(mode:, form_id: 2, form_slug: form_data.form_slug)
expect(response).to have_http_status(:found)
expect(response.location).to eq(form_page_url(mode:, form_id: 2, form_slug: form_data.form_slug, page_slug: 1))
end
end
end
shared_examples "check your answers page" do
it "returns 'ok' status code" do
expect(response).to have_http_status(:ok)
end
it "Displays a back link to the last page of the form" do
expect(response.body).to include(form_page_path(mode:, form_id: 2, form_slug: form_data.form_slug, page_slug: 2))
end
it "Returns the correct X-Robots-Tag header" do
expect(response.headers["X-Robots-Tag"]).to eq("noindex, nofollow")
end
it "Contains a change link for each page" do
expect(response.body).to include(form_change_answer_path(2, form_data.form_slug, 1))
expect(response.body).to include(form_change_answer_path(2, form_data.form_slug, 2))
end
context "when the form has a question that can be answered more than once" do
let(:steps_data) do
[
{
id: 1,
position: 1,
next_step_id: 2,
type: "question_page",
data: {
answer_type: "date",
is_optional: nil,
question_text: "Question one",
},
},
{
id: 2,
position: 2,
next_step_id: 3,
type: "question_page",
data: {
answer_type: "date",
is_optional: nil,
question_text: "Question two",
},
},
{
id: 3,
position: 3,
type: "question_page",
data: {
answer_type: "date",
is_optional: nil,
is_repeatable: true,
question_text: "Question three",
},
},
]
end
let(:store) do
{
answers: {
"2" => {
"1" => {
"date_year" => "2000",
"date_month" => "1",
"date_day" => "1",
},
"2" => {
"date_year" => "2023",
"date_month" => "6",
"date_day" => "9",
},
"3" => [
{
"date_year" => "2024",
"date_month" => "9",
"date_day" => "6",
},
],
},
},
}
end
it "contains a change link to the add another answer page" do
expect(response.body)
.to include(change_add_another_answer_path(2, form_data.form_slug, 3))
end
context "and that question is optional and has been skipped" do
let(:steps_data) do
[
{
id: 1,
position: 1,
next_step_id: 2,
type: "question_page",
data: {
answer_type: "date",
is_optional: nil,
question_text: "Question one",
},
},
{
id: 2,
position: 2,
next_step_id: 3,
type: "question_page",
data: {
answer_type: "date",
is_optional: nil,
question_text: "Question two",
},
},
{
id: 3,
position: 3,
type: "question_page",
data: {
answer_type: "date",
is_optional: true,
is_repeatable: true,
question_text: "Question three",
},
},
]
end
let(:store) do
{
answers: {
"2" => {
"1" => {
"date_year" => "2000",
"date_month" => "1",
"date_day" => "1",
},
"2" => {
"date_year" => "2023",
"date_month" => "6",
"date_day" => "9",
},
"3" => [
{
"date_year" => "",
"date_month" => "",
"date_day" => "",
},
],
},
},
}
end
it "shows the question as not completed" do
expect(response.body)
.to include "Not completed"
end
it "contains a change link to the question page" do
expect(response.body)
.to include(form_change_answer_path(2, form_data.form_slug, 3))
end
end
end
context "when the form has a file question with a heading" do
let(:page_heading) { Faker::Lorem.sentence }
let(:store) do
{
answers: {
"2" => {
"1" => {
"original_filename" => "file.txt",
"uploaded_file_key" => "some_file_key",
},
},
},
}
end
let(:steps_data) do
[
{
id: 1,
position: 1,
type: "question_page",
data: {
answer_type: "file",
is_optional: nil,
page_heading:,
question_text: "Question one",
},
},
]
end
it "returns 'ok' status code" do
expect(response).to have_http_status(:ok)
end
it "Displays a back link to the last page of the form" do
expect(response.body).to include(form_page_path(mode:, form_id: 2, form_slug: form_data.form_slug, page_slug: 1))
end
it "Returns the correct X-Robots-Tag header" do
expect(response.headers["X-Robots-Tag"]).to eq("noindex, nofollow")
end
it "Contains a change link for each page" do
expect(response.body).to include(form_change_answer_path(2, form_data.form_slug, 1))
end
it "Contains the guidance page heading in a caption" do
expect(response.body).to have_css("span.govuk-caption-m", text: page_heading)
end
end
end
context "with preview mode on" do
let(:api_url_suffix) { "/draft" }
let(:mode) { "preview-draft" }
include_examples "for redirecting if the form is incomplete"
context "with all questions answered and valid" do
before do
allow(EventLogger).to receive(:log).at_least(:once)
get check_your_answers_path(mode:, form_id: 2, form_slug: form_data.form_slug)
end
it_behaves_like "check your answers page"
it "does not log the form_check_answers event" do
expect(EventLogger).not_to have_received(:log)
end
include_examples "for notification references"
end
end
context "with preview mode off" do
let(:api_url_suffix) { "/live" }
let(:mode) { "form" }
include_examples "for redirecting if the form is incomplete"
context "with all questions answered and valid" do
before do
allow(EventLogger).to receive(:log_form_event).at_least(:once)
get check_your_answers_path(mode:, form_id: 2, form_slug: form_data.form_slug)
end
it_behaves_like "check your answers page"
it "Logs the form_check_answers event" do
expect(EventLogger).to have_received(:log_form_event).with("check_answers")
end
include_examples "for notification references"
end
end
end
describe "#submit_answers" do
before do
allow_mailer_to_return_mail_with_govuk_notify_response_with(
FormSubmissionConfirmationMailer,
:send_confirmation_email,
id: confirmation_email_id,
)
end
shared_examples "for notification references" do
it "includes the confirmation_email_reference in the logging_context" do
expect(log_lines[0]["confirmation_email_reference"]).to eq(confirmation_email_reference)
end
end
context "with preview mode on" do
let(:mode) { "preview-live" }
before do
travel_to frozen_time do
perform_enqueued_jobs do
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
end
end
it "redirects to confirmation page" do
expect(response).to redirect_to(form_submitted_path)
end
it "emails the form submission" do
deliveries = ActionMailer::Base.deliveries
expect(deliveries.length).to eq 2
mail = deliveries[0]
expect(mail.to).to eq [submission_email]
expect(mail.subject).to match("TEST FORM SUBMISSION: #{form_data.name} - reference: #{reference}")
end
it "includes the confirmation_email_id in the logging context" do
expect(log_lines[0]["confirmation_email_id"]).to eq(confirmation_email_id)
end
include_examples "for notification references"
end
context "with preview mode off" do
let(:mode) { "form" }
before do
travel_to frozen_time do
perform_enqueued_jobs do
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
end
end
it "redirects to confirmation page" do
expect(response).to redirect_to(form_submitted_path)
end
it "emails the form submission" do
deliveries = ActionMailer::Base.deliveries
expect(deliveries.length).to eq 2
mail = deliveries[0]
expect(mail.to).to eq [submission_email]
expect(mail.subject).to match("Form submission: #{form_data.name} - reference: #{reference}")
end
it "includes the confirmation_email_id in the logging context" do
expect(log_lines[0]["confirmation_email_id"]).to eq(confirmation_email_id)
end
include_examples "for notification references"
end
context "when answers have already been submitted" do
let(:repeat_form_submission) { true }
before do
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
it "redirects to repeat submission error page" do
expect(response).to redirect_to(error_repeat_submission_path(2))
end
end
context "when the form is incomplete" do
let(:store) do
{
answers: {
"2" => {
"1" => {
"date_year" => "2000",
"date_month" => "1",
"date_day" => "1",
},
},
},
}
end
before do
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
it "renders the incomplete submission error page" do
expect(response).to render_template "errors/incomplete_submission"
end
end
context "when user has not specified whether they want a confirmation email" do
let(:email_confirmation_input) do
{
send_confirmation: nil,
confirmation_email_reference:,
}
end
before do
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
it "return 422 error code" do
expect(response).to have_http_status(:unprocessable_content)
end
it "renders the check your answers page" do
expect(response).to render_template("forms/check_your_answers/show")
end
it "does not generate a new submission reference" do
expect(response.body).to include confirmation_email_reference
end
end
context "when user has not specified the confirmation email address" do
let(:email_confirmation_input) do
{
send_confirmation: "send_email",
confirmation_email_address: nil,
confirmation_email_reference:,
}
end
before do
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
it "return 422 error code" do
expect(response).to have_http_status(:unprocessable_content)
end
it "renders the check your answers page" do
expect(response).to render_template("forms/check_your_answers/show")
end
it "does not generate a new submission reference" do
expect(response.body).to include confirmation_email_reference
end
include_examples "for notification references"
end
context "when user has not requested a confirmation email" do
let(:email_confirmation_input) do
{
send_confirmation: "skip_confirmation",
confirmation_email_address: nil,
confirmation_email_reference:,
}
end
before do
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
it "redirects to confirmation page" do
expect(response).to redirect_to(form_submitted_path)
end
it "does not include the confirmation_email_id in the logging context" do
expect(log_lines[0].keys).not_to include("confirmation_email_id")
end
it "does not include confirmation_email_reference in logging context" do
expect(log_lines[0].keys).not_to include("confirmation_email_reference")
end
end
context "when user has requested a confirmation email" do
let(:email_confirmation_input) do
{ send_confirmation: "send_email",
confirmation_email_address: Faker::Internet.email,
confirmation_email_reference: }
end
before do
travel_to timestamp_of_request do
perform_enqueued_jobs do
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
end
end
it "redirects to confirmation page" do
expect(response).to redirect_to(form_submitted_path)
end
it "sends a confirmation email" do
deliveries = ActionMailer::Base.deliveries
expect(deliveries.length).to eq 2
mail = deliveries[1]
expect(mail.to).to eq([email_confirmation_input[:confirmation_email_address]])
expected_personalisation = {
title: form_data.name,
what_happens_next_text: form_data.what_happens_next_markdown,
support_contact_details: contact_support_details_format,
submission_time: "10:00am",
submission_date: "14 December 2022",
test: "no",
submission_reference: reference,
include_payment_link: "no",
payment_link: "",
}
expect(mail.body.raw_source).to include(expected_personalisation.to_s)
expect(mail.govuk_notify_reference).to eq confirmation_email_reference
end
it "includes the confirmation_email_id in the logging context" do
expect(log_lines[0]["confirmation_email_id"]).to eq(confirmation_email_id)
end
include_examples "for notification references"
end
context "when there is a submission error" do
let(:email_confirmation_input) do
{ send_confirmation: "send_email",
confirmation_email_address: Faker::Internet.email,
confirmation_email_reference: }
end
before do
allow(FormSubmissionService).to receive(:call).and_raise(StandardError)
allow(Sentry).to receive(:capture_exception)
travel_to timestamp_of_request do
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
end
it "calls Sentry" do
expect(Sentry).to have_received(:capture_exception)
end
it "renders the submission_error template" do
expect(response).to render_template("errors/submission_error")
end
it "returns 500" do
expect(response).to have_http_status(:internal_server_error)
end
include_examples "for notification references"
end
context "when there is an ActionMailer error with the confirmation email address" do
before do
mock_form_submission_service = instance_double(FormSubmissionService)
allow(FormSubmissionService).to receive(:new).and_return(mock_form_submission_service)
allow(mock_form_submission_service).to receive(:submit).and_raise(FormSubmissionService::ConfirmationEmailToAddressError)
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
end
it "return 422 error code" do
expect(response).to have_http_status(:unprocessable_content)
end
it "renders the check your answers page" do
expect(response).to render_template("forms/check_your_answers/show")
end
it "has a validation error for the confirmation email address" do
expect(response.body).to include(I18n.t("activemodel.errors.models.email_confirmation_input.attributes.confirmation_email_address.invalid_email"))
end
end
end
private
def contact_support_details_format
phone_number = "#{form_data.support_phone}\n\n[#{I18n.t('support_details.call_charges')}](https://www.gov.uk/call-charges)"
email = "[#{form_data.support_email}](mailto:#{form_data.support_email})"
online = "[#{form_data.support_url_text}](#{form_data.support_url})"
[phone_number, email, online].compact_blank.join("\n\n")
end
def log_lines
output.string.split("\n").map { |line| JSON.parse(line) }
end
end