aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/info_request_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/info_request_spec.rb')
-rw-r--r--spec/models/info_request_spec.rb203
1 files changed, 194 insertions, 9 deletions
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index ab36a201c..7281b74b6 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -1,7 +1,53 @@
+# encoding: utf-8
+# == Schema Information
+#
+# Table name: info_requests
+#
+# id :integer not null, primary key
+# title :text not null
+# user_id :integer
+# public_body_id :integer not null
+# created_at :datetime not null
+# updated_at :datetime not null
+# described_state :string(255) not null
+# awaiting_description :boolean default(FALSE), not null
+# prominence :string(255) default("normal"), not null
+# url_title :text not null
+# law_used :string(255) default("foi"), not null
+# allow_new_responses_from :string(255) default("anybody"), not null
+# handle_rejected_responses :string(255) default("bounce"), not null
+# idhash :string(255) not null
+# external_user_name :string(255)
+# external_url :string(255)
+# attention_requested :boolean default(FALSE)
+# comments_allowed :boolean default(TRUE), not null
+#
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe InfoRequest do
+ describe 'when validating', :focus => true do
+
+ it 'should accept a summary with ascii characters' do
+ info_request = InfoRequest.new(:title => 'abcde')
+ info_request.valid?
+ info_request.errors[:title].should be_empty
+ end
+
+ it 'should accept a summary with unicode characters' do
+ info_request = InfoRequest.new(:title => 'кажете')
+ info_request.valid?
+ info_request.errors[:title].should be_empty
+ end
+
+ it 'should not accept a summary with no ascii or unicode characters' do
+ info_request = InfoRequest.new(:title => '55555')
+ info_request.valid?
+ info_request.errors[:title].should_not be_empty
+ end
+ end
+
describe 'when generating a user name slug' do
before do
@@ -376,10 +422,12 @@ describe InfoRequest do
it 'should add extra conditions if supplied' do
expected_conditions = ["awaiting_description = ?
- AND (SELECT created_at
- FROM info_request_events
+ AND (SELECT info_request_events.created_at
+ FROM info_request_events, incoming_messages
WHERE info_request_events.info_request_id = info_requests.id
AND info_request_events.event_type = 'response'
+ AND incoming_messages.id = info_request_events.incoming_message_id
+ AND incoming_messages.prominence = 'normal'
ORDER BY created_at desc LIMIT 1) < ?
AND url_title != 'holding_pen'
AND user_id IS NOT NULL
@@ -394,21 +442,25 @@ describe InfoRequest do
InfoRequest.find_old_unclassified({:conditions => ["prominence != 'backpage'"]})
end
- it 'should ask the database for requests that are awaiting description, have a last response older
+ it 'should ask the database for requests that are awaiting description, have a last public response older
than 21 days old, have a user, are not the holding pen and are not backpaged' do
expected_conditions = ["awaiting_description = ?
- AND (SELECT created_at
- FROM info_request_events
+ AND (SELECT info_request_events.created_at
+ FROM info_request_events, incoming_messages
WHERE info_request_events.info_request_id = info_requests.id
AND info_request_events.event_type = 'response'
+ AND incoming_messages.id = info_request_events.incoming_message_id
+ AND incoming_messages.prominence = 'normal'
ORDER BY created_at desc LIMIT 1) < ?
AND url_title != 'holding_pen'
AND user_id IS NOT NULL".split(' ').join(' '),
true, Time.now - 21.days]
- expected_select = "*, (SELECT created_at
- FROM info_request_events
+ expected_select = "*, (SELECT info_request_events.created_at
+ FROM info_request_events, incoming_messages
WHERE info_request_events.info_request_id = info_requests.id
AND info_request_events.event_type = 'response'
+ AND incoming_messages.id = info_request_events.incoming_message_id
+ AND incoming_messages.prominence = 'normal'
ORDER BY created_at desc LIMIT 1)
AS last_response_time".split(' ').join(' ')
InfoRequest.should_receive(:find) do |all, query_params|
@@ -423,12 +475,55 @@ describe InfoRequest do
end
+ describe 'when asked for random old unclassified requests with normal prominence' do
+
+ it "should not return requests that don't have normal prominence" do
+ dog_request = info_requests(:fancy_dog_request)
+ old_unclassified = InfoRequest.get_random_old_unclassified(1, :conditions => ["prominence = 'normal'"])
+ old_unclassified.length.should == 1
+ old_unclassified.first.should == dog_request
+ dog_request.prominence = 'requester_only'
+ dog_request.save!
+ old_unclassified = InfoRequest.get_random_old_unclassified(1, :conditions => ["prominence = 'normal'"])
+ old_unclassified.length.should == 0
+ dog_request.prominence = 'hidden'
+ dog_request.save!
+ old_unclassified = InfoRequest.get_random_old_unclassified(1, :conditions => ["prominence = 'normal'"])
+ old_unclassified.length.should == 0
+ end
+
+ end
+
+ describe 'when asked to count old unclassified requests with normal prominence' do
+
+ it "should not return requests that don't have normal prominence" do
+ dog_request = info_requests(:fancy_dog_request)
+ old_unclassified = InfoRequest.count_old_unclassified(:conditions => ["prominence = 'normal'"])
+ old_unclassified.should == 1
+ dog_request.prominence = 'requester_only'
+ dog_request.save!
+ old_unclassified = InfoRequest.count_old_unclassified(:conditions => ["prominence = 'normal'"])
+ old_unclassified.should == 0
+ dog_request.prominence = 'hidden'
+ dog_request.save!
+ old_unclassified = InfoRequest.count_old_unclassified(:conditions => ["prominence = 'normal'"])
+ old_unclassified.should == 0
+ 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', :response? => false)
- @mock_response_event = mock_model(InfoRequestEvent, :created_at => Time.now - 22.days, :event_type => 'response', :response? => true)
+ @mock_comment_event = mock_model(InfoRequestEvent, :created_at => Time.now - 23.days,
+ :event_type => 'comment',
+ :response? => false)
+ mock_incoming_message = mock_model(IncomingMessage, :all_can_view? => true)
+ @mock_response_event = mock_model(InfoRequestEvent, :created_at => Time.now - 22.days,
+ :event_type => 'response',
+ :response? => true,
+ :incoming_message => mock_incoming_message)
@info_request = InfoRequest.new(:prominence => 'normal',
:awaiting_description => true,
:info_request_events => [@mock_response_event, @mock_comment_event])
@@ -565,6 +660,96 @@ describe InfoRequest do
end
end
+ describe 'when asked for the last public response event' do
+
+ before do
+ @info_request = FactoryGirl.create(:info_request_with_incoming)
+ @incoming_message = @info_request.incoming_messages.first
+ end
+
+ it 'should not return an event with a hidden prominence message' do
+ @incoming_message.prominence = 'hidden'
+ @incoming_message.save!
+ @info_request.get_last_public_response_event.should == nil
+ end
+
+ it 'should not return an event with a requester_only prominence message' do
+ @incoming_message.prominence = 'requester_only'
+ @incoming_message.save!
+ @info_request.get_last_public_response_event.should == nil
+ end
+
+ it 'should return an event with a normal prominence message' do
+ @incoming_message.prominence = 'normal'
+ @incoming_message.save!
+ @info_request.get_last_public_response_event.should == @incoming_message.response_event
+ end
+ end
+
+ describe 'when asked for the last public outgoing event' do
+
+ before do
+ @info_request = FactoryGirl.create(:info_request)
+ @outgoing_message = @info_request.outgoing_messages.first
+ end
+
+ it 'should not return an event with a hidden prominence message' do
+ @outgoing_message.prominence = 'hidden'
+ @outgoing_message.save!
+ @info_request.get_last_public_outgoing_event.should == nil
+ end
+
+ it 'should not return an event with a requester_only prominence message' do
+ @outgoing_message.prominence = 'requester_only'
+ @outgoing_message.save!
+ @info_request.get_last_public_outgoing_event.should == nil
+ end
+
+ it 'should return an event with a normal prominence message' do
+ @outgoing_message.prominence = 'normal'
+ @outgoing_message.save!
+ @info_request.get_last_public_outgoing_event.should == @outgoing_message.info_request_events.first
+ end
+
+ end
+
+ describe 'when asked who can be sent a followup' do
+
+ before do
+ @info_request = FactoryGirl.create(:info_request_with_plain_incoming)
+ @incoming_message = @info_request.incoming_messages.first
+ @public_body = @info_request.public_body
+ end
+
+ it 'should not include details from a hidden prominence response' do
+ @incoming_message.prominence = 'hidden'
+ @incoming_message.save!
+ @info_request.who_can_followup_to.should == [[@public_body.name,
+ @public_body.request_email,
+ nil]]
+ end
+
+ it 'should not include details from a requester_only prominence response' do
+ @incoming_message.prominence = 'requester_only'
+ @incoming_message.save!
+ @info_request.who_can_followup_to.should == [[@public_body.name,
+ @public_body.request_email,
+ nil]]
+ end
+
+ it 'should include details from a normal prominence response' do
+ @incoming_message.prominence = 'normal'
+ @incoming_message.save!
+ @info_request.who_can_followup_to.should == [[@public_body.name,
+ @public_body.request_email,
+ nil],
+ ['Bob Responder',
+ "bob@example.com",
+ @incoming_message.id]]
+ end
+
+ end
+
describe 'when generating json for the api' do
before do