aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/api_controller_spec.rb263
-rw-r--r--spec/controllers/request_controller_spec.rb35
-rw-r--r--spec/fixtures/info_request_events.yml10
-rw-r--r--spec/fixtures/info_requests.yml14
-rw-r--r--spec/fixtures/outgoing_messages.yml11
-rw-r--r--spec/fixtures/public_bodies.yml6
-rw-r--r--spec/models/incoming_message_spec.rb1
-rw-r--r--spec/models/xapian_spec.rb8
-rw-r--r--spec/views/public_body/show.rhtml_spec.rb5
-rw-r--r--spec/views/request/list.rhtml_spec.rb3
10 files changed, 344 insertions, 12 deletions
diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb
new file mode 100644
index 000000000..1f65576b6
--- /dev/null
+++ b/spec/controllers/api_controller_spec.rb
@@ -0,0 +1,263 @@
+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 = {
+ "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
+ number_of_requests = InfoRequest.count(
+ :conditions => [
+ "public_body_id = ?",
+ public_bodies(:geraldine_public_body).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 => public_bodies(:geraldine_public_body).api_key, :request_json => request_data.to_json
+ response.should be_success
+
+ 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 = ?",
+ 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
+ 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"].strip
+
+ new_request.public_body_id.should == public_bodies(:geraldine_public_body).id
+ end
+
+ 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",
+
+ "external_url" => "http://www.example.gov.uk/foi/chickens_23",
+ "external_user_name" => "Bob Smith",
+ }.to_json
+ response.content_type.should == "application/json"
+ return ActiveSupport::JSON.decode(response.body)["id"]
+ end
+
+ it "should add a response to a request" 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
+
+ # 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 be_equal_modulo_whitespace_to(response_body)
+ end
+
+ it "should add a followup to a request" do
+ # First we need an external request
+ request_id = info_requests(:external_request).id
+
+ # 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 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 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
+ # 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 be_equal_modulo_whitespace_to(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 == load_file_fixture("tfl.pdf")
+ 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
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb
index 01a663bf8..530e9b2c3 100644
--- a/spec/controllers/request_controller_spec.rb
+++ b/spec/controllers/request_controller_spec.rb
@@ -193,6 +193,7 @@ describe RequestController, "when changing things that appear on the request pag
end
describe RequestController, "when showing one request" do
+ integrate_views
before(:each) do
load_raw_emails_data
@@ -209,6 +210,12 @@ describe RequestController, "when showing one request" do
response.should render_template('show')
end
+ it "should show the request" do
+ get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
+ response.should be_success
+ response.body.should include("Why do you have such a fancy dog?")
+ end
+
it "should assign the request" do
get :show, :url_title => 'why_do_you_have_such_a_fancy_dog'
assigns[:info_request].should == info_requests(:fancy_dog_request)
@@ -1486,7 +1493,7 @@ describe RequestController, "sending unclassified new response reminder alerts"
deliveries = ActionMailer::Base.deliveries
deliveries.size.should == 3 # sufficiently late it sends reminders too
mail = deliveries[0]
- mail.body.should =~ /To let us know/
+ mail.body.should =~ /To let everyone know/
mail.to_addrs.first.to_s.should == info_requests(:fancy_dog_request).user.name_and_email
mail.body =~ /(http:\/\/.*\/c\/(.*))/
mail_url = $1
@@ -1843,9 +1850,17 @@ describe RequestController, "when showing similar requests" do
end
-describe RequestController, "when reporting a request" do
- integrate_views
+describe RequestController, "when reporting a request when not logged in" do
+ it "should only allow logged-in users to report requests" do
+ get :report_request, :url_title => info_requests(:badger_request).url_title
+ post_redirect = PostRedirect.get_last_post_redirect
+ response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token)
+ end
+end
+describe RequestController, "when reporting a request (logged in)" do
+ integrate_views
+
before do
@user = users(:robin_user)
session[:user_id] = @user.id
@@ -1856,19 +1871,29 @@ describe RequestController, "when reporting a request" do
title = ir.url_title
get :show, :url_title => title
assigns[:info_request].attention_requested.should == false
+
post :report_request, :url_title => title
+ response.should redirect_to(:action => :show, :url_title => title)
+
get :show, :url_title => title
+ response.should be_success
assigns[:info_request].attention_requested.should == true
assigns[:info_request].described_state.should == "attention_requested"
end
it "should not allow a request to be reported twice" do
title = info_requests(:badger_request).url_title
+
post :report_request, :url_title => title
+ response.should redirect_to(:action => :show, :url_title => title)
get :show, :url_title => title
+ response.should be_success
response.body.should include("has been reported")
+
post :report_request, :url_title => title
+ response.should redirect_to(:action => :show, :url_title => title)
get :show, :url_title => title
+ response.should be_success
response.body.should include("has already been reported")
end
@@ -1876,10 +1901,14 @@ describe RequestController, "when reporting a request" do
title = info_requests(:badger_request).url_title
get :show, :url_title => title
response.body.should include("Offensive?")
+
post :report_request, :url_title => title
+ response.should redirect_to(:action => :show, :url_title => title)
+
get :show, :url_title => title
response.body.should_not include("Offensive?")
response.body.should include("This request has been reported")
+
info_requests(:badger_request).set_described_state("successful")
get :show, :url_title => title
response.body.should_not include("This request has been reported")
diff --git a/spec/fixtures/info_request_events.yml b/spec/fixtures/info_request_events.yml
index 3266ec634..e3e8a4460 100644
--- a/spec/fixtures/info_request_events.yml
+++ b/spec/fixtures/info_request_events.yml
@@ -150,3 +150,13 @@ spam_2_incoming_message_event:
described_state: successful
calculated_state: successful
+external_outgoing_message_event:
+ id: 914
+ params_yaml: "--- \n\
+ :outgoing_message_id: 8\n"
+ outgoing_message_id: 8
+ info_request_id: 109
+ event_type: sent
+ created_at: 2009-01-02 02:23:45.6789100
+ described_state: waiting_response
+ calculated_state: waiting_response
diff --git a/spec/fixtures/info_requests.yml b/spec/fixtures/info_requests.yml
index 33e9a16f2..e4f2287c0 100644
--- a/spec/fixtures/info_requests.yml
+++ b/spec/fixtures/info_requests.yml
@@ -61,7 +61,7 @@ spam_1_request:
title: Cheap v1agra
url_title: spam_1
created_at: 2010-01-01 01:23:45.6789100
- created_at: 2010-01-01 01:23:45.6789100
+ updated_at: 2010-01-01 01:23:45.6789100
public_body_id: 5
user_id: 5
described_state: successful
@@ -72,9 +72,19 @@ spam_2_request:
title: Cheap v1agra
url_title: spam_2
created_at: 2010-01-01 02:23:45.6789100
- created_at: 2010-01-01 02:23:45.6789100
+ updated_at: 2010-01-01 02:23:45.6789100
public_body_id: 6
user_id: 5
described_state: successful
awaiting_description: false
idhash: 173fd005
+external_request:
+ id: 109
+ title: Balalas
+ url_title: balalas
+ external_user_name: Bob Smith
+ external_url: http://www.example.org/request/balala
+ public_body_id: 2
+ described_state: waiting_response
+ awaiting_description: false
+ idhash: a1234567
diff --git a/spec/fixtures/outgoing_messages.yml b/spec/fixtures/outgoing_messages.yml
index d33ca4292..32b322bd7 100644
--- a/spec/fixtures/outgoing_messages.yml
+++ b/spec/fixtures/outgoing_messages.yml
@@ -86,3 +86,14 @@ spam_2_outgoing_message:
updated_at: 2007-01-12 02:56:58.586598
what_doing: normal_sort
+external_outgoing_message:
+ id: 8
+ info_request_id: 109
+ message_type: initial_request
+ status: sent
+ body: "I should like to know about balalas."
+ last_sent_at: 2009-01-12 01:57:58.586598
+ created_at: 2009-01-12 01:56:58.586598
+ updated_at: 2009-01-12 01:56:58.586598
+ what_doing: normal_sort
+
diff --git a/spec/fixtures/public_bodies.yml b/spec/fixtures/public_bodies.yml
index a0893f1e5..367e0fc50 100644
--- a/spec/fixtures/public_bodies.yml
+++ b/spec/fixtures/public_bodies.yml
@@ -10,6 +10,7 @@ geraldine_public_body:
short_name: TGQ
url_name: tgq
created_at: 2007-10-24 10:51:01.161639
+ api_key: 1
humpadink_public_body:
name: "Department for Humpadinking"
first_letter: D
@@ -23,6 +24,7 @@ humpadink_public_body:
url_name: dfh
created_at: 2007-10-25 10:51:01.161639
notes: An albatross told me!!!
+ api_key: 2
forlorn_public_body:
name: "Department of Loneliness"
first_letter: D
@@ -36,6 +38,7 @@ forlorn_public_body:
url_name: lonely
created_at: 2011-01-26 14:11:02.12345
notes: A very lonely public body that no one has corresponded with
+ api_key: 3
silly_walks_public_body:
id: 5
version: 1
@@ -49,6 +52,7 @@ silly_walks_public_body:
url_name: msw
created_at: 2007-10-25 10:51:01.161639
notes: You know the one.
+ api_key: 4
sensible_walks_public_body:
id: 6
version: 1
@@ -62,3 +66,5 @@ sensible_walks_public_body:
last_edit_comment: Another stunning innovation from your friendly national government
last_edit_editor: robin
created_at: 2008-10-25 10:51:01.161639
+ api_key: 5
+
diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb
index 6cfd2eb64..c6658905c 100644
--- a/spec/models/incoming_message_spec.rb
+++ b/spec/models/incoming_message_spec.rb
@@ -120,6 +120,7 @@ describe IncomingMessage, " folding quoted parts of emails" do
@user.stub!(:name).and_return("Sir [ Bobble")
@info_request = mock_model(InfoRequest)
@info_request.stub!(:user).and_return(@user)
+ @info_request.stub!(:user_name).and_return(@user.name)
@incoming_message = IncomingMessage.new()
@incoming_message.info_request = @info_request
diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb
index 81c066184..195b39eee 100644
--- a/spec/models/xapian_spec.rb
+++ b/spec/models/xapian_spec.rb
@@ -76,13 +76,13 @@ describe PublicBody, " when indexing requests by body they are to" do
it "should find requests to the body" do
xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1)
- xapian_object.results.size.should == 4
+ xapian_object.results.size.should == PublicBody.find_by_url_name("tgq").info_requests.map(&:info_request_events).flatten.size
end
it "should update index correctly when URL name of body changes" do
# initial search
xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1)
- xapian_object.results.size.should == 4
+ xapian_object.results.size.should == PublicBody.find_by_url_name("tgq").info_requests.map(&:info_request_events).flatten.size
models_found_before = xapian_object.results.map { |x| x[:model] }
# change the URL name of the body
@@ -96,7 +96,7 @@ describe PublicBody, " when indexing requests by body they are to" do
xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 0
xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:gq", 'created_at', true, nil, 100, 1)
- xapian_object.results.size.should == 4
+ xapian_object.results.size.should == PublicBody.find_by_url_name("gq").info_requests.map(&:info_request_events).flatten.size
models_found_after = xapian_object.results.map { |x| x[:model] }
models_found_before.should == models_found_after
@@ -118,7 +118,7 @@ describe PublicBody, " when indexing requests by body they are to" do
xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:gq", 'created_at', true, nil, 100, 1)
xapian_object.results.size.should == 0
xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:" + body.url_name, 'created_at', true, nil, 100, 1)
- xapian_object.results.size.should == 4
+ xapian_object.results.size.should == public_bodies(:geraldine_public_body).info_requests.map(&:info_request_events).flatten.size
models_found_after = xapian_object.results.map { |x| x[:model] }
end
end
diff --git a/spec/views/public_body/show.rhtml_spec.rb b/spec/views/public_body/show.rhtml_spec.rb
index a37d8be0d..1d21f80c4 100644
--- a/spec/views/public_body/show.rhtml_spec.rb
+++ b/spec/views/public_body/show.rhtml_spec.rb
@@ -102,9 +102,10 @@ def mock_event
:info_request => mock_model(InfoRequest,
:title => 'Title',
:url_title => 'title',
- :display_status => 'awaiting_response',
- :calculate_status => 'awaiting_response',
+ :display_status => 'waiting_response',
+ :calculate_status => 'waiting_response',
:public_body => @pb,
+ :is_external? => false,
:user => mock_model(User, :name => 'Test User', :url_name => 'testuser')
),
:incoming_message => nil, :is_incoming_message? => false,
diff --git a/spec/views/request/list.rhtml_spec.rb b/spec/views/request/list.rhtml_spec.rb
index c7067294f..94ece5e76 100644
--- a/spec/views/request/list.rhtml_spec.rb
+++ b/spec/views/request/list.rhtml_spec.rb
@@ -19,7 +19,8 @@ describe "when listing recent requests" do
:display_status => 'awaiting_response',
:calculate_status => 'awaiting_response',
:public_body => mock_model(PublicBody, :name => 'Test Quango', :url_name => 'testquango'),
- :user => mock_model(User, :name => 'Test User', :url_name => 'testuser')
+ :user => mock_model(User, :name => 'Test User', :url_name => 'testuser'),
+ :is_external? => false
),
:incoming_message => nil, :is_incoming_message? => false,
:outgoing_message => nil, :is_outgoing_message? => false,