aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/request_controller_spec.rb
blob: fa43b401275fd66d9276cde6c2e87d160ce1e2c7 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
require File.dirname(__FILE__) + '/../spec_helper'

describe RequestController, "when listing recent requests" do
    
    before(:all) do
        rebuild_xapian_index
    end
    
    it "should be successful" do
        get :list, :view => 'recent'
        response.should be_success
    end

    it "should render with 'list' template" do
        get :list, :view => 'recent'
        response.should render_template('list')
    end

    it "should assign the first page of results" do
        xap_results = mock_model(ActsAsXapian::Search, 
                   :results => (1..25).to_a.map { |m| { :model => m } },
                   :matches_estimated => 103)

        InfoRequest.should_receive(:full_search).
          with([InfoRequestEvent],"variety:sent", "created_at", anything, anything, anything, anything).
          and_return(xap_results)
        get :list, :view => 'recent'
        assigns[:list_results].size.should == 25
    end
end


describe RequestController, "when showing one request" do
    
    fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views
  
    it "should be successful" do
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should be_success
    end

    it "should render with 'show' template" do
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('show')
    end

    it "should assign the request" do
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        assigns[:info_request].should == info_requests(:fancy_dog_request)
    end

    it "should redirect from a numeric URL to pretty one" do
        get :show, :url_title => info_requests(:naughty_chicken_request).id
        response.should redirect_to(:action => 'show', :url_title => info_requests(:naughty_chicken_request).url_title)
    end

     
    describe 'when handling an update_status parameter' do
        
        before do 
            mock_request = mock_model(InfoRequest, :url_title => 'test_title', 
                                                   :title => 'test title', 
                                                   :null_object => true)
            InfoRequest.stub!(:find_by_url_title).and_return(mock_request)
        end

        it 'should assign the "update status" flag to the view as true if the parameter is present' do
            get :show, :url_title => 'test_title', :update_status => 1
            assigns[:update_status].should be_true
        end

        it 'should assign the "update status" flag to the view as true if the parameter is present' do
            get :show, :url_title => 'test_title'
            assigns[:update_status].should be_false
        end
        
    end

    describe 'when handling incoming mail' do 
      
        integrate_views
        
        it "should receive incoming messages, send email to creator, and show them" do
            get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
            size_before = assigns[:info_request_events].size

            ir = info_requests(:fancy_dog_request) 
            receive_incoming_mail('incoming-request-plain.email', ir.incoming_email)
            deliveries = ActionMailer::Base.deliveries
            deliveries.size.should == 1
            mail = deliveries[0]
            mail.body.should =~ /You have a new response to the Freedom of Information request/

            get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
            (assigns[:info_request_events].size - size_before).should == 1
        end
      
        it "should download attachments" do
            get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
            response.content_type.should == "text/html"
            size_before = assigns[:info_request_events].size

            ir = info_requests(:fancy_dog_request) 
            receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email)

            get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
            (assigns[:info_request_events].size - size_before).should == 1

            get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt']
            response.content_type.should == "text/plain"
            response.should have_text(/Second hello/)        
            get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 3, :file_name => ['hello.txt']
            response.content_type.should == "text/plain"
            response.should have_text(/First hello/)        
        end

        it "should not download attachments with wrong file name" do
            ir = info_requests(:fancy_dog_request) 
            receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email)

            lambda {
                get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, 
                    :file_name => ['http://trying.to.hack']
            }.should raise_error(RuntimeError)
        end

        it "should censor attachments downloaded as binary" do
            ir = info_requests(:fancy_dog_request) 

            censor_rule = CensorRule.new()
            censor_rule.text = "Second"
            censor_rule.replacement = "Mouse"
            censor_rule.last_edit_editor = "unknown"
            censor_rule.last_edit_comment = "none"
            ir.censor_rules << censor_rule

            receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email)

            get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt']
            response.content_type.should == "text/plain"
            response.should have_text(/xxxxxx hello/)        
        end

        it "should censor with rules on the user (rather than the request)" do
            ir = info_requests(:fancy_dog_request) 

            censor_rule = CensorRule.new()
            censor_rule.text = "Second"
            censor_rule.replacement = "Mouse"
            censor_rule.last_edit_editor = "unknown"
            censor_rule.last_edit_comment = "none"
            ir.user.censor_rules << censor_rule

            receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email)

            get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt']
            response.content_type.should == "text/plain"
            response.should have_text(/xxxxxx hello/)        
        end

        it "should censor attachment names" do
            ir = info_requests(:fancy_dog_request) 
            receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email)

            get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
            response.body.should have_tag("p.attachment strong", /hello.txt/m) 

            censor_rule = CensorRule.new()
            censor_rule.text = "hello.txt"
            censor_rule.replacement = "goodbye.txt"
            censor_rule.last_edit_editor = "unknown"
            censor_rule.last_edit_comment = "none"
            ir.censor_rules << censor_rule

            get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
            response.body.should have_tag("p.attachment strong", /goodbye.txt/m) 
        end

 
    end
end

