-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
request_controller_spec.rb
2193 lines (1914 loc) · 68.8 KB
/
request_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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'spec_helper'
RSpec.describe RequestController, "when listing recent requests" do
it "should be successful" do
get :list, params: { view: 'all' }
expect(response).to be_successful
end
it "should render with 'list' template" do
get :list, params: { view: 'all' }
expect(response).to render_template('list')
end
it "should return 404 for pages we don't want to serve up" do
expect {
get :list, params: { view: 'all', page: 100 }
}.to raise_error(ActiveRecord::RecordNotFound)
end
it "raise unknown format error" do
expect { get :list, params: { view: "all", format: :json } }.to(
raise_error ActionController::UnknownFormat
)
end
it 'should not raise an error for a page param of less than zero, but should treat it as a param of 1' do
expect { get :list, params: { view: 'all', page: "-1" } }.not_to raise_error
expect(assigns[:page]).to eq(1)
end
it 'sets title based on page' do
get :list, params: { view: 'all' }
expect(assigns[:title]).to eq('Browse and search requests')
get :list, params: { view: 'all', page: 2 }
expect(assigns[:title]).to eq('Browse and search requests (page 2)')
end
it 'sets title based on if tag matches an request category' do
FactoryBot.create(:category, :info_request,
title: 'Climate requests', category_tag: 'climate')
update_xapian_index
get :list, params: { view: 'all', tag: 'climate' }
expect(assigns[:title]).
to eq("Found 0 requests in the category ‘Climate requests’")
FactoryBot.create(:info_request, tag_string: 'climate')
update_xapian_index
get :list, params: { view: 'all', tag: 'climate' }
expect(assigns[:title]).
to eq("Found 1 request in the category ‘Climate requests’")
end
it 'sets title based on if tag does not match an request category' do
update_xapian_index
get :list, params: { view: 'all', tag: 'other' }
expect(assigns[:title]).to eq("Found 0 requests tagged ‘other’")
FactoryBot.create(:info_request, tag_string: 'other')
update_xapian_index
get :list, params: { view: 'all', tag: 'other' }
expect(assigns[:title]).to eq("Found 1 request tagged ‘other’")
end
end
RSpec.describe RequestController, "when showing one request" do
render_views
before(:each) do
load_raw_emails_data
end
it "should be successful" do
get :show, params: { url_title: 'why_do_you_have_such_a_fancy_dog' }
expect(response).to be_successful
end
it "should render with 'show' template" do
get :show, params: { url_title: 'why_do_you_have_such_a_fancy_dog' }
expect(response).to render_template('show')
end
it "should assign the request" do
get :show, params: { url_title: 'why_do_you_have_such_a_fancy_dog' }
expect(assigns[:info_request]).to eq(info_requests(:fancy_dog_request))
end
it 'should return a 404 for GET requests to a malformed request URL' do
expect {
get :show, params: { url_title: '228%85' }
}.to raise_error(ActiveRecord::RecordNotFound)
end
context 'when the request has similar requests' do
let(:info_request) { FactoryBot.create(:info_request) }
let(:similar_requests) { double.as_null_object }
let(:similar_more) { double.as_null_object }
before do
allow_any_instance_of(InfoRequest).
to receive(:similar_requests).
and_return([similar_requests, similar_more])
get :show, params: { url_title: info_request.url_title }
end
it 'assigns similar_requests' do
expect(assigns[:similar_requests]).to eq(similar_requests)
end
it 'assigns similar_more' do
expect(assigns[:similar_more]).to eq(similar_more)
end
end
context 'when the request has citations' do
let(:info_request) { FactoryBot.create(:info_request) }
let(:citations) do
FactoryBot.create_list(:citation, 5, citable: info_request)
end
before { get :show, params: { url_title: info_request.url_title } }
it 'assigns newest 3 citations' do
expect(assigns[:citations]).to match_array(citations.reverse.take(3))
end
end
context 'when the request does not have citations' do
let(:info_request) { FactoryBot.create(:info_request) }
before { get :show, params: { url_title: info_request.url_title } }
it 'assigns an empty array of citations' do
expect(assigns[:citations]).to be_empty
end
end
describe "livery used", feature: :alaveteli_pro do
let(:pro_user) { FactoryBot.create(:pro_user) }
before { sign_in pro_user }
context "when showing pros their own requests" do
context "when the request is embargoed" do
let(:info_request) do
FactoryBot.create(:embargoed_request, user: pro_user)
end
it 'uses the pro livery' do
get :show, params: { url_title: info_request.url_title }
expect(assigns[:in_pro_area]).to be true
end
end
context "when the request is not embargoed" do
let(:info_request) do
FactoryBot.create(:info_request, user: pro_user)
end
it "should not use the pro livery" do
get :show, params: { url_title: info_request.url_title }
expect(assigns[:in_pro_area]).to be false
end
end
end
context 'when a cancelled pro views their embargoed request' do
before do
pro_user.remove_role(:pro)
end
let(:info_request) do
FactoryBot.create(:embargoed_request, user: pro_user)
end
it 'uses the pro livery' do
get :show, params: { url_title: info_request.url_title }
expect(assigns[:in_pro_area]).to be true
end
end
context "when showing pros a someone else's request" do
it "should not user the pro livery" do
get :show, params: { url_title: 'why_do_you_have_such_a_fancy_dog' }
expect(assigns[:in_pro_area]).to be false
end
end
end
context 'when the request is embargoed' do
it 'raises ActiveRecord::RecordNotFound' do
embargoed_request = FactoryBot.create(:embargoed_request)
expect {
get :show, params: { url_title: embargoed_request.url_title }
}.to raise_error(ActiveRecord::RecordNotFound)
end
it "doesn't even redirect from a numeric id" do
embargoed_request = FactoryBot.create(:embargoed_request)
expect {
get :show, params: { url_title: embargoed_request.id }
}.to raise_error(ActiveRecord::RecordNotFound)
end
end
describe 'when showing an external request' do
describe 'when viewing anonymously' do
it 'should be successful' do
sign_in nil
get :show, params: { url_title: 'balalas' }
expect(response).to be_successful
end
end
describe 'when the request is being viewed by an admin' do
def make_request
sign_in users(:admin_user)
get :show, params: { url_title: 'balalas' }
end
it 'should be successful' do
make_request
expect(response).to be_successful
end
end
end
describe 'when handling an update_status parameter' do
describe 'when the request is external' do
it 'should assign the "update status" flag to the view as falsey if the parameter is present' do
get :show, params: { url_title: 'balalas', update_status: 1 }
expect(assigns[:update_status]).to be_falsey
end
it 'should assign the "update status" flag to the view as falsey if the parameter is not present' do
get :show, params: { url_title: 'balalas' }
expect(assigns[:update_status]).to be_falsey
end
end
it 'should assign the "update status" flag to the view as truthy if the parameter is present' do
get :show, params: {
url_title: 'why_do_you_have_such_a_fancy_dog',
update_status: 1
}
expect(assigns[:update_status]).to be_truthy
end
it 'should assign the "update status" flag to the view as falsey if the parameter is not present' do
get :show, params: { url_title: 'why_do_you_have_such_a_fancy_dog' }
expect(assigns[:update_status]).to be_falsey
end
it 'should require login' do
sign_in nil
get :show, params: {
url_title: 'why_do_you_have_such_a_fancy_dog',
update_status: 1
}
expect(response).
to redirect_to(signin_path(token: get_last_post_redirect.token))
end
it 'should work if logged in as the requester' do
sign_in users(:bob_smith_user)
get :show, params: {
url_title: 'why_do_you_have_such_a_fancy_dog',
update_status: 1
}
expect(response).to render_template "request/show"
end
it 'should not work if logged in as not the requester' do
sign_in users(:silly_name_user)
get :show, params: {
url_title: 'why_do_you_have_such_a_fancy_dog',
update_status: 1
}
expect(response).to render_template "user/wrong_user"
end
it 'should work if logged in as an admin user' do
sign_in users(:admin_user)
get :show, params: {
url_title: 'why_do_you_have_such_a_fancy_dog',
update_status: 1
}
expect(response).to render_template "request/show"
end
end
describe 'when params[:pro] is true and a pro user is logged in' do
let(:pro_user) { FactoryBot.create(:pro_user) }
before :each do
sign_in pro_user
get :show, params: {
url_title: 'why_do_you_have_such_a_fancy_dog',
pro: "1"
}
end
it "should set @in_pro_area to true" do
expect(assigns[:in_pro_area]).to be true
end
it "should set @sidebar_template to the pro sidebar" do
expect(assigns[:sidebar_template]).
to eq("alaveteli_pro/info_requests/sidebar")
end
end
describe 'when params[:pro] is not set' do
before :each do
get :show, params: { url_title: 'why_do_you_have_such_a_fancy_dog' }
end
it "should set @in_pro_area to false" do
expect(assigns[:in_pro_area]).to be false
end
it "should set @sidebar_template to the normal sidebar" do
expect(assigns[:sidebar_template]).to eq("sidebar")
end
end
describe "@show_top_describe_state_form" do
let(:pro_user) { FactoryBot.create(:pro_user) }
let(:pro_request) { FactoryBot.create(:embargoed_request, user: pro_user) }
context "when @in_pro_area is true" do
it "is false" do
with_feature_enabled(:alaveteli_pro) do
sign_in pro_user
get :show, params: {
url_title: pro_request.url_title,
pro: "1",
update_status: "1"
}
expect(assigns[:show_top_describe_state_form]).to be false
end
end
end
context "when @in_pro_area is false" do
context "and @update_status is false" do
it "is false" do
info_request = info_requests(:naughty_chicken_request)
expect(info_request.awaiting_description).to be false
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_top_describe_state_form]).to be false
end
context "but the request is awaiting_description" do
it "is true" do
get :show, params: {
url_title: 'why_do_you_have_such_a_fancy_dog'
}
expect(assigns[:show_top_describe_state_form]).to be true
end
end
end
context "and @update_status is true" do
it "is true" do
sign_in users(:bob_smith_user)
info_request = info_requests(:naughty_chicken_request)
expect(info_request.awaiting_description).to be false
get :show, params: {
url_title: info_request.url_title,
update_status: "1"
}
expect(assigns[:show_top_describe_state_form]).to be true
end
context "and the request is awaiting_description" do
it "is true" do
get :show, params: {
url_title: 'why_do_you_have_such_a_fancy_dog',
update_status: "1"
}
expect(assigns[:show_top_describe_state_form]).to be true
end
end
end
end
context "when there are no valid state transitions" do
it "is false" do
info_request = FactoryBot.create(:info_request)
info_request.set_described_state('not_foi')
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_top_describe_state_form]).to be false
end
end
end
describe "@show_bottom_describe_state_form" do
let(:pro_user) { FactoryBot.create(:pro_user) }
let(:pro_request) { FactoryBot.create(:embargoed_request, user: pro_user) }
context "when @in_pro_area is true" do
it "is false" do
with_feature_enabled(:alaveteli_pro) do
sign_in pro_user
get :show, params: {
url_title: pro_request.url_title,
pro: "1"
}
expect(assigns[:show_bottom_describe_state_form]).to be false
end
end
end
context "when @in_pro_area is false" do
context "and the request is awaiting_description" do
it "is true" do
get :show, params: { url_title: 'why_do_you_have_such_a_fancy_dog' }
expect(assigns[:show_bottom_describe_state_form]).to be true
end
end
context "and the request is not awaiting_description" do
it "is false" do
info_request = info_requests(:naughty_chicken_request)
expect(info_request.awaiting_description).to be false
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_bottom_describe_state_form]).to be false
end
end
end
context "when there are no valid state transitions" do
it "is false" do
info_request = FactoryBot.create(:info_request)
info_request.set_described_state('not_foi')
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_bottom_describe_state_form]).to be false
end
end
end
it "should set @state_transitions for the request" do
info_request = FactoryBot.create(:info_request)
expected_transitions = {
pending: {
"waiting_response" => "<strong>No response</strong> has been " \
"received <small>(maybe there's just an acknowledgement)</small>",
"waiting_clarification" => "<strong>Clarification</strong> has been " \
"requested",
"gone_postal" => "A response will be sent <strong>by postal " \
"mail</strong>"
},
complete: {
"not_held" => "The authority do <strong>not have</strong> the " \
"information <small>(maybe they say who does)</small>",
"partially_successful" => "<strong>Some of the information</strong> " \
"has been sent ",
"successful" => "<strong>All the information</strong> has been sent",
"rejected" => "The request has been <strong>refused</strong>"
},
other: {
"error_message" => "An <strong>error message</strong> has been received"
}
}
get :show, params: { url_title: info_request.url_title }
expect(assigns(:state_transitions)).to eq(expected_transitions)
end
describe "showing update status actions" do
let(:user) { FactoryBot.create(:user) }
before do
sign_in user
end
context "when the request is old and unclassified" do
let(:info_request) { FactoryBot.create(:old_unclassified_request) }
it "@show_owner_update_status_action should be false" do
expect(info_request.is_old_unclassified?).to be true
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_owner_update_status_action]).to be false
end
it "@show_other_user_update_status_action should be true" do
expect(info_request.is_old_unclassified?).to be true
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_other_user_update_status_action]).to be true
end
end
context "when the request is not old and unclassified" do
let(:info_request) { FactoryBot.create(:info_request) }
it "@show_owner_update_status_action should be true" do
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_owner_update_status_action]).to be true
end
it "@show_other_user_update_status_action should be false" do
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_other_user_update_status_action]).to be false
end
end
context "when there are no state_transitions" do
let(:info_request) { FactoryBot.create(:info_request) }
before do
info_request.set_described_state('not_foi')
end
it "should hide all status update options" do
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_owner_update_status_action]).to be false
expect(assigns[:show_other_user_update_status_action]).to be false
end
end
end
context 'when the request author is banned' do
let(:user) { FactoryBot.create(:user, :banned) }
let(:info_request) { FactoryBot.create(:info_request, user: user) }
before do
user.create_profile_photo!(data: load_file_fixture('parrot.png'))
end
it 'does not show the profile_photo' do
get :show, params: { url_title: info_request.url_title }
expect(assigns[:show_profile_photo]).to eq(false)
end
end
end
RSpec.describe RequestController, 'when handling prominence' do
def expect_hidden(hidden_template)
expect(response.media_type).to eq('text/html')
expect(response).to render_template(hidden_template)
expect(response.code).to eq('403')
end
let(:info_request) do
FactoryBot.create(:info_request_with_pdf_attachment, prominence: prominence)
end
context 'when the request is hidden' do
let(:prominence) { 'hidden' }
it 'does not show the request if not logged in' do
get :show, params: { url_title: info_request.url_title }
expect_hidden('hidden')
end
it 'does not show the request even if logged in as their owner' do
sign_in info_request.user
get :show, params: { url_title: info_request.url_title }
expect_hidden('hidden')
end
it 'does not show the request if requested using json' do
sign_in info_request.user
get :show, params: { url_title: info_request.url_title, format: 'json' }
expect(response.code).to eq('403')
end
it 'shows the request if logged in as super user' do
sign_in FactoryBot.create(:admin_user)
get :show, params: { url_title: info_request.url_title }
expect(response).to render_template('show')
end
end
context 'when the request is requester_only' do
let(:prominence) { 'requester_only' }
it 'does not show the request if not logged in' do
get :show, params: { url_title: info_request.url_title }
expect_hidden('hidden')
end
it 'does not show the request if logged in but not the requester' do
sign_in FactoryBot.create(:user)
get :show, params: { url_title: info_request.url_title }
expect_hidden('hidden')
end
it 'shows the request to the requester' do
sign_in info_request.user
get :show, params: { url_title: info_request.url_title }
expect(response).to render_template('show')
end
it 'shows the request to an admin' do
sign_in FactoryBot.create(:admin_user)
get :show, params: { url_title: info_request.url_title }
expect(response).to render_template('show')
end
end
context 'when the request is backpage' do
let(:prominence) { 'backpage' }
it 'shows the request if not logged in' do
get :show, params: { url_title: info_request.url_title }
expect(response).to render_template('show')
end
it 'sets a noindex header' do
get :show, params: { url_title: info_request.url_title }
expect(response.headers['X-Robots-Tag']).to eq 'noindex'
end
end
end
# TODO: do this for invalid ids
# it "should render 404 file" do
# response.should render_template("#{Rails.root}/public/404.html")
# response.headers["Status"].should == "404 Not Found"
# end
RSpec.describe RequestController, "when searching for an authority" do
# Whether or not sign-in is required for this step is configurable,
# so we make sure we're logged in, just in case
before do
@user = users(:bob_smith_user)
update_xapian_index
end
it "should return matching bodies" do
sign_in @user
get :select_authority, params: { query: "Quango" }
expect(response).to render_template('select_authority')
assigns[:xapian_requests].results.size == 1
expect(assigns[:xapian_requests].results[0][:model].name).
to eq(public_bodies(:geraldine_public_body).name)
end
it "remembers the search params" do
sign_in @user
search_params = {
'query' => 'Quango',
'page' => '1',
'bodies' => '1'
}
get :select_authority, params: search_params
flash_params = flash[:search_params].to_unsafe_h
expect(flash_params).to eq(search_params)
end
describe 'when params[:pro] is true' do
context "and a pro user is logged in " do
let(:pro_user) { FactoryBot.create(:pro_user) }
before do
sign_in pro_user
end
it "should set @in_pro_area to true" do
get :select_authority, params: { pro: "1" }
expect(assigns[:in_pro_area]).to be true
end
it "should not redirect pros to the info request form for pros" do
with_feature_enabled(:alaveteli_pro) do
public_body = FactoryBot.create(:public_body)
get :select_authority, params: { pro: "1" }
expect(response).to be_successful
end
end
end
context "and a pro user is not logged in" do
before do
sign_in nil
end
it "should set @in_pro_area to false" do
get :select_authority, params: { pro: "1" }
expect(assigns[:in_pro_area]).to be false
end
it "should not redirect users to the info request form for pros" do
with_feature_enabled(:alaveteli_pro) do
public_body = FactoryBot.create(:public_body)
get :select_authority, params: { pro: "1" }
expect(response).to be_successful
end
end
end
end
describe 'when params[:pro] is not set' do
it "should set @in_pro_area to false" do
get :select_authority
expect(assigns[:in_pro_area]).to be false
end
it "should redirect pros to the info request form for pros" do
with_feature_enabled(:alaveteli_pro) do
pro_user = FactoryBot.create(:pro_user)
public_body = FactoryBot.create(:public_body)
sign_in pro_user
get :select_authority
expect(response).to redirect_to(new_alaveteli_pro_info_request_url)
end
end
end
end
RSpec.describe RequestController, "when creating a new request" do
render_views
before do
@user = users(:bob_smith_user)
@body = public_bodies(:geraldine_public_body)
end
it "should redirect to front page if no public body specified" do
get :new
expect(response).to redirect_to(controller: 'general', action: 'frontpage')
end
it "should redirect to front page if no public body specified, when logged in" do
sign_in @user
get :new
expect(response).to redirect_to(controller: 'general', action: 'frontpage')
end
it "should redirect 'bad request' page when a body has no email address" do
@body.request_email = ""
@body.save!
get :new, params: { public_body_id: @body.id }
expect(response).to render_template('new_bad_contact')
end
context "the outgoing message includes an email address" do
context "there is no logged in user" do
it "displays a flash error message without escaping the HTML" do
post :new, params: {
info_request: {
public_body_id: @body.id,
title: "Test Request"
},
outgoing_message: { body: "me@here.com" },
submitted_new_request: 1,
preview: 1
}
expect(response.body).to have_css('div#error p')
expect(response.body).to_not have_content('<p>')
expect(response.body).
to have_content('You do not need to include your email')
end
end
context "the user is logged in" do
it "displays a flash error message without escaping the HTML" do
sign_in @user
post :new, params: {
info_request: {
public_body_id: @body.id,
title: "Test Request" },
outgoing_message: { body: "me@here.com" },
submitted_new_request: 1,
preview: 1
}
expect(response.body).to have_css('div#error p')
expect(response.body).to_not have_content('<p>')
expect(response.body).
to have_content('You do not need to include your email')
end
end
end
context "the outgoing message includes a postcode" do
it 'displays an error message warning about the postcode' do
post :new, params: {
info_request: {
public_body_id: @body.id,
title: "Test Request"
},
outgoing_message: { body: "SW1A 1AA" },
submitted_new_request: 1,
preview: 1
}
expect(response.body).to have_content('Your request contains a postcode')
end
end
context 'a network error occurs while sending the initial request' do
def send_request
sign_in @user
post :new, params: {
info_request: {
public_body_id: @body.id,
title: 'Test request',
tag_string: ''
},
outgoing_message: {
body: 'This is a silly letter.'
},
submitted_new_request: 1,
preview: 0
}
end
let(:request) { assigns[:info_request] }
let(:outgoing_message) { request.reload.outgoing_messages.last }
it_behaves_like 'NetworkSendErrors'
end
it "should redirect pros to the pro version" do
with_feature_enabled(:alaveteli_pro) do
pro_user = FactoryBot.create(:pro_user)
public_body = FactoryBot.create(:public_body)
sign_in pro_user
get :new, params: { url_name: public_body.url_name }
expected_url = new_alaveteli_pro_info_request_url(
public_body: public_body.url_name)
expect(response).to redirect_to(expected_url)
end
end
it "should accept a public body parameter" do
get :new, params: { public_body_id: @body.id }
expect(assigns[:info_request].public_body).to eq(@body)
expect(response).to render_template('new')
end
it 'assigns a default text for the request' do
get :new, params: { public_body_id: @body.id }
expect(assigns[:info_request].public_body).to eq(@body)
expect(response).to render_template('new')
default_message = <<-EOF.strip_heredoc
Dear Geraldine Quango,
Yours faithfully,
EOF
expect(assigns[:outgoing_message].body).to include(default_message.strip)
end
it 'allows the default text to be set via the default_letter param' do
get :new, params: { url_name: @body.url_name, default_letter: "test" }
default_message = <<-EOF.strip_heredoc
Dear Geraldine Quango,
test
Yours faithfully,
EOF
expect(assigns[:outgoing_message].body).to include(default_message.strip)
end
it 'should display one meaningful error message when no message body is added' do
post :new, params: {
info_request: { public_body_id: @body.id },
outgoing_message: { body: "" },
submitted_new_request: 1,
preview: 1
}
expect(assigns[:info_request].errors.full_messages).
not_to include('Outgoing messages is invalid')
expect(assigns[:outgoing_message].errors.full_messages).
to include('Body Please enter your letter requesting information')
end
it "should give an error and render 'new' template when a summary isn't given" do
post :new,
params: {
info_request: { public_body_id: @body.id },
outgoing_message: {
body: "This is a silly letter. It is too short to be interesting."
},
submitted_new_request: 1,
preview: 1
}
expect(assigns[:info_request].errors[:title]).not_to be_nil
expect(response).to render_template('new')
end
it "should redirect to sign in page when input is good and nobody is logged in" do
params = {
info_request: {
public_body_id: @body.id,
title: "Why is your quango called Geraldine?", tag_string: ""
},
outgoing_message: {
body: "This is a silly letter. It is too short to be interesting."
},
submitted_new_request: 1, preview: 0
}
post :new, params: params
expect(response).
to redirect_to(signin_path(token: get_last_post_redirect.token))
# post_redirect.post_params.should == params # TODO: get this working.
# there's a : vs '' problem amongst others
end
it 'redirects to the frontpage if the action is sent the invalid
public_body param' do
post :new, params: {
info_request: {
public_body: @body.id,
title: 'Why Geraldine?',
tag_string: ''
},
outgoing_message: { body: 'This is a silly letter.' },
submitted_new_request: 1,
preview: 1
}
expect(response).to redirect_to frontpage_url
end
it "should show preview when input is good" do
sign_in @user
post :new, params: {
info_request: {
public_body_id: @body.id,
title: "Why is your quango called Geraldine?",
tag_string: ""
},
outgoing_message: {
body: "This is a silly letter. It is too short to be interesting."
},
submitted_new_request: 1,
preview: 1
}
expect(response).to render_template('preview')
end
it "should allow re-editing of a request" do
post :new, params: {
info_request: {
public_body_id: @body.id,
title: "Why is your quango called Geraldine?",
tag_string: ""
},
outgoing_message: {
body: "This is a silly letter. It is too short to be interesting."
},
submitted_new_request: 1,
preview: 0,
reedit: "Re-edit this request"
}
expect(response).to render_template('new')
end
it "re-editing preserves the message body" do
post :new, params: {
info_request: {
public_body_id: @body.id,
title: "Why is your quango called Geraldine?",
tag_string: ""
},
outgoing_message: {
body: "This is a silly letter. It is too short to be interesting."
},
submitted_new_request: 1,
preview: 0,
reedit: "Re-edit this request"
}
expect(assigns[:outgoing_message].body).
to include('This is a silly letter. It is too short to be interesting.')
end
it "should create the request and outgoing message, and send the outgoing message by email, and redirect to request page when input is good and somebody is logged in" do
sign_in @user
post :new, params: {
info_request: {
public_body_id: @body.id,
title: "Why is your quango called Geraldine?",
tag_string: ""
},
outgoing_message: {
body: "This is a silly letter. It is too short to be interesting."
},
submitted_new_request: 1,
preview: 0
}
ir_array = InfoRequest.where(title: "Why is your quango called Geraldine?")
expect(ir_array.size).to eq(1)
ir = ir_array[0]
expect(ir.outgoing_messages.size).to eq(1)
om = ir.outgoing_messages[0]
expect(om.body).
to eq("This is a silly letter. It is too short to be interesting.")
deliveries = ActionMailer::Base.deliveries