diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/admin_general_controller_spec.rb | 8 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 12 | ||||
-rw-r--r-- | spec/controllers/admin_request_controller_spec.rb | 55 | ||||
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 42 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 4 | ||||
-rw-r--r-- | spec/fixtures/info_requests.yml | 6 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 34 | ||||
-rw-r--r-- | spec/models/request_mailer_spec.rb | 44 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 18 |
9 files changed, 211 insertions, 12 deletions
diff --git a/spec/controllers/admin_general_controller_spec.rb b/spec/controllers/admin_general_controller_spec.rb index 4c3708268..820d1e7f3 100644 --- a/spec/controllers/admin_general_controller_spec.rb +++ b/spec/controllers/admin_general_controller_spec.rb @@ -5,8 +5,14 @@ describe AdminGeneralController, "when viewing front page of admin interface" do before { basic_auth_login @request } it "should render the front page" do - get :index + get :index, :suppress_redirect => 1 response.should render_template('index') end + it "should redirect to include trailing slash" do + get :index + response.should redirect_to(:controller => 'admin_general', + :action => 'index') + end + end diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index 6b88fe39d..357564211 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -110,27 +110,27 @@ describe AdminPublicBodyController, "when administering public bodies with i18n" end it "creates a new public body" do - I18n.locale = :es + I18n.default_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 + I18n.default_locale = :en end it "edits a public body" do - I18n.locale = :es + I18n.default_locale = :es get :edit, {:id => 3, :locale => :es} response.body.should include('Baguette') - I18n.locale = :en + I18n.default_locale = :en end it "saves edits to a public body" do - I18n.locale = :es + I18n.default_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 + I18n.default_locale = :en pb = PublicBody.find(public_bodies(:humpadink_public_body).id) PublicBody.with_locale(:es) do diff --git a/spec/controllers/admin_request_controller_spec.rb b/spec/controllers/admin_request_controller_spec.rb index d82e4a49c..1d9ebcdf3 100644 --- a/spec/controllers/admin_request_controller_spec.rb +++ b/spec/controllers/admin_request_controller_spec.rb @@ -39,3 +39,58 @@ describe AdminRequestController, "when administering requests" do end +describe AdminRequestController, "when administering the holding pen" do + integrate_views + fixtures :info_requests, :incoming_messages, :raw_emails, :users, :public_bodies, :public_body_translations + before(:each) do + basic_auth_login @request + load_raw_emails_data(raw_emails) + end + + it "shows a rejection reason for an incoming message from an invalid address" do + ir = info_requests(:fancy_dog_request) + ir.allow_new_responses_from = 'authority_only' + ir.handle_rejected_responses = 'holding_pen' + ir.save! + receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com") + get :show_raw_email, :id => InfoRequest.holding_pen_request.get_last_response.raw_email.id + response.should have_text(/Only the authority can reply to this request/) + end + + it "allows redelivery even to a closed request" do + ir = info_requests(:fancy_dog_request) + ir.allow_new_responses_from = 'nobody' + ir.handle_rejected_responses = 'holding_pen' + ir.save! + InfoRequest.holding_pen_request.incoming_messages.length.should == 0 + ir.incoming_messages.length.should == 1 + receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com") + InfoRequest.holding_pen_request.incoming_messages.length.should == 1 + new_im = InfoRequest.holding_pen_request.incoming_messages[0] + ir.incoming_messages.length.should == 1 + post :redeliver_incoming, :redeliver_incoming_message_id => new_im.id, :url_title => ir.url_title + ir = InfoRequest.find_by_url_title(ir.url_title) + ir.incoming_messages.length.should == 2 + response.should redirect_to('http://test.host/admin/request/show/101') + InfoRequest.holding_pen_request.incoming_messages.length.should == 0 + end + + it "guesses a misdirected request" do + ir = info_requests(:fancy_dog_request) + ir.handle_rejected_responses = 'holding_pen' + ir.save! + mail_to = "request-#{ir.id}-asdfg@example.com" + receive_incoming_mail('incoming-request-plain.email', mail_to) + get :show_raw_email, :id => InfoRequest.holding_pen_request.get_last_response.raw_email.id + response.should have_text(/Could not identify the request/) + assigns[:info_requests][0].should == ir + end + + it "destroys an incoming message" do + im = incoming_messages(:useless_incoming_message) + raw_email = im.raw_email.filepath + post :destroy_incoming, :incoming_message_id => im.id + assert_equal File.exists?(raw_email), false + end + +end diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index 7807a5541..bc744a9cd 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -17,6 +17,48 @@ describe GeneralController, "when searching" do response.should be_success end + it "should render the front page with default language" do + get :frontpage + response.should have_tag('html[lang="en"]') + end + + it "should render the front page with default language" do + old_default_locale = I18n.default_locale + I18n.default_locale = "es" + get :frontpage + response.should have_tag('html[lang="es"]') + I18n.default_locale = old_default_locale + end + + it "should render the front page with default language and ignore the browser setting" do + config = MySociety::Config.load_default() + config['USE_DEFAULT_BROWSER_LANGUAGE'] = false + accept_language = "en-GB,en-US;q=0.8,en;q=0.6" + request.env['HTTP_ACCEPT_LANGUAGE'] = accept_language + old_default_locale = I18n.default_locale + I18n.default_locale = "es" + get :frontpage + response.should have_tag('html[lang="es"]') + I18n.default_locale = old_default_locale + end + + it "should render the front page with browser-selected language when there's no default set" do + config = MySociety::Config.load_default() + config['USE_DEFAULT_BROWSER_LANGUAGE'] = true + accept_language = "es-ES,en-GB,en-US;q=0.8,en;q=0.6" + request.env['HTTP_ACCEPT_LANGUAGE'] = accept_language + get :frontpage + response.should have_tag('html[lang="es"]') + request.env['HTTP_ACCEPT_LANGUAGE'] = nil + end + + it "doesn't raise an error when there's no user matching the one in the session" do + session[:user_id] = 999 + get :frontpage + 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") # URL /search/:query diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index d15482e51..0050678d2 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -72,13 +72,13 @@ describe PublicBodyController, "when listing bodies" do end it "should list bodies in alphabetical order with different locale" do - I18n.locale = :es + I18n.default_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 + I18n.default_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/fixtures/info_requests.yml b/spec/fixtures/info_requests.yml index bdeef4800..c1e3c1910 100644 --- a/spec/fixtures/info_requests.yml +++ b/spec/fixtures/info_requests.yml @@ -8,6 +8,7 @@ fancy_dog_request: user_id: 1 described_state: waiting_response awaiting_description: true + idhash: 50929748 naughty_chicken_request: id: 103 title: How much public money is wasted on breeding naughty chickens? @@ -17,6 +18,5 @@ naughty_chicken_request: public_body_id: 2 user_id: 1 described_state: waiting_response - awaiting_description: false - - + awaiting_description: false + idhash: e8d18c84 diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index d0b0e0e32..ba80256ab 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -2,6 +2,40 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe InfoRequest do + describe "guessing a request from an email" do + fixtures :info_requests, :public_bodies, :incoming_messages, :raw_emails + + before do + @im = incoming_messages(:useless_incoming_message) + load_raw_emails_data(raw_emails) + end + + it 'should compute a hash' do + @info_request = InfoRequest.new(:title => "testing", + :public_body => public_bodies(:geraldine_public_body), + :user_id => 1) + @info_request.save! + @info_request.idhash.should_not == nil + end + + it 'should find a request based on an email with an intact id and a broken hash' do + ir = info_requests(:fancy_dog_request) + id = ir.id + @im.mail.to = "request-#{id}-asdfg@example.com" + guessed = InfoRequest.guess_by_incoming_email(@im) + guessed[0].idhash.should == ir.idhash + end + + it 'should find a request based on an email with a broken id and an intact hash' do + ir = info_requests(:fancy_dog_request) + idhash = ir.idhash + @im.mail.to = "request-123ab-#{idhash}@example.com" + guessed = InfoRequest.guess_by_incoming_email(@im) + guessed[0].id.should == ir.id + end + + end + describe "making up the URL title" do before do @info_request = InfoRequest.new diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb index a5f59b9bf..fbe22c220 100644 --- a/spec/models/request_mailer_spec.rb +++ b/spec/models/request_mailer_spec.rb @@ -27,7 +27,49 @@ describe RequestMailer, " when receiving incoming mail" do receive_incoming_mail('incoming-request-plain.email', 'dummy@localhost') ir.incoming_messages.size.should == 1 InfoRequest.holding_pen_request.incoming_messages.size.should == 1 + last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event + last_event.params[:rejected_reason].should == "Could not identify the request from the email address" + + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.to.should == [ MySociety::Config.get("CONTACT_EMAIL", 'contact@localhost') ] + deliveries.clear + end + it "should store mail in holding pen and send to admin when the from email is empty and only authorites can reply" do + ir = info_requests(:fancy_dog_request) + ir.allow_new_responses_from = 'authority_only' + ir.handle_rejected_responses = 'holding_pen' + ir.save! + ir.incoming_messages.size.should == 1 + InfoRequest.holding_pen_request.incoming_messages.size.should == 0 + receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "") + ir.incoming_messages.size.should == 1 + InfoRequest.holding_pen_request.incoming_messages.size.should == 1 + last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event + last_event.params[:rejected_reason].should =~ /there is no "From" address/ + + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.to.should == [ MySociety::Config.get("CONTACT_EMAIL", 'contact@localhost') ] + deliveries.clear + end + + it "should store mail in holding pen and send to admin when the from email is unknown and only authorites can reply" do + ir = info_requests(:fancy_dog_request) + ir.allow_new_responses_from = 'authority_only' + ir.handle_rejected_responses = 'holding_pen' + ir.save! + ir.incoming_messages.size.should == 1 + InfoRequest.holding_pen_request.incoming_messages.size.should == 0 + receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com") + ir.incoming_messages.size.should == 1 + InfoRequest.holding_pen_request.incoming_messages.size.should == 1 + last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event + last_event.params[:rejected_reason].should =~ /Only the authority can reply/ + deliveries = ActionMailer::Base.deliveries deliveries.size.should == 1 mail = deliveries[0] @@ -108,6 +150,8 @@ describe RequestMailer, " when receiving incoming mail" do receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) ir.incoming_messages.size.should == 1 InfoRequest.holding_pen_request.incoming_messages.size.should == 1 # arrives in holding pen + last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event + last_event.params[:rejected_reason].should =~ /allow new responses from nobody/ # should be a message to admin regarding holding pen deliveries = ActionMailer::Base.deliveries diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index c913ce3a8..ee6916ffc 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -15,6 +15,24 @@ describe User, "making up the URL name" do @user.url_name.should == 'user' end end + + +describe User, "showing the name" do + before do + @user = User.new + @user.name = 'Some Name ' + end + + it 'should strip whitespace' do + @user.name.should == 'Some Name' + end + + it 'should show if user has been banned' do + @user.ban_text = "Naughty user" + @user.name.should == 'Some Name (Banned)' + end + +end describe User, " when authenticating" do before do |