aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/api_controller_spec.rb
diff options
context:
space:
mode:
authorRobin Houston <robin.houston@gmail.com>2012-09-04 16:23:03 +0100
committerRobin Houston <robin.houston@gmail.com>2012-09-04 16:23:03 +0100
commit9839e0e23ba78b405779b1a9c9d1e41f02991ebd (patch)
treed07209a4ad2003111c03948d537127d40dda9799 /spec/controllers/api_controller_spec.rb
parent62a20d6696275a6f83ca4cf835c487873ca89c99 (diff)
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.
Diffstat (limited to 'spec/controllers/api_controller_spec.rb')
-rw-r--r--spec/controllers/api_controller_spec.rb32
1 files changed, 32 insertions, 0 deletions
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