describe RequestController, "when changing prominence of a request" do
    fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views

    it "should not show hidden requests" do
        ir = info_requests(:fancy_dog_request)
        ir.prominence = 'hidden'
        ir.save!

        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('hidden')
    end

    it "should not show hidden requests even if logged in as their owner" do
        ir = info_requests(:fancy_dog_request)
        ir.prominence = 'hidden'
        ir.save!

        session[:user_id] = ir.user.id # bob_smith_user
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('hidden')
    end

    it "should show hidden requests if logged in as super user" do
        ir = info_requests(:fancy_dog_request)
        ir.prominence = 'hidden'
        ir.save!

        session[:user_id] = users(:admin_user)
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('show')
    end

    it "should not show requester_only requests if you're not logged in" do
        ir = info_requests(:fancy_dog_request)
        ir.prominence = 'requester_only'
        ir.save!

        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('hidden')
    end

    it "should show requester_only requests to requester and admin if logged in" do
        ir = info_requests(:fancy_dog_request)
        ir.prominence = 'requester_only'
        ir.save!

        session[:user_id] = users(:silly_name_user).id
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('hidden')

        session[:user_id] = ir.user.id # bob_smith_user
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('show')

        session[:user_id] = users(:admin_user).id
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('show')

    end

    it "should not download attachments if hidden" do
        ir = info_requests(:fancy_dog_request) 
        ir.prominence = 'hidden'
        ir.save!
        receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email)

        get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2
        response.content_type.should == "text/html"
        response.should_not have_text(/Second hello/)        
        response.should render_template('request/hidden')
        get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 3
        response.content_type.should == "text/html"
        response.should_not have_text(/First hello/)        
        response.should render_template('request/hidden')
    end

end
 
# XXX 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

describe RequestController, "when creating a new request" do
    integrate_views
    fixtures :info_requests, :outgoing_messages, :public_bodies, :users

    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
        response.should redirect_to(:controller => 'general', :action => 'frontpage')
    end

    it "should redirect to front page if no public body specified, when logged in" do
        session[:user_id] = @user.id
        get :new
        response.should redirect_to(:controller => 'general', :action => 'frontpage')
    end

    it "should accept a public body parameter" do
        get :new, :public_body_id => @body.id
        assigns[:info_request].public_body.should == @body    
        response.should render_template('new')
    end

    it "should give an error and render 'new' template when a summary isn't given" do
        post :new, :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
        assigns[:info_request].errors[:title].should_not be_nil
        response.should render_template('new')
    end

    it "should show preview when input is good" do
        post :new, { :info_request => { :public_body_id => @body.id, 
            :title => "Why is your quango called Geraldine?"},
            :outgoing_message => { :body => "This is a silly letter. It is too short to be interesting." },
            :submitted_new_request => 1, :preview => 1
        }
        response.should render_template('preview')
    end

    it "should allow re-editing of a request" do
        post :new, :info_request => { :public_body_id => @body.id,
            :title => "Why is your quango called Geraldine?"},
            :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"
        response.should 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?"},
            :outgoing_message => { :body => "This is a silly letter. It is too short to be interesting." },
            :submitted_new_request => 1, :preview => 0
        }
        post :new, params
        post_redirect = PostRedirect.get_last_post_redirect
        response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token)
        # post_redirect.post_params.should == params # XXX get this working. there's a : vs '' problem amongst others
    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
        session[:user_id] = @user.id
        post :new, :info_request => { :public_body_id => @body.id, 
            :title => "Why is your quango called Geraldine?"},
            :outgoing_message => { :body => "This is a silly letter. It is too short to be interesting." },
            :submitted_new_request => 1, :preview => 0

        ir_array = InfoRequest.find(:all, :conditions => ["title = ?", "Why is your quango called Geraldine?"])
        ir_array.size.should == 1
        ir = ir_array[0]
        ir.outgoing_messages.size.should == 1
        om = ir.outgoing_messages[0]
        om.body.should == "This is a silly letter. It is too short to be interesting."

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 1
        mail = deliveries[0]
        mail.body.should =~ /This is a silly letter. It is too short to be interesting./
        #STDERR.puts "=====" + mail.body + "======"

        response.should redirect_to(:action => 'show', :url_title => ir.url_title)
    end

    it "should give an error if the same request is submitted twice" do
        session[:user_id] = @user.id

        # We use raw_body here, so white space is the same
        post :new, :info_request => { :public_body_id => info_requests(:fancy_dog_request).public_body_id, 
            :title => info_requests(:fancy_dog_request).title },
            :outgoing_message => { :body => info_requests(:fancy_dog_request).outgoing_messages[0].raw_body},
            :submitted_new_request => 1, :preview => 0, :mouse_house => 1
        response.should render_template('new')
    end

    it "should give an error if the same request is submitted twice with extra whitespace in the body" do
        # This only works for PostgreSQL databases which have regexp_replace -
        # see model method InfoRequest.find_by_existing_request for more info
        if ActiveRecord::Base.connection.adapter_name == "PostgreSQL"
            session[:user_id] = @user.id

            post :new, :info_request => { :public_body_id => info_requests(:fancy_dog_request).public_body_id, 
                :title => info_requests(:fancy_dog_request).title },
                :outgoing_message => { :body => "\n" + info_requests(:fancy_dog_request).outgoing_messages[0].body + " "},
                :submitted_new_request => 1, :preview => 0, :mouse_house => 1
            response.should render_template('new')
        end
    end

    it "should let you submit another request with the same title" do
        session[:user_id] = @user.id

        post :new, :info_request => { :public_body_id => @body.id, 
            :title => "Why is your quango called Geraldine?"},
            :outgoing_message => { :body => "This is a silly letter. It is too short to be interesting." },
            :submitted_new_request => 1, :preview => 0

        post :new, :info_request => { :public_body_id => @body.id, 
            :title => "Why is your quango called Geraldine?"},
            :outgoing_message => { :body => "This is a sensible letter. It is too long to be boring." },
            :submitted_new_request => 1, :preview => 0

        ir_array = InfoRequest.find(:all, :conditions => ["title = ?", "Why is your quango called Geraldine?"], :order => "id")
        ir_array.size.should == 2

        ir = ir_array[0]
        ir2 = ir_array[1]

        ir.url_title.should_not == ir2.url_title

        response.should redirect_to(:action => 'show', :url_title => ir2.url_title)
    end

