From 9839e0e23ba78b405779b1a9c9d1e41f02991ebd Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Tue, 4 Sep 2012 16:23:03 +0100 Subject: API errors should be JSON The API was returning Rails (HTML) errors for certain error conditions, which is inconvenient because it makes it difficult for the client to extract the error message. This patch changes add_correspondence to return JSON errors (still with suitable HTTP status codes) for two common exceptional conditions, and adds tests. --- spec/controllers/api_controller_spec.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 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 98751a93a..f9296e7e1 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -314,4 +314,36 @@ describe ApiController, "when using the API" do response.should be_success assigns[:event_data].should == [first_event] end + + it "should return a JSON 404 error for non-existent requests" do + request_id = 123459876 # Let's hope this doesn't exist! + 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 + response.status.should == "404 Not Found" + ActiveSupport::JSON.decode(response.body)["errors"].should == ["Could not find request 123459876"] + end + + it "should return a JSON 500 error if we try to add correspondence to a request we don't own" do + request_id = info_requests(:naughty_chicken_request).id + 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 + response.status.should == "500 Internal Server Error" + ActiveSupport::JSON.decode(response.body)["errors"].should == ["Request #{request_id} cannot be updated using the API"] + end end -- cgit v1.2.3 From f49f9e3b187b45f7be8b7d8b3243eee1af50937c Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Fri, 7 Sep 2012 10:41:01 +0100 Subject: Update other tests Update the other API tests to take account of the changed error behaviour. --- spec/controllers/api_controller_spec.rb | 45 ++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 20 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 f9296e7e1..925b7adb4 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -149,16 +149,19 @@ describe ApiController, "when using the API" 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 + request_id = info_requests(:naughty_chicken_request).id + post :add_correspondence, + :k => public_bodies(:geraldine_public_body).api_key, + :id => request_id, + :correspondence_json => { + "direction" => "request", + "sent_at" => Time.now.iso8601, + "body" => "xxx" + }.to_json + + response.status.should == "500 Internal Server Error" + ActiveSupport::JSON.decode(response.body)["errors"].should == [ + "Request #{request_id} cannot be updated using the API"] IncomingMessage.count.should == n_incoming_messages OutgoingMessage.count.should == n_outgoing_messages @@ -169,16 +172,18 @@ describe ApiController, "when using the API" do 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 + 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 + + response.status.should == "500 Internal Server Error" + ActiveSupport::JSON.decode(response.body)["errors"].should == [ + "You do not own request #{request_id}"] IncomingMessage.count.should == n_incoming_messages OutgoingMessage.count.should == n_outgoing_messages -- cgit v1.2.3 From 938cb83a9405bb1e5f49f4841adc3657e73d85bd Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Mon, 10 Sep 2012 17:10:44 +0100 Subject: Add a since_date parameter to the API feed --- spec/controllers/api_controller_spec.rb | 15 +++++++++++++++ 1 file changed, 15 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 925b7adb4..ded9a040a 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -320,6 +320,21 @@ describe ApiController, "when using the API" do assigns[:event_data].should == [first_event] end + it "should honour the since_date parameter for the Atom feed" do + get :body_request_events, + :id => public_bodies(:humpadink_public_body).id, + :k => public_bodies(:humpadink_public_body).api_key, + :since_date => "2010-01-01", + :feed_type => "atom" + + response.should be_success + response.should render_template("api/request_events.atom") + assigns[:events].size.should > 0 + assigns[:events].each do |event| + event.created_at.should >= Date.new(2010, 1, 1) + end + end + it "should return a JSON 404 error for non-existent requests" do request_id = 123459876 # Let's hope this doesn't exist! sent_at = "2012-05-28T12:35:39+01:00" -- cgit v1.2.3