aboutsummaryrefslogtreecommitdiffstats
path: root/spec/integration/download_request_spec.rb
blob: dd492e42a55347290e9b4e7be702bec1e0530c2b (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
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

    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) 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 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_hello world.txt').should match('Second hello')
                zip.read('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('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 == '410'
                # 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 made "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 == '410'
                # Non-owner can't
                @non_owner.get_via_redirect "request/#{@info_request.url_title}/download"
                @non_owner.response.code.should == '410'
                # 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' 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.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.post_via_redirect "/en/admin/incoming/update/#{info_request.incoming_messages.first.id}", post_data
                admin.response.should be_success

                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

            end

            it 'should include the incoming message and attachments in a download of the entire request
                by the owner'

        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