aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/incoming_message_spec.rb4
-rw-r--r--spec/models/info_request_batch_spec.rb150
-rw-r--r--spec/models/info_request_spec.rb115
-rw-r--r--spec/models/outgoing_message_spec.rb14
-rw-r--r--spec/models/profile_photo_spec.rb22
-rw-r--r--spec/models/public_body_change_request_spec.rb139
-rw-r--r--spec/models/public_body_spec.rb31
-rw-r--r--spec/models/track_thing_spec.rb2
-rw-r--r--spec/models/user_spec.rb31
9 files changed, 496 insertions, 12 deletions
diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb
index c0a7e5340..c27870afc 100644
--- a/spec/models/incoming_message_spec.rb
+++ b/spec/models/incoming_message_spec.rb
@@ -165,7 +165,7 @@ describe IncomingMessage, " when dealing with incoming mail" do
message = File.read(file)
parsed = IncomingMessage.remove_quoted_sections(message)
expected = File.read("#{file}.expected")
- parsed.should include(expected)
+ parsed.should be_equal_modulo_whitespace_to expected
end
end
@@ -462,7 +462,7 @@ describe IncomingMessage, " when censoring data" do
data.should == "His email was x\000x\000x\000@\000x\000x\000x\000.\000x\000x\000x\000, indeed"
end
- it 'should handle multibyte characters correctly', :focus => true do
+ it 'should handle multibyte characters correctly' do
orig_data = 'á'
data = orig_data.dup
@regex_censor_rule = CensorRule.new()
diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb
new file mode 100644
index 000000000..53158ebe2
--- /dev/null
+++ b/spec/models/info_request_batch_spec.rb
@@ -0,0 +1,150 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe InfoRequestBatch, "when validating" do
+
+ before do
+ @info_request_batch = FactoryGirl.build(:info_request_batch)
+ end
+
+ it 'should require a user' do
+ @info_request_batch.user = nil
+ @info_request_batch.valid?.should be_false
+ @info_request_batch.errors.full_messages.should == ["User can't be blank"]
+ end
+
+ it 'should require a title' do
+ @info_request_batch.title = nil
+ @info_request_batch.valid?.should be_false
+ @info_request_batch.errors.full_messages.should == ["Title can't be blank"]
+ end
+
+ it 'should require a body' do
+ @info_request_batch.body = nil
+ @info_request_batch.valid?.should be_false
+ @info_request_batch.errors.full_messages.should == ["Body can't be blank"]
+ end
+
+end
+
+describe InfoRequestBatch, "when finding an existing batch" do
+
+ before do
+ @first_body = FactoryGirl.create(:public_body)
+ @second_body = FactoryGirl.create(:public_body)
+ @info_request_batch = FactoryGirl.create(:info_request_batch, :title => 'Matched title',
+ :body => 'Matched body',
+ :public_bodies => [@first_body,
+ @second_body])
+ end
+
+ it 'should return a batch with the same user, title and body sent to one of the same public bodies' do
+ InfoRequestBatch.find_existing(@info_request_batch.user,
+ @info_request_batch.title,
+ @info_request_batch.body,
+ [@first_body]).should_not be_nil
+ end
+
+ it 'should not return a batch with the same title and body sent to another public body' do
+ InfoRequestBatch.find_existing(@info_request_batch.user,
+ @info_request_batch.title,
+ @info_request_batch.body,
+ [FactoryGirl.create(:public_body)]).should be_nil
+ end
+
+ it 'should not return a batch sent the same public bodies with a different title and body' do
+ InfoRequestBatch.find_existing(@info_request_batch.user,
+ 'Other title',
+ 'Other body',
+ [@first_body]).should be_nil
+ end
+
+ it 'should not return a batch sent to one of the same public bodies with the same title and body by
+ a different user' do
+ InfoRequestBatch.find_existing(FactoryGirl.create(:user),
+ @info_request_batch.title,
+ @info_request_batch.body,
+ [@first_body]).should be_nil
+ end
+end
+
+describe InfoRequestBatch, "when creating a batch", :focus => true do
+
+ before do
+ @title = 'A test title'
+ @body = "Dear [Authority name],\nA message\nYours faithfully,\nRequester"
+ @first_public_body = FactoryGirl.create(:public_body)
+ @second_public_body = FactoryGirl.create(:public_body)
+ @user = FactoryGirl.create(:user)
+ @info_request_batch = InfoRequestBatch.create!({:title => @title,
+ :body => @body,
+ :public_bodies => [@first_public_body,
+ @second_public_body],
+ :user => @user})
+ end
+
+ it 'should substitute authority name for the placeholder in each request' do
+ unrequestable = @info_request_batch.create_batch!
+ [@first_public_body, @second_public_body].each do |public_body|
+ request = @info_request_batch.info_requests.detect do |info_request|
+ info_request.public_body == public_body
+ end
+ expected = "Dear #{public_body.name},\nA message\nYours faithfully,\nRequester"
+ request.outgoing_messages.first.body.should == expected
+ end
+ end
+
+ it 'should send requests to requestable public bodies, and return a list of unrequestable ones' do
+ @first_public_body.stub(:is_requestable?).and_return(false)
+ unrequestable = @info_request_batch.create_batch!
+ unrequestable.should == [@first_public_body]
+ @info_request_batch.info_requests.size.should == 1
+ request = @info_request_batch.info_requests.first
+ request.outgoing_messages.first.status.should == 'sent'
+ end
+
+ it 'should set the sent_at value of the info request batch' do
+ @info_request_batch.create_batch!
+ @info_request_batch.sent_at.should_not be_nil
+ end
+
+end
+
+describe InfoRequestBatch, "when sending batches" do
+
+ before do
+ @title = 'A test title'
+ @body = "Dear [Authority name],\nA message\nYours faithfully,\nRequester"
+ @first_public_body = FactoryGirl.create(:public_body)
+ @second_public_body = FactoryGirl.create(:public_body)
+ @user = FactoryGirl.create(:user)
+ @info_request_batch = InfoRequestBatch.create!({:title => @title,
+ :body => @body,
+ :public_bodies => [@first_public_body,
+ @second_public_body],
+ :user => @user})
+ @sent_batch = InfoRequestBatch.create!({:title => @title,
+ :body => @body,
+ :public_bodies => [@first_public_body,
+ @second_public_body],
+ :user => @user,
+ :sent_at => Time.now})
+ end
+
+ it 'should send requests and notifications for only unsent batch requests' do
+ InfoRequestBatch.send_batches
+ ActionMailer::Base.deliveries.size.should == 3
+ first_email = ActionMailer::Base.deliveries.first
+ first_email.to.should == [@first_public_body.request_email]
+ first_email.subject.should == 'Freedom of Information request - A test title'
+
+ second_email = ActionMailer::Base.deliveries.second
+ second_email.to.should == [@second_public_body.request_email]
+ second_email.subject.should == 'Freedom of Information request - A test title'
+
+ third_email = ActionMailer::Base.deliveries.third
+ third_email.to.should == [@user.email]
+ third_email.subject.should == 'Your batch request "A test title" has been sent'
+ end
+
+end
+
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index dcc94e967..9766f928f 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -46,6 +46,19 @@ describe InfoRequest do
info_request.valid?
info_request.errors[:title].should_not be_empty
end
+
+ it 'should require a public body id by default' do
+ info_request = InfoRequest.new
+ info_request.valid?
+ info_request.errors[:public_body_id].should_not be_empty
+ end
+
+ it 'should not require a public body id if it is a batch request template' do
+ info_request = InfoRequest.new
+ info_request.is_batch_request_template = true
+ info_request.valid?
+ info_request.errors[:public_body_id].should be_empty
+ end
end
describe 'when generating a user name slug' do
@@ -600,6 +613,12 @@ describe InfoRequest do
@info_request.apply_censor_rules_to_text!(@text)
end
+ it 'should not raise an error if the request is a batch request template' do
+ @info_request.stub!(:public_body).and_return(nil)
+ @info_request.is_batch_request_template = true
+ lambda{ @info_request.apply_censor_rules_to_text!(@text) }.should_not raise_error
+ end
+
end
context 'when applying censor rules to binary files' do
@@ -1177,4 +1196,100 @@ describe InfoRequest do
request_events.map(&:info_request).select{|x|x.url_title =~ /^spam/}.length.should == 1
end
end
+
+ describe InfoRequest, "when constructing a list of requests by query" do
+
+ before(:each) do
+ get_fixtures_xapian_index
+ end
+
+ def apply_filters(filters)
+ results = InfoRequest.request_list(filters, page=1, per_page=100, max_results=100)
+ results[:results].map(&:info_request)
+ end
+
+ it "should filter requests" do
+ apply_filters(:latest_status => 'all').should =~ InfoRequest.all
+
+ # default sort order is the request with the most recently created event first
+ apply_filters(:latest_status => 'all').should == InfoRequest.all(
+ :order => "(SELECT max(info_request_events.created_at)
+ FROM info_request_events
+ WHERE info_request_events.info_request_id = info_requests.id)
+ DESC")
+
+ apply_filters(:latest_status => 'successful').should =~ InfoRequest.all(
+ :conditions => "id in (
+ SELECT info_request_id
+ FROM info_request_events
+ WHERE NOT EXISTS (
+ SELECT *
+ FROM info_request_events later_events
+ WHERE later_events.created_at > info_request_events.created_at
+ AND later_events.info_request_id = info_request_events.info_request_id
+ AND later_events.described_state IS NOT null
+ )
+ AND info_request_events.described_state IN ('successful', 'partially_successful')
+ )")
+
+ end
+
+ it "should filter requests by date" do
+ # The semantics of the search are that it finds any InfoRequest
+ # that has any InfoRequestEvent created in the specified range
+ filters = {:latest_status => 'all', :request_date_before => '13/10/2007'}
+ apply_filters(filters).should =~ InfoRequest.all(
+ :conditions => "id IN (SELECT info_request_id
+ FROM info_request_events
+ WHERE created_at < '2007-10-13'::date)")
+
+ filters = {:latest_status => 'all', :request_date_after => '13/10/2007'}
+ apply_filters(filters).should =~ InfoRequest.all(
+ :conditions => "id IN (SELECT info_request_id
+ FROM info_request_events
+ WHERE created_at > '2007-10-13'::date)")
+
+ filters = {:latest_status => 'all',
+ :request_date_after => '13/10/2007',
+ :request_date_before => '01/11/2007'}
+ apply_filters(filters).should =~ InfoRequest.all(
+ :conditions => "id IN (SELECT info_request_id
+ FROM info_request_events
+ WHERE created_at BETWEEN '2007-10-13'::date
+ AND '2007-11-01'::date)")
+ end
+
+
+ it "should list internal_review requests as unresolved ones" do
+
+ # This doesn’t precisely duplicate the logic of the actual
+ # query, but it is close enough to give the same result with
+ # the current set of test data.
+ results = apply_filters(:latest_status => 'awaiting')
+ results.should =~ InfoRequest.all(
+ :conditions => "id IN (SELECT info_request_id
+ FROM info_request_events
+ WHERE described_state in (
+ 'waiting_response', 'waiting_clarification',
+ 'internal_review', 'gone_postal', 'error_message', 'requires_admin'
+ ) and not exists (
+ select *
+ from info_request_events later_events
+ where later_events.created_at > info_request_events.created_at
+ and later_events.info_request_id = info_request_events.info_request_id
+ ))")
+
+
+ results.include?(info_requests(:fancy_dog_request)).should == false
+
+ event = info_request_events(:useless_incoming_message_event)
+ event.described_state = event.calculated_state = "internal_review"
+ event.save!
+ rebuild_xapian_index
+ results = apply_filters(:latest_status => 'awaiting')
+ results.include?(info_requests(:fancy_dog_request)).should == true
+ end
+
+
+ end
end
diff --git a/spec/models/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb
index bb270ca16..a3e2d1c68 100644
--- a/spec/models/outgoing_message_spec.rb
+++ b/spec/models/outgoing_message_spec.rb
@@ -57,7 +57,8 @@ describe OutgoingMessage, " when making an outgoing message" do
info_request = mock_model(InfoRequest, :public_body => public_body,
:url_title => 'a_test_title',
:title => 'A test title',
- :apply_censor_rules_to_text! => nil)
+ :apply_censor_rules_to_text! => nil,
+ :is_batch_request_template? => false)
outgoing_message = OutgoingMessage.new({
:status => 'ready',
:message_type => 'followup',
@@ -68,6 +69,15 @@ describe OutgoingMessage, " when making an outgoing message" do
outgoing_message.body.should include(expected_text)
end
+ context "when associated with a batch template request" do
+
+ it 'should produce a salutation with a placeholder' do
+ @om.info_request.is_batch_request_template = true
+ @om.get_salutation.should == 'Dear [Authority name],'
+ end
+ end
+
+
describe 'when asked if a user can view it' do
before do
@@ -166,7 +176,7 @@ describe OutgoingMessage, " when censoring data" do
end
end
-describe OutgoingMessage, "when validating the format of the message body", :focus => true do
+describe OutgoingMessage, "when validating the format of the message body" do
it 'should handle a salutation with a bracket in it' do
outgoing_message = FactoryGirl.build(:initial_request)
diff --git a/spec/models/profile_photo_spec.rb b/spec/models/profile_photo_spec.rb
index 0e157e2c5..e70f474a0 100644
--- a/spec/models/profile_photo_spec.rb
+++ b/spec/models/profile_photo_spec.rb
@@ -10,12 +10,12 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
-describe ProfilePhoto, "when constructing a new photo" do
+describe ProfilePhoto, "when constructing a new photo" do
- before do
+ before do
@mock_user = mock_model(User)
end
-
+
it 'should take no image as invalid' do
profile_photo = ProfilePhoto.new(:data => nil, :user => @mock_user)
profile_photo.valid?.should == false
@@ -26,7 +26,15 @@ describe ProfilePhoto, "when constructing a new photo" do
profile_photo.valid?.should == false
end
- it 'should accept and convert a PNG to right size' do
+ it 'should translate a no image error message' do
+ I18n.with_locale(:es) do
+ profile_photo = ProfilePhoto.new(:data => nil, :user => @mock_user)
+ profile_photo.valid?.should == false
+ profile_photo.errors[:data].should == ['Por favor elige el fichero que contiene tu foto']
+ end
+ end
+
+ it 'should accept and convert a PNG to right size' do
data = load_file_fixture("parrot.png")
profile_photo = ProfilePhoto.new(:data => data, :user => @mock_user)
profile_photo.valid?.should == true
@@ -35,7 +43,7 @@ describe ProfilePhoto, "when constructing a new photo" do
profile_photo.image.rows.should == 96
end
- it 'should accept and convert a JPEG to right format and size' do
+ it 'should accept and convert a JPEG to right format and size' do
data = load_file_fixture("parrot.jpg")
profile_photo = ProfilePhoto.new(:data => data, :user => @mock_user)
profile_photo.valid?.should == true
@@ -44,7 +52,7 @@ describe ProfilePhoto, "when constructing a new photo" do
profile_photo.image.rows.should == 96
end
- it 'should accept a draft PNG and not resize it' do
+ it 'should accept a draft PNG and not resize it' do
data = load_file_fixture("parrot.png")
profile_photo = ProfilePhoto.new(:data => data, :draft => true)
profile_photo.valid?.should == true
@@ -53,6 +61,6 @@ describe ProfilePhoto, "when constructing a new photo" do
profile_photo.image.rows.should == 289
end
-
+
end
diff --git a/spec/models/public_body_change_request_spec.rb b/spec/models/public_body_change_request_spec.rb
new file mode 100644
index 000000000..0c4cea67b
--- /dev/null
+++ b/spec/models/public_body_change_request_spec.rb
@@ -0,0 +1,139 @@
+# == Schema Information
+#
+# Table name: public_body_change_requests
+#
+# id :integer not null, primary key
+# user_email :string(255)
+# user_name :string(255)
+# user_id :integer
+# public_body_name :text
+# public_body_id :integer
+# public_body_email :string(255)
+# source_url :text
+# notes :text
+# is_open :boolean default(TRUE), not null
+# created_at :datetime not null
+# updated_at :datetime not null
+#
+
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe PublicBodyChangeRequest, 'when validating' do
+
+ it 'should not be valid without a public body name' do
+ change_request = PublicBodyChangeRequest.new()
+ change_request.valid?.should be_false
+ change_request.errors[:public_body_name].should == ['Please enter the name of the authority']
+ end
+
+ it 'should not be valid without a user name if there is no user' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body')
+ change_request.valid?.should be_false
+ change_request.errors[:user_name].should == ['Please enter your name']
+ end
+
+ it 'should not be valid without a user email address if there is no user' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body')
+ change_request.valid?.should be_false
+ change_request.errors[:user_email].should == ['Please enter your email address']
+ end
+
+ it 'should be valid with a user and no name or email address' do
+ user = FactoryGirl.build(:user)
+ change_request = PublicBodyChangeRequest.new(:user => user,
+ :public_body_name => 'New Body')
+ change_request.valid?.should be_true
+ end
+
+ it 'should validate the format of a user email address entered' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body',
+ :user_email => '@example.com')
+ change_request.valid?.should be_false
+ change_request.errors[:user_email].should == ["Your email doesn't look like a valid address"]
+ end
+
+ it 'should validate the format of a public body email address entered' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body',
+ :public_body_email => '@example.com')
+ change_request.valid?.should be_false
+ change_request.errors[:public_body_email].should == ["The authority email doesn't look like a valid address"]
+ end
+
+end
+
+describe PublicBodyChangeRequest, 'get_user_name' do
+
+ it 'should return the user_name field if there is no user association' do
+ change_request = PublicBodyChangeRequest.new(:user_name => 'Test User')
+ change_request.get_user_name.should == 'Test User'
+ end
+
+ it 'should return the name of the associated user if there is one' do
+ user = FactoryGirl.build(:user)
+ change_request = PublicBodyChangeRequest.new(:user => user)
+ change_request.get_user_name.should == user.name
+ end
+
+end
+
+
+describe PublicBodyChangeRequest, 'get_user_email' do
+
+ it 'should return the user_email field if there is no user association' do
+ change_request = PublicBodyChangeRequest.new(:user_email => 'user@example.com')
+ change_request.get_user_email.should == 'user@example.com'
+ end
+
+ it 'should return the email of the associated user if there is one' do
+ user = FactoryGirl.build(:user)
+ change_request = PublicBodyChangeRequest.new(:user => user)
+ change_request.get_user_email.should == user.email
+ end
+
+end
+
+
+describe PublicBodyChangeRequest, 'get_public_body_name' do
+
+ it 'should return the public_body_name field if there is no public body association' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'Test Authority')
+ change_request.get_public_body_name.should == 'Test Authority'
+ end
+
+ it 'should return the name of the associated public body if there is one' do
+ public_body = FactoryGirl.build(:public_body)
+ change_request = PublicBodyChangeRequest.new(:public_body => public_body)
+ change_request.get_public_body_name.should == public_body.name
+ end
+
+end
+
+describe PublicBodyChangeRequest, 'when creating a comment for the associated public body' do
+
+ it 'should include requesting user, source_url and notes' do
+ change_request = PublicBodyChangeRequest.new(:user_name => 'Test User',
+ :user_email => 'test@example.com',
+ :source_url => 'http://www.example.com',
+ :notes => 'Some notes')
+ expected = "Requested by: Test User (test@example.com)\nSource URL: http://www.example.com\nNotes: Some notes"
+ change_request.comment_for_public_body.should == expected
+ end
+
+end
+
+describe PublicBodyChangeRequest, 'when creating a default subject for a response email' do
+
+ it 'should create an appropriate subject for a request to add a body' do
+ change_request = PublicBodyChangeRequest.new(:public_body_name => 'Test Body')
+ change_request.default_response_subject.should == 'Your request to add Test Body to Alaveteli'
+ end
+
+ it 'should create an appropriate subject for a request to update an email address' do
+ public_body = FactoryGirl.build(:public_body)
+ change_request = PublicBodyChangeRequest.new(:public_body => public_body)
+ change_request.default_response_subject.should == "Your request to update #{public_body.name} on Alaveteli"
+
+ end
+
+end
+
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index ed24ced52..dc09bdfa6 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -300,6 +300,37 @@ describe PublicBody, "when searching" do
end
end
+describe PublicBody, "when asked for the internal_admin_body" do
+ before(:each) do
+ # Make sure that there's no internal_admin_body before each of
+ # these tests:
+ PublicBody.connection.delete("DELETE FROM public_bodies WHERE url_name = 'internal_admin_body'")
+ PublicBody.connection.delete("DELETE FROM public_body_translations WHERE url_name = 'internal_admin_body'")
+ end
+
+ it "should create the internal_admin_body if it didn't exist" do
+ iab = PublicBody.internal_admin_body
+ iab.should_not be_nil
+ end
+
+ it "should find the internal_admin_body even if the default locale has changed since it was created" do
+ with_default_locale("en") do
+ I18n.with_locale(:en) do
+ iab = PublicBody.internal_admin_body
+ iab.should_not be_nil
+ end
+ end
+ with_default_locale("es") do
+ I18n.with_locale(:es) do
+ iab = PublicBody.internal_admin_body
+ iab.should_not be_nil
+ end
+ end
+ end
+
+end
+
+
describe PublicBody, " when dealing public body locales" do
it "shouldn't fail if it internal_admin_body was created in a locale other than the default" do
# first time, do it with the non-default locale
diff --git a/spec/models/track_thing_spec.rb b/spec/models/track_thing_spec.rb
index 86d3c0cda..1c582564b 100644
--- a/spec/models/track_thing_spec.rb
+++ b/spec/models/track_thing_spec.rb
@@ -39,7 +39,7 @@ describe TrackThing, "when tracking changes" do
it "will find existing tracks which are the same" do
track_thing = TrackThing.create_track_for_search_query('fancy dog')
- found_track = TrackThing.find_by_existing_track(users(:silly_name_user), track_thing)
+ found_track = TrackThing.find_existing(users(:silly_name_user), track_thing)
found_track.should == @track_thing
end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index cbbf4b5ce..b6f48dad3 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -337,3 +337,34 @@ describe User, "when emails have bounced" do
user.email_bounce_message.should == "The reason we think the email bounced (e.g. a bounce message)"
end
end
+
+describe User, "when calculating if a user has exceeded the request limit" do
+
+ before do
+ @info_request = FactoryGirl.create(:info_request)
+ @user = @info_request.user
+ end
+
+ it 'should return false if no request limit is set' do
+ AlaveteliConfiguration.stub!(:max_requests_per_user_per_day).and_return nil
+ @user.exceeded_limit?.should be_false
+ end
+
+ it 'should return false if the user has not submitted more than the limit' do
+ AlaveteliConfiguration.stub!(:max_requests_per_user_per_day).and_return(2)
+ @user.exceeded_limit?.should be_false
+ end
+
+ it 'should return true if the user has submitted more than the limit' do
+ AlaveteliConfiguration.stub!(:max_requests_per_user_per_day).and_return(0)
+ @user.exceeded_limit?.should be_true
+ end
+
+ it 'should return false if the user is allowed to make batch requests' do
+ @user.can_make_batch_requests = true
+ AlaveteliConfiguration.stub!(:max_requests_per_user_per_day).and_return(0)
+ @user.exceeded_limit?.should be_false
+ end
+
+
+end