aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'spec/controllers')
-rw-r--r--spec/controllers/admin_public_body_controller_spec.rb51
-rw-r--r--spec/controllers/api_controller_spec.rb3
-rw-r--r--spec/controllers/application_controller_spec.rb22
-rw-r--r--spec/controllers/general_controller_spec.rb136
-rw-r--r--spec/controllers/public_body_controller_spec.rb1
5 files changed, 143 insertions, 70 deletions
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb
index be33802c5..504ddc5cc 100644
--- a/spec/controllers/admin_public_body_controller_spec.rb
+++ b/spec/controllers/admin_public_body_controller_spec.rb
@@ -166,6 +166,13 @@ describe AdminPublicBodyController, "when administering public bodies and paying
config['SKIP_ADMIN_AUTH'] = true
end
+ def setup_emergency_credentials(username, password)
+ config = MySociety::Config.load_default()
+ config['SKIP_ADMIN_AUTH'] = false
+ config['ADMIN_USERNAME'] = username
+ config['ADMIN_PASSWORD'] = password
+ @request.env["HTTP_AUTHORIZATION"] = ""
+ end
it "disallows non-authenticated users to do anything" do
@request.env["HTTP_AUTHORIZATION"] = ""
@@ -180,19 +187,14 @@ describe AdminPublicBodyController, "when administering public bodies and paying
config = MySociety::Config.load_default()
config['SKIP_ADMIN_AUTH'] = true
@request.env["HTTP_AUTHORIZATION"] = ""
-
n = PublicBody.count
post :destroy, { :id => public_bodies(:forlorn_public_body).id }
PublicBody.count.should == n - 1
session[:using_admin].should == 1
end
- it "doesn't let people with bad credentials log in" do
- config = MySociety::Config.load_default()
- config['SKIP_ADMIN_AUTH'] = false
- config['ADMIN_USERNAME'] = 'biz'
- config['ADMIN_PASSWORD'] = 'fuz'
- @request.env["HTTP_AUTHORIZATION"] = ""
+ it "doesn't let people with bad emergency account credentials log in" do
+ setup_emergency_credentials('biz', 'fuz')
n = PublicBody.count
basic_auth_login(@request, "baduser", "badpassword")
post :destroy, { :id => public_bodies(:forlorn_public_body).id }
@@ -201,12 +203,8 @@ describe AdminPublicBodyController, "when administering public bodies and paying
session[:using_admin].should == nil
end
- it "allows people with good credentials log in using HTTP Basic Auth" do
- config = MySociety::Config.load_default()
- config['SKIP_ADMIN_AUTH'] = false
- config['ADMIN_USERNAME'] = 'biz'
- config['ADMIN_PASSWORD'] = 'fuz'
- @request.env["HTTP_AUTHORIZATION"] = ""
+ it "allows people with good emergency account credentials log in using HTTP Basic Auth" do
+ setup_emergency_credentials('biz', 'fuz')
n = PublicBody.count
basic_auth_login(@request, "biz", "fuz")
post :show, { :id => public_bodies(:humpadink_public_body).id, :emergency => 1}
@@ -235,6 +233,33 @@ describe AdminPublicBodyController, "when administering public bodies and paying
PublicBody.count.should == n
session[:using_admin].should == nil
end
+
+ describe 'when asked for the admin current user' do
+
+ it 'returns the emergency account name for someone who logged in with the emergency account' do
+ setup_emergency_credentials('biz', 'fuz')
+ basic_auth_login(@request, "biz", "fuz")
+ post :show, { :id => public_bodies(:humpadink_public_body).id, :emergency => 1 }
+ controller.send(:admin_current_user).should == 'biz'
+ end
+
+ it 'returns the current user url_name for a superuser' do
+ session[:user_id] = users(:admin_user).id
+ @request.env["HTTP_AUTHORIZATION"] = ""
+ post :show, { :id => public_bodies(:humpadink_public_body).id }
+ controller.send(:admin_current_user).should == users(:admin_user).url_name
+ end
+
+ it 'returns the REMOTE_USER value from the request environment when skipping admin auth' do
+ config = MySociety::Config.load_default()
+ config['SKIP_ADMIN_AUTH'] = true
+ @request.env["HTTP_AUTHORIZATION"] = ""
+ @request.env["REMOTE_USER"] = "i_am_admin"
+ post :show, { :id => public_bodies(:humpadink_public_body).id }
+ controller.send(:admin_current_user).should == "i_am_admin"
+ end
+
+ end
end
describe AdminPublicBodyController, "when administering public bodies with i18n" do
diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb
index ded9a040a..8d8a39950 100644
--- a/spec/controllers/api_controller_spec.rb
+++ b/spec/controllers/api_controller_spec.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
def normalise_whitespace(s)
@@ -167,7 +168,7 @@ describe ApiController, "when using the API" do
OutgoingMessage.count.should == n_outgoing_messages
end
- it "should not allow other people’s requests to be updated" do
+ 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
diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb
index f16cee312..08e68d85e 100644
--- a/spec/controllers/application_controller_spec.rb
+++ b/spec/controllers/application_controller_spec.rb
@@ -2,41 +2,41 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'fakeweb'
describe ApplicationController, "when accessing third party services" do
+
before (:each) do
FakeWeb.clean_registry
end
+
after (:each) do
FakeWeb.clean_registry
end
+
it "should succeed if the service responds OK" do
- config = MySociety::Config.load_default()
- config['GAZE_URL'] = 'http://denmark.com'
+ Configuration.stub!(:gaze_url).and_return('http://denmark.com')
FakeWeb.register_uri(:get, %r|denmark.com|, :body => "DK")
country = self.controller.send :country_from_ip
country.should == "DK"
end
it "should fail silently if the country_from_ip domain doesn't exist" do
- config = MySociety::Config.load_default()
- config['GAZE_URL'] = 'http://12123sdf14qsd.com'
+ Configuration.stub!(:gaze_url).and_return('http://12123sdf14qsd.com')
country = self.controller.send :country_from_ip
- country.should == config['ISO_COUNTRY_CODE']
+ country.should == Configuration.iso_country_code
end
it "should fail silently if the country_from_ip service doesn't exist" do
- config = MySociety::Config.load_default()
- config['GAZE_URL'] = 'http://www.google.com'
+ Configuration.stub!(:gaze_url).and_return('http://www.google.com')
country = self.controller.send :country_from_ip
- country.should == config['ISO_COUNTRY_CODE']
+ country.should == Configuration.iso_country_code
end
it "should fail silently if the country_from_ip service returns an error" do
FakeWeb.register_uri(:get, %r|500.com|, :body => "Error", :status => ["500", "Error"])
- config = MySociety::Config.load_default()
- config['GAZE_URL'] = 'http://500.com'
+ Configuration.stub!(:gaze_url).and_return('http://500.com')
country = self.controller.send :country_from_ip
- country.should == config['ISO_COUNTRY_CODE']
+ country.should == Configuration.iso_country_code
end
end
describe ApplicationController, "when caching fragments" do
+
it "should not fail with long filenames" do
long_name = "blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah.txt"
path = self.controller.send(:foi_fragment_cache_path, long_name)
diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb
index de8dd8422..935f8eab6 100644
--- a/spec/controllers/general_controller_spec.rb
+++ b/spec/controllers/general_controller_spec.rb
@@ -17,12 +17,38 @@ describe GeneralController, "when trying to show the blog" do
end
end
-describe GeneralController, "when searching" do
+describe GeneralController, 'when getting the blog feed' do
+
+ it 'should add a lang param correctly to a url with no querystring' do
+ Configuration.stub!(:blog_feed).and_return("http://blog.example.com")
+ get :blog
+ assigns[:feed_url].should == "http://blog.example.com?lang=en"
+ end
+
+ it 'should add a lang param correctly to a url with an existing querystring' do
+ Configuration.stub!(:blog_feed).and_return("http://blog.example.com?alt=rss")
+ get :blog
+ assigns[:feed_url].should == "http://blog.example.com?alt=rss&lang=en"
+ end
+
+end
+
+describe GeneralController, "when showing the frontpage" do
+
integrate_views
- before(:each) do
- load_raw_emails_data
- rebuild_xapian_index
+ before do
+ public_body = mock_model(PublicBody, :name => "Example Public Body",
+ :url_name => 'example_public_body')
+ info_request = mock_model(InfoRequest, :public_body => public_body,
+ :title => 'Example Request',
+ :url_title => 'example_request')
+ info_request_event = mock_model(InfoRequestEvent, :created_at => Time.now,
+ :info_request => info_request,
+ :described_at => Time.now,
+ :search_text_main => 'example text')
+ xapian_result = mock('xapian result', :results => [{:model => info_request_event}])
+ controller.stub!(:perform_search).and_return(xapian_result)
end
it "should render the front page successfully" do
@@ -71,11 +97,6 @@ describe GeneralController, "when searching" do
response.should be_success
end
- it "should redirect from search query URL to pretty URL" do
- post :search_redirect, :query => "mouse" # query hidden in POST parameters
- response.should redirect_to(:action => 'search', :combined => "mouse", :view => "all") # URL /search/:query/all
- end
-
describe "when using different locale settings" do
home_link_regex = /href=".*\/en\//
it "should generate URLs with a locale prepended when there's more than one locale set" do
@@ -117,23 +138,41 @@ describe GeneralController, "when searching" do
I18n.available_locales = old_i18n_available_locales
end
end
+end
+describe GeneralController, "when showing the front page with fixture data" do
describe 'when constructing the list of recent requests' do
+
before(:each) do
- load_raw_emails_data
- rebuild_xapian_index
+ rebuild_xapian_index
end
- it 'should list the newest successful request first' do
- # Make sure the newest is listed first even if an older one
- # has a newer comment or was reclassified more recently:
- # https://github.com/mysociety/alaveteli/issues/370
- #
- # This is a deliberate behaviour change, in that the
- # previous behaviour (showing more-recently-reclassified
- # requests first) was intentional.
- get :frontpage
- assigns[:request_events].first.info_request.should == info_requests(:another_boring_request)
+ describe 'when there are fewer than five successful requests' do
+
+ it 'should list the most recently sent and successful requests by the creation date of the
+ request event' do
+ # Make sure the newest response is listed first even if a request
+ # with an older response has a newer comment or was reclassified more recently:
+ # https://github.com/mysociety/alaveteli/issues/370
+ #
+ # This is a deliberate behaviour change, in that the
+ # previous behaviour (showing more-recently-reclassified
+ # requests first) was intentional.
+ get :frontpage
+
+ request_events = assigns[:request_events]
+ previous = nil
+ request_events.each do |event|
+ if previous
+ previous.created_at.should be >= event.created_at
+ end
+ ['sent', 'response'].include?(event.event_type).should be_true
+ if event.event_type == 'response'
+ ['successful', 'partially_successful'].include?(event.calculated_state).should be_true
+ end
+ previous = event
+ end
+ end
end
it 'should coalesce duplicate requests' do
@@ -142,37 +181,44 @@ describe GeneralController, "when searching" do
end
end
- describe 'when using xapian search' do
+end
- # rebuild xapian index after fixtures loaded
- before(:each) do
- load_raw_emails_data
- rebuild_xapian_index
- end
+describe GeneralController, 'when using xapian search' do
- it "should find info request when searching for '\"fancy dog\"'" do
- get :search, :combined => ['"fancy dog"']
- response.should render_template('search')
- assigns[:xapian_requests].matches_estimated.should == 1
- assigns[:xapian_requests].results.size.should == 1
- assigns[:xapian_requests].results[0][:model].should == info_request_events(:useless_outgoing_message_event)
+ integrate_views
- assigns[:xapian_requests].words_to_highlight == ["fancy", "dog"]
- end
+ # rebuild xapian index after fixtures loaded
+ before(:each) do
+ load_raw_emails_data
+ rebuild_xapian_index
+ end
- it "should find public body and incoming message when searching for 'geraldine quango'" do
- get :search, :combined => ['geraldine quango']
- response.should render_template('search')
+ it "should redirect from search query URL to pretty URL" do
+ post :search_redirect, :query => "mouse" # query hidden in POST parameters
+ response.should redirect_to(:action => 'search', :combined => "mouse", :view => "all") # URL /search/:query/all
+ end
+
+ it "should find info request when searching for '\"fancy dog\"'" do
+ get :search, :combined => ['"fancy dog"']
+ response.should render_template('search')
+ assigns[:xapian_requests].matches_estimated.should == 1
+ assigns[:xapian_requests].results.size.should == 1
+ assigns[:xapian_requests].results[0][:model].should == info_request_events(:useless_outgoing_message_event)
+
+ assigns[:xapian_requests].words_to_highlight == ["fancy", "dog"]
+ end
- assigns[:xapian_requests].matches_estimated.should == 1
- assigns[:xapian_requests].results.size.should == 1
- assigns[:xapian_requests].results[0][:model].should == info_request_events(:useless_incoming_message_event)
+ it "should find public body and incoming message when searching for 'geraldine quango'" do
+ get :search, :combined => ['geraldine quango']
+ response.should render_template('search')
- assigns[:xapian_bodies].matches_estimated.should == 1
- assigns[:xapian_bodies].results.size.should == 1
- assigns[:xapian_bodies].results[0][:model].should == public_bodies(:geraldine_public_body)
- end
+ assigns[:xapian_requests].matches_estimated.should == 1
+ assigns[:xapian_requests].results.size.should == 1
+ assigns[:xapian_requests].results[0][:model].should == info_request_events(:useless_incoming_message_event)
+ assigns[:xapian_bodies].matches_estimated.should == 1
+ assigns[:xapian_bodies].results.size.should == 1
+ assigns[:xapian_bodies].results[0][:model].should == public_bodies(:geraldine_public_body)
end
it "should filter results based on end of URL being 'all'" do
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb
index 9eca43aee..d12818a1c 100644
--- a/spec/controllers/public_body_controller_spec.rb
+++ b/spec/controllers/public_body_controller_spec.rb
@@ -1,3 +1,4 @@
+# coding: utf-8
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe PublicBodyController, "when showing a body" do