end

# These go with the previous set, but use mocks instead of fixtures. 
# TODO harmonise these
describe RequestController, "when making a new request" do

    before do
        @user = mock_model(User, :id => 3481, :name => 'Testy')
        @user.stub!(:get_undescribed_requests).and_return([])
        @user.stub!(:can_leave_requests_undescribed?).and_return(false)
        @user.stub!(:can_file_requests?).and_return(true)
        User.stub!(:find).and_return(@user)

        @body = mock_model(PublicBody, :id => 314, :eir_only? => false, :is_requestable? => true, :name => "Test Quango")
        PublicBody.stub!(:find).and_return(@body)
    end

    it "should allow you to have one undescribed request" do
        @user.stub!(:get_undescribed_requests).and_return([ 1 ])
        @user.stub!(:can_leave_requests_undescribed?).and_return(false)
        session[:user_id] = @user.id
        get :new, :public_body_id => @body.id
        response.should render_template('new')
    end

    it "should fail if more than one request undescribed" do
        @user.stub!(:get_undescribed_requests).and_return([ 1, 2 ])
        @user.stub!(:can_leave_requests_undescribed?).and_return(false)
        session[:user_id] = @user.id
        get :new, :public_body_id => @body.id
        response.should render_template('new_please_describe')
    end

    it "should allow you if more than one request undescribed but are allowed to leave requests undescribed" do
        @user.stub!(:get_undescribed_requests).and_return([ 1, 2 ])
        @user.stub!(:can_leave_requests_undescribed?).and_return(true)
        session[:user_id] = @user.id
        get :new, :public_body_id => @body.id
        response.should render_template('new')
    end

    it "should fail if user is banned" do
        @user.stub!(:can_file_requests?).and_return(false)
        @user.should_receive(:can_fail_html).and_return('FAIL!')
        session[:user_id] = @user.id
        get :new, :public_body_id => @body.id
        response.should render_template('user/banned')
    end

end

describe RequestController, "when viewing an individual response for reply/followup" do
    integrate_views
    fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views
  
    it "should ask for login if you are logged in as wrong person" do
        session[:user_id] = users(:silly_name_user).id
        get :show_response, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message)
        response.should render_template('user/wrong_user')
    end

    it "should show the response if you are logged in as right person" do
        session[:user_id] = users(:bob_smith_user).id
        get :show_response, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message)
        response.should render_template('show_response')
    end

    it "should not show individual responses if request hidden, even if request owner" do
        ir = info_requests(:fancy_dog_request) 
        ir.prominence = 'hidden'
        ir.save!

        session[:user_id] = users(:bob_smith_user).id
        get :show_response, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message)
        response.should render_template('request/hidden')
    end
end

