aboutsummaryrefslogtreecommitdiffstats
path: root/spec/integration/download_request_spec.rb
blob: 48b42b11d7fb86a7163a23ae513f7c5c503cee0d (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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/alaveteli_dsl')

describe 'when making a zipfile available' do

    after do
        FileUtils.rm_rf(InfoRequest.download_zip_dir)
    end

    def inspect_zip_download(session, info_request)
        session.get_via_redirect "request/#{info_request.url_title}/download"
        session.response.should be_success
        Tempfile.open('download') do |f|
            f.binmode
            f.write(session.response.body)
            f.flush
            Zip::ZipFile::open(f.path) do |zip|
               yield zip
            end
        end
    end

    def sleep_and_receive_mail(name, info_request)
        # The path of the zip file is based on the hash of the timestamp of the last request
        # in the thread, so we wait for a second to make sure this one will have a different
        # timestamp than the previous.
        sleep 1
        receive_incoming_mail(name, info_request.incoming_email)
    end

    context 'when an html to pdf converter is supplied' do

        before do
            # We want to test the contents of the pdf, and we don't know whether a particular
            # instance will have a working html_to_pdf tool, so just copy the HTML rendered
            # to the PDF file for the purposes of checking it doesn't contain anything that
            # shouldn't be there.
            AlaveteliConfiguration.stub!(:html_to_pdf_command).and_return('/bin/cp')
        end

        context 'when an incoming message is made "requester_only"' do

            it 'should not include the incoming message or attachments in a download of the entire request
                by a non-request owner but should retain them for owner and admin' do

                # Non-owner can download zip with incoming and attachments
                non_owner = login(FactoryGirl.create(:user))
                info_request = FactoryGirl.create(:info_request_with_incoming_attachments)

                inspect_zip_download(non_owner, info_request) do |zip|
                    zip.count.should == 3
                    zip.read('correspondence.pdf').should match('hereisthetext')
                end

                # Admin makes the incoming message requester only
                admin = login(FactoryGirl.create(:admin_user))
                post_data = {:incoming_message => {:prominence => 'requester_only',
                                                   :prominence_reason => 'boring'}}
                admin.put_via_redirect "/en/admin/incoming_messages/#{info_request.incoming_messages.first.id}", post_data
                admin.response.should be_success

                # Admin retains the requester only things
                inspect_zip_download(admin, info_request) do |zip|
                    zip.count.should == 3
                    zip.read('correspondence.pdf').should match('hereisthetext')
                end

                # Zip for non owner is now without requester_only things
                inspect_zip_download(non_owner, info_request) do |zip|
                    zip.count.should == 1
                    correspondence_text = zip.read('correspondence.pdf')
                    correspondence_text.should_not match('hereisthetext')
                    expected_text = "This message has been hidden.\n    boring"
                    correspondence_text.should match(expected_text)
                end

                # Requester retains the requester only things
                owner = login(info_request.user)
                inspect_zip_download(owner, info_request) do |zip|
                    zip.count.should == 3
                    zip.read('correspondence.pdf').should match('hereisthetext')
                end

            end

        end

        context 'when an outgoing message is made "requester_only"' do

            it 'should not include the outgoing message in a download of the entire request
                by a non-request owner but should retain them for owner and admin' do

                # Non-owner can download zip with outgoing
                non_owner = login(FactoryGirl.create(:user))
                info_request = FactoryGirl.create(:info_request)

                inspect_zip_download(non_owner, info_request) do |zip|
                    zip.count.should == 1
                    zip.read('correspondence.pdf').should match('Some information please')
                end

                # Admin makes the incoming message requester only
                admin = login(FactoryGirl.create(:admin_user))
                post_data = {:outgoing_message => {:prominence => 'requester_only',
                                                   :prominence_reason => 'boring',
                                                   :body => 'Some information please'}}
                admin.put_via_redirect "/en/admin/outgoing_messages/#{info_request.outgoing_messages.first.id}", post_data
                admin.response.should be_success

                # Admin retains the requester only things
                inspect_zip_download(admin, info_request) do |zip|
                    zip.count.should == 1
                    zip.read('correspondence.pdf').should match('Some information please')
                end

                # Zip for non owner is now without requester_only things
                inspect_zip_download(non_owner, info_request) do |zip|
                    zip.count.should == 1
                    correspondence_text = zip.read('correspondence.pdf')
                    correspondence_text.should_not match('Some information please')
                    expected_text = "This message has been hidden.\n    boring"
                    correspondence_text.should match(expected_text)
                end

                # Requester retains the requester only things
                owner = login(info_request.user)
                inspect_zip_download(owner, info_request) do |zip|
                    zip.count.should == 1
                    zip.read('correspondence.pdf').should match('Some information please')
                end

            end

        end

    end

    context 'when no html to pdf converter is supplied' do

        before do
            AlaveteliConfiguration.stub!(:html_to_pdf_command).and_return('')
        end

        it "should update the contents of the zipfile when the request changes" do

            info_request = FactoryGirl.create(:info_request_with_incoming)
            request_owner = login(info_request.user)
            inspect_zip_download(request_owner, info_request) do |zip|
                zip.count.should == 1 # just the message
                expected = 'This is a plain-text version of the Freedom of Information request "Example Title"'
                zip.read('correspondence.txt').should match expected
            end

            sleep_and_receive_mail('incoming-request-two-same-name.email', info_request)

            inspect_zip_download(request_owner, info_request) do |zip|
                zip.count.should == 3 # the message plus two "hello-world.txt" files
                zip.read('2_2_hello world.txt').should match('Second hello')
                zip.read('2_3_hello world.txt').should match('First hello')
            end

            sleep_and_receive_mail('incoming-request-attachment-unknown-extension.email', info_request)

            inspect_zip_download(request_owner, info_request) do |zip|
                zip.count.should == 4  # the message plus two "hello-world.txt" files, and the new attachment
                zip.read('3_2_hello.qwglhm').should match('This is an unusual')
            end
        end

        context 'when a request is "requester_only"' do

            before do
                @non_owner = login(FactoryGirl.create(:user))
                @info_request = FactoryGirl.create(:info_request_with_incoming,
                                                   :prominence => 'requester_only')
                @request_owner = login(@info_request.user)
                @admin = login(FactoryGirl.create(:admin_user))
            end


            it 'should allow a download of the request by the request owner and admin only' do
                # Requester can access the zip
                inspect_zip_download(@request_owner, @info_request) do |zip|
                    zip.count.should == 1
                    zip.read('correspondence.txt').should match('hereisthetext')
                end
                # Non-owner can't
                @non_owner.get_via_redirect "request/#{@info_request.url_title}/download"
                @non_owner.response.code.should == '403'
                # Admin can
                inspect_zip_download(@admin, @info_request) do |zip|
                    zip.count.should == 1
                    zip.read('correspondence.txt').should match('hereisthetext')
                end
            end
        end

        context 'when a request is "hidden"' do

            it 'should not allow a download of the request by an admin only' do
                @non_owner = login(FactoryGirl.create(:user))
                @info_request = FactoryGirl.create(:info_request_with_incoming,
                                                   :prominence => 'hidden')
                @request_owner = login(@info_request.user)
                @admin = login(FactoryGirl.create(:admin_user))

                # Requester can't access the zip
                @request_owner.get_via_redirect "request/#{@info_request.url_title}/download"
                @request_owner.response.code.should == '403'
                # Non-owner can't
                @non_owner.get_via_redirect "request/#{@info_request.url_title}/download"
                @non_owner.response.code.should == '403'
                # Admin can
                inspect_zip_download(@admin, @info_request) do |zip|
                    zip.count.should == 1
                    zip.read('correspondence.txt').should match('hereisthetext')
                end
            end

        end

        context 'when an incoming message is made "requester_only"' do

            it 'should not include the incoming message or attachments in a download of the entire request
                by a non-request owner but should retain them for owner and admin' do

                # Non-owner can download zip with outgoing
                non_owner = login(FactoryGirl.create(:user))
                info_request = FactoryGirl.create(:info_request_with_incoming_attachments)

                inspect_zip_download(non_owner, info_request) do |zip|
                    zip.count.should == 3
                    zip.read('correspondence.txt').should match('hereisthetext')
                end

                # Admin makes the incoming message requester only
                admin = login(FactoryGirl.create(:admin_user))
                post_data = {:incoming_message => {:prominence => 'requester_only',
                                                   :prominence_reason => 'boring'}}
                admin.put_via_redirect "/en/admin/incoming_messages/#{info_request.incoming_messages.first.id}", post_data
                admin.response.should be_success

                # Admin retains the requester only things
                inspect_zip_download(admin, info_request) do |zip|
                    zip.count.should == 3
                    zip.read('correspondence.txt').should match('hereisthetext')
                end

                # Zip for non owner is now without requester_only things
                inspect_zip_download(non_owner, info_request) do |zip|
                    zip.count.should == 1
                    correspondence_text = zip.read('correspondence.txt')
                    correspondence_text.should_not match('hereisthetext')
                    expected_text = 'This message has been hidden. boring'
                    correspondence_text.should match(expected_text)
                end

                # Requester retains the requester only things
                owner = login(info_request.user)
                inspect_zip_download(owner, info_request) do |zip|
                    zip.count.should == 3
                    zip.read('correspondence.txt').should match('hereisthetext')
                end

            end

        end

        context 'when an outgoing message is made "requester_only"' do

            it 'should not include the outgoing message in a download of the entire request
                by a non-request owner but should retain them for owner and admin' do

                # Non-owner can download zip with incoming and attachments
                non_owner = login(FactoryGirl.create(:user))
                info_request = FactoryGirl.create(:info_request)

                inspect_zip_download(non_owner, info_request) do |zip|
                    zip.count.should == 1
                    zip.read('correspondence.txt').should match('Some information please')
                end

                # Admin makes the incoming message requester only
                admin = login(FactoryGirl.create(:admin_user))
                post_data = {:outgoing_message => {:prominence => 'requester_only',
                                                   :prominence_reason => 'boring',
                                                   :body => 'Some information please'}}
                admin.put_via_redirect "/en/admin/outgoing_messages/#{info_request.outgoing_messages.first.id}", post_data
                admin.response.should be_success

                # Admin retains the requester only things
                inspect_zip_download(admin, info_request) do |zip|
                    zip.count.should == 1
                    zip.read('correspondence.txt').should match('Some information please')
                end

                # Zip for non owner is now without requester_only things
                inspect_zip_download(non_owner, info_request) do |zip|
                    zip.count.should == 1
                    correspondence_text = zip.read('correspondence.txt')
                    correspondence_text.should_not match('Some information please')
                    expected_text = 'This message has been hidden. boring'
                    correspondence_text.should match(expected_text)
                end

                # Requester retains the requester only things
                owner = login(info_request.user)
                inspect_zip_download(owner, info_request) do |zip|
                    zip.count.should == 1
                    zip.read('correspondence.txt').should match('Some information please')
                end

            end

        end

        it 'should successfully make a zipfile for an external request' do
            external_request = FactoryGirl.create(:external_request)
            user = login(FactoryGirl.create(:user))
            inspect_zip_download(user, external_request){ |zip|  zip.count.should == 1 }
        end
    end

end