From 9a5c01d781129aeb1ab2b18771ac131ecdfb56c1 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 24 Oct 2013 14:35:25 +0100 Subject: Add InfoRequestBatch model. Re-annotate models. Index InfoRequestBatches by user - we'll display the batches for a user when they view their own requests. --- spec/models/info_request_batch_spec.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 spec/models/info_request_batch_spec.rb (limited to 'spec/models/info_request_batch_spec.rb') diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb new file mode 100644 index 000000000..f6d9bea0c --- /dev/null +++ b/spec/models/info_request_batch_spec.rb @@ -0,0 +1,21 @@ +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 + +end -- cgit v1.2.3 From ce262657761c1c47c47e2f6cc7bf683543ebbdb7 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 24 Oct 2013 17:03:52 +0100 Subject: Add check for double submission. --- spec/models/info_request_batch_spec.rb | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'spec/models/info_request_batch_spec.rb') diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index f6d9bea0c..787b812cf 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -18,4 +18,49 @@ describe InfoRequestBatch, "when validating" do @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 + @info_request_batch = FactoryGirl.create(:info_request_batch, :title => 'Matched title', + :body => 'Matched body') + @first_request = FactoryGirl.create(:info_request, :info_request_batch => @info_request_batch) + @second_request = FactoryGirl.create(:info_request, :info_request_batch => @info_request_batch) + 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_request.public_body_id]).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).id]).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_request.public_body_id]).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_request.public_body_id]).should be_nil + end end -- cgit v1.2.3 From 90fd2c06e48c17909b9a7d2aae6d01c41047b4de Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Mon, 28 Oct 2013 13:33:08 +0000 Subject: Add the specific salutations to each request. Also, wrap model creation in a transaction and do the message sending separately - we may ultimately want to do this outside the request cycle. --- spec/models/info_request_batch_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'spec/models/info_request_batch_spec.rb') diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index 787b812cf..ba4bcd181 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -64,3 +64,25 @@ describe InfoRequestBatch, "when finding an existing batch" do [@first_request.public_body_id]).should be_nil end end + +describe InfoRequestBatch, "when creating a batch", :focus => true do + + it 'should substitute authority name for the placeholder in each request' do + info_request_params = {:title => 'A test title'} + outgoing_message_params = {: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) + results = InfoRequestBatch.create_batch!(info_request_params, + outgoing_message_params, + [first_public_body.id, second_public_body.id], + user) + info_requests = results[:batch].info_requests + [first_public_body, second_public_body].each do |public_body| + request = info_requests.detect{|info_request| info_request.public_body == public_body} + request.outgoing_messages.first.body.should == "Dear #{public_body.name},\nA message\nYours faithfully,\nRequester" + end + end + +end -- cgit v1.2.3 From 51c80db7a35a41fbbab9b28e86a5d60166791b4c Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 27 Nov 2013 12:03:28 +0000 Subject: Split the creation of a batch and the associated requests. We're going to want to actually create and send the requests later. --- spec/models/info_request_batch_spec.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'spec/models/info_request_batch_spec.rb') diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index ba4bcd181..7f6905c8e 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -61,26 +61,25 @@ describe InfoRequestBatch, "when finding an existing batch" do InfoRequestBatch.find_existing(FactoryGirl.create(:user), @info_request_batch.title, @info_request_batch.body, - [@first_request.public_body_id]).should be_nil + [@first_body]).should be_nil end end describe InfoRequestBatch, "when creating a batch", :focus => true do it 'should substitute authority name for the placeholder in each request' do - info_request_params = {:title => 'A test title'} - outgoing_message_params = {:body => "Dear [Authority name],\nA message\nYours faithfully,\nRequester", - } + 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) - results = InfoRequestBatch.create_batch!(info_request_params, - outgoing_message_params, - [first_public_body.id, second_public_body.id], - user) - info_requests = results[:batch].info_requests + info_request_batch = InfoRequestBatch.create!({:title => 'A test title', + :body => body, + :public_bodies => [first_public_body, + second_public_body], + :user => user}) + results = info_request_batch.create_batch! [first_public_body, second_public_body].each do |public_body| - request = info_requests.detect{|info_request| info_request.public_body == public_body} + request = info_request_batch.info_requests.detect{|info_request| info_request.public_body == public_body} request.outgoing_messages.first.body.should == "Dear #{public_body.name},\nA message\nYours faithfully,\nRequester" end end -- cgit v1.2.3 From d898b9308f98f84c4f44bcc3316193e80b007b23 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 27 Nov 2013 12:05:11 +0000 Subject: Use public bodies not requests in find_existing The requests may not have been created at this point. --- spec/models/info_request_batch_spec.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'spec/models/info_request_batch_spec.rb') diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index 7f6905c8e..2323c3bd3 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -29,31 +29,33 @@ 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') - @first_request = FactoryGirl.create(:info_request, :info_request_batch => @info_request_batch) - @second_request = FactoryGirl.create(:info_request, :info_request_batch => @info_request_batch) + :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_request.public_body_id]).should_not be_nil + [@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).id]).should be_nil + [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_request.public_body_id]).should be_nil + [@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 -- cgit v1.2.3 From e55d7850d36b147275c2f8ffcacccbcf97e0a1d5 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 27 Nov 2013 17:40:59 +0000 Subject: Simplify return value, set sent_at in create_batch! --- spec/models/info_request_batch_spec.rb | 47 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'spec/models/info_request_batch_spec.rb') diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index 2323c3bd3..8914da3fa 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -69,21 +69,42 @@ 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 - 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 => 'A test title', - :body => body, - :public_bodies => [first_public_body, - second_public_body], - :user => user}) - results = info_request_batch.create_batch! - [first_public_body, second_public_body].each do |public_body| - request = info_request_batch.info_requests.detect{|info_request| info_request.public_body == public_body} - request.outgoing_messages.first.body.should == "Dear #{public_body.name},\nA message\nYours faithfully,\nRequester" + 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 -- cgit v1.2.3 From d0a78300b84021e50268c1a7f947f0ddd5d35f51 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 27 Nov 2013 18:39:03 +0000 Subject: Send batch requests as a cron job. --- spec/models/info_request_batch_spec.rb | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'spec/models/info_request_batch_spec.rb') diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index 8914da3fa..53158ebe2 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -108,3 +108,43 @@ describe InfoRequestBatch, "when creating a batch", :focus => true do 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 + -- cgit v1.2.3