aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/admin_request_controller_spec.rb
blob: 4eb4639635b408687b1db0ef37aad12c68daa8f0 (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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe AdminRequestController, "when administering requests" do
    render_views
    before { basic_auth_login @request }

    before(:each) do
        load_raw_emails_data
    end

    it "shows the index/list page" do
        get :index
    end

    it "shows a public body" do
        get :show, :id => info_requests(:fancy_dog_request)
    end

    it 'shows an external public body with no username' do
        get :show, :id => info_requests(:anonymous_external_request)
        response.should be_success
    end

    it "edits a public body" do
        get :edit, :id => info_requests(:fancy_dog_request)
    end

    it "saves edits to a request" do
        info_requests(:fancy_dog_request).title.should == "Why do you have & such a fancy dog?"
        post :update, { :id => info_requests(:fancy_dog_request),
                        :info_request => { :title => "Renamed",
                                           :prominence => "normal",
                                           :described_state => "waiting_response",
                                           :awaiting_description => false,
                                           :allow_new_responses_from => 'anybody',
                                           :handle_rejected_responses => 'bounce' } }
        request.flash[:notice].should include('successful')
        ir = InfoRequest.find(info_requests(:fancy_dog_request).id)
        ir.title.should == "Renamed"
    end

    it 'expires the request cache when saving edits to it' do
        info_request = info_requests(:fancy_dog_request)
        @controller.should_receive(:expire_for_request).with(info_request)
        post :update, { :id => info_request,
                        :info_request => { :title => "Renamed",
                                           :prominence => "normal",
                                           :described_state => "waiting_response",
                                           :awaiting_description => false,
                                           :allow_new_responses_from => 'anybody',
                                           :handle_rejected_responses => 'bounce' } }

    end

    describe 'when fully destroying a request' do

        it 'expires the file cache for that request' do
            info_request = info_requests(:badger_request)
            @controller.should_receive(:expire_for_request).with(info_request)
            get :destroy, { :id => info_request }
        end

        it 'uses a different flash message to avoid trying to fetch a non existent user record' do
            info_request = info_requests(:external_request)
            post :destroy, { :id => info_request.id }
            request.flash[:notice].should include('external')
        end

    end

end

describe AdminRequestController, "when administering the holding pen" do
    render_views
    before(:each) do
        basic_auth_login @request
        load_raw_emails_data
    end

    it "shows a suitable default 'your email has been hidden' message" do
        ir = info_requests(:fancy_dog_request)
        get :show, :id => ir.id
        assigns[:request_hidden_user_explanation].should include(ir.user.name)
        assigns[:request_hidden_user_explanation].should include("vexatious")
        get :show, :id => ir.id, :reason => "not_foi"
        assigns[:request_hidden_user_explanation].should_not include("vexatious")
        assigns[:request_hidden_user_explanation].should include("not a valid FOI")
    end

    describe 'when hiding requests' do

        it "hides requests and sends a notification email that it has done so" do
            ir = info_requests(:fancy_dog_request)
            post :hide, :id => ir.id, :explanation => "Foo", :reason => "vexatious"
            ir.reload
            ir.prominence.should == "requester_only"
            ir.described_state.should == "vexatious"
            deliveries = ActionMailer::Base.deliveries
            deliveries.size.should == 1
            mail = deliveries[0]
            mail.body.should =~ /Foo/
        end

        it 'expires the file cache for the request' do
            ir = info_requests(:fancy_dog_request)
            @controller.should_receive(:expire_for_request).with(ir)
            post :hide, :id => ir.id, :explanation => "Foo", :reason => "vexatious"
        end

        describe 'when hiding an external request' do

            before do
                @controller.stub!(:expire_for_request)
                @info_request = mock_model(InfoRequest, :prominence= => nil,
                                                        :log_event => nil,
                                                        :set_described_state => nil,
                                                        :save! => nil,
                                                        :user => nil,
                                                        :user_name => 'External User',
                                                        :is_external? => true)
                InfoRequest.stub!(:find).with(@info_request.id.to_s).and_return(@info_request)
                @default_params = { :id => @info_request.id,
                                    :explanation => 'Foo',
                                    :reason => 'vexatious' }
            end

            def make_request(params=@default_params)
                post :hide, params
            end

            it 'should redirect the the admin page for the request' do
                make_request
                response.should redirect_to(:controller => 'admin_request',
                                            :action => 'show',
                                            :id => @info_request.id)
            end

            it 'should set the request prominence to "requester_only"' do
                @info_request.should_receive(:prominence=).with('requester_only')
                @info_request.should_receive(:save!)
                make_request
            end

            it 'should not send a notification email' do
                ContactMailer.should_not_receive(:from_admin_message)
                make_request
            end

            it 'should add a notice to the flash saying that the request has been hidden' do
                make_request
                request.flash[:notice].should == "This external request has been hidden"
            end

            it 'should expire the file cache for the request' do
                @controller.should_receive(:expire_for_request)
                make_request
            end
        end

    end

end