describe RequestController, "when classifying an information request" do

    fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views

    before do 
        @dog_request = info_requests(:fancy_dog_request)
        @dog_request.stub!(:is_old_unclassified?).and_return(false)
        InfoRequest.stub!(:find).and_return(@dog_request)
    end

    def post_status(status)
        post :describe_state, :incoming_message => { :described_state => status }, 
                              :id => @dog_request.id, 
                              :last_info_request_event_id => @dog_request.last_event_id_needing_description, 
                              :submitted_describe_state => 1
    end

    it "should require login" do
        post_status('rejected')
        post_redirect = PostRedirect.get_last_post_redirect
        response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token)
    end

    it 'should ask whether the request is old and unclassified' do 
        @dog_request.should_receive(:is_old_unclassified?)
        post_status('rejected')
    end
    
    it "should not classify the request if logged in as the wrong user" do
        session[:user_id] = users(:silly_name_user).id
        post_status('rejected')
        response.should render_template('user/wrong_user')
    end
    
    describe 'when the request is old and unclassified' do 
        
        before do 
            @dog_request.stub!(:is_old_unclassified?).and_return(true)
            RequestMailer.stub!(:deliver_old_unclassified_updated)
        end
        
        describe 'when the user is not logged in' do 
            
            it 'should require login' do 
                session[:user_id] = nil
                post_status('rejected')
                post_redirect = PostRedirect.get_last_post_redirect
                response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token)
            end
            
        end
        
        describe 'when the user is logged in as a different user' do 
            
            before do
                @other_user = mock_model(User)
                session[:user_id] = users(:silly_name_user).id
            end
            
            it 'should classify the request' do
                @dog_request.stub!(:calculate_status).and_return('rejected') 
                @dog_request.should_receive(:set_described_state).with('rejected')
                post_status('rejected')
            end
        
            it 'should log a status update event' do 
                expected_params = {:user_id => users(:silly_name_user).id, 
                                   :old_described_state => 'waiting_response', 
                                   :described_state => 'rejected'}
                @dog_request.should_receive(:log_event).with("status_update", expected_params)
                post_status('rejected')
            end
            
            it 'should send an email to the requester letting them know someone has updated the status of their request' do 
                RequestMailer.should_receive(:deliver_old_unclassified_updated)
                post_status('rejected')
            end
            
            it 'should redirect to the request page' do 
                post_status('rejected')
                response.should redirect_to(:action => 'show', :controller => 'request', :url_title => @dog_request.url_title)
            end
            
            it 'should show a message thanking the user for a good deed' do 
                post_status('rejected')
                flash[:notice].should == 'Thank you for updating this request!'
            end
            
        end
    end
    
    describe 'when logged in as an admin user who is not the actual requester' do 
    
        before do 
            @admin_user = users(:admin_user)
            session[:user_id] = @admin_user.id
            @dog_request = info_requests(:fancy_dog_request)
            InfoRequest.stub!(:find).and_return(@dog_request)
        end

        it 'should update the status of the request' do 
            @dog_request.stub!(:calculate_status).and_return('rejected')
            @dog_request.should_receive(:set_described_state).with('rejected')
            post_status('rejected')
        end
        
        it 'should log a status update event' do 
            expected_params = {:user_id => @admin_user.id, 
                               :old_described_state => 'waiting_response', 
                               :described_state => 'rejected'}
            @dog_request.should_receive(:log_event).with("status_update", expected_params)
            post_status('rejected')
        end

        it 'should send an email to the requester letting them know someone has updated the status of their request' do 
            RequestMailer.should_receive(:deliver_old_unclassified_updated)
            post_status('rejected')
        end

        it 'should redirect to the request page' do 
            post_status('rejected')
            response.should redirect_to(:action => 'show', :controller => 'request', :url_title => @dog_request.url_title)
        end

        it 'should show a message thanking the user for a good deed' do 
            post_status('rejected')
            flash[:notice].should == 'Thank you for updating this request!'
        end
     end

    describe 'when logged in as an admin user who is also the actual requester' do 
    
        before do 
            @admin_user = users(:admin_user)
            session[:user_id] = @admin_user.id
            @dog_request = info_requests(:fancy_dog_request)
            @dog_request.user = @admin_user
            @dog_request.save!
            InfoRequest.stub!(:find).and_return(@dog_request)
        end

        it 'should update the status of the request' do 
            @dog_request.stub!(:calculate_status).and_return('rejected')
            @dog_request.should_receive(:set_described_state).with('rejected')
            post_status('rejected')
        end
       
        it 'should not log a status update event' do 
            @dog_request.should_not_receive(:log_event)
            post_status('rejected')
        end

        it 'should not send an email to the requester letting them know someone has updated the status of their request' do 
            RequestMailer.should_not_receive(:deliver_old_unclassified_updated)
            post_status('rejected')
        end
 
        it 'should say it is showing advice as to what to do next' do 
            post_status('rejected')
            flash[:notice].should match(/Here is what to do now/) 
        end
        
        it 'should redirect to the unhappy page' do 
            post_status('rejected')
            response.should redirect_to(:controller => 'help', :action => 'unhappy', :url_title => @dog_request.url_title)
        end

    end
 
    describe 'when logged in as the requestor' do 
    
        before do 
            @request_owner = users(:bob_smith_user)
            session[:user_id] = @request_owner.id
            @dog_request.awaiting_description.should == true
        end
        
        it "should successfully classify response if logged in as user controlling request" do
            post_status('rejected')
            response.should redirect_to(:controller => 'help', :action => 'unhappy', :url_title => @dog_request.url_title)
            @dog_request.reload
            @dog_request.awaiting_description.should == false
            @dog_request.described_state.should == 'rejected'
            @dog_request.get_last_response_event.should == info_request_events(:useless_incoming_message_event)
            @dog_request.get_last_response_event.calculated_state.should == 'rejected'
        end

        it 'should not log a status update event' do 
            @dog_request.should_not_receive(:log_event)
            post_status('rejected')
        end
        
        it 'should not send an email to the requester letting them know someone has updated the status of their request' do 
            RequestMailer.should_not_receive(:deliver_old_unclassified_updated)
            post_status('rejected')
        end
        
        it "should send email when classified as requires_admin" do
            post :describe_state, :incoming_message => { :described_state => "requires_admin" }, :id => @dog_request.id, :incoming_message_id => incoming_messages(:useless_incoming_message), :last_info_request_event_id => @dog_request.last_event_id_needing_description, :submitted_describe_state => 1
            response.should redirect_to(:controller => 'help', :action => 'contact')

            @dog_request.reload
            @dog_request.awaiting_description.should == false
            @dog_request.described_state.should == 'requires_admin'
            @dog_request.get_last_response_event.calculated_state.should == 'requires_admin'

            deliveries = ActionMailer::Base.deliveries
            deliveries.size.should == 1
            mail = deliveries[0]
            mail.body.should =~ /as needing admin/
            mail.from_addrs.to_s.should == @request_owner.name_and_email
        end

        it 'should say it is showing advice as to what to do next' do 
            post_status('rejected')
            flash[:notice].should match(/Here is what to do now/) 
        end
        
        it 'should redirect to the unhappy page' do 
            post_status('rejected')
            response.should redirect_to(:controller => 'help', :action => 'unhappy', :url_title => @dog_request.url_title)
        end
    end
    
    describe 'when redirecting after a successful status update by the request owner' do 
        
        before do 
            @request_owner = users(:bob_smith_user)
            session[:user_id] = @request_owner.id
            @dog_request = info_requests(:fancy_dog_request)
            InfoRequest.stub!(:find).and_return(@dog_request)
        end

        def request_url
            "request/#{@dog_request.url_title}"
        end

        def unhappy_url
            "help/unhappy/#{@dog_request.url_title}"
        end
         
        def expect_redirect(status, redirect_path)
            post_status(status)
            response.should redirect_to("http://test.host/#{redirect_path}")
        end
        
        it 'should redirect to the "request url" with a message in the right tense when status is updated to "waiting response" and the response is not overdue' do
            @dog_request.stub!(:date_response_required_by).and_return(Time.now.to_date+1)
            @dog_request.stub!(:date_very_overdue_after).and_return(Time.now.to_date+40)

            expect_redirect("waiting_response", "request/#{@dog_request.url_title}")
            flash[:notice].should match(/should get a response/)
        end
    
        it 'should redirect to the "request url" with a message in the right tense when status is updated to "waiting response" and the response is overdue' do 
            @dog_request.stub!(:date_response_required_by).and_return(Time.now.to_date-1)
            @dog_request.stub!(:date_very_overdue_after).and_return(Time.now.to_date+40)
            expect_redirect('waiting_response', request_url)
            flash[:notice].should match(/should have got a response/)
        end

        it 'should redirect to the "request url" with a message in the right tense when status is updated to "waiting response" and the response is overdue' do 
            @dog_request.stub!(:date_response_required_by).and_return(Time.now.to_date-2)
            @dog_request.stub!(:date_very_overdue_after).and_return(Time.now.to_date-1)
            expect_redirect('waiting_response', unhappy_url)
            flash[:notice].should match(/is long overdue/)
        end
         
        it 'should redirect to the "request url" when status is updated to "not held"' do 
            expect_redirect('not_held', request_url)
        end
        
        it 'should redirect to the "request url" when status is updated to "successful"' do 
            expect_redirect('successful', request_url)
        end
        
        it 'should redirect to the "unhappy url" when status is updated to "rejected"' do 
            expect_redirect('rejected', "help/unhappy/#{@dog_request.url_title}")
        end
        
        it 'should redirect to the "unhappy url" when status is updated to "partially successful"' do 
            expect_redirect('partially_successful', "help/unhappy/#{@dog_request.url_title}")
        end
        
        it 'should redirect to the "response url" when status is updated to "waiting clarification" and there is a last response' do 
            incoming_message = mock_model(IncomingMessage)
            @dog_request.stub!(:get_last_response).and_return(incoming_message)
            expect_redirect('waiting_clarification', "request/#{@dog_request.id}/response/#{incoming_message.id}")
        end
        
        it 'should redirect to the "response no followup url" when status is updated to "waiting clarification" and there are no events needing description' do 
            @dog_request.stub!(:get_last_response).and_return(nil)
            expect_redirect('waiting_clarification', "request/#{@dog_request.id}/response")
        end

        it 'should redirect to the "respond to last url" when status is updated to "gone postal"' do 
            expect_redirect('gone_postal', "request/#{@dog_request.id}/response/#{@dog_request.get_last_response.id}?gone_postal=1")
        end
        
        it 'should redirect to the "request url" when status is updated to "internal review"' do 
            expect_redirect('internal_review', request_url)
        end
        
        it 'should redirect to the "help general url" when status is updated to "requires admin"' do 
            expect_redirect('requires_admin', "help/contact")
        end
        
        it 'should redirect to the "help general url" when status is updated to "error message"' do 
            expect_redirect('error_message', "help/contact")
        end
        
        it 'should redirect to the "respond to last url url" when status is updated to "user_withdrawn"' do 
            expect_redirect('user_withdrawn', "request/#{@dog_request.id}/response/#{@dog_request.get_last_response.id}")
        end
         
    end
