/
public_body_controller_spec.rb
504 lines (425 loc) · 17.5 KB
/
public_body_controller_spec.rb
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
require 'spec_helper'
require 'nokogiri'
RSpec.describe PublicBodyController, "when showing a body" do
render_views
before(:each) do
load_raw_emails_data
update_xapian_index
end
it "should be successful" do
get :show, params: { url_name: "dfh", view: 'all' }
expect(response).to be_successful
end
it "should render with 'show' template" do
get :show, params: { url_name: "dfh", view: 'all' }
expect(response).to render_template('show')
end
it "should assign the body" do
get :show, params: { url_name: "dfh", view: 'all' }
expect(assigns[:public_body]).to eq(public_bodies(:humpadink_public_body))
end
it "should assign the requests (1)" do
get :show, params: { url_name: "tgq", view: 'all' }
conditions = { public_body_id: public_bodies(:geraldine_public_body).id }
actual = assigns[:xapian_requests].results.map do |x|
x[:model].info_request
end
expect(actual).to match_array(InfoRequest.where(conditions))
end
it "should assign the requests (2)" do
get :show, params: { url_name: "tgq", view: 'successful' }
conditions = { described_state: 'successful',
public_body_id: public_bodies(:geraldine_public_body).id }
actual = assigns[:xapian_requests].results.map do |x|
x[:model].info_request
end
expect(actual).to match_array(InfoRequest.where(conditions))
end
it "should assign the requests (3)" do
get :show, params: { url_name: "dfh", view: 'all' }
conditions = { public_body_id: public_bodies(:humpadink_public_body).id }
actual = assigns[:xapian_requests].results.map do |x|
x[:model].info_request
end
expect(actual).to match_array(InfoRequest.where(conditions))
end
it "should display the body using same locale as that used in url_name" do
get :show, params: { url_name: "edfh", view: 'all' },
session: { locale: 'es' }
expect(response.body).to have_content("Baguette")
end
it 'should show public body names in the selected locale language if present for a locale with underscores' do
AlaveteliLocalization.set_locales('he_IL en', 'en')
get :show, params: { url_name: 'dfh', view: 'all' },
session: { locale: 'he_IL' }
expect(response.body).to have_content('Hebrew Humpadinking')
end
it "should redirect use to the relevant locale even when url_name is for a different locale" do
get :show, params: { url_name: "edfh", view: 'all' }
expect(response).to redirect_to "http://test.host/body/dfh"
end
it "should redirect to newest name if you use historic name of public body in URL" do
get :show, params: { url_name: "hdink", view: 'all' }
expect(response).to redirect_to(
controller: 'public_body',
action: 'show',
url_name: "dfh"
)
end
it "should redirect to lower case name if you use mixed case name in URL" do
get :show, params: { url_name: "dFh", view: 'all' }
expect(response).to redirect_to(
controller: 'public_body',
action: 'show',
url_name: "dfh"
)
end
it 'keeps the search_params flash' do
# Make two get requests to simulate the flash getting swept after the
# first response.
search_params = { 'query' => 'Quango' }
get :show, params: { url_name: 'dfh', view: 'all' },
flash: { search_params: search_params }
get :show, params: { url_name: 'dfh', view: 'all' }
expect(flash[:search_params]).to eq(search_params)
end
it 'should not show high page offsets as these are extremely slow to generate' do
expect {
get :show, params: { url_name: 'dfh', view: 'all', page: 25 }
}.to raise_error(ActiveRecord::RecordNotFound)
end
it 'should not raise an error when given an empty query param' do
get :show, params: { url_name: "dfh", view: 'all', query: nil }
expect(response).to be_successful
end
end
RSpec.describe PublicBodyController, "when listing bodies" do
render_views
it "should be successful" do
get :list
expect(response).to be_successful
end
def make_single_language_example(locale)
result = nil
with_default_locale(locale) do
AlaveteliLocalization.with_locale(locale) do
case locale
when :en
result = PublicBody.new(name: 'English only',
short_name: 'EO')
when :es
result = PublicBody.new(name: 'Español Solamente',
short_name: 'ES')
when :en_GB
result = PublicBody.new(name: 'GB English',
short_name: 'GB')
else
raise StandardError, "Unknown locale #{locale}"
end
result.request_email = "#{locale}@example.org"
result.last_edit_editor = 'test'
result.last_edit_comment = ''
result.save!
end
end
result
end
it "with no fallback, should only return bodies from the current locale" do
@english_only = make_single_language_example :en
@spanish_only = make_single_language_example :es
get :list, session: { locale: 'es' }
expect(assigns[:public_bodies].include?(@english_only)).to eq(false)
expect(assigns[:public_bodies].include?(@spanish_only)).to eq(true)
end
it "if fallback is requested, should list all bodies from default locale, even when there are no translations for selected locale" do
allow(AlaveteliConfiguration).
to receive(:public_body_list_fallback_to_default_locale).
and_return(true)
@english_only = make_single_language_example :en
get :list, session: { locale: 'es' }
expect(assigns[:public_bodies].include?(@english_only)).to eq(true)
end
it 'if fallback is requested, should still list public bodies only with translations in the current locale' do
allow(AlaveteliConfiguration).
to receive(:public_body_list_fallback_to_default_locale).
and_return(true)
@spanish_only = make_single_language_example :es
get :list, session: { locale: 'es' }
expect(assigns[:public_bodies].include?(@spanish_only)).to eq(true)
end
it "if fallback is requested, make sure that there are no duplicates listed" do
allow(AlaveteliConfiguration).
to receive(:public_body_list_fallback_to_default_locale).
and_return(true)
get :list, session: { locale: 'es' }
pb_ids = assigns[:public_bodies].map(&:id)
unique_pb_ids = pb_ids.uniq
expect(pb_ids.sort).to eq(unique_pb_ids.sort)
end
it 'should show public body names in the selected locale language if present' do
get :list, session: { locale: 'es' }
expect(response.body).to have_content('El Department for Humpadinking')
end
it 'show public body names of the selected underscore locale language' do
AlaveteliLocalization.set_locales(available_locales='en en_GB',
default_locale='en')
@gb_only = make_single_language_example :en_GB
get :list, session: { locale: 'en_GB' }
expect(response.body).to have_content(@gb_only.name)
end
it 'should not show the internal admin authority' do
PublicBody.internal_admin_body
get :list, session: { locale: 'en' }
expect(response.body).not_to have_content('Internal admin authority')
end
it 'should order on the translated name, even with the fallback' do
# The names of each public body is in:
# <span class="head"><a>Public Body Name</a></span>
# ... eo extract all of those, and check that they are ordered:
allow(AlaveteliConfiguration).
to receive(:public_body_list_fallback_to_default_locale).
and_return(true)
get :list, session: { locale: 'es' }
parsed = Nokogiri::HTML(response.body)
public_body_names = parsed.xpath '//span[@class="head"]/a/text()'
public_body_names = public_body_names.map(&:to_s)
expect(public_body_names).to eq(public_body_names.sort)
end
it 'should show public body names in the selected locale language if present for a locale with underscores' do
AlaveteliLocalization.set_locales('he_IL en', 'en')
get :list, session: { locale: 'he_IL' }
expect(response.body).to have_content('Hebrew Humpadinking')
end
it "should list bodies in alphabetical order" do
# Note that they are alphabetised by localised name
get :list
expect(response).to render_template('list')
expect(assigns[:public_bodies]).to eq(
[
public_bodies(:other_public_body),
public_bodies(:humpadink_public_body),
public_bodies(:forlorn_public_body),
public_bodies(:geraldine_public_body),
public_bodies(:sensible_walks_public_body),
public_bodies(:silly_walks_public_body)
]
)
expect(assigns[:tag]).to eq("all")
expect(assigns[:description]).to eq("Found 6 public authorities")
end
it 'list bodies in collate order according to the locale with the fallback set' do
allow(AlaveteliConfiguration).
to receive(:public_body_list_fallback_to_default_locale).
and_return(true)
allow(DatabaseCollation).to receive(:supports?).
with(an_instance_of(String)).
and_return(true)
get :list, session: { locale: 'en_GB' }
expect(assigns[:public_bodies].to_sql).to include('COLLATE')
end
it 'list bodies in default order according to the locale with the fallback set' do
allow(AlaveteliConfiguration).
to receive(:public_body_list_fallback_to_default_locale).
and_return(true)
allow(DatabaseCollation).to receive(:supports?).
with(an_instance_of(String)).
and_return(false)
get :list, session: { locale: 'unknown' }
expect(assigns[:public_bodies].to_sql).to_not include('COLLATE')
end
it 'list bodies in collate order according to the locale' do
allow(AlaveteliConfiguration).
to receive(:public_body_list_fallback_to_default_locale).
and_return(false)
allow(DatabaseCollation).to receive(:supports?).
with(an_instance_of(String)).
and_return(true)
get :list, session: { locale: 'en_GB' }
expect(assigns[:public_bodies].to_sql).to include('COLLATE')
end
it 'list bodies in alphabetical order according to the locale' do
allow(AlaveteliConfiguration).
to receive(:public_body_list_fallback_to_default_locale).
and_return(false)
allow(DatabaseCollation).to receive(:supports?).
with(an_instance_of(String)).
and_return(false)
get :list, session: { locale: 'unknown' }
expect(assigns[:public_bodies].to_sql).to_not include('COLLATE')
end
it "should support simple searching of bodies by title" do
get :list, params: { public_body_query: 'quango' }
expect(assigns[:public_bodies]).
to eq([public_bodies(:geraldine_public_body)])
end
it "should support simple searching of bodies by short_name" do
get :list, params: { public_body_query: 'DfH' }
expect(assigns[:public_bodies]).
to eq([public_bodies(:humpadink_public_body)])
end
xit "should support simple searching of bodies by notes" do
get :list, params: { public_body_query: 'Albatross' }
expect(assigns[:public_bodies]).
to eq([public_bodies(:humpadink_public_body)])
end
it "should list bodies in alphabetical order with different locale" do
with_default_locale(:es) do
get :list
expect(response).to render_template('list')
expect(assigns[:public_bodies]).to eq(
[
public_bodies(:geraldine_public_body),
public_bodies(:humpadink_public_body)
]
)
expect(assigns[:tag]).to eq("all")
expect(assigns[:description]).to eq("Found 2 public authorities")
end
end
it "should list a tagged thing on the appropriate list page, and others on the other page, and all still on the all page" do
category = FactoryBot.create(:category, :public_body, category_tag: 'dink')
public_bodies(:humpadink_public_body).tag_string = category.category_tag
get :list, params: { tag: category.category_tag }
expect(response).to render_template('list')
expect(assigns[:public_bodies]).
to eq([public_bodies(:humpadink_public_body)])
expect(assigns[:tag]).to eq(category.category_tag)
expect(assigns[:description]).
to eq("Found 1 public authority in the category ‘#{category.title}’")
get :list, params: { tag: "other" }
expect(response).to render_template('list')
expect(assigns[:public_bodies]).to eq(
[
public_bodies(:other_public_body),
public_bodies(:sensible_walks_public_body),
]
)
get :list
expect(response).to render_template('list')
expect(assigns[:public_bodies]).to eq(
[
public_bodies(:other_public_body),
public_bodies(:humpadink_public_body),
public_bodies(:forlorn_public_body),
public_bodies(:geraldine_public_body),
public_bodies(:sensible_walks_public_body),
public_bodies(:silly_walks_public_body)
]
)
end
it "should list a machine tagged thing, should get it in both ways" do
public_bodies(:humpadink_public_body).tag_string = "eats_cheese:stilton"
get :list, params: { tag: "eats_cheese" }
expect(response).to render_template('list')
expect(assigns[:public_bodies]).
to eq([public_bodies(:humpadink_public_body)])
expect(assigns[:tag]).to eq("eats_cheese")
get :list, params: { tag: "eats_cheese:jarlsberg" }
expect(response).to render_template('list')
expect(assigns[:public_bodies]).to eq([ ])
expect(assigns[:tag]).to eq("eats_cheese:jarlsberg")
get :list, params: { tag: "eats_cheese:stilton" }
expect(response).to render_template('list')
expect(assigns[:public_bodies]).
to eq([public_bodies(:humpadink_public_body)])
expect(assigns[:tag]).to eq("eats_cheese:stilton")
end
it 'should not include hidden requests in the request count' do
fake_pb = FactoryBot.create(:public_body)
hidden_request = FactoryBot.create(:info_request,
prominence: 'hidden',
public_body: fake_pb)
visible_request = FactoryBot.create(:info_request, public_body: fake_pb)
fake_pb.reload
expect(fake_pb.info_requests.size).to eq(2)
expect(fake_pb.info_requests.is_searchable.size).to eq(1)
fake_list = PublicBody.where(id: fake_pb.id)
allow(fake_list).to receive(:with_tag).and_return(fake_list)
allow(fake_list).to receive(:with_query).and_return(fake_list)
allow(fake_list).to receive(:joins).and_return(fake_list)
allow(fake_list).to receive(:paginate).and_return(fake_list)
allow(fake_list).to receive(:order).and_return(fake_list)
allow(fake_list).to receive(:total_entries).and_return(1)
allow(fake_list).to receive(:total_pages).and_return(1)
allow(PublicBody).to receive(:where).and_return(fake_list)
get :list
expect(response.body).to have_content('1 request.')
end
it 'raises an UnknownFormat error if asked for a json version of a list' do
expect {
get :list, params: { format: 'json' }
}.to raise_error(ActionController::UnknownFormat)
end
it "should list authorities starting with a multibyte first letter" do
AlaveteliLocalization.set_locales('cs', 'cs')
authority = AlaveteliLocalization.with_locale(:cs) do
FactoryBot.create(:public_body, name: "Åčçèñtéd Authority")
end
get :list, params: { tag: "å" }, session: { locale: 'cs' }
expect(response).to render_template('list')
expect(assigns[:public_bodies]).to eq([ authority ])
expect(assigns[:tag]).to eq("Å")
end
end
RSpec.describe PublicBodyController, "when showing JSON version for API" do
it "should be successful" do
get :show, params: { url_name: "dfh", format: "json", view: 'all' }
pb = JSON.parse(response.body)
expect(pb.class.to_s).to eq('Hash')
expect(pb['url_name']).to eq('dfh')
expect(pb['notes']).to eq('An albatross told me!!!')
end
end
RSpec.describe PublicBodyController, "when asked to export public bodies as CSV" do
before do
allow(Rails.root).to receive(:join).with('cache', 'all-authorities.csv').
and_return(fixture)
end
context 'with cached CSV' do
let(:fixture) { Rails.root.join('spec', 'fixtures', 'all-authorities.csv') }
it 'sends CSV file as response' do
get :list_all_csv
expect(response.body).to eq IO.binread(fixture)
end
end
context 'without cached CSV' do
let(:fixture) { Rails.root.join('spec', 'fixtures', 'missing.csv') }
it 'respond with 204 "no content" status' do
get :list_all_csv
expect(response.status).to eq 204
end
end
end
RSpec.describe PublicBodyController, "when doing type ahead searches" do
render_views
before(:each) do
load_raw_emails_data
update_xapian_index
end
it 'returns a 400 bad request status code without a query param' do
get :search_typeahead
expect(response.status).to eq(400)
end
it 'renders the search_ahead template' do
get :search_typeahead, params: { query: "" }
expect(response).to render_template('public_body/_search_ahead')
end
it 'assigns the xapian search to the view as xapian_requests' do
get :search_typeahead, params: { query: "Geraldine Humpadinking" }
expect(assigns[:xapian_requests]).to be_an_instance_of ActsAsXapian::Search
end
it "shows the number of bodies matching the keywords" do
get :search_typeahead, params: { query: "Geraldine Humpadinking" }
expect(response.body).to match("2 matching authorities")
end
it 'remembers the search params' do
search_params = {
'query' => 'Quango',
'page' => '1',
'bodies' => '1'
}
get :search_typeahead, params: search_params
flash_params = flash[:search_params].to_unsafe_h
expect(flash_params).to eq(search_params)
end
end