-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfill_in_file_upload_question_spec.rb
More file actions
163 lines (130 loc) · 5.13 KB
/
fill_in_file_upload_question_spec.rb
File metadata and controls
163 lines (130 loc) · 5.13 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
require "rails_helper"
feature "Fill in and submit a form with a file upload question", type: :feature do
include ActiveJob::TestHelper
let(:steps) { [(build :v2_question_page_step, answer_type: "file", id: 1, routing_conditions: [], question_text:)] }
let(:form) { build :v2_form_document, :live?, id: 1, name: "Fill in this form", steps:, start_page: 1 }
let(:question_text) { Faker::Lorem.question }
let(:answer_text) { "Answer 1" }
let(:reference) { Faker::Alphanumeric.alphanumeric(number: 8).upcase }
let(:req_headers) { { "Accept" => "application/json" } }
let(:post_headers) { { "Content-Type" => "application/json" } }
let(:test_file) { "tmp/a-file.txt" }
let(:test_file_content) { "some content" }
let(:mock_s3_client) do
Aws::S3::Client.new(stub_responses: {
get_object: { body: test_file_content },
})
end
after do
File.delete(test_file) if File.exist?(test_file)
end
before do
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v2/forms/1/live", req_headers, form.to_json, 200
end
allow(ReferenceNumberService).to receive(:generate).and_return(reference)
allow(Aws::S3::Client).to receive(:new).and_return(mock_s3_client)
allow(mock_s3_client).to receive(:put_object)
allow(mock_s3_client).to receive(:get_object_tagging).and_return({ tag_set: [{ key: "GuardDutyMalwareScanStatus", value: scan_status }] })
File.write(test_file, test_file_content)
end
context "when the file is successfully uploaded" do
let(:scan_status) { "NO_THREATS_FOUND" }
scenario "As a form filler" do
when_i_visit_the_form_start_page
then_i_should_see_the_first_question
then_i_see_the_file_upload_component
when_i_upload_a_file
and_i_click_on_continue
then_i_see_the_review_file_page
and_i_click_on_continue
then_i_should_see_the_check_your_answers_page
when_i_opt_out_of_email_confirmation
and_i_submit_my_form
then_my_form_should_be_submitted
and_i_should_receive_a_reference_number
and_an_email_submission_should_have_been_sent
end
end
context "when the file fails the virus scan" do
let(:scan_status) { "THREATS_FOUND" }
scenario "As a form filler" do
when_i_visit_the_form_start_page
then_i_should_see_the_first_question
then_i_see_the_file_upload_component
when_i_upload_a_file
and_i_click_on_continue
then_i_see_an_error_message_that_the_file_contains_a_virus
end
end
context "when the file unexpectedly contains binary" do
# A PNG file which has been renamed to look like a TXT
let(:test_file_content) { File.read("spec/fixtures/disguised_binary_file.txt") }
let(:scan_status) { "NO_THREATS_FOUND" }
scenario "As a form filler" do
when_i_visit_the_form_start_page
then_i_should_see_the_first_question
then_i_see_the_file_upload_component
when_i_upload_a_file
and_i_click_on_continue
then_i_see_the_review_file_page
and_i_click_on_continue
then_i_should_see_the_check_your_answers_page
when_i_opt_out_of_email_confirmation
and_i_submit_my_form
then_my_form_should_be_submitted
and_i_should_receive_a_reference_number
and_an_email_submission_should_have_been_sent
end
end
def when_i_visit_the_form_start_page
visit form_path(mode: "form", form_id: 1, form_slug: "fill-in-this-form")
expect_page_to_have_no_axe_errors(page)
end
def then_i_should_see_the_first_question
expect(page.find("h1")).to have_text question_text
end
def then_i_see_the_file_upload_component
expect(page).to have_css("input[type=file]")
end
def when_i_upload_a_file
attach_file question_text, test_file
end
def and_i_click_on_continue
click_button "Continue"
end
def then_i_see_the_review_file_page
expect(page.find("h1")).to have_text question_text
expect(page).to have_text "Check your uploaded file"
expect(page).to have_text File.basename(File.path(test_file))
end
def then_i_should_see_the_check_your_answers_page
expect(page.find("h1")).to have_text "Check your answers before submitting your form"
expect(page).to have_text question_text
expect(page).to have_text File.basename(test_file)
expect_page_to_have_no_axe_errors(page)
end
def when_i_opt_out_of_email_confirmation
choose "No"
end
def and_i_submit_my_form
click_on "Submit"
end
def then_my_form_should_be_submitted
expect(page.find("h1")).to have_text "Your form has been submitted"
expect_page_to_have_no_axe_errors(page)
end
def and_i_should_receive_a_reference_number
expect(page).to have_text reference
end
def and_an_email_submission_should_have_been_sent
perform_enqueued_jobs
expect(ActionMailer::Base.deliveries.first).to be_present
delivered_email = ActionMailer::Base.deliveries.first
expect(delivered_email.html_part.body.raw_source).to match(/a-file_#{reference}.txt/)
end
def then_i_see_an_error_message_that_the_file_contains_a_virus
expect(page.find(".govuk-error-summary")).to have_text "The selected file contains a virus"
expect(page).to have_css("input[type=file]")
end
end