diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/admin_incoming_message_controller_spec.rb | 11 | ||||
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 3 | ||||
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 34 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 208 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 33 | ||||
-rw-r--r-- | spec/factories.rb | 7 | ||||
-rw-r--r-- | spec/fixtures/files/fake-authority-type.csv | 2 | ||||
-rw-r--r-- | spec/fixtures/outgoing_messages.yml | 2 | ||||
-rw-r--r-- | spec/fixtures/public_bodies.yml | 63 | ||||
-rw-r--r-- | spec/fixtures/public_body_translations.yml | 12 | ||||
-rw-r--r-- | spec/fixtures/users.yml | 2 | ||||
-rw-r--r-- | spec/integration/cookie_stripping_spec.rb | 12 | ||||
-rw-r--r-- | spec/lib/theme_spec.rb | 25 | ||||
-rw-r--r-- | spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb | 56 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 10 | ||||
-rw-r--r-- | spec/models/outgoing_message_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 156 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 23 | ||||
-rw-r--r-- | spec/spec_helper.rb | 52 |
20 files changed, 555 insertions, 160 deletions
diff --git a/spec/controllers/admin_incoming_message_controller_spec.rb b/spec/controllers/admin_incoming_message_controller_spec.rb index b969a8a3f..21c744e5b 100644 --- a/spec/controllers/admin_incoming_message_controller_spec.rb +++ b/spec/controllers/admin_incoming_message_controller_spec.rb @@ -50,6 +50,17 @@ describe AdminIncomingMessageController, "when administering incoming messages" :url_title => destination_info_request.url_title end + it 'should succeed, even if a duplicate xapian indexing job is created' do + + with_duplicate_xapian_job_creation do + current_info_request = info_requests(:fancy_dog_request) + destination_info_request = info_requests(:naughty_chicken_request) + incoming_message = incoming_messages(:useless_incoming_message) + post :redeliver, :redeliver_incoming_message_id => incoming_message.id, + :url_title => destination_info_request.url_title + end + + end end diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index 8a72db724..fe5087d7c 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -55,7 +55,8 @@ describe AdminPublicBodyController, "when administering public bodies" do end it "mass assigns tags" do - n = PublicBody.count + condition = "public_body_translations.locale = ?" + n = PublicBody.joins(:translations).where([condition, "en"]).count post :mass_tag_add, { :new_tag => "department", :table_name => "substring" } request.flash[:notice].should == "Added tag to table of bodies." response.should redirect_to(:action=>'list') diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb index 8c86ad0be..116dbe07a 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 @@ -219,6 +217,17 @@ describe GeneralController, 'when using xapian search' do assigns[:xapian_bodies].should == nil end + it 'should highlight words for a user-only request' do + get :search, :combined => "bob/users" + assigns[:highlight_words].should == ['bob'] + end + + it 'should show spelling corrections for a user-only request' do + get :search, :combined => "rob/users" + assigns[:spelling_correction].should == 'bob' + response.body.should include('did_you_mean') + end + it "should filter results based on end of URL being 'requests'" do get :search, :combined => "bob/requests" assigns[:xapian_requests].results.map{|x|x[:model]}.should =~ [ @@ -238,6 +247,11 @@ describe GeneralController, 'when using xapian search' do assigns[:xapian_bodies].results.map{|x|x[:model]}.should == [public_bodies(:geraldine_public_body)] end + it 'should show "Browse all" link if there are no results for a search restricted to bodies' do + get :search, :combined => "noresultsshouldbefound/bodies" + response.body.should include('Browse all') + end + it "should show help when searching for nothing" do get :search_redirect, :query => nil response.should render_template('search') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 2d1b1466f..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 @@ -250,6 +252,13 @@ describe PublicBodyController, "when listing bodies" do response.code.should == '406' end + it "should list authorities starting with a multibyte first letter" do + get :list, {:tag => "å", :show_locale => 'cs'} + response.should render_template('list') + assigns[:public_bodies].should == [ public_bodies(:accented_public_body) ] + assigns[:tag].should == "Å" + end + end describe PublicBodyController, "when showing JSON version for API" do @@ -266,41 +275,142 @@ 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 - config = MySociety::Config.load_default() - config['MINIMUM_REQUESTS_FOR_STATISTICS'] = 1 - config['PUBLIC_BODY_STATISTICS_PAGE'] = true - get :statistics - response.should render_template('public_body/statistics') - # There are 5 different graphs we're creating at the moment. - assigns[:graph_list].length.should == 5 - # The first is the only one with raw values, the rest are - # percentages with error bars: - assigns[:graph_list].each_with_index do |graph, index| - if index == 0 - graph['errorbars'].should be_false - graph['x_values'].length.should == 4 - graph['x_values'].should == [0, 1, 2, 3] - graph['y_values'].should == [1, 2, 2, 4] - else - graph['errorbars'].should be_true - # Just check the first one: - if index == 1 - graph['x_values'].should == [0, 1, 2, 3] - graph['y_values'].should == [0, 50, 100, 100] + it "should render the right template with the right data" do + config = MySociety::Config.load_default() + config['MINIMUM_REQUESTS_FOR_STATISTICS'] = 1 + config['PUBLIC_BODY_STATISTICS_PAGE'] = true + get :statistics + response.should render_template('public_body/statistics') + # There are 5 different graphs we're creating at the moment. + assigns[:graph_list].length.should == 5 + # The first is the only one with raw values, the rest are + # percentages with error bars: + assigns[:graph_list].each_with_index do |graph, index| + if index == 0 + graph['errorbars'].should be_false + graph['x_values'].length.should == 4 + graph['x_values'].should == [0, 1, 2, 3] + graph['y_values'].should == [1, 2, 2, 4] + else + graph['errorbars'].should be_true + # Just check the first one: + if index == 1 + graph['x_values'].should == [0, 1, 2, 3] + graph['y_values'].should == [0, 50, 100, 100] + end + # Check that at least every confidence interval value is + # a Float (rather than NilClass, say): + graph['cis_below'].each { |v| v.should be_instance_of(Float) } + graph['cis_above'].each { |v| v.should be_instance_of(Float) } + end end - # Check that at least every confidence interval value is - # a Float (rather than NilClass, say): - graph['cis_below'].each { |v| v.should be_instance_of(Float) } - graph['cis_above'].each { |v| v.should be_instance_of(Float) } - end end - end end +describe PublicBodyController, "when converting data for graphing" do + + before(:each) do + @raw_count_data = PublicBody.get_request_totals(n=3, + highest=true, + minimum_requests=1) + @percentages_data = PublicBody.get_request_percentages( + column='info_requests_successful_count', + n=3, + highest=false, + minimum_requests=1) + end + + it "should not include the real public body model instance" do + to_draw = controller.simplify_stats_for_graphs(@raw_count_data, + column='blah_blah', + percentages=false, + {} ) + to_draw['public_bodies'][0].class.should == Hash + to_draw['public_bodies'][0].has_key?('request_email').should be_false + end + + it "should generate the expected id" do + to_draw = controller.simplify_stats_for_graphs(@raw_count_data, + column='blah_blah', + percentages=false, + {:highest => true} ) + to_draw['id'].should == "blah_blah-highest" + to_draw = controller.simplify_stats_for_graphs(@raw_count_data, + column='blah_blah', + percentages=false, + {:highest => false} ) + to_draw['id'].should == "blah_blah-lowest" + end + + it "should have exactly the expected keys" do + to_draw = controller.simplify_stats_for_graphs(@raw_count_data, + column='blah_blah', + percentages=false, + {} ) + to_draw.keys.sort.should == ["errorbars", "id", "public_bodies", + "title", "tooltips", "totals", + "x_axis", "x_ticks", "x_values", + "y_axis", "y_max", "y_values"] + + to_draw = controller.simplify_stats_for_graphs(@percentages_data, + column='whatever', + percentages=true, + {}) + to_draw.keys.sort.should == ["cis_above", "cis_below", + "errorbars", "id", "public_bodies", + "title", "tooltips", "totals", + "x_axis", "x_ticks", "x_values", + "y_axis", "y_max", "y_values"] + end + + it "should have values of the expected class and length" do + [controller.simplify_stats_for_graphs(@raw_count_data, + column='blah_blah', + percentages=false, + {}), + controller.simplify_stats_for_graphs(@percentages_data, + column='whatever', + percentages=true, + {})].each do |to_draw| + per_pb_keys = ["cis_above", "cis_below", "public_bodies", + "tooltips", "totals", "x_ticks", "x_values", + "y_values"] + # These should be all be arrays with one element per public body: + per_pb_keys.each do |key| + if to_draw.has_key? key + to_draw[key].class.should == Array + to_draw[key].length.should eq(3), "for key #{key}" + end + end + # Just check that the rest aren't of class Array: + to_draw.keys.each do |key| + unless per_pb_keys.include? key + to_draw[key].class.should_not eq(Array), "for key #{key}" + end + end + end + end + +end + + describe PublicBodyController, "when doing type ahead searches" do render_views diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index a16024828..57d084f6b 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -55,6 +55,39 @@ describe TrackController, "when making a new track on a request" do end +describe TrackController, "when unsubscribing from a track" do + + before do + @track_thing = FactoryGirl.create(:track_thing) + end + + it 'should destroy the track thing' do + get :update, {:track_id => @track_thing.id, + :track_medium => 'delete', + :r => 'http://example.com'}, + {:user_id => @track_thing.tracking_user.id} + TrackThing.find(:first, :conditions => ['id = ? ', @track_thing.id]).should == nil + end + + it 'should redirect to a URL on the site' do + get :update, {:track_id => @track_thing.id, + :track_medium => 'delete', + :r => '/'}, + {:user_id => @track_thing.tracking_user.id} + response.should redirect_to('/') + end + + it 'should not redirect to a url on another site' do + track_thing = FactoryGirl.create(:track_thing) + get :update, {:track_id => @track_thing.id, + :track_medium => 'delete', + :r => 'http://example.com/'}, + {:user_id => @track_thing.tracking_user.id} + response.should redirect_to('/') + end + +end + describe TrackController, "when sending alerts for a track" do render_views diff --git a/spec/factories.rb b/spec/factories.rb index 653525920..7d8f94ac1 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -137,4 +137,11 @@ FactoryGirl.define do last_edit_comment "Making an edit" end + factory :track_thing do + association :tracking_user, :factory => :user + track_medium 'email_daily' + track_type 'search_query' + track_query 'Example Query' + end + end diff --git a/spec/fixtures/files/fake-authority-type.csv b/spec/fixtures/files/fake-authority-type.csv index cb25050c6..a320941c7 100644 --- a/spec/fixtures/files/fake-authority-type.csv +++ b/spec/fixtures/files/fake-authority-type.csv @@ -2,3 +2,5 @@ ,Scottish Fake Authority,scottish_foi@localhost ,Fake Authority of Northern Ireland,ni_foi@localhost ,Gobierno de Aragón,spain_foi@localhost +,Nordic æøå,no_foi@localhost + diff --git a/spec/fixtures/outgoing_messages.yml b/spec/fixtures/outgoing_messages.yml index 971ce0cc5..92de076c4 100644 --- a/spec/fixtures/outgoing_messages.yml +++ b/spec/fixtures/outgoing_messages.yml @@ -12,6 +12,8 @@ # last_sent_at :datetime # incoming_message_followup_id :integer # what_doing :string(255) not null +# prominence :string(255) default("normal"), not null +# prominence_reason :text # useless_outgoing_message: diff --git a/spec/fixtures/public_bodies.yml b/spec/fixtures/public_bodies.yml index 1fa016d3a..d0eb572b3 100644 --- a/spec/fixtures/public_bodies.yml +++ b/spec/fixtures/public_bodies.yml @@ -2,23 +2,27 @@ # # Table name: public_bodies # -# id :integer not null, primary key -# name :text not null -# short_name :text not null -# request_email :text not null -# version :integer not null -# last_edit_editor :string(255) not null -# last_edit_comment :text not null -# created_at :datetime not null -# updated_at :datetime not null -# url_name :text not null -# home_page :text default(""), not null -# notes :text default(""), not null -# first_letter :string(255) not null -# publication_scheme :text default(""), not null -# api_key :string(255) not null -# info_requests_count :integer default(0), not null -# disclosure_log :text default(""), not null +# id :integer not null, primary key +# name :text not null +# short_name :text not null +# request_email :text not null +# version :integer not null +# last_edit_editor :string(255) not null +# last_edit_comment :text not null +# created_at :datetime not null +# updated_at :datetime not null +# url_name :text not null +# home_page :text default(""), not null +# notes :text default(""), not null +# first_letter :string(255) not null +# publication_scheme :text default(""), not null +# api_key :string(255) not null +# info_requests_count :integer default(0), not null +# disclosure_log :text default(""), not null +# info_requests_successful_count :integer +# info_requests_not_held_count :integer +# info_requests_overdue_count :integer +# info_requests_visible_classified_count :integer # geraldine_public_body: @@ -35,6 +39,7 @@ geraldine_public_body: created_at: 2007-10-24 10:51:01.161639 api_key: 1 info_requests_count: 4 + info_requests_visible_classified_count: 4 info_requests_successful_count: 0 info_requests_not_held_count: 0 info_requests_overdue_count: 3 @@ -53,6 +58,7 @@ humpadink_public_body: notes: An albatross told me!!! api_key: 2 info_requests_count: 2 + info_requests_visible_classified_count: 2 info_requests_successful_count: 1 info_requests_not_held_count: 0 info_requests_overdue_count: 1 @@ -71,6 +77,7 @@ forlorn_public_body: notes: A very lonely public body that no one has corresponded with api_key: 3 info_requests_count: 0 + info_requests_visible_classified_count: 0 info_requests_successful_count: 0 info_requests_not_held_count: 0 info_requests_overdue_count: 0 @@ -89,6 +96,7 @@ silly_walks_public_body: notes: You know the one. api_key: 4 info_requests_count: 2 + info_requests_visible_classified_count: 2 info_requests_successful_count: 2 info_requests_not_held_count: 0 info_requests_overdue_count: 0 @@ -107,6 +115,7 @@ sensible_walks_public_body: created_at: 2008-10-25 10:51:01.161639 api_key: 5 info_requests_count: 1 + info_requests_visible_classified_count: 1 info_requests_successful_count: 1 info_requests_not_held_count: 0 info_requests_overdue_count: 0 @@ -125,6 +134,26 @@ other_public_body: created_at: 2008-10-25 10:51:01.161639 api_key: 6 info_requests_count: 0 + info_requests_visible_classified_count: 0 + info_requests_successful_count: 0 + info_requests_not_held_count: 0 + info_requests_overdue_count: 0 +accented_public_body: + id: 8 + version: 1 + name: 'Åčçèñtéd Authority' + first_letter: Å + request_email: accented@localhost + short_name: 'Åčçèñtéd Authority' + url_name: accented_authority + notes: This is to test unicode handling in body names + updated_at: 2008-10-25 10:51:01.161639 + last_edit_comment: Another edit + last_edit_editor: louise + created_at: 2008-10-25 10:51:01.161639 + api_key: 7 + info_requests_count: 0 + info_requests_visible_classified_count: 0 info_requests_successful_count: 0 info_requests_not_held_count: 0 info_requests_overdue_count: 0 diff --git a/spec/fixtures/public_body_translations.yml b/spec/fixtures/public_body_translations.yml index de1bf2f18..2030804ac 100644 --- a/spec/fixtures/public_body_translations.yml +++ b/spec/fixtures/public_body_translations.yml @@ -115,3 +115,15 @@ humpadink_he_IL_public_body_translation: publication_scheme: "" disclosure_log: "" +accented_public_body_translation: + id: 10 + public_body_id: 8 + locale: cs + name: "Åčçèñtéd Authority" + first_letter: 'Å' + request_email: accented@localhost + short_name: "Åčçèñtéd Authority" + url_name: accented_authority + notes: This is to test unicode handling in body names + publication_scheme: "" + disclosure_log: "" diff --git a/spec/fixtures/users.yml b/spec/fixtures/users.yml index d996fd31e..9eb91ee9c 100644 --- a/spec/fixtures/users.yml +++ b/spec/fixtures/users.yml @@ -20,8 +20,6 @@ # email_bounce_message :text default(""), not null # no_limit :boolean default(FALSE), not null # receive_email_alerts :boolean default(TRUE), not null -# address :string(255) -# dob :date # bob_smith_user: 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/theme_spec.rb b/spec/lib/theme_spec.rb new file mode 100644 index 000000000..829c1a269 --- /dev/null +++ b/spec/lib/theme_spec.rb @@ -0,0 +1,25 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "theme_url_to_theme_name" do + + it "should deal with a typical bare repo URL" do + url = 'git://wherever/blah-theme.git' + theme_url_to_theme_name(url).should == 'blah-theme' + end + + it "should deal with a typical bare repo URL with trailing slashes" do + url = 'ssh://wherever/blah-theme.git//' + theme_url_to_theme_name(url).should == 'blah-theme' + end + + it "should deal with a typical non-bare repo URL" do + url = '/home/whoever/themes/blah-theme' + theme_url_to_theme_name(url).should == 'blah-theme' + end + + it "should deal with a typical non-bare repo URL with a trailing slash" do + url = '/home/whoever/themes/blah-theme/' + theme_url_to_theme_name(url).should == 'blah-theme' + 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/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 7281b74b6..64ad1972e 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -770,6 +770,16 @@ describe InfoRequest do end end + describe 'when working out a subject for request emails' do + + it 'should create a standard request subject' do + info_request = FactoryGirl.build(:info_request) + expected_text = "Freedom of Information request - #{info_request.title}" + info_request.email_subject_request.should == expected_text + end + + end + describe 'when working out a subject for a followup emails' do it "should not be confused by an nil subject in the incoming message" do diff --git a/spec/models/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb index 8c000665b..bb270ca16 100644 --- a/spec/models/outgoing_message_spec.rb +++ b/spec/models/outgoing_message_spec.rb @@ -12,6 +12,8 @@ # last_sent_at :datetime # incoming_message_followup_id :integer # what_doing :string(255) not null +# prominence :string(255) default("normal"), not null +# prominence_reason :text # require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 582f1430e..23842ccff 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -3,23 +3,27 @@ # # Table name: public_bodies # -# id :integer not null, primary key -# name :text not null -# short_name :text not null -# request_email :text not null -# version :integer not null -# last_edit_editor :string(255) not null -# last_edit_comment :text not null -# created_at :datetime not null -# updated_at :datetime not null -# url_name :text not null -# home_page :text default(""), not null -# notes :text default(""), not null -# first_letter :string(255) not null -# publication_scheme :text default(""), not null -# api_key :string(255) not null -# info_requests_count :integer default(0), not null -# disclosure_log :text default(""), not null +# id :integer not null, primary key +# name :text not null +# short_name :text not null +# request_email :text not null +# version :integer not null +# last_edit_editor :string(255) not null +# last_edit_comment :text not null +# created_at :datetime not null +# updated_at :datetime not null +# url_name :text not null +# home_page :text default(""), not null +# notes :text default(""), not null +# first_letter :string(255) not null +# publication_scheme :text default(""), not null +# api_key :string(255) not null +# info_requests_count :integer default(0), not null +# disclosure_log :text default(""), not null +# info_requests_successful_count :integer +# info_requests_not_held_count :integer +# info_requests_overdue_count :integer +# info_requests_visible_classified_count :integer # require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') @@ -190,6 +194,17 @@ describe PublicBody, " when saving" do @public_body.first_letter.should == 'T' end + it "should update first letter, even if it's a multibyte character" do + pb = PublicBody.new(:name => 'åccents, lower-case', + :short_name => 'ALC', + :request_email => 'foo@localhost', + :last_edit_editor => 'test', + :last_edit_comment => '') + pb.first_letter.should be_nil + pb.save! + pb.first_letter.should == 'Å' + end + it "should save the name when renaming an existing public body" do public_body = public_bodies(:geraldine_public_body) public_body.name = "Mark's Public Body" @@ -300,7 +315,7 @@ describe PublicBody, " when loading CSV files" do errors.should == [] notes.size.should == 2 notes[0].should == "line 1: creating new authority 'aBody' (locale: en):\n\t{\"name\":\"aBody\"}" - notes[1].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ + notes[1].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( .+\n)*You may want to delete them manually.\n/ end it "should do a dry run successfully" do @@ -309,14 +324,15 @@ describe PublicBody, " when loading CSV files" do csv_contents = normalize_string_to_utf8(load_file_fixture("fake-authority-type.csv")) errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin') # true means dry run errors.should == [] - notes.size.should == 5 - notes[0..3].should == [ + notes.size.should == 6 + notes[0..4].should == [ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}", "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}", "line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}", "line 4: creating new authority 'Gobierno de Aragón' (locale: en):\n\t\{\"name\":\"Gobierno de Arag\\u00f3n\",\"request_email\":\"spain_foi@localhost\"}", + "line 5: creating new authority 'Nordic æøå' (locale: en):\n\t{\"name\":\"Nordic \\u00e6\\u00f8\\u00e5\",\"request_email\":\"no_foi@localhost\"}" ] - notes[4].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ + notes[5].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( .+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count end @@ -327,16 +343,17 @@ describe PublicBody, " when loading CSV files" do csv_contents = normalize_string_to_utf8(load_file_fixture("fake-authority-type.csv")) errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin') # false means real run errors.should == [] - notes.size.should == 5 - notes[0..3].should == [ + notes.size.should == 6 + notes[0..4].should == [ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}", "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}", "line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}", "line 4: creating new authority 'Gobierno de Aragón' (locale: en):\n\t\{\"name\":\"Gobierno de Arag\\u00f3n\",\"request_email\":\"spain_foi@localhost\"}", + "line 5: creating new authority 'Nordic æøå' (locale: en):\n\t{\"name\":\"Nordic \\u00e6\\u00f8\\u00e5\",\"request_email\":\"no_foi@localhost\"}" ] - notes[4].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ + notes[5].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( .+\n)*You may want to delete them manually.\n/ - PublicBody.count.should == original_count + 4 + PublicBody.count.should == original_count + 5 end it "should do imports without a tag successfully" do @@ -345,15 +362,16 @@ describe PublicBody, " when loading CSV files" do csv_contents = normalize_string_to_utf8(load_file_fixture("fake-authority-type.csv")) errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin') # false means real run errors.should == [] - notes.size.should == 5 - notes[0..3].should == [ + notes.size.should == 6 + notes[0..4].should == [ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}", "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}", "line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}", "line 4: creating new authority 'Gobierno de Aragón' (locale: en):\n\t\{\"name\":\"Gobierno de Arag\\u00f3n\",\"request_email\":\"spain_foi@localhost\"}", + "line 5: creating new authority 'Nordic æøå' (locale: en):\n\t{\"name\":\"Nordic \\u00e6\\u00f8\\u00e5\",\"request_email\":\"no_foi@localhost\"}" ] - notes[4].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ - PublicBody.count.should == original_count + 4 + notes[5].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( .+\n)*You may want to delete them manually.\n/ + PublicBody.count.should == original_count + 5 end it "should handle a field list and fields out of order" do @@ -368,7 +386,7 @@ describe PublicBody, " when loading CSV files" do "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"\}", "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\",\"tag_string\":\"fake aTag\"\}", ] - notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ + notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( .+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count end @@ -425,7 +443,7 @@ describe PublicBody, " when loading CSV files" do "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\",\"tag_string\":\"fake aTag\"}", "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: es):\n\t{\"name\":\"Autoridad Irlandesa\"}", ] - notes[6].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ + notes[6].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( .+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count + 3 @@ -451,7 +469,7 @@ describe PublicBody, " when loading CSV files" do "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"}", "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\",\"tag_string\":\"fake aTag\"}", ] - notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ + notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( .+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count end @@ -512,3 +530,77 @@ describe PublicBody, " when override all public body request emails set" do @geraldine.request_email.should == "catch_all_test_email@foo.com" end end + +describe PublicBody, "when calculating statistics" do + + it "should not include unclassified or hidden requests in percentages" do + with_hidden_and_successful_requests do + totals_data = PublicBody.get_request_totals(n=3, + highest=true, + minimum_requests=1) + # For the total number of requests, we still include + # hidden or unclassified requests: + totals_data['public_bodies'][-1].name.should == "Geraldine Quango" + totals_data['totals'][-1].should == 4 + + # However, for percentages, don't include the hidden or + # unclassified requests. So, for the Geraldine Quango + # we've made sure that there are only two visible and + # classified requests, one of which is successful, so the + # percentage should be 50%: + + percentages_data = PublicBody.get_request_percentages(column='info_requests_successful_count', + n=3, + highest=false, + minimum_requests=1) + geraldine_index = percentages_data['public_bodies'].index do |pb| + pb.name == "Geraldine Quango" + end + + percentages_data['y_values'][geraldine_index].should == 50 + end + end + + it "should only return totals for those with at least a minimum number of requests" do + minimum_requests = 1 + with_enough_info_requests = PublicBody.where(["info_requests_count >= ?", + minimum_requests]).length + all_data = PublicBody.get_request_totals 4, true, minimum_requests + all_data['public_bodies'].length.should == with_enough_info_requests + end + + it "should only return percentages for those with at least a minimum number of requests" do + with_hidden_and_successful_requests do + # With minimum requests at 3, this should return nil + # (corresponding to zero public bodies) since the only + # public body with just more than 3 info requests (The + # Geraldine Quango) has a hidden and an unclassified + # request within this block: + minimum_requests = 3 + with_enough_info_requests = PublicBody.where(["info_requests_visible_classified_count >= ?", + minimum_requests]).length + all_data = PublicBody.get_request_percentages(column='info_requests_successful_count', + n=10, + true, + minimum_requests) + all_data.should be_nil + end + end + + it "should only return those with at least a minimum number of requests, but not tagged 'test'" do + hpb = PublicBody.find_by_name 'Department for Humpadinking' + + original_tag_string = hpb.tag_string + hpb.add_tag_if_not_already_present 'test' + + begin + minimum_requests = 1 + with_enough_info_requests = PublicBody.where(["info_requests_count >= ?", minimum_requests]) + all_data = PublicBody.get_request_totals 4, true, minimum_requests + all_data['public_bodies'].length.should == 3 + ensure + hpb.tag_string = original_tag_string + end + end + +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index f380f6f13..cbbf4b5ce 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -20,8 +20,6 @@ # email_bounce_message :text default(""), not null # no_limit :boolean default(FALSE), not null # receive_email_alerts :boolean default(TRUE), not null -# address :string(255) -# dob :date # require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index c7c21e3a0..3c9fff784 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -406,28 +406,13 @@ describe InfoRequestEvent, " when faced with a race condition during xapian_mark before(:each) do load_raw_emails_data get_fixtures_xapian_index - # Use the before create job hook to simulate a race condition with another process - # by creating an acts_as_xapian_job record for the same model - class InfoRequestEvent - def xapian_before_create_job_hook(action, model, model_id) - ActsAsXapian::ActsAsXapianJob.create!(:model => model, - :model_id => model_id, - :action => action) - end - end - end - - after(:each) do - # Reset the before create job hook - class InfoRequestEvent - def xapian_before_create_job_hook(action, model, model_id) - end - end end it 'should not raise an error but should fail silently' do - ir = info_requests(:naughty_chicken_request) - ir.reindex_request_events + with_duplicate_xapian_job_creation do + ir = info_requests(:naughty_chicken_request) + ir.reindex_request_events + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0d8f8fac5..d22f3c0ff 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -125,6 +125,25 @@ Spork.prefork do rebuild_xapian_index end + # Use the before create job hook to simulate a race condition with + # another process by creating an acts_as_xapian_job record for the + # same model: + def with_duplicate_xapian_job_creation + InfoRequestEvent.module_eval do + def xapian_before_create_job_hook(action, model, model_id) + ActsAsXapian::ActsAsXapianJob.create!(:model => model, + :model_id => model_id, + :action => action) + end + end + yield + ensure + InfoRequestEvent.module_eval do + def xapian_before_create_job_hook(action, model, model_id) + end + end + end + def with_env_tz(new_tz = 'US/Eastern') old_tz, ENV['TZ'] = ENV['TZ'], new_tz yield @@ -139,6 +158,39 @@ Spork.prefork do ActiveRecord::Base.default_timezone = old_zone end + # To test the statistics calculations, it's helpful to have the + # request fixtures in different states, but changing the fixtures + # themselves disrupts many other tests. This function takes a + # block, and runs that block with the info requests for the + # Geraldine Quango altered so that one is hidden and there's a + # successful one. + def with_hidden_and_successful_requests + external = info_requests(:external_request) + chicken = info_requests(:naughty_chicken_request) + old_external_prominence = external.prominence + old_chicken_described_state = chicken.described_state + begin + external.prominence = 'hidden' + external.save! + chicken.described_state = 'successful' + chicken.save! + yield + ensure + external.prominence = old_external_prominence + external.save! + chicken.described_state = old_chicken_described_state + chicken.save! + 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", |