diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 52 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 26 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 18 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 2 | ||||
-rw-r--r-- | spec/controllers/user_controller_spec.rb | 7 | ||||
-rw-r--r-- | spec/helpers/link_to_helper_spec.rb | 3 | ||||
-rw-r--r-- | spec/models/customstates.rb | 62 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 36 | ||||
-rw-r--r-- | spec/spec_helper.rb | 19 | ||||
-rw-r--r-- | spec/views/request/show.rhtml_spec.rb | 4 |
10 files changed, 195 insertions, 34 deletions
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index a32c27dd9..cb622dabd 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -3,7 +3,14 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe AdminPublicBodyController, "when administering public bodies" do integrate_views fixtures :public_bodies, :public_body_translations - + + before do + username = MySociety::Config.get('ADMIN_USERNAME', '') + password = MySociety::Config.get('ADMIN_PASSWORD', '') + basic_auth_login @request + end + + it "shows the index page" do get :index end @@ -41,6 +48,24 @@ describe AdminPublicBodyController, "when administering public bodies" do PublicBody.count.should == 1 end + it "don't allow non-authenticated users to do anything" do + @request.env["HTTP_AUTHORIZATION"] = "" + PublicBody.count.should == 2 + post :destroy, { :id => 3 } + response.code.should == "401" + PublicBody.count.should == 2 + end + + it "when no username/password set, skip admin authorisation" do + config = MySociety::Config.load_default() + config['ADMIN_USERNAME'] = '' + config['ADMIN_PASSWORD'] = '' + @request.env["HTTP_AUTHORIZATION"] = "" + PublicBody.count.should == 2 + post :destroy, { :id => 3 } + PublicBody.count.should == 1 + end + end @@ -48,6 +73,12 @@ describe AdminPublicBodyController, "when administering public bodies with i18n" integrate_views fixtures :public_bodies, :public_body_translations + before do + username = MySociety::Config.get('ADMIN_USERNAME', '') + password = MySociety::Config.get('ADMIN_PASSWORD', '') + basic_auth_login @request + end + it "shows the index page" do get :index end @@ -62,24 +93,28 @@ describe AdminPublicBodyController, "when administering public bodies with i18n" end it "creates a new public body" do + I18n.locale = :es PublicBody.count.should == 2 post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } } PublicBody.count.should == 3 + I18n.locale = :en end it "edits a public body" do - get :edit, {:id => 3, :locale => 'es'} + I18n.locale = :es + get :edit, {:id => 3, :locale => :es} response.body.should include('Baguette') + I18n.locale = :en end it "saves edits to a public body" do - PublicBody.with_locale(:es) do - pb = PublicBody.find(id=3) - pb.name.should == "El Department for Humpadinking" - end + I18n.locale = :es + pb = PublicBody.find(id=3) + pb.name.should == "El Department for Humpadinking" + post :update, { :id => 3, :public_body => { :name => "Renamed", :short_name => "", :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code' }} + response.flash[:notice].should include('successful') + I18n.locale = :en - post :update, { :id => 3, :public_body => { :name => "Renamed", :short_name => "", :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code' }, :locale => "es" } - response.flash[:notice].should include('successful') pb = PublicBody.find(public_bodies(:humpadink_public_body).id) PublicBody.with_locale(:es) do pb.name.should == "Renamed" @@ -95,5 +130,4 @@ describe AdminPublicBodyController, "when administering public bodies with i18n" PublicBody.count.should == 1 end - end diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 8b84b89ec..d15482e51 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -22,20 +22,23 @@ describe PublicBodyController, "when showing a body" do end it "should assign the body using different locale from that used for url_name" do - get :show, {:url_name => "dfh", :locale => 'es'} - assigns[:public_body].notes.should == "Baguette" + PublicBody.with_locale(:es) do + get :show, {:url_name => "dfh"} + assigns[:public_body].notes.should == "Baguette" + end end it "should assign the body using same locale as that used in url_name" do - get :show, {:url_name => "edfh", :locale => 'es'} - assigns[:public_body].notes.should == "Baguette" + PublicBody.with_locale(:es) do + get :show, {:url_name => "edfh"} + assigns[:public_body].notes.should == "Baguette" + end end - it "should assign the body using same locale as that used in url_name even with wrongly set locale" do - PublicBody.with_locale(:en) do - get :show, {:url_name => "edfh", :locale => 'es'} - response.body.should include('Baguette') - end + it "should redirect use to the relevant locale even when url_name is for a different locale" do + ActionController::Routing::Routes.filters.clear + get :show, {:url_name => "edfh"} + response.should redirect_to "http://test.host/body/dfh" end it "should redirect to newest name if you use historic name of public body in URL" do @@ -69,12 +72,13 @@ describe PublicBodyController, "when listing bodies" do end it "should list bodies in alphabetical order with different locale" do - get :list, :locale => "es" + I18n.locale = :es + get :list response.should render_template('list') - assigns[:public_bodies].should == [ public_bodies(:geraldine_public_body), public_bodies(:humpadink_public_body) ] assigns[:tag].should == "all" assigns[:description].should == "all" + I18n.locale = :en end it "should list a tagged thing on the appropriate list page, and others on the other page, and all still on the all page" do diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index ac44cb905..64f3f8061 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -526,6 +526,8 @@ describe RequestController, "when classifying an information request" do response.should render_template('user/wrong_user') end + + describe 'when the request is old and unclassified' do before do @@ -714,6 +716,16 @@ describe RequestController, "when classifying an information request" do post_status('rejected') response.should redirect_to(:controller => 'help', :action => 'unhappy', :url_title => @dog_request.url_title) end + + describe "when using custom statuses from the theme" do + InfoRequest.send(:require, File.expand_path(File.join(File.dirname(__FILE__), '..', 'models', 'customstates'))) + + it "knows about extended states" do + Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01)) + post_status('deadline_extended') + flash[:notice].should == 'Authority has requested extension of the deadline.' + end + end end describe 'when redirecting after a successful status update by the request owner' do @@ -735,7 +747,7 @@ describe RequestController, "when classifying an information request" do def expect_redirect(status, redirect_path) post_status(status) - response.should redirect_to("http://test.host/#{redirect_path}") + response.should redirect_to("http://test.host/en/#{redirect_path}") end it 'should redirect to the "request url" with a message in the right tense when status is updated to "waiting response" and the response is not overdue' do @@ -903,7 +915,7 @@ describe RequestController, "sending overdue request alerts" do deliveries = ActionMailer::Base.deliveries deliveries.size.should == 1 mail = deliveries[0] - mail.body.should =~ /promptly, as normally\s+required by law/ + mail.body.should =~ /promptly, as normally/ mail.to_addrs.to_s.should == info_requests(:naughty_chicken_request).user.name_and_email mail.body =~ /(http:\/\/.*\/c\/(.*))/ @@ -931,7 +943,7 @@ describe RequestController, "sending overdue request alerts" do deliveries = ActionMailer::Base.deliveries deliveries.size.should == 1 mail = deliveries[0] - mail.body.should =~ /promptly, as normally\s+required by law during term time/ + mail.body.should =~ /promptly, as normally/ mail.to_addrs.to_s.should == info_requests(:naughty_chicken_request).user.name_and_email end diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index 8076d40c2..0a8919268 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -43,6 +43,8 @@ describe TrackController, "when sending alerts for a track" do end it "should send alerts" do + # Don't do clever locale-insertion-unto-URL stuff + ActionController::Routing::Routes.filters.clear # set the time the comment event happened at to within the last week ire = info_request_events(:silly_comment_event) ire.created_at = Time.now - 3.days diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index 4e21bc467..c6817fbc6 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -75,6 +75,7 @@ describe UserController, "when signing in" do end it "should log in when you give right email/password, and redirect to where you were" do + ActionController::Routing::Routes.filters.clear get :signin, :r => "/list" response.should render_template('sign') post_redirect = get_last_postredirect @@ -82,6 +83,7 @@ describe UserController, "when signing in" do :token => post_redirect.token } session[:user_id].should == users(:bob_smith_user).id + # response doesn't contain /en/ but redirect_to does... response.should redirect_to(:controller => 'request', :action => 'list', :post_redirect => 1) response.should_not send_email end @@ -107,6 +109,7 @@ describe UserController, "when signing in" do end it "should confirm your email, log you in and redirect you to where you were after you click an email link" do + ActionController::Routing::Routes.filters.clear get :signin, :r => "/list" post_redirect = get_last_postredirect @@ -191,6 +194,7 @@ describe UserController, "when signing out" do end it "should log you out and redirect you to where you were" do + ActionController::Routing::Routes.filters.clear session[:user_id] = users(:bob_smith_user).id get :signout, :r => '/list' session[:user_id].should be_nil @@ -420,8 +424,7 @@ describe UserController, "when changing email address" do "signchangeemail"=>{ "old_email"=>"bob@localhost", "new_email"=>"newbob@localhost"}, - "controller"=>"user", - "locale"=>"en"} + "controller"=>"user"} post :signchangeemail, post_redirect.post_params response.should redirect_to(:controller => 'user', :action => 'show', :url_name => 'bob_smith') diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb index 401c324fe..aae00c298 100644 --- a/spec/helpers/link_to_helper_spec.rb +++ b/spec/helpers/link_to_helper_spec.rb @@ -7,6 +7,7 @@ describe LinkToHelper do describe 'when creating a url for a request' do before do + ActionController::Routing::Routes.filters.clear @mock_request = mock_model(InfoRequest, :url_title => 'test_title') end @@ -20,4 +21,4 @@ describe LinkToHelper do end -end
\ No newline at end of file +end diff --git a/spec/models/customstates.rb b/spec/models/customstates.rb new file mode 100644 index 000000000..de8d04ffb --- /dev/null +++ b/spec/models/customstates.rb @@ -0,0 +1,62 @@ +module InfoRequestCustomStates + # Mixin methods for InfoRequest + def theme_display_status(status) + if status == 'deadline_extended' + _("Deadline extended.") + elsif status == 'wrong_response' + _("Wrong Response.") + else + raise _("unknown status ") + status + end + end + + def theme_extra_states + return ['deadline_extended', + 'wrong_response'] + end + + + def theme_calculate_status + return 'waiting_classification' if self.awaiting_description + waiting_response = self.described_state == "waiting_response" || self.described_state == "deadline_extended" + return self.described_state unless waiting_response + if self.described_state == 'deadline_extended' + return 'deadline_extended' if + Time.now.strftime("%Y-%m-%d") < self.date_deadline_extended.strftime("%Y-%m-%d") + return 'waiting_response_very_overdue' if + Time.now.strftime("%Y-%m-%d") > Holiday.due_date_from(self.date_deadline_extended, 15).strftime("%Y-%m-%d") + return 'waiting_response_overdue' + end + return 'waiting_response_very_overdue' if + Time.now.strftime("%Y-%m-%d") > self.date_very_overdue_after.strftime("%Y-%m-%d") + return 'waiting_response_overdue' if + Time.now.strftime("%Y-%m-%d") > self.date_response_required_by.strftime("%Y-%m-%d") + return 'waiting_response' + end + + def date_deadline_extended + # XXX shouldn't this be 15 days after the date the status was + # changed to "deadline extended"? Or perhaps 15 days ater the + # initial request due date? + return Holiday.due_date_from(self.date_response_required_by, 15) + end + +end + +module RequestControllerCustomStates + + def theme_describe_state(info_request) + # called after the core describe_state code. It should + # end by raising an error if the status is unknown + if info_request.calculate_status == 'deadline_extended' + flash[:notice] = _("Authority has requested extension of the deadline.") + redirect_to unhappy_url(info_request) + elsif info_request.calculate_status == 'wrong_response' + flash[:notice] = _("Oh no! Sorry to hear that your request was wrong. Here is what to do now.") + redirect_to unhappy_url(info_request) + else + raise "unknown calculate_status " + info_request.calculate_status + end + end + +end diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 96a4f5bc8..91b1b0876 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -159,6 +159,42 @@ describe InfoRequest do end end + + describe "when using a plugin and calculating the status" do + + fixtures :info_requests + + before do + InfoRequest.send(:require, File.expand_path(File.dirname(__FILE__) + '/customstates')) + @ir = info_requests(:naughty_chicken_request) + @ir.load_custom_states + end + + it "rejects invalid states" do + lambda {@ir.set_described_state("foo")}.should raise_error(ActiveRecord::RecordInvalid) + end + + it "accepts core states" do + @ir.set_described_state("requires_admin") + end + + it "accepts extended states" do + # this time would normally be "overdue" + Time.stub!(:now).and_return(Time.utc(2007, 11, 10, 00, 01)) + @ir.set_described_state("deadline_extended") + @ir.display_status.should == 'Deadline extended.' + @ir.date_deadline_extended + end + + it "is not overdue if it's had the deadline extended" do + when_overdue = Time.utc(2007, 11, 10, 00, 01) + 16.days + Time.stub!(:now).and_return(when_overdue) + @ir.calculate_status.should == 'waiting_response_overdue' + end + + end + + describe "when calculating the status for a school" do fixtures :info_requests, :info_request_events, :holidays, :public_bodies, :public_body_translations diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 87a89f755..33793dea3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,6 +5,11 @@ require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environ require 'spec/autorun' require 'spec/rails' +# set a default username and password so we can test +config = MySociety::Config.load_default() +config['ADMIN_USERNAME'] = 'foo' +config['ADMIN_PASSWORD'] = 'baz' + # Uncomment the next line to use webrat's matchers #require 'webrat/integrations/rspec-rails' @@ -103,6 +108,12 @@ def validate_as_body(html) "<html><head><title>Test</title></head><body>#{html}</body></html>") end +def basic_auth_login(request) + username = MySociety::Config.get('ADMIN_USERNAME') + password = MySociety::Config.get('ADMIN_PASSWORD') + request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("#{username}:#{password}") +end + # Monkeypatch! Validate HTML in tests. $html_validation_script = "/usr/bin/validate" # from Debian package wdg-html-validator if $tempfilecount.nil? @@ -114,19 +125,13 @@ if $tempfilecount.nil? alias :original_process :process def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET') - # Call original process function - if parameters.nil? - parameters = {:locale => "en"} - elsif not parameters.has_key?(:locale) - parameters[:locale] = "en" - end self.original_process(action, parameters, session, flash, http_method) # XXX Is there a better way to check this than calling a private method? return unless @response.template.controller.instance_eval { integrate_views? } # And then if HTML, not a redirect (302, 301) - if @response.content_type == "text/html" && (@response.response_code != 302) && (@response.response_code != 301) + if @response.content_type == "text/html" && ! [301,302,401].include?(@response.response_code) validate_html(@response.body) end end diff --git a/spec/views/request/show.rhtml_spec.rb b/spec/views/request/show.rhtml_spec.rb index 54ce9c63a..ca4663afc 100644 --- a/spec/views/request/show.rhtml_spec.rb +++ b/spec/views/request/show.rhtml_spec.rb @@ -83,7 +83,8 @@ describe 'when viewing an information request' do describe 'when there is a last response' do - before do + before do + ActionController::Routing::Routes.filters.clear @mock_response = mock_model(IncomingMessage) @mock_request.stub!(:get_last_response).and_return(@mock_response) end @@ -99,6 +100,7 @@ describe 'when viewing an information request' do describe 'when there is no last response' do before do + ActionController::Routing::Routes.filters.clear @mock_request.stub!(:get_last_response).and_return(nil) end |