-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathconsultations_controller_test.rb
More file actions
407 lines (344 loc) · 17.3 KB
/
Copy pathconsultations_controller_test.rb
File metadata and controls
407 lines (344 loc) · 17.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
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
require "test_helper"
class Admin::ConsultationsControllerTest < ActionController::TestCase
include TaxonomyHelper
setup do
login_as :writer
ConsultationResponseForm.any_instance.stubs(:consultation_participation).returns(stub(consultation: stub(auth_bypass_id: "auth bypass id", id: 1)))
end
should_be_an_admin_controller
should_allow_creating_of :consultation
should_allow_editing_of :consultation
should_allow_association_with_topical_event_documents :consultation
should_allow_lead_and_supporting_organisations_for :consultation
should_allow_alternative_format_provider_for :consultation
should_allow_scheduled_publication_of :consultation
access_limiting_organisations_should_allow_access_limiting_of :consultation
access_limiting_individuals_should_allow_access_limiting_of :consultation
should_render_govspeak_history_and_fact_checking_tabs_for :consultation
view_test "new displays consultation fields" do
get :new
assert_select "form#new_edition" do
assert_select "textarea[name='edition[summary]']"
assert_select "input[name*='edition[opening_at']", count: 3
assert_select "select[name*='edition[opening_at']", count: 2
assert_select "input[name*='edition[closing_at']", count: 3
assert_select "select[name*='edition[closing_at']", count: 2
assert_select "input[type='text'][name='edition[consultation_participation_attributes][link_url]']"
assert_select "input[type='text'][name='edition[consultation_participation_attributes][email]']"
assert_select "input[type='text'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][title]']"
assert_select "input[type='file'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][consultation_response_form_data_attributes][file]']"
end
end
test "create should create a new consultation" do
attributes = controller_attributes_for(
:consultation,
consultation_participation_attributes: {
link_url: "http://participation.com",
email: "countmein@participation.com",
consultation_response_form_attributes: {
title: "the title of the response form",
consultation_response_form_data_attributes: {
file: upload_fixture("two-pages.pdf"),
},
},
},
)
post :create, params: { edition: attributes }
consultation = Consultation.last
response_form = consultation.consultation_participation.consultation_response_form
assert_equal attributes[:summary], consultation.summary
assert_equal attributes[:opening_at], consultation.opening_at
assert_equal attributes[:closing_at], consultation.closing_at
assert_equal "http://participation.com", consultation.consultation_participation.link_url
assert_equal "countmein@participation.com", consultation.consultation_participation.email
assert_equal "the title of the response form", response_form.title
assert response_form.consultation_response_form_data.file.present?
end
test "create should not persist external_url if the external checkbox is not checked" do
attributes = controller_attributes_for(
:consultation,
external: "0",
external_url: "http://participation.com",
consultation_participation_attributes: {
link_url: "http://participation.com",
email: "countmein@participation.com",
consultation_response_form_attributes: {
title: "the title of the response form",
consultation_response_form_data_attributes: {
file: upload_fixture("two-pages.pdf"),
},
},
},
)
post :create, params: { edition: attributes }
consultation = Consultation.last
assert consultation.external_url.blank?
end
test "create should create a new consultation without consultation participation if participation fields are all blank" do
attributes = controller_attributes_for(
:consultation,
consultation_participation_attributes: {
link_url: nil,
email: nil,
consultation_response_form_attributes: {
title: nil,
consultation_response_form_data_attributes: {
file: nil,
},
},
},
)
post :create, params: { edition: attributes }
consultation = Consultation.last
assert_nil consultation.consultation_participation
end
view_test "creating a consultation with invalid data but valid form file should still display the cached form file" do
attributes = controller_attributes_for(
:consultation,
consultation_participation_attributes: {
link_url: nil,
email: nil,
consultation_response_form_attributes: {
title: nil,
consultation_response_form_data_attributes: {
file: upload_fixture("two-pages.pdf"),
},
},
},
)
post :create, params: { edition: attributes }
assert_select "form#new_edition" do
assert_select "input[name='edition[consultation_participation_attributes][consultation_response_form_attributes][consultation_response_form_data_attributes][file_cache]'][value$='two-pages.pdf']"
assert_select ".govuk-body.already-uploaded", text: "two-pages.pdf already uploaded"
end
end
view_test "create should show 'Processing' tag if variant is missing" do
response_form = create(:consultation_response_form)
participation = create(:consultation_participation, consultation_response_form: response_form)
consultation = create(:consultation, consultation_participation: participation)
response_form.consultation_response_form_data.assets = []
get :edit, params: { id: consultation }
assert_select "span[class='govuk-tag govuk-tag--green']", text: "Processing"
end
view_test "show renders the summary" do
draft_consultation = create(:draft_consultation, summary: "a-simple-summary")
stub_publishing_api_expanded_links_with_taxons(draft_consultation.content_id, [])
get :show, params: { id: draft_consultation }
assert_select ".page-header .govuk-body-lead", text: "a-simple-summary"
end
view_test "show renders the preview link for foreign only consultations" do
french_consultation = create(:draft_consultation, primary_locale: "fr")
french_consultation.translations.first.update!(locale: "fr")
stub_publishing_api_expanded_links_with_taxons(french_consultation.content_id, [])
get :show, params: { id: french_consultation }
assert_select ".app-view-summary__section a", text: "Preview on website (opens in new tab)", href: french_consultation.public_url(draft: true, locale: "fr")
end
view_test "edit displays consultation fields" do
response_form = create(:consultation_response_form)
participation = create(:consultation_participation, consultation_response_form: response_form)
consultation = create(:consultation, consultation_participation: participation)
get :edit, params: { id: consultation }
assert_select "form#edit_edition" do
assert_select "textarea[name='edition[summary]']"
assert_select "input[name*='edition[opening_at']", count: 3
assert_select "select[name*='edition[opening_at']", count: 2
assert_select "input[name*='edition[closing_at']", count: 3
assert_select "select[name*='edition[closing_at']", count: 2
assert_select "input[type='text'][name='edition[consultation_participation_attributes][link_url]']"
assert_select "input[type='text'][name='edition[consultation_participation_attributes][email]']"
assert_select "textarea[name='edition[consultation_participation_attributes][postal_address]']"
assert_select "input[type='hidden'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][id]'][value='#{response_form.id}']"
assert_select "input[type='text'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][title]']"
assert_select "input[type='hidden'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][consultation_response_form_data_attributes][id]'][value='#{response_form.consultation_response_form_data.id}']"
assert_select "input[type='file'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][consultation_response_form_data_attributes][file]']"
end
end
test "update should save modified consultation attributes" do
consultation = create(:consultation)
put :update,
params: { id: consultation,
edition: {
summary: "new-summary",
opening_at: 1.day.ago,
closing_at: 50.days.from_now,
consultation_participation_attributes: {
link_url: "http://consult.com",
email: "tell-us-what-you-think@gov.uk",
},
} }
consultation.reload
assert_equal "new-summary", consultation.summary
assert_equal 1.day.ago, consultation.opening_at
assert_equal 50.days.from_now, consultation.closing_at
assert_equal "http://consult.com", consultation.consultation_participation.link_url
assert_equal "tell-us-what-you-think@gov.uk", consultation.consultation_participation.email
end
test "update should save consultation without consultation participation if participation fields are all blank" do
consultation = create(:consultation)
put :update,
params: { id: consultation,
edition: {
consultation_participation_attributes: {
link_url: nil,
email: nil,
},
} }
consultation.reload
assert_nil consultation.consultation_participation
end
view_test "updating should not allow removal of response form without explicit action" do
response_form = create(:consultation_response_form)
participation = create(:consultation_participation, consultation_response_form: response_form)
consultation = create(:consultation, consultation_participation: participation)
put :update,
params: { id: consultation,
edition: {
consultation_participation_attributes: {
id: participation.id,
consultation_response_form_attributes: {
id: response_form.id,
_destroy: "1",
},
},
} }
refute_select ".errors"
consultation.reload
assert_not_nil consultation.consultation_participation.consultation_response_form
end
view_test "updating should respect the attachment_action for response forms to keep it" do
two_pages_pdf = upload_fixture("two-pages.pdf")
greenpaper_pdf = upload_fixture("greenpaper.pdf")
response_form = create(:consultation_response_form, file: two_pages_pdf)
participation = create(:consultation_participation, consultation_response_form: response_form)
consultation = create(:consultation, consultation_participation: participation)
put :update,
params: { id: consultation,
edition: {
consultation_participation_attributes: {
id: participation.id,
consultation_response_form_attributes: {
id: response_form.id,
attachment_action: "keep",
_destroy: "1",
consultation_response_form_data_attributes: {
id: response_form.consultation_response_form_data.id,
file: greenpaper_pdf,
},
},
},
} }
refute_select ".errors"
consultation.reload
assert_not_nil consultation.consultation_participation.consultation_response_form
assert_equal "two-pages.pdf", consultation.consultation_participation.consultation_response_form.consultation_response_form_data.carrierwave_file
end
view_test "updating should respect the attachment_action for response forms to remove it" do
AssetManagerDeleteAssetJob.stubs(:perform_async)
response_form = create(:consultation_response_form)
participation = create(:consultation_participation, consultation_response_form: response_form)
consultation = create(:consultation, consultation_participation: participation)
put :update,
params: { id: consultation,
edition: {
consultation_participation_attributes: {
id: participation.id,
consultation_response_form_attributes: {
id: response_form.id,
attachment_action: "remove",
},
},
} }
refute_select ".errors"
consultation.reload
assert_nil consultation.consultation_participation.consultation_response_form
assert_raise(ActiveRecord::RecordNotFound) do
response_form.consultation_response_form_data.reload
end
assert_raise(ActiveRecord::RecordNotFound) do
response_form.reload
end
end
view_test "updating should respect the attachment_action for response forms to replace it" do
Services.asset_manager.stubs(:delete_asset)
two_pages_pdf = upload_fixture("two-pages.pdf")
greenpaper_pdf = upload_fixture("greenpaper.pdf")
response_form = create(:consultation_response_form, file: two_pages_pdf)
participation = create(:consultation_participation, consultation_response_form: response_form)
consultation = create(:consultation, consultation_participation: participation)
put :update,
params: { id: consultation,
edition: {
consultation_participation_attributes: {
id: participation.id,
consultation_response_form_attributes: {
id: response_form.id,
attachment_action: "replace",
_destroy: "1",
consultation_response_form_data_attributes: {
id: response_form.consultation_response_form_data.id,
file: greenpaper_pdf,
},
},
},
} }
refute_select ".errors"
consultation.reload
assert_not_nil consultation.consultation_participation.consultation_response_form
assert_equal "greenpaper.pdf", consultation.consultation_participation.consultation_response_form.consultation_response_form_data.carrierwave_file
end
test "PUT :update discards file_cache when a file is provided" do
two_pages_pdf = upload_fixture("two-pages.pdf")
greenpaper_pdf = upload_fixture("greenpaper.pdf")
response_form = create(:consultation_response_form)
participation = create(:consultation_participation, consultation_response_form: response_form)
consultation = create(:consultation, consultation_participation: participation)
response_form_data = build(:consultation_response_form_data, file: two_pages_pdf)
AssetManagerCreateAssetJob.expects(:perform_async).with(regexp_matches(/two-pages/), anything, anything, anything, anything, anything).never
AssetManagerCreateAssetJob.expects(:perform_async).with(regexp_matches(/greenpaper/), anything, anything, anything, anything, anything).times(1)
put :update,
params: { id: consultation,
edition: {
consultation_participation_attributes: {
id: participation.id,
consultation_response_form_attributes: {
id: response_form.id,
attachment_action: "replace",
_destroy: "1",
consultation_response_form_data_attributes: {
id: response_form.consultation_response_form_data.id,
file: greenpaper_pdf,
file_cache: response_form_data.file_cache,
},
},
},
} }
end
test "POST :create discards file_cache when a file is provided" do
two_pages_pdf = upload_fixture("two-pages.pdf")
greenpaper_pdf = upload_fixture("greenpaper.pdf")
response_form_data = build(:consultation_response_form_data, file: two_pages_pdf)
AssetManagerCreateAssetJob.expects(:perform_async).with(regexp_matches(/two-pages/), anything, anything, anything, anything, anything).never
AssetManagerCreateAssetJob.expects(:perform_async).with(regexp_matches(/greenpaper/), anything, anything, anything, anything, anything).times(1)
attributes = controller_attributes_for(
:consultation,
consultation_participation_attributes: {
link_url: nil,
email: nil,
consultation_response_form_attributes: {
title: "cache_test",
consultation_response_form_data_attributes: {
file: greenpaper_pdf,
file_cache: response_form_data.file_cache,
},
},
},
)
post :create, params: { edition: attributes }
end
private
def controller_attributes_for(edition_type, attributes = {})
super.except(:alternative_format_provider).reverse_merge(
alternative_format_provider_id: create(:alternative_format_provider).id,
)
end
end