diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 60 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 58 | ||||
-rw-r--r-- | spec/fixtures/public_body_translations.yml | 20 | ||||
-rw-r--r-- | spec/integration/cookie_stripping_spec.rb | 12 | ||||
-rw-r--r-- | spec/lib/mail_handler/mail_handler_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/sendmail_return_path_spec.rb | 88 | ||||
-rw-r--r-- | spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb | 56 | ||||
-rw-r--r-- | spec/mailers/application_mailer_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 58 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 2 | ||||
-rw-r--r-- | spec/spec_helper.rb | 12 |
11 files changed, 176 insertions, 194 deletions
diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index 593d51683..e67cc9492 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -73,11 +73,10 @@ describe GeneralController, "when showing the frontpage" do 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_selector('html[lang="es"]') - I18n.default_locale = old_default_locale + with_default_locale("es") do + get :frontpage + response.should have_selector('html[lang="es"]') + end end it "should render the front page with default language and ignore the browser setting" do @@ -85,11 +84,10 @@ describe GeneralController, "when showing the frontpage" do 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_selector('html[lang="es"]') - I18n.default_locale = old_default_locale + with_default_locale("es") do + get :frontpage + response.should have_selector('html[lang="es"]') + end end it "should render the front page with browser-selected language when there's no default set" do @@ -118,49 +116,7 @@ describe GeneralController, "when showing the frontpage" do 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 - get_fixtures_xapian_index - end - - 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 - get :frontpage - assigns[:request_events].map(&:info_request).select{|x|x.url_title =~ /^spam/}.length.should == 1 - end - end - -end describe GeneralController, 'when using xapian search' do diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 6800765f2..63989baaa 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -82,21 +82,23 @@ describe PublicBodyController, "when listing bodies" do def make_single_language_example(locale) result = nil - I18n.with_locale(locale) do - case locale - when :en - result = PublicBody.new(:name => 'English only', - :short_name => 'EO') - when :es - result = PublicBody.new(:name => 'Español Solamente', - :short_name => 'ES') - else - raise StandardError.new "Unknown locale #{locale}" + with_default_locale(locale) do + I18n.with_locale(locale) do + case locale + when :en + result = PublicBody.new(:name => 'English only', + :short_name => 'EO') + when :es + result = PublicBody.new(:name => 'Español Solamente', + :short_name => 'ES') + else + raise StandardError.new "Unknown locale #{locale}" + end + result.request_email = "#{locale}@example.org" + result.last_edit_editor = 'test' + result.last_edit_comment = '' + result.save end - result.request_email = "#{locale}@example.org" - result.last_edit_editor = 'test' - result.last_edit_comment = '' - result.save end result end @@ -188,13 +190,13 @@ describe PublicBodyController, "when listing bodies" do end it "should list bodies in alphabetical order with different locale" do - 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 == "" - I18n.default_locale = :en + with_default_locale(:es) do + 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 == "" + end 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 @@ -273,6 +275,20 @@ describe PublicBodyController, "when showing JSON version for API" do end +describe PublicBodyController, "when asked to export public bodies as CSV" do + + it "should return a valid CSV file with the right number of rows" do + get :list_all_csv + all_data = CSV.parse response.body + all_data.length.should == 8 + # Check that the header has the right number of columns: + all_data[0].length.should == 11 + # And an actual line of data: + all_data[1].length.should == 11 + end + +end + describe PublicBodyController, "when showing public body statistics" do it "should render the right template with the right data" do diff --git a/spec/fixtures/public_body_translations.yml b/spec/fixtures/public_body_translations.yml index 2030804ac..225bd74e2 100644 --- a/spec/fixtures/public_body_translations.yml +++ b/spec/fixtures/public_body_translations.yml @@ -10,6 +10,8 @@ geraldine_es_public_body_translation: notes: "" publication_scheme: "" disclosure_log: "" + created_at: 2007-10-24 10:51:01.161639 + updated_at: 2007-10-24 10:51:01.161639 geraldine_en_public_body_translation: name: Geraldine Quango @@ -23,6 +25,8 @@ geraldine_en_public_body_translation: notes: "" publication_scheme: "" disclosure_log: "" + created_at: 2007-10-24 10:51:01.161639 + updated_at: 2007-10-24 10:51:01.161639 humpadink_es_public_body_translation: name: "El Department for Humpadinking" @@ -36,6 +40,8 @@ humpadink_es_public_body_translation: notes: Baguette publication_scheme: "" disclosure_log: "" + created_at: 2007-10-24 10:51:01.161639 + updated_at: 2007-10-24 10:51:01.161639 humpadink_en_public_body_translation: name: "Department for Humpadinking" @@ -49,6 +55,8 @@ humpadink_en_public_body_translation: notes: An albatross told me!!! publication_scheme: "" disclosure_log: "" + created_at: 2007-10-24 10:51:01.161639 + updated_at: 2007-10-24 10:51:01.161639 forlorn_en_public_body_translation: name: "Department of Loneliness" @@ -62,6 +70,8 @@ forlorn_en_public_body_translation: notes: A very lonely public body that no one has corresponded with publication_scheme: "" disclosure_log: "" + created_at: 2007-10-24 10:51:01.161639 + updated_at: 2007-10-24 10:51:01.161639 silly_walks_en_public_body_translation: id: 6 @@ -75,6 +85,8 @@ silly_walks_en_public_body_translation: notes: You know the one. publication_scheme: "" disclosure_log: "" + created_at: 2007-10-24 10:51:01.161639 + updated_at: 2007-10-24 10:51:01.161639 sensible_walks_en_public_body_translation: id: 7 @@ -88,6 +100,8 @@ sensible_walks_en_public_body_translation: notes: I bet you’ve never heard of it. publication_scheme: "" disclosure_log: "" + created_at: 2008-10-25 10:51:01.161639 + updated_at: 2008-10-25 10:51:01.161639 other_public_body_translation: id: 8 @@ -101,6 +115,8 @@ other_public_body_translation: notes: More notes publication_scheme: "" disclosure_log: "" + created_at: 2008-10-25 10:51:01.161639 + updated_at: 2008-10-25 10:51:01.161639 humpadink_he_IL_public_body_translation: name: "Hebrew Humpadinking" @@ -114,6 +130,8 @@ humpadink_he_IL_public_body_translation: notes: An albatross told me!!! publication_scheme: "" disclosure_log: "" + created_at: 2007-10-24 10:51:01.161639 + updated_at: 2007-10-24 10:51:01.161639 accented_public_body_translation: id: 10 @@ -127,3 +145,5 @@ accented_public_body_translation: notes: This is to test unicode handling in body names publication_scheme: "" disclosure_log: "" + created_at: 2008-10-25 10:51:01.161639 + updated_at: 2008-10-25 10:51:01.161639 diff --git a/spec/integration/cookie_stripping_spec.rb b/spec/integration/cookie_stripping_spec.rb new file mode 100644 index 000000000..897899fd5 --- /dev/null +++ b/spec/integration/cookie_stripping_spec.rb @@ -0,0 +1,12 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require File.expand_path(File.dirname(__FILE__) + '/alaveteli_dsl') + +describe 'when making stripping cookies' do + + it 'should not set a cookie when no significant session data is set' do + get 'country_message' + response.headers['Set-Cookie'].should be_blank + end + +end + diff --git a/spec/lib/mail_handler/mail_handler_spec.rb b/spec/lib/mail_handler/mail_handler_spec.rb index aa351bd94..bc027eaec 100644 --- a/spec/lib/mail_handler/mail_handler_spec.rb +++ b/spec/lib/mail_handler/mail_handler_spec.rb @@ -235,7 +235,7 @@ describe 'when deriving a name, email and formatted address from a message from it 'should quote a name with quotes in it' do should_render_from_address('"FOI \" Person" <foiperson@localhost>', - ['FOI \" Person', + ['FOI " Person', 'foiperson@localhost', '"FOI \" Person" <foiperson@localhost>']) end diff --git a/spec/lib/sendmail_return_path_spec.rb b/spec/lib/sendmail_return_path_spec.rb deleted file mode 100644 index 83436c2bd..000000000 --- a/spec/lib/sendmail_return_path_spec.rb +++ /dev/null @@ -1,88 +0,0 @@ -# This is a test of the monkey patches in sendmail_return_path.rb - -# In Rails 3 the monkeypatches are not needed anymore because sendmail now has the "-f" flag -# set correctly. So, strictly these tests are testing the Rails internals. So, that means we really -# should delete them. Let's do that later when things have settled down. For the time being leave -# them in - -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe "when sending email with an altered return path" do - before(:each) { ActionMailer::Base.deliveries = [] } - - it "should default to delivery method test" do - ActionMailer::Base.delivery_method.should == :test - end - - it "should let the helper change the method" do - with_delivery_method :smtp do - ActionMailer::Base.delivery_method.should == :smtp - end - ActionMailer::Base.delivery_method.should == :test - end - - # Documentation for fancy mock functions: http://rspec.info/documentation/mocks/message_expectations.html - it "should set the return path when sending email using SMTP" do - mock_smtp = mock("smtp") - mock_smtp_session = mock("smtp_session") - - mock_smtp.should_receive(:start).once.and_yield(mock_smtp_session) - # the second parameter to the SMTP session is the sender (return path) - mock_smtp_session.should_receive(:sendmail).once.with(anything(), "test@localhost", anything()) - - Net::SMTP.stub!(:new).and_return(mock_smtp) - - with_delivery_method :smtp do - ContactMailer.to_admin_message( - "Mr. Test", "test@localhost", "Test script spec/lib/sendmail_return_path_spec.rb", - "This is just a test for a test script", nil, nil, nil - ).deliver - end - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 0 - end - - it "should set the return path when sending email using sendmail" do - with_stub_popen do - IO.should_receive(:popen).once.with('/usr/sbin/sendmail -i -t -f "test@localhost" postmaster@localhost', "w+") - with_delivery_method :sendmail do - ContactMailer.to_admin_message( - "Mr. Test", "test@localhost", "Test script spec/lib/sendmail_return_path_spec.rb", - "This is just a test for a test script", nil, nil, nil - ).deliver - end - end - - deliveries = ActionMailer::Base.deliveries - deliveries.size.should == 0 - end - - - protected - # Change the way Rails delivers memory, just for current scope - def with_delivery_method(new_delivery_method) - old_delivery_method, ActionMailer::Base.delivery_method = ActionMailer::Base.delivery_method, new_delivery_method - yield - ensure - ActionMailer::Base.delivery_method = old_delivery_method - end - - # By default, we can't stub popen, presumably because it is a builtin written in C. - # Replace it entirely with a normal method that just calls the C one, so we can stub it - - # this leaves IO working afterwards (for other tests that run in the same instance). - def with_stub_popen() - IO.class_eval "@orig_popen = self.method(:popen); def self.popen(a, b, &c); @orig_popen.call(a, b, &c); end" - begin - yield - ensure - # in theory would undo the popen alterations and return IO to a pristine state, but - # don't know how to (much fiddling with alias bind and the like didn't help). It - # doesn't matter - the new popen should behave just the same. - end - end - - -end - - diff --git a/spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb b/spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb index 9bd5ccb93..fcd729b48 100644 --- a/spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb +++ b/spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb @@ -1,71 +1,71 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') describe WhatDoTheyKnow::StripEmptySessions do - + def make_response(session_data, response_headers) app = lambda do |env| env['rack.session'] = session_data - return [200, response_headers, ['content']] + return [200, response_headers, ['content']] end strip_empty_sessions = WhatDoTheyKnow::StripEmptySessions app = strip_empty_sessions.new(app, {:key => 'mykey', :path => '', :httponly => true}) response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/html') end - - it 'should not prevent a cookie being set if there is data in the session' do - session_data = { :some_real_data => 'important', - :session_id => 'my_session_id', - :_csrf_token => 'hi_there' } - application_response_headers = { 'Content-Type' => 'text/html', + + it 'should not prevent a cookie being set if there is data in the session' do + session_data = { 'some_real_data' => 'important', + 'session_id' => 'my_session_id', + '_csrf_token' => 'hi_there' } + application_response_headers = { 'Content-Type' => 'text/html', 'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'} response = make_response(session_data, application_response_headers) response.headers['Set-Cookie'].should == 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly' end - describe 'if there is no meaningful data in the session' do + describe 'if there is no meaningful data in the session' do - before do - @session_data = { :session_id => 'my_session_id', - :_csrf_token => 'hi_there' } + before do + @session_data = { 'session_id' => 'my_session_id', + '_csrf_token' => 'hi_there' } end - - it 'should not strip any other header' do + + it 'should not strip any other header' do application_response_headers = { 'Content-Type' => 'text/html', 'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'} response = make_response(@session_data, application_response_headers) response.headers['Content-Type'].should == 'text/html' end - - it 'should strip the session cookie setting header ' do - application_response_headers = { 'Content-Type' => 'text/html', + + it 'should strip the session cookie setting header ' do + application_response_headers = { 'Content-Type' => 'text/html', 'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'} response = make_response(@session_data, application_response_headers) response.headers['Set-Cookie'].should == "" end - - it 'should strip the session cookie setting header even with a locale' do - @session_data[:locale] = 'en' - application_response_headers = { 'Content-Type' => 'text/html', + + it 'should strip the session cookie setting header even with a locale' do + @session_data['locale'] = 'en' + application_response_headers = { 'Content-Type' => 'text/html', 'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'} response = make_response(@session_data, application_response_headers) response.headers['Set-Cookie'].should == "" end - it 'should not strip the session cookie setting for admins' do - @session_data[:using_admin] = 1 - application_response_headers = { 'Content-Type' => 'text/html', + it 'should not strip the session cookie setting for admins' do + @session_data['using_admin'] = 1 + application_response_headers = { 'Content-Type' => 'text/html', 'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'} response = make_response(@session_data, application_response_headers) response.headers['Set-Cookie'].should == "mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly" end - - it 'should strip the session cookie setting header (but no other cookie setting header) if there is more than one' do - application_response_headers = { 'Content-Type' => 'text/html', + + it 'should strip the session cookie setting header (but no other cookie setting header) if there is more than one' do + application_response_headers = { 'Content-Type' => 'text/html', 'Set-Cookie' => ['mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly', 'other=mydata']} response = make_response(@session_data, application_response_headers) response.headers['Set-Cookie'].should == ['other=mydata'] end - + end end diff --git a/spec/mailers/application_mailer_spec.rb b/spec/mailers/application_mailer_spec.rb index d8993f78f..718ac47fb 100644 --- a/spec/mailers/application_mailer_spec.rb +++ b/spec/mailers/application_mailer_spec.rb @@ -13,7 +13,7 @@ describe ApplicationMailer do end def add_mail_methods(method_names) - method_names.each{ |method_name| ApplicationMailer.send(:define_method, method_name){} } + method_names.each{ |method_name| ApplicationMailer.send(:define_method, method_name){ mail() } } end def remove_mail_methods(method_names) diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 64ad1972e..dcc94e967 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -27,7 +27,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe InfoRequest do - describe 'when validating', :focus => true do + describe 'when validating' do it 'should accept a summary with ascii characters' do info_request = InfoRequest.new(:title => 'abcde') @@ -1030,7 +1030,7 @@ describe InfoRequest do end end - context "another series of events on a request", :focus => true do + context "another series of events on a request" do it "should have sensible event states" do # An initial request is sent request.log_event('sent', {}) @@ -1122,5 +1122,59 @@ describe InfoRequest do end + describe InfoRequest, 'when getting similar requests' do + + before(:each) do + get_fixtures_xapian_index + end + + it 'should return similar requests' do + similar, more = info_requests(:spam_1_request).similar_requests(1) + similar.results.first[:model].info_request.should == info_requests(:spam_2_request) + end + + it 'should return a flag set to true' do + similar, more = info_requests(:spam_1_request).similar_requests(1) + more.should be_true + end + + end + + describe InfoRequest, 'when constructing the list of recent requests' do + before(:each) do + get_fixtures_xapian_index + end + + 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. + request_events, request_events_all_successful = InfoRequest.recent_requests + 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 + request_events, request_events_all_successful = InfoRequest.recent_requests + request_events.map(&:info_request).select{|x|x.url_title =~ /^spam/}.length.should == 1 + end + end end diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index 3c9fff784..a1e060d8e 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -370,7 +370,7 @@ describe PublicBody, " when only indexing selected things on a rebuild" do end end -# I would expect ActsAsXapian to have some tests under vendor/plugins/acts_as_xapian, but +# I would expect ActsAsXapian to have some tests under lib/acts_as_xapian, but # it looks like this is not the case. Putting a test here instead. describe ActsAsXapian::Search, "#words_to_highlight" do before(:each) do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9d16f6387..1eeb8603b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,10 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ SimpleCov.start('rails') do add_filter 'commonlib' add_filter 'vendor/plugins' + add_filter 'lib/strip_attributes' + add_filter 'lib/has_tag_string' + add_filter 'lib/acts_as_xapian' + add_filter 'lib/themes' end Spork.prefork do @@ -183,6 +187,14 @@ Spork.prefork do end end + def with_default_locale(locale) + original_default_locale = I18n.default_locale + I18n.default_locale = locale + yield + ensure + I18n.default_locale = original_default_locale + end + def load_test_categories PublicBodyCategories.add(:en, [ "Local and regional", |