aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/info_request_spec.rb
blob: 00ae218da5c1ae014703327d9c147f825c61b974 (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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe InfoRequest do 

    describe "making up the URL title" do 
        before do
            @info_request = InfoRequest.new
        end

        it 'should remove spaces, and make lower case' do 
            @info_request.title = 'Something True'
            @info_request.url_title.should == 'something_true'
        end

        it 'should not allow a numeric title' do 
            @info_request.title = '1234'
            @info_request.url_title.should == 'request'
        end
    end
     
    describe "when asked for the last event id that needs description" do 
    
        before do
            @info_request = InfoRequest.new
        end
        
        it 'should return the last undescribed event id if there is one' do 
            last_mock_event = mock_model(InfoRequestEvent)
            other_mock_event = mock_model(InfoRequestEvent)
            @info_request.stub!(:events_needing_description).and_return([other_mock_event, last_mock_event])
            @info_request.last_event_id_needing_description.should == last_mock_event.id
        end 
        
        it 'should return zero if there are no undescribed events' do
            @info_request.stub!(:events_needing_description).and_return([])
            @info_request.last_event_id_needing_description.should == 0
        end
        
    end
    
    describe " when emailing" do
    
        fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users

        before do
            @info_request = info_requests(:fancy_dog_request)
        end

        it "should have a valid incoming email" do
            @info_request.incoming_email.should_not be_nil
        end

        it "should have a sensible incoming name and email" do
            @info_request.incoming_name_and_email.should == "Bob Smith <" + @info_request.incoming_email + ">"
        end

        it "should have a sensible recipient name and email" do
            @info_request.recipient_name_and_email.should == "FOI requests at TGQ <geraldine-requests@localhost>"
        end

        it "should recognise its own incoming email" do
            incoming_email = @info_request.incoming_email
            found_info_request = InfoRequest.find_by_incoming_email(incoming_email)
            found_info_request.should == (@info_request)
        end

        it "should recognise its own incoming email with some capitalisation" do
            incoming_email = @info_request.incoming_email.gsub(/request/, "Request")
            found_info_request = InfoRequest.find_by_incoming_email(incoming_email)
            found_info_request.should == (@info_request)
        end

        it "should recognise its own incoming email with quotes" do
            incoming_email = "'" + @info_request.incoming_email + "'"
            found_info_request = InfoRequest.find_by_incoming_email(incoming_email)
            found_info_request.should == (@info_request)
        end

        it "should recognise l and 1 as the same in incoming emails" do
            # Make info request with a 1 in it
            while true
                ir = InfoRequest.new(:title => "testing", :public_body => public_bodies(:geraldine_public_body),
                    :user => users(:bob_smith_user))
                ir.save!
                hash_part = ir.incoming_email.match(/-[0-9a-f]+@/)[0]
                break if hash_part.match(/1/)
            end
        
            # Make email with a 1 in the hash part changed to l
            test_email = ir.incoming_email
            new_hash_part = hash_part.gsub(/1/, "l")
            test_email.gsub!(hash_part, new_hash_part)

            # Try and find with an l
            found_info_request = InfoRequest.find_by_incoming_email(test_email)
            found_info_request.should == (ir)
        end

        it "should recognise old style request-bounce- addresses" do
            incoming_email = @info_request.magic_email("request-bounce-")
            found_info_request = InfoRequest.find_by_incoming_email(incoming_email)
            found_info_request.should == (@info_request)
        end

        it "should return nil when receiving email for a deleted request" do
            deleted_request_address = InfoRequest.magic_email_for_id("request-", 98765)  
            found_info_request = InfoRequest.find_by_incoming_email(deleted_request_address)
            found_info_request.should be_nil
        end

        it "should cope with indexing after item is deleted" do
            rebuild_xapian_index

            # delete event from underneath indexing; shouldn't cause error
            info_request_events(:useless_incoming_message_event).save!
            info_request_events(:useless_incoming_message_event).destroy
            update_xapian_index
        end

    end 

    describe "when calculating the status" do
        fixtures :info_requests, :info_request_events, :holidays, :public_bodies, :public_body_translations

        before do
            @ir = info_requests(:naughty_chicken_request)
        end

        it "has expected sent date" do
            @ir.last_event_forming_initial_request.outgoing_message.last_sent_at.strftime("%F").should == '2007-10-14'
        end

        it "has correct due date" do
            @ir.date_response_required_by.strftime("%F").should == '2007-11-09'
        end

        it "has correct very overdue after date" do
            @ir.date_very_overdue_after.strftime("%F").should == '2007-12-10'
        end

        it "isn't overdue on due date (20 working days after request sent)" do
            Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59)) 
            @ir.calculate_status.should == 'waiting_response'
        end

        it "is overdue a day after due date (20 working days after request sent)" do
            Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01)) 
            @ir.calculate_status.should == 'waiting_response_overdue'
        end

        it "is still overdue 40 working days after request sent" do
            Time.stub!(:now).and_return(Time.utc(2007, 12, 10, 23, 59)) 
            @ir.calculate_status.should == 'waiting_response_overdue'
        end

        it "is very overdue the day after 40 working days after request sent" do
            Time.stub!(:now).and_return(Time.utc(2007, 12, 11, 00, 01)) 
            @ir.calculate_status.should == 'waiting_response_very_overdue'
        end
    end

    describe "when calculating the status for a school" do
        fixtures :info_requests, :info_request_events, :holidays, :public_bodies, :public_body_translations

        before do
            @ir = info_requests(:naughty_chicken_request)
            @ir.public_body.tag_string = "school"
            @ir.public_body.is_school?.should == true
        end

        it "has expected sent date" do
            @ir.last_event_forming_initial_request.outgoing_message.last_sent_at.strftime("%F").should == '2007-10-14'
        end

        it "has correct due date" do
            @ir.date_response_required_by.strftime("%F").should == '2007-11-09'
        end

        it "has correct very overdue after date" do
            @ir.date_very_overdue_after.strftime("%F").should == '2008-01-11' # 60 working days for schools
        end

        it "isn't overdue on due date (20 working days after request sent)" do
            Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59)) 
            @ir.calculate_status.should == 'waiting_response'
        end

        it "is overdue a day after due date (20 working days after request sent)" do
            Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01)) 
            @ir.calculate_status.should == 'waiting_response_overdue'
        end

        it "is still overdue 40 working days after request sent" do
            Time.stub!(:now).and_return(Time.utc(2007, 12, 10, 23, 59)) 
            @ir.calculate_status.should == 'waiting_response_overdue'
        end

        it "is still overdue the day after 40 working days after request sent" do
            Time.stub!(:now).and_return(Time.utc(2007, 12, 11, 00, 01)) 
            @ir.calculate_status.should == 'waiting_response_overdue'
        end

        it "is still overdue 60 working days after request sent" do
            Time.stub!(:now).and_return(Time.utc(2008, 01, 11, 23, 59)) 
            @ir.calculate_status.should == 'waiting_response_overdue'
        end

        it "is very overdue the day after 60 working days after request sent" do
            Time.stub!(:now).and_return(Time.utc(2008, 01, 12, 00, 01)) 
            @ir.calculate_status.should == 'waiting_response_very_overdue'
        end
    end
  
    describe 'when asked if a user is the owning user for this request' do 
    
        before do 
            @mock_user = mock_model(User)
            @info_request = InfoRequest.new(:user => @mock_user)
            @other_mock_user = mock_model(User)
        end
        
        it 'should return false if a nil object is passed to it' do 
            @info_request.is_owning_user?(nil).should be_false
        end
        
        it 'should return true if the user is the request\'s owner' do 
            @info_request.is_owning_user?(@mock_user).should be_true
        end
        
        it 'should return false for a user that is not the owner and does not own every request' do 
            @other_mock_user.stub!(:owns_every_request?).and_return(false)
            @info_request.is_owning_user?(@other_mock_user).should be_false
        end
        
        it 'should return true if the user is not the owner but owns every request' do
            @other_mock_user.stub!(:owns_every_request?).and_return(true)
            @info_request.is_owning_user?(@other_mock_user).should be_true
        end
        
    end
    

    describe 'when asked if it requires admin' do 
    
        before do 
            @info_request = InfoRequest.new
        end
        
        it 'should return true if its described state is error_message' do 
            @info_request.described_state = 'error_message'
            @info_request.requires_admin?.should be_true
        end
        
        it 'should return true if its described state is requires_admin' do 
            @info_request.described_state = 'requires_admin'
            @info_request.requires_admin?.should be_true
        end
        
        it 'should return false if its described state is waiting_response' do 
            @info_request.described_state = 'waiting_response'
            @info_request.requires_admin?.should be_false
        end
        
    end
    
    describe 'when asked for old unclassified requests' do 
    
        before do 
            Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59))
        end
        
        it 'should ask for requests using any limit param supplied' do 
            InfoRequest.should_receive(:find).with(:all, {:select => anything, 
                                                          :order => anything, 
                                                          :conditions=> anything, 
                                                          :limit => 5})
            InfoRequest.find_old_unclassified(:limit => 5)
        end
        
        it 'should not limit the number of requests returned by default' do 
            InfoRequest.should_not_receive(:find).with(:all, {:select => anything, 
                                                              :order => anything, 
                                                              :conditions=> anything, 
                                                              :limit => anything})
            InfoRequest.find_old_unclassified
        end 
        
        it 'should add extra conditions if supplied' do 
            InfoRequest.should_receive(:find).with(:all, 
                  {:select=> anything, 
                   :order=> anything, 
                   :conditions=>["awaiting_description = ? and (select created_at from info_request_events where info_request_events.info_request_id = info_requests.id and info_request_events.event_type = 'response' order by created_at desc limit 1) < ? and url_title != 'holding_pen' and prominence != 'backpage'", 
                    true, Time.now - 21.days]})
            InfoRequest.find_old_unclassified({:conditions => ["prominence != 'backpage'"]})
        end
        
        it 'should ask the database for requests that are awaiting description, have a last response older than 21 days old, are not the holding pen and are not backpaged' do 
            InfoRequest.should_receive(:find).with(:all, 
                  {:select=>"*, (select created_at from info_request_events where info_request_events.info_request_id = info_requests.id and info_request_events.event_type = 'response' order by created_at desc limit 1) as last_response_time", 
                   :order=>"last_response_time", 
                   :conditions=>["awaiting_description = ? and (select created_at from info_request_events where info_request_events.info_request_id = info_requests.id and info_request_events.event_type = 'response' order by created_at desc limit 1) < ? and url_title != 'holding_pen'", 
                    true, Time.now - 21.days]})
            InfoRequest.find_old_unclassified
        end
        
    end
    
    describe 'when an instance is asked if it is old and unclassified' do 
        
        before do 
            Time.stub!(:now).and_return(Time.utc(2007, 11, 9, 23, 59))
            @mock_comment_event = mock_model(InfoRequestEvent, :created_at => Time.now - 23.days, :event_type => 'comment')
            @mock_response_event = mock_model(InfoRequestEvent, :created_at => Time.now - 22.days, :event_type => 'response')
            @info_request = InfoRequest.new(:prominence => 'normal', 
                                            :awaiting_description => true, 
                                            :info_request_events => [@mock_response_event, @mock_comment_event])
        end
        
        it 'should return false if it is the holding pen' do 
            @info_request.stub!(:url_title).and_return('holding_pen')
            @info_request.is_old_unclassified?.should be_false
        end
        
        it 'should return false if it is not awaiting description' do 
            @info_request.stub!(:awaiting_description).and_return(false)
            @info_request.is_old_unclassified?.should be_false
        end
        
        it 'should return false if its last response event occurred less than 21 days ago' do 
            @mock_response_event.stub!(:created_at).and_return(Time.now - 20.days)
            @info_request.is_old_unclassified?.should be_false
        end
        
        it 'should return true if it is awaiting description, isn\'t the holding pen and hasn\'t had an event in 21 days' do 
            @info_request.is_old_unclassified?.should be_true
        end
        
    end
    
end