diff options
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/info_request_batch_spec.rb | 150 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 19 | ||||
-rw-r--r-- | spec/models/outgoing_message_spec.rb | 14 | ||||
-rw-r--r-- | spec/models/track_thing_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 31 |
5 files changed, 213 insertions, 3 deletions
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 ade75e2cc..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 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/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 |