end

describe RequestController, "when sending a followup message" do
    integrate_views
    fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views
  
    it "should require login" do
        post :show_response, :outgoing_message => { :body => "What a useless response! You suck.", :what_doing => 'normal_sort' }, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1
        post_redirect = PostRedirect.get_last_post_redirect
        response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token)
    end

    it "should not let you if you are logged in as the wrong user" do
        session[:user_id] = users(:silly_name_user).id
        post :show_response, :outgoing_message => { :body => "What a useless response! You suck.", :what_doing => 'normal_sort' }, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1
        response.should render_template('user/wrong_user')
    end

    it "should give an error and render 'show_response' template when a body isn't given" do
        session[:user_id] = users(:bob_smith_user).id
        post :show_response, :outgoing_message => { :body => "", :what_doing => 'normal_sort'}, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1

        # XXX how do I check the error message here?
        response.should render_template('show_response')
    end

    it "should show preview when input is good" do
        session[:user_id] = users(:bob_smith_user).id
        post :show_response, :outgoing_message => { :body => "What a useless response! You suck.", :what_doing => 'normal_sort'}, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1, :preview => 1
        response.should render_template('followup_preview')
    end

    it "should allow re-editing of a preview" do
        session[:user_id] = users(:bob_smith_user).id
        post :show_response, :outgoing_message => { :body => "What a useless response! You suck.", :what_doing => 'normal_sort'}, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1, :preview => 0, :reedit => "Re-edit this request"
        response.should render_template('show_response')
    end
 
    it "should send the follow up message if you are the right user" do
        # fake that this is a clarification
        info_requests(:fancy_dog_request).set_described_state('waiting_clarification')
        info_requests(:fancy_dog_request).described_state.should == 'waiting_clarification'
        info_requests(:fancy_dog_request).get_last_response_event.calculated_state.should == 'waiting_clarification'

        # make the followup
        session[:user_id] = users(:bob_smith_user).id
        post :show_response, :outgoing_message => { :body => "What a useless response! You suck.", :what_doing => 'normal_sort' }, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1

        # check it worked
        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 1
        mail = deliveries[0]
        mail.body.should =~ /What a useless response! You suck./
        mail.to_addrs.to_s.should == "FOI Person <foiperson@localhost>"
        #STDERR.puts "=====" + mail.body + "======"

        response.should redirect_to(:action => 'show', :url_title => info_requests(:fancy_dog_request).url_title)

        # and that the status changed
        info_requests(:fancy_dog_request).reload
        info_requests(:fancy_dog_request).described_state.should == 'waiting_response'
        info_requests(:fancy_dog_request).get_last_response_event.calculated_state.should == 'waiting_clarification'
    end

    it "should give an error if the same followup is submitted twice" do
        session[:user_id] = users(:bob_smith_user).id

        # make the followup once
        post :show_response, :outgoing_message => { :body => "Stop repeating yourself!", :what_doing => 'normal_sort' }, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1
        response.should redirect_to(:action => 'show', :url_title => info_requests(:fancy_dog_request).url_title)
        
        # second time should give an error
        post :show_response, :outgoing_message => { :body => "Stop repeating yourself!", :what_doing => 'normal_sort' }, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1
        # XXX how do I check the error message here?
        response.should render_template('show_response')
    end

