-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathremove_file_controller_spec.rb
More file actions
221 lines (178 loc) · 7.32 KB
/
remove_file_controller_spec.rb
File metadata and controls
221 lines (178 loc) · 7.32 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
require "rails_helper"
RSpec.describe Forms::RemoveFileController, type: :request do
let(:form_data) do
build(:v2_form_document, :with_support, :live?,
id: 1,
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)
end
let(:file_upload_step) do
build :v2_question_page_step,
id: 1,
next_step_id: 2,
answer_type: "file"
end
let(:text_question_step) do
build :v2_question_page_step, :with_text_settings,
id: 2
end
let(:steps_data) { [file_upload_step, text_question_step] }
let(:req_headers) { { "Accept" => "application/json" } }
let(:api_url_suffix) { "/live" }
let(:mode) { "form" }
let(:changing_existing_answer) { false }
let(:uploaded_filename) { "test.jpg" }
let(:uploaded_file_key) { "test_key" }
let(:store) do
{
answers: {
form_data.id.to_s => {
file_upload_step.id.to_s => {
"original_filename" => uploaded_filename,
"uploaded_file_key" => uploaded_file_key,
},
},
},
}
end
before do
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/api/v2/forms/1#{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:)
context_spy
end
end
describe "#show" do
before do
get remove_file_confirmation_path(mode:, form_id: form_data.id, form_slug: form_data.form_slug, page_slug:, changing_existing_answer:)
end
context "when the question is a file upload question" do
let(:page_slug) { file_upload_step.id }
context "when a file has been uploaded" do
it "renders the remove file template" do
expect(response).to render_template("forms/remove_file/show")
end
it "displays the uploaded filename" do
expect(response.body).to include(uploaded_filename)
end
it "displays a back link to the review file page" do
expect(response.body).to include(review_file_path(form_data.id, form_data.form_slug, page_slug, changing_existing_answer:))
end
end
context "when a file has not been uploaded" do
let(:store) { {} }
it "redirects to the show page route" do
expect(response).to redirect_to form_page_path(form_data.id, form_data.form_slug, page_slug)
end
end
context "when changing an existing answer" do
let(:changing_existing_answer) { true }
it "includes the changing_existing_answer query parameter for the confirmation URL" do
rendered = Capybara.string(response.body)
expected_url = remove_file_confirmation_path(mode:, form_id: form_data.id, form_slug: form_data.form_slug, page_slug:, changing_existing_answer:)
expect(rendered).to have_css("form[action='#{expected_url}'][method='post']")
end
end
end
context "when the question isn't a file upload question" do
let(:page_slug) { text_question_step.id }
it "redirects to the show page route" do
expect(response).to redirect_to form_page_path(form_data.id, form_data.form_slug, page_slug)
end
end
end
describe "#delete" do
let(:mock_s3_client) { Aws::S3::Client.new(stub_responses: true) }
let(:remove) { "yes" }
before do
allow(Aws::S3::Client).to receive(:new).and_return(mock_s3_client)
allow(mock_s3_client).to receive(:delete_object)
delete remove_file_path(mode:, form_id: form_data.id, form_slug: form_data.form_slug, page_slug:, changing_existing_answer:, remove_input: { remove: })
end
context "when the question is a file upload question" do
let(:page_slug) { file_upload_step.id.to_s }
context "when the input object validation fails" do
let(:remove) { "invalid" }
it "renders the remove file page with 422 status" do
expect(response).to render_template("forms/remove_file/show")
expect(response).to have_http_status :unprocessable_content
end
it "displays an error" do
rendered = Capybara.string(response.body)
expect(rendered).to have_css(".govuk-error-summary")
end
it "displays a back link to the review file page" do
expect(response.body).to include(review_file_path(form_data.id, form_data.form_slug, page_slug, changing_existing_answer:))
end
end
context "when the user has confirmed they want to remove their file" do
context "when a file has been uploaded" do
it "deletes the file from S3" do
expect(mock_s3_client).to have_received(:delete_object)
end
it "removes the answer from the session" do
expect(store[:answers][form_data.id.to_s]).not_to have_key page_slug
end
it "redirects to the show page route" do
expect(response).to redirect_to form_page_path(form_data.id, form_data.form_slug, page_slug)
end
it "displays a success banner" do
expect(flash[:success]).to eq(I18n.t("banner.success.file_removed"))
end
context "when changing an existing answer" do
let(:changing_existing_answer) { true }
it "redirects to the change answer route" do
expect(response).to redirect_to form_change_answer_path(form_data.id, form_data.form_slug, page_slug)
end
end
end
context "when a file has not been uploaded" do
let(:uploaded_file_key) { nil }
it "does not remove the answer from the session" do
expect(store[:answers][form_data.id.to_s]).to have_key page_slug
end
it "redirects to the show page route" do
expect(response).to redirect_to form_page_path(form_data.id, form_data.form_slug, page_slug)
end
end
end
context "when the user has not confirmed they want to remove their file" do
let(:remove) { "no" }
it "does not delete the file from S3" do
expect(mock_s3_client).not_to have_received(:delete_object)
end
it "does not remove the answer from the session" do
expect(store[:answers][form_data.id.to_s]).to have_key page_slug
end
it "redirects to the review file page route" do
expect(response).to redirect_to review_file_path(form_data.id, form_data.form_slug, page_slug, changing_existing_answer:)
end
end
end
context "when the question isn't a file upload question" do
let(:page_slug) { text_question_step.id.to_s }
let(:store) do
{
answers: {
form_data.id.to_s => {
text_question_step.id.to_s => {
"text" => "foo",
},
},
},
}
end
it "does not remove the answer from the session" do
expect(store[:answers][form_data.id.to_s]).to have_key page_slug
end
it "redirects to the show page route" do
expect(response).to redirect_to form_page_path(form_data.id, form_data.form_slug, page_slug)
end
end
end
end