From 8263c7a92c8ef4313a65dc6bfd75b6eca1250ebc Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Thu, 24 May 2012 13:25:53 +0100 Subject: Test for creating a new request via the API This is not yet implemented. Test first! --- spec/controllers/api_controller_spec.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 spec/controllers/api_controller_spec.rb (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb new file mode 100644 index 000000000..91d113dac --- /dev/null +++ b/spec/controllers/api_controller_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe ApiController, "when using the API" do + it "should create a new request from a POST" do + geraldine = public_bodies(:geraldine_public_body) + number_of_requests = InfoRequest.count(:conditions = ["public_body_id = ?", geraldine.id]) + + request_data = { + "title" => "Tell me about your chickens", + "body" => "Dear Sir,\n\nI should like to know about your chickens.\n\nYours in faith,\nBob\n", + + "external_url" => "http://www.example.gov.uk/foi/chickens_23", + "external_user_name" => "Bob Smith", + } + + post :create_request, :k => geraldine.api_key, :request_json => request_data.to_json + response.should be_success + response.headers["Content-Type"].should == "application/json" + + response_body = response.body.from_json + response_body["url"].should =~ /^http/ + + InfoRequest.count(:conditions = ["public_body_id = ?", geraldine.id]).should == number_of_requests + 1 + + new_request = InfoRequest.find(response_body["id"]) + new_request.user_id.should be_nil + new_request.external_user_name.should == request_data["external_user_name"] + new_request.external_url.should == request_data["external_url"] + + new_request.title.should == request_data["title"] + new_request.last_event_forming_initial_request.outgoing_message.body.should == request_data["body"] + end +end -- cgit v1.2.3 From 257ba137aa1ba1b2357e3fdc8a83e2c6947cae3d Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Thu, 24 May 2012 13:50:23 +0100 Subject: Fix test and fixtures so they only fail due to missing business logic (corrected typo, added missing fixture data, etc) --- spec/controllers/api_controller_spec.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index 91d113dac..dec652361 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe ApiController, "when using the API" do it "should create a new request from a POST" do geraldine = public_bodies(:geraldine_public_body) - number_of_requests = InfoRequest.count(:conditions = ["public_body_id = ?", geraldine.id]) + number_of_requests = InfoRequest.count(:conditions => ["public_body_id = ?", geraldine.id]) request_data = { "title" => "Tell me about your chickens", @@ -15,12 +15,13 @@ describe ApiController, "when using the API" do post :create_request, :k => geraldine.api_key, :request_json => request_data.to_json response.should be_success - response.headers["Content-Type"].should == "application/json" + + response.content_type.should == "application/json" - response_body = response.body.from_json + response_body = ActiveSupport::JSON.decode(response.body) response_body["url"].should =~ /^http/ - InfoRequest.count(:conditions = ["public_body_id = ?", geraldine.id]).should == number_of_requests + 1 + InfoRequest.count(:conditions => ["public_body_id = ?", geraldine.id]).should == number_of_requests + 1 new_request = InfoRequest.find(response_body["id"]) new_request.user_id.should be_nil -- cgit v1.2.3 From c2681d8effb64e4f49ab17d867a5616613dea4f7 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 10:21:36 +0100 Subject: API create_request method is working "when using the API", it "should create a new request from a POST", AND IT DOES! --- spec/controllers/api_controller_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index dec652361..9d072e359 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -19,6 +19,7 @@ describe ApiController, "when using the API" do response.content_type.should == "application/json" response_body = ActiveSupport::JSON.decode(response.body) + response_body["errors"].should be_nil response_body["url"].should =~ /^http/ InfoRequest.count(:conditions => ["public_body_id = ?", geraldine.id]).should == number_of_requests + 1 @@ -29,6 +30,6 @@ describe ApiController, "when using the API" do new_request.external_url.should == request_data["external_url"] new_request.title.should == request_data["title"] - new_request.last_event_forming_initial_request.outgoing_message.body.should == request_data["body"] + new_request.last_event_forming_initial_request.outgoing_message.body.should == request_data["body"].strip end end -- cgit v1.2.3 From ceefc42a75a06f693fe614d345e619ace5014d23 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 14:49:40 +0100 Subject: API: we can add a response to a request --- spec/controllers/api_controller_spec.rb | 60 +++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index 9d072e359..9d9d95215 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -1,6 +1,23 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe ApiController, "when using the API" do + it "should check the API key" do + request_data = { + "title" => "Tell me about your chickens", + "body" => "Dear Sir,\n\nI should like to know about your chickens.\n\nYours in faith,\nBob\n", + + "external_url" => "http://www.example.gov.uk/foi/chickens_23", + "external_user_name" => "Bob Smith", + } + + number_of_requests = InfoRequest.count + expect { + post :create_request, :k => "This is not really an API key", :request_json => request_data.to_json + }.to raise_error ApplicationController::PermissionDenied + + InfoRequest.count.should == number_of_requests + end + it "should create a new request from a POST" do geraldine = public_bodies(:geraldine_public_body) number_of_requests = InfoRequest.count(:conditions => ["public_body_id = ?", geraldine.id]) @@ -31,5 +48,48 @@ describe ApiController, "when using the API" do new_request.title.should == request_data["title"] new_request.last_event_forming_initial_request.outgoing_message.body.should == request_data["body"].strip + + new_request.public_body_id.should == geraldine.id + end + + it "should add a response to a request" do + geraldine = public_bodies(:geraldine_public_body) + + # First we need to create a request + post :create_request, :k => geraldine.api_key, :request_json => { + "title" => "Tell me about your chickens", + "body" => "Dear Sir,\n\nI should like to know about your chickens.\n\nYours in faith,\nBob\n", + + "external_url" => "http://www.example.gov.uk/foi/chickens_23", + "external_user_name" => "Bob Smith", + }.to_json + response.content_type.should == "application/json" + request_id = ActiveSupport::JSON.decode(response.body)["id"] + IncomingMessage.count(:conditions => ["info_request_id = ?", request_id]).should == 0 + + # Now add a response + sent_at = "2012-05-28T12:35:39+01:00" + response_body = "Thank you for your request for information, which we are handling in accordance with the Freedom of Information Act 2000. You will receive a response within 20 working days or before the next full moon, whichever is sooner.\n\nYours sincerely,\nJohn Gandermulch,\nExample Council FOI Officer\n" + post :add_correspondence, :k => geraldine.api_key, :id => request_id, :correspondence_json => { + "direction" => "response", + "sent_at" => sent_at, + "body" => response_body + }.to_json + + response.should be_success + incoming_messages = IncomingMessage.all(:conditions => ["info_request_id = ?", request_id]) + incoming_messages.count.should == 1 + incoming_message = incoming_messages[0] + + incoming_message.sent_at.should == Time.iso8601(sent_at) + incoming_message.get_main_body_text_folded.should == response_body + end + + it "should allow attachments to be uploaded" do + + end + + it "should show information about a request" do + end end -- cgit v1.2.3 From 8e390112010abe9bb0a1831bae1ae66fcac17d7f Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 15:06:22 +0100 Subject: API: add followup to request You can add a followup to a request using the API. --- spec/controllers/api_controller_spec.rb | 74 +++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 13 deletions(-) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index 9d9d95215..cf05a0525 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -19,8 +19,12 @@ describe ApiController, "when using the API" do end it "should create a new request from a POST" do - geraldine = public_bodies(:geraldine_public_body) - number_of_requests = InfoRequest.count(:conditions => ["public_body_id = ?", geraldine.id]) + number_of_requests = InfoRequest.count( + :conditions => [ + "public_body_id = ?", + public_bodies(:geraldine_public_body).id + ] + ) request_data = { "title" => "Tell me about your chickens", @@ -30,7 +34,7 @@ describe ApiController, "when using the API" do "external_user_name" => "Bob Smith", } - post :create_request, :k => geraldine.api_key, :request_json => request_data.to_json + post :create_request, :k => public_bodies(:geraldine_public_body).api_key, :request_json => request_data.to_json response.should be_success response.content_type.should == "application/json" @@ -39,7 +43,10 @@ describe ApiController, "when using the API" do response_body["errors"].should be_nil response_body["url"].should =~ /^http/ - InfoRequest.count(:conditions => ["public_body_id = ?", geraldine.id]).should == number_of_requests + 1 + InfoRequest.count(:conditions => [ + "public_body_id = ?", + public_bodies(:geraldine_public_body).id] + ).should == number_of_requests + 1 new_request = InfoRequest.find(response_body["id"]) new_request.user_id.should be_nil @@ -49,14 +56,13 @@ describe ApiController, "when using the API" do new_request.title.should == request_data["title"] new_request.last_event_forming_initial_request.outgoing_message.body.should == request_data["body"].strip - new_request.public_body_id.should == geraldine.id + new_request.public_body_id.should == public_bodies(:geraldine_public_body).id end - it "should add a response to a request" do - geraldine = public_bodies(:geraldine_public_body) - - # First we need to create a request - post :create_request, :k => geraldine.api_key, :request_json => { + def _create_request + post :create_request, + :k => public_bodies(:geraldine_public_body).api_key, + :request_json => { "title" => "Tell me about your chickens", "body" => "Dear Sir,\n\nI should like to know about your chickens.\n\nYours in faith,\nBob\n", @@ -64,18 +70,29 @@ describe ApiController, "when using the API" do "external_user_name" => "Bob Smith", }.to_json response.content_type.should == "application/json" - request_id = ActiveSupport::JSON.decode(response.body)["id"] + return ActiveSupport::JSON.decode(response.body)["id"] + end + + it "should add a response to a request" do + # First we need to create a request + request_id = _create_request + + # Initially it has no incoming messages IncomingMessage.count(:conditions => ["info_request_id = ?", request_id]).should == 0 - # Now add a response + # Now add one sent_at = "2012-05-28T12:35:39+01:00" response_body = "Thank you for your request for information, which we are handling in accordance with the Freedom of Information Act 2000. You will receive a response within 20 working days or before the next full moon, whichever is sooner.\n\nYours sincerely,\nJohn Gandermulch,\nExample Council FOI Officer\n" - post :add_correspondence, :k => geraldine.api_key, :id => request_id, :correspondence_json => { + post :add_correspondence, + :k => public_bodies(:geraldine_public_body).api_key, + :id => request_id, + :correspondence_json => { "direction" => "response", "sent_at" => sent_at, "body" => response_body }.to_json + # And make sure it worked response.should be_success incoming_messages = IncomingMessage.all(:conditions => ["info_request_id = ?", request_id]) incoming_messages.count.should == 1 @@ -84,6 +101,37 @@ describe ApiController, "when using the API" do incoming_message.sent_at.should == Time.iso8601(sent_at) incoming_message.get_main_body_text_folded.should == response_body end + + it "should add a followup to a request" do + # First we need to create a request + request_id = _create_request + + # Initially it has one outgoing message + OutgoingMessage.count(:conditions => ["info_request_id = ?", request_id]).should == 1 + + # Add another, as a followup + sent_at = "2012-05-29T12:35:39+01:00" + followup_body = "Pls answer ASAP.\nkthxbye\n" + post :add_correspondence, + :k => public_bodies(:geraldine_public_body).api_key, + :id => request_id, + :correspondence_json => { + "direction" => "request", + "sent_at" => sent_at, + "body" => followup_body + }.to_json + + # Make sure it worked + response.should be_success + followup_messages = OutgoingMessage.all( + :conditions => ["info_request_id = ? and message_type = 'followup'", request_id] + ) + followup_messages.size.should == 1 + followup_message = followup_messages[0] + + followup_message.last_sent_at.should == Time.iso8601(sent_at) + followup_message.body.should == followup_body.strip + end it "should allow attachments to be uploaded" do -- cgit v1.2.3 From fd7b5b6006e6528372e8d6fb0c888e21848b1acf Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 15:22:46 +0100 Subject: API: test also for refusal conditions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The API must not allow people to update requests that they shouldn’t, i.e. only requests that were created by the same public body, using the API, can be added to using the API. --- spec/controllers/api_controller_spec.rb | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index cf05a0525..b08b1f527 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -133,6 +133,45 @@ describe ApiController, "when using the API" do followup_message.body.should == followup_body.strip end + it "should not allow internal requests to be updated" do + n_incoming_messages = IncomingMessage.count + n_outgoing_messages = OutgoingMessage.count + + expect { + post :add_correspondence, + :k => public_bodies(:geraldine_public_body).api_key, + :id => info_requests(:naughty_chicken_request).id, + :correspondence_json => { + "direction" => "request", + "sent_at" => Time.now.iso8601, + "body" => "xxx" + }.to_json + }.to raise_error ActiveRecord::RecordNotFound + + IncomingMessage.count.should == n_incoming_messages + OutgoingMessage.count.should == n_outgoing_messages + end + + it "should not allow other people’s requests to be updated" do + request_id = _create_request + n_incoming_messages = IncomingMessage.count + n_outgoing_messages = OutgoingMessage.count + + expect { + post :add_correspondence, + :k => public_bodies(:humpadink_public_body).api_key, + :id => request_id, + :correspondence_json => { + "direction" => "request", + "sent_at" => Time.now.iso8601, + "body" => "xxx" + }.to_json + }.to raise_error ActiveRecord::RecordNotFound + + IncomingMessage.count.should == n_incoming_messages + OutgoingMessage.count.should == n_outgoing_messages + end + it "should allow attachments to be uploaded" do end -- cgit v1.2.3 From a72660063d1add8a7cd4bcc540496249b2a4a0aa Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 15:46:01 +0100 Subject: API: Some testing of show_request --- spec/controllers/api_controller_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index b08b1f527..cd47cc2f2 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -177,6 +177,19 @@ describe ApiController, "when using the API" do end it "should show information about a request" do + info_request = info_requests(:naughty_chicken_request) + get :show_request, + :k => public_bodies(:geraldine_public_body).api_key, + :id => info_request.id + response.should be_success + assigns[:request].id.should == info_request.id + + r = ActiveSupport::JSON.decode(response.body) + r["title"].should == info_request.title + # Let’s not test all the fields here, because it would + # essentially just be a matter of copying the code that + # assigns them and changing assignment to an equality + # check, which does not really test anything at all. end end -- cgit v1.2.3 From 2ce2fde7db39f66bff7a8093150ed16e3619af16 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 16:01:21 +0100 Subject: Use a fixture for the external request --- spec/controllers/api_controller_spec.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index cd47cc2f2..e5422d48a 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -74,8 +74,8 @@ describe ApiController, "when using the API" do end it "should add a response to a request" do - # First we need to create a request - request_id = _create_request + # First we need an external request + request_id = info_requests(:external_request).id # Initially it has no incoming messages IncomingMessage.count(:conditions => ["info_request_id = ?", request_id]).should == 0 @@ -104,7 +104,7 @@ describe ApiController, "when using the API" do it "should add a followup to a request" do # First we need to create a request - request_id = _create_request + request_id = info_requests(:external_request).id # Initially it has one outgoing message OutgoingMessage.count(:conditions => ["info_request_id = ?", request_id]).should == 1 @@ -172,7 +172,11 @@ describe ApiController, "when using the API" do OutgoingMessage.count.should == n_outgoing_messages end - it "should allow attachments to be uploaded" do + it "should not allow files to be attached to a followup" do + + end + + it "should allow files to be attached to a response" do end -- cgit v1.2.3 From f5cd4f050a80a23ea0dead8bcaf9b79ce07cb18a Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 16:05:30 +0100 Subject: Correct comment\ --- spec/controllers/api_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index e5422d48a..5a5496556 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -103,7 +103,7 @@ describe ApiController, "when using the API" do end it "should add a followup to a request" do - # First we need to create a request + # First we need an external request request_id = info_requests(:external_request).id # Initially it has one outgoing message -- cgit v1.2.3 From 198083920f4cbcf50e61eab9e3d74edda1405e22 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 16:24:25 +0100 Subject: Test for attaching a file via the API Not yet implemented, so the test fails. --- spec/controllers/api_controller_spec.rb | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index 5a5496556..9cd4c0820 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -177,7 +177,43 @@ describe ApiController, "when using the API" do end it "should allow files to be attached to a response" do + # First we need an external request + request_id = info_requests(:external_request).id + + # Initially it has no incoming messages + IncomingMessage.count(:conditions => ["info_request_id = ?", request_id]).should == 0 + + # Now add one + sent_at = "2012-05-28T12:35:39+01:00" + response_body = "Thank you for your request for information, which we are handling in accordance with the Freedom of Information Act 2000. You will receive a response within 20 working days or before the next full moon, whichever is sooner.\n\nYours sincerely,\nJohn Gandermulch,\nExample Council FOI Officer\n" + post :add_correspondence, + :k => public_bodies(:geraldine_public_body).api_key, + :id => request_id, + :correspondence_json => { + "direction" => "response", + "sent_at" => sent_at, + "body" => response_body + }.to_json, + :attachments => [ + fixture_file_upload("files/tfl.pdf") + ] + + # And make sure it worked + response.should be_success + incoming_messages = IncomingMessage.all(:conditions => ["info_request_id = ?", request_id]) + incoming_messages.count.should == 1 + incoming_message = incoming_messages[0] + + incoming_message.sent_at.should == Time.iso8601(sent_at) + incoming_message.get_main_body_text_folded.should == response_body + + # Get the attachment + attachments = incoming_message.get_attachments_for_display + attachments.size.should == 1 + attachment = attachments[0] + attachment.filename.should == "tfl.pdf" + attachment.body.should == open("files/tfl.pdf", &:read) end it "should show information about a request" do -- cgit v1.2.3 From e19238dd262e3254c0cd269f2f3b28f3cee0fd09 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 28 May 2012 17:00:34 +0100 Subject: WIP attachment uploading via API --- spec/controllers/api_controller_spec.rb | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index 9cd4c0820..a39471775 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -173,7 +173,23 @@ describe ApiController, "when using the API" do end it "should not allow files to be attached to a followup" do + post :add_correspondence, + :k => public_bodies(:geraldine_public_body).api_key, + :id => info_requests(:external_request).id, + :correspondence_json => { + "direction" => "request", + "sent_at" => Time.now.iso8601, + "body" => "Are you joking, or are you serious?" + }.to_json, + :attachments => [ + fixture_file_upload("files/tfl.pdf") + ] + + # Make sure it worked + response.status.to_i.should == 500 + errors = ActiveSupport::JSON.decode(response.body)["errors"] + errors.should == ["You cannot attach files to messages in the 'request' direction"] end it "should allow files to be attached to a response" do @@ -190,10 +206,10 @@ describe ApiController, "when using the API" do :k => public_bodies(:geraldine_public_body).api_key, :id => request_id, :correspondence_json => { - "direction" => "response", - "sent_at" => sent_at, - "body" => response_body - }.to_json, + "direction" => "response", + "sent_at" => sent_at, + "body" => response_body + }.to_json, :attachments => [ fixture_file_upload("files/tfl.pdf") ] -- cgit v1.2.3 From 89459d3902583fa3d6dad78462d2bf2fa6f94db6 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 18 Jun 2012 12:55:11 +0100 Subject: API controller tests all pass --- spec/controllers/api_controller_spec.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'spec/controllers/api_controller_spec.rb') diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index a39471775..1f65576b6 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -1,5 +1,17 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +def normalise_whitespace(s) + s = s.gsub(/^\s+|\s+$/, "") + s = s.gsub(/\s+/, " ") + return s +end + +Spec::Matchers.define :be_equal_modulo_whitespace_to do |expected| + match do |actual| + normalise_whitespace(actual) == normalise_whitespace(expected) + end +end + describe ApiController, "when using the API" do it "should check the API key" do request_data = { @@ -99,7 +111,7 @@ describe ApiController, "when using the API" do incoming_message = incoming_messages[0] incoming_message.sent_at.should == Time.iso8601(sent_at) - incoming_message.get_main_body_text_folded.should == response_body + incoming_message.get_main_body_text_folded.should be_equal_modulo_whitespace_to(response_body) end it "should add a followup to a request" do @@ -221,7 +233,7 @@ describe ApiController, "when using the API" do incoming_message = incoming_messages[0] incoming_message.sent_at.should == Time.iso8601(sent_at) - incoming_message.get_main_body_text_folded.should == response_body + incoming_message.get_main_body_text_folded.should be_equal_modulo_whitespace_to(response_body) # Get the attachment attachments = incoming_message.get_attachments_for_display @@ -229,7 +241,7 @@ describe ApiController, "when using the API" do attachment = attachments[0] attachment.filename.should == "tfl.pdf" - attachment.body.should == open("files/tfl.pdf", &:read) + attachment.body.should == load_file_fixture("tfl.pdf") end it "should show information about a request" do -- cgit v1.2.3