end

# XXX Stuff after here should probably be in request_mailer_spec.rb - but then
# it can't check the URLs in the emails I don't think, ugh.

describe RequestController, "sending overdue request alerts" do
    integrate_views
    fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views
 
    it "should send an overdue alert mail to creators of overdue requests" do
        chicken_request = info_requests(:naughty_chicken_request)
        chicken_request.outgoing_messages[0].last_sent_at = Time.now() - 30.days
        chicken_request.outgoing_messages[0].save!

        RequestMailer.alert_overdue_requests

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 1
        mail = deliveries[0]
        mail.body.should =~ /promptly, as normally\s+required by law/
        mail.to_addrs.to_s.should == info_requests(:naughty_chicken_request).user.name_and_email

        mail.body =~ /(http:\/\/.*\/c\/(.*))/
        mail_url = $1
        mail_token = $2

        session[:user_id].should be_nil
        controller.test_code_redirect_by_email_token(mail_token, self) # XXX hack to avoid having to call User controller for email link
        session[:user_id].should == info_requests(:naughty_chicken_request).user.id

        response.should render_template('show_response')
        assigns[:info_request].should == info_requests(:naughty_chicken_request)
    end

    it "should include clause for schools when sending an overdue alert mail to creators of overdue requests" do
        chicken_request = info_requests(:naughty_chicken_request)
        chicken_request.outgoing_messages[0].last_sent_at = Time.now() - 30.days
        chicken_request.outgoing_messages[0].save!

        chicken_request.public_body.tag_string = "school"
        chicken_request.public_body.save!

        RequestMailer.alert_overdue_requests

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 1
        mail = deliveries[0]
        mail.body.should =~ /promptly, as normally\s+required by law during term time/
        mail.to_addrs.to_s.should == info_requests(:naughty_chicken_request).user.name_and_email
    end

    it "should send not actually send the overdue alert if the user is banned" do
        user = info_requests(:naughty_chicken_request).user
        user.ban_text = 'Banned'
        user.save!

        RequestMailer.alert_overdue_requests

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 0
    end

    it "should send a very overdue alert mail to creators of very overdue requests" do
        chicken_request = info_requests(:naughty_chicken_request)
        chicken_request.outgoing_messages[0].last_sent_at = Time.now() - 60.days
        chicken_request.outgoing_messages[0].save!

        RequestMailer.alert_overdue_requests

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 1
        mail = deliveries[0]
        mail.body.should =~ /required by law/
        mail.to_addrs.to_s.should == info_requests(:naughty_chicken_request).user.name_and_email

        mail.body =~ /(http:\/\/.*\/c\/(.*))/
        mail_url = $1
        mail_token = $2

        session[:user_id].should be_nil
        controller.test_code_redirect_by_email_token(mail_token, self) # XXX hack to avoid having to call User controller for email link
        session[:user_id].should == info_requests(:naughty_chicken_request).user.id

        response.should render_template('show_response')
        assigns[:info_request].should == info_requests(:naughty_chicken_request)
    end

end

describe RequestController, "sending unclassified new response reminder alerts" do
    integrate_views
    fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views
 
    it "should send an alert" do
        RequestMailer.alert_new_response_reminders

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 3 # sufficiently late it sends reminders too
        mail = deliveries[0]
        mail.body.should =~ /To let us know/
        mail.to_addrs.to_s.should == info_requests(:fancy_dog_request).user.name_and_email
        mail.body =~ /(http:\/\/.*\/c\/(.*))/
        mail_url = $1
        mail_token = $2

        session[:user_id].should be_nil
        controller.test_code_redirect_by_email_token(mail_token, self) # XXX hack to avoid having to call User controller for email link
        session[:user_id].should == info_requests(:fancy_dog_request).user.id

        response.should render_template('show')
        assigns[:info_request].should == info_requests(:fancy_dog_request)
        # XXX should check anchor tag here :) that it goes to last new response
    end

end

describe RequestController, "clarification required alerts" do
    integrate_views
    fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views
 
    it "should send an alert" do
        ir = info_requests(:fancy_dog_request)
        ir.set_described_state('waiting_clarification')
        # this is pretty horrid, but will do :) need to make it waiting
        # clarification more than 3 days ago for the alerts to go out.
        ActiveRecord::Base.connection.update "update info_requests set updated_at = '" + (Time.now - 5.days).strftime("%Y-%m-%d %H:%M:%S") + "' where id = " + ir.id.to_s
        ir.reload

        RequestMailer.alert_not_clarified_request

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 1
        mail = deliveries[0]
        mail.body.should =~ /asked you to explain/
        mail.to_addrs.to_s.should == info_requests(:fancy_dog_request).user.name_and_email
        mail.body =~ /(http:\/\/.*\/c\/(.*))/
        mail_url = $1
        mail_token = $2

        session[:user_id].should be_nil
        controller.test_code_redirect_by_email_token(mail_token, self) # XXX hack to avoid having to call User controller for email link
        session[:user_id].should == info_requests(:fancy_dog_request).user.id

        response.should render_template('show_response')
        assigns[:info_request].should == info_requests(:fancy_dog_request)
    end

    it "should not send an alert if you are banned" do
        ir = info_requests(:fancy_dog_request)
        ir.set_described_state('waiting_clarification')

        ir.user.ban_text = 'Banned'
        ir.user.save!

        # this is pretty horrid, but will do :) need to make it waiting
        # clarification more than 3 days ago for the alerts to go out.
        ActiveRecord::Base.connection.update "update info_requests set updated_at = '" + (Time.now - 5.days).strftime("%Y-%m-%d %H:%M:%S") + "' where id = " + ir.id.to_s
        ir.reload

        RequestMailer.alert_not_clarified_request

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 0
    end

end

describe RequestController, "comment alerts" do
    integrate_views
    fixtures :info_requests, :info_request_events, :public_bodies, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views
 
    it "should send an alert (once and once only)" do
        # delete ficture comment and make new one, so is in last month (as
        # alerts are only for comments in last month, see
        # RequestMailer.alert_comment_on_request)
        existing_comment = info_requests(:fancy_dog_request).comments[0]
        existing_comment.info_request_events[0].destroy
        existing_comment.destroy
        new_comment = info_requests(:fancy_dog_request).add_comment('I really love making annotations.', users(:silly_name_user))

        # send comment alert
        RequestMailer.alert_comment_on_request
        deliveries = ActionMailer::Base.deliveries
        mail = deliveries[0]
        mail.body.should =~ /has annotated your/
        mail.to_addrs.to_s.should == info_requests(:fancy_dog_request).user.name_and_email
        mail.body =~ /(http:\/\/.*)/
        mail_url = $1

        # XXX check mail_url here somehow, can't call comment_url like this:
        # mail_url.should == comment_url(comments(:silly_comment))

        #STDERR.puts mail.body
        
        # check if we send again, no more go out
        deliveries.clear
        RequestMailer.alert_comment_on_request
        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 0
    end

    it "should not send an alert when you comment on your own request" do
        # delete ficture comment and make new one, so is in last month (as
        # alerts are only for comments in last month, see
        # RequestMailer.alert_comment_on_request)
        existing_comment = info_requests(:fancy_dog_request).comments[0]
        existing_comment.info_request_events[0].destroy
        existing_comment.destroy
        new_comment = info_requests(:fancy_dog_request).add_comment('I also love making annotations.', users(:bob_smith_user))

        # try to send comment alert
        RequestMailer.alert_comment_on_request

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 0
    end

    it "should send an alert when there are two new comments" do
        # add two comments - the second one sould be ignored, as is by the user who made the request.
        # the new comment here, will cause the one in the fixture to be picked up as a new comment by alert_comment_on_request also.
        new_comment = info_requests(:fancy_dog_request).add_comment('Not as daft as this one', users(:silly_name_user))
        new_comment = info_requests(:fancy_dog_request).add_comment('Or this one!!!', users(:bob_smith_user))

        RequestMailer.alert_comment_on_request

        deliveries = ActionMailer::Base.deliveries
        deliveries.size.should == 1
        mail = deliveries[0]
        mail.body.should =~ /There are 2 new annotations/
        mail.to_addrs.to_s.should == info_requests(:fancy_dog_request).user.name_and_email
        mail.body =~ /(http:\/\/.*)/
        mail_url = $1

        # XXX check mail_url here somehow, can't call comment_url like this:
        # mail_url.should == comment_url(comments(:silly_comment))

        #STDERR.puts mail.body
    end

end

describe RequestController, "when viewing comments" do
    integrate_views
    fixtures :users

    it "should link to the user who submitted it" do
        session[:user_id] = users(:bob_smith_user).id
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.body.should have_tag("div#comment-1 h2", /Silly.*left an annotation/m) 
        response.body.should_not have_tag("div#comment-1 h2", /You.*left an annotation/m) 
    end

    it "should link to the user who submitted to it, even if it is you" do
        session[:user_id] = users(:silly_name_user).id
        get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.body.should have_tag("div#comment-1 h2", /Silly.*left an annotation/m) 
        response.body.should_not have_tag("div#comment-1 h2", /You.*left an annotation/m) 
    end

end


describe RequestController, "authority uploads a response from the web interface" do
    fixtures :info_requests, :info_request_events, :public_bodies, :users

    before(:all) do
        # domain after the @ is used for authentication of FOI officers, so to test it
        # we need a user which isn't at localhost.
        @normal_user = User.new(:name => "Mr. Normal", :email => "normal-user@flourish.org",  
                                      :password => PostRedirect.generate_random_token)
        @normal_user.save!

        @foi_officer_user = User.new(:name => "The Geraldine Quango", :email => "geraldine-requests@localhost", 
                                      :password => PostRedirect.generate_random_token)
        @foi_officer_user.save!
    end
  
    it "should require login to view the form to upload" do
        @ir = info_requests(:fancy_dog_request) 
        @ir.public_body.is_foi_officer?(@normal_user).should == false
        session[:user_id] = @normal_user.id

        get :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('user/wrong_user')
    end

   it "should let you view upload form if you are an FOI officer" do
        @ir = info_requests(:fancy_dog_request) 
        @ir.public_body.is_foi_officer?(@foi_officer_user).should == true
        session[:user_id] = @foi_officer_user.id

        get :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog'
        response.should render_template('request/upload_response')
    end

    it "should prevent uploads if you are not a requester" do
        @ir = info_requests(:fancy_dog_request) 
        incoming_before = @ir.incoming_messages.size
        session[:user_id] = @normal_user.id

        # post up a photo of the parrot
        parrot_upload = fixture_file_upload('parrot.png','image/png')
        post :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog',
            :body => "Find attached a picture of a parrot",
            :file_1 => parrot_upload,
            :submitted_upload_response => 1
        response.should render_template('user/wrong_user')
    end

    it "should prevent entirely blank uploads" do
        session[:user_id] = @foi_officer_user.id

        post :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog', :body => "", :submitted_upload_response => 1
        response.should render_template('request/upload_response')
        flash[:error].should match(/Please type a message/)
    end

    # How do I test a file upload in rails?
    # http://stackoverflow.com/questions/1178587/how-do-i-test-a-file-upload-in-rails
    it "should let the requester upload a file" do
        @ir = info_requests(:fancy_dog_request) 
        incoming_before = @ir.incoming_messages.size
        session[:user_id] = @foi_officer_user.id

        # post up a photo of the parrot
        parrot_upload = fixture_file_upload('parrot.png','image/png')
        post :upload_response, :url_title => 'why_do_you_have_such_a_fancy_dog',
            :body => "Find attached a picture of a parrot",
            :file_1 => parrot_upload,
            :submitted_upload_response => 1

        response.should redirect_to(:action => 'show', :url_title => 'why_do_you_have_such_a_fancy_dog')
        flash[:notice].should match(/Thank you for responding to this FOI request/)

        # check there is a new attachment
        incoming_after = @ir.incoming_messages.size
        incoming_after.should == incoming_before + 1

        # check new attachment looks vaguely OK
        new_im = @ir.incoming_messages[-1]
        new_im.mail.body.should match(/Find attached a picture of a parrot/)
        attachments = new_im.get_attachments_for_display
        attachments.size.should == 1
        attachments[0].filename.should == "parrot.png"
        attachments[0].display_size.should == "94K"
    end
end