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 | 62 | ||||
-rw-r--r-- | spec/controllers/general_controller_spec.rb | 45 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 4 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 29 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 14 | ||||
-rw-r--r-- | spec/controllers/user_controller_spec.rb | 4 | ||||
-rw-r--r-- | spec/fixtures/files/fake-authority-type-with-field-names.csv | 4 | ||||
-rw-r--r-- | spec/fixtures/info_requests.yml | 6 | ||||
-rw-r--r-- | spec/integration/search_request_spec.rb | 9 | ||||
-rw-r--r-- | spec/lib/i18n_interpolation.rb | 15 | ||||
-rw-r--r-- | spec/models/incoming_message_spec.rb | 39 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 34 | ||||
-rw-r--r-- | spec/models/outgoing_message_spec.rb | 7 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 65 | ||||
-rw-r--r-- | spec/models/request_mailer_spec.rb | 44 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 6 |
18 files changed, 372 insertions, 35 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..423c2fb49 100644 --- a/spec/controllers/admin_request_controller_spec.rb +++ b/spec/controllers/admin_request_controller_spec.rb @@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe AdminRequestController, "when administering requests" do integrate_views - fixtures :info_requests, :outgoing_messages, :users, :info_request_events + fixtures :info_requests, :outgoing_messages, :users, :info_request_events, :public_bodies, :public_body_translations before { basic_auth_login @request } it "shows the index/list page" do @@ -39,3 +39,63 @@ 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.allow_new_responses_from = 'authority_only' + ir.save! + mail_to = "request-#{ir.id}-asdfg@example.com" + receive_incoming_mail('incoming-request-plain.email', mail_to) + interesting_email = InfoRequest.holding_pen_request.get_last_response.raw_email.id + # now we add another message to the queue, which we're not interested in + receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "") + InfoRequest.holding_pen_request.incoming_messages.length.should == 2 + get :show_raw_email, :id => interesting_email + 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..3640a8148 100644 --- a/spec/controllers/general_controller_spec.rb +++ b/spec/controllers/general_controller_spec.rb @@ -12,11 +12,56 @@ describe GeneralController, "when searching" do :incoming_messages, :comments ] + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should render the front page successfully" do get :frontpage 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/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 69c1433f2..f69cf414c 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -7,8 +7,10 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'json' describe RequestController, "when listing recent requests" do - + fixtures :info_requests, :outgoing_messages, :users, :info_request_events, :public_bodies, :public_body_translations, :incoming_messages, :raw_emails, :comments + before(:each) do + load_raw_emails_data(raw_emails) rebuild_xapian_index end @@ -123,6 +125,14 @@ describe RequestController, "when showing one request" do response.should have_text(/First hello/) end + it "should generate valid HTML verson of plain text attachments " do + ir = info_requests(:fancy_dog_request) + receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email) + get :get_attachment_as_html, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt.html'], :skip_cache => 1 + response.content_type.should == "text/html" + response.should have_text(/Second hello/) + end + it "should treat attachments with unknown extensions as binary" do ir = info_requests(:fancy_dog_request) receive_incoming_mail('incoming-request-attachment-unknown-extension.email', ir.incoming_email) @@ -306,6 +316,13 @@ describe RequestController, "when creating a new request" do response.should redirect_to(:controller => 'general', :action => 'frontpage') end + it "should redirect 'bad request' page when a body has no email address" do + @body.request_email = "" + @body.save! + get :new, :public_body_id => @body.id + response.should render_template('new_bad_contact') + end + it "should accept a public body parameter" do get :new, :public_body_id => @body.id assigns[:info_request].public_body.should == @body @@ -496,7 +513,7 @@ describe RequestController, "when classifying an information request" do fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages, :comments # all needed as integrating views - before do + before(:each) do @dog_request = info_requests(:fancy_dog_request) @dog_request.stub!(:is_old_unclassified?).and_return(false) InfoRequest.stub!(:find).and_return(@dog_request) @@ -776,6 +793,8 @@ describe RequestController, "when classifying an information request" do @dog_request.stub!(:date_very_overdue_after).and_return(Time.now.to_date-1) expect_redirect('waiting_response', unhappy_url) flash[:notice].should match(/is long overdue/) + flash[:notice].should match(/by more than 40 working days/) + flash[:notice].should match(/within 20 working days/) end it 'should redirect to the "request url" when status is updated to "not held"' do @@ -831,7 +850,11 @@ end describe RequestController, "when sending a followup message" do integrate_views fixtures :info_requests, :info_request_events, :public_bodies, :public_body_translations, :users, :incoming_messages, :raw_emails, :outgoing_messages # all needed as integrating views - + + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should require login" do post :show_response, :outgoing_message => { :body => "What a useless response! You suck.", :what_doing => 'normal_sort' }, :id => info_requests(:fancy_dog_request).id, :incoming_message_id => incoming_messages(:useless_incoming_message), :submitted_followup => 1 post_redirect = PostRedirect.get_last_post_redirect diff --git a/spec/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index f7002a9d4..2f3f903f9 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -37,14 +37,12 @@ describe TrackController, "when sending alerts for a track" do integrate_views fixtures :info_requests, :outgoing_messages, :incoming_messages, :raw_emails, :info_request_events, :users, :track_things, :track_things_sent_emails, :public_bodies, :public_body_translations include LinkToHelper # for main_url + before(:each) do load_raw_emails_data(raw_emails) - end - - before do rebuild_xapian_index end - + it "should send alerts" do # Don't do clever locale-insertion-unto-URL stuff ActionController::Routing::Routes.filters.clear @@ -101,9 +99,9 @@ describe TrackController, "when viewing RSS feed for a track" do integrate_views fixtures :info_requests, :outgoing_messages, :incoming_messages, :raw_emails, :info_request_events, :users, :track_things, :comments, :public_bodies, :public_body_translations - before do - rebuild_xapian_index + before(:each) do load_raw_emails_data(raw_emails) + rebuild_xapian_index end it "should get the RSS feed" do @@ -127,9 +125,9 @@ describe TrackController, "when viewing JSON version of a track feed" do integrate_views fixtures :info_requests, :outgoing_messages, :incoming_messages, :raw_emails, :info_request_events, :users, :track_things, :comments, :public_bodies, :public_body_translations - before do - rebuild_xapian_index + before(:each) do load_raw_emails_data(raw_emails) + rebuild_xapian_index end it "should get the feed" do diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index 0b5e96711..c974c8a0d 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -179,7 +179,7 @@ describe UserController, "when signing up" do deliveries = ActionMailer::Base.deliveries deliveries.size.should == 1 - deliveries[0].body.should include("have an account") + deliveries[0].body.should include("when you already have an") end # XXX need to do bob@localhost signup and check that sends different email @@ -236,7 +236,7 @@ describe UserController, "when sending another user a message" do deliveries = ActionMailer::Base.deliveries deliveries.size.should == 1 mail = deliveries[0] - mail.body.should include("Bob Smith has used WhatDoTheyKnow to send you the message below") + mail.body.should include("Bob Smith has used #{MySociety::Config.get('SITE_NAME')} to send you the message below") mail.body.should include("Just a test!") #mail.to_addrs.to_s.should == users(:silly_name_user).name_and_email # XXX fix some nastiness with quoting name_and_email mail.from_addrs.to_s.should == users(:bob_smith_user).name_and_email diff --git a/spec/fixtures/files/fake-authority-type-with-field-names.csv b/spec/fixtures/files/fake-authority-type-with-field-names.csv new file mode 100644 index 000000000..93ce00a25 --- /dev/null +++ b/spec/fixtures/files/fake-authority-type-with-field-names.csv @@ -0,0 +1,4 @@ +#id,email,name,name.es +,north_west_foi@localhost,North West Fake Authority,Autoridad del Nordeste +,scottish_foi@localhost,Scottish Fake Authority,Autoridad Escocesa +,ni_foi@localhost,Fake Authority of Northern Ireland,Autoridad Irlandesa 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/integration/search_request_spec.rb b/spec/integration/search_request_spec.rb new file mode 100644 index 000000000..9398519b7 --- /dev/null +++ b/spec/integration/search_request_spec.rb @@ -0,0 +1,9 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "When searching" do + it "should not strip quotes from quoted query" do + request_via_redirect("post", "/search", :query => '"mouse stilton"') + response.body.should include(""mouse stilton"") + end +end + diff --git a/spec/lib/i18n_interpolation.rb b/spec/lib/i18n_interpolation.rb new file mode 100644 index 000000000..8c86413ad --- /dev/null +++ b/spec/lib/i18n_interpolation.rb @@ -0,0 +1,15 @@ +# This is a test of the set_content_type monkey patch in +# lib/tmail_extensions.rb + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "when using i18n" do + + it "should not complain if we're missing variables from the string" do + result = _('Hello', :dip => 'hummus') + result.should == 'Hello' + result = _('Hello {{dip}}', :dip => 'hummus') + result.should == 'Hello hummus' + end +end + diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index 42ea748fd..183a258af 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe IncomingMessage, " when dealing with incoming mail" do fixtures :incoming_messages, :raw_emails, :info_requests - before do + before(:each) do @im = incoming_messages(:useless_incoming_message) load_raw_emails_data(raw_emails) end @@ -19,6 +19,13 @@ describe IncomingMessage, " when dealing with incoming mail" do end +describe IncomingMessage, "when parsing HTML mail" do + it "should display UTF-8 characters in the plain text version correctly" do + html = "<html><b>foo</b> është" + plain_text = IncomingMessage._get_attachment_text_internal_one_file('text/html', html) + plain_text.should match(/është/) + end +end describe IncomingMessage, "when getting the attachment text" do @@ -112,7 +119,7 @@ end describe IncomingMessage, " when censoring data" do fixtures :incoming_messages, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :users - before do + before(:each) do @test_data = "There was a mouse called Stilton, he wished that he was blue." @im = incoming_messages(:useless_incoming_message) @@ -160,10 +167,12 @@ describe IncomingMessage, " when censoring data" do data.should == "His email was x\000x\000x\000@\000x\000x\000x\000.\000x\000x\000x\000, indeed" end - # As at March 9th 2010: This test fails with pdftk 1.41+dfsg-1 installed - # which is in Ubuntu Karmic. It works again for the lasest version - # 1.41+dfsg-7 in Debian unstable. And it works for Debian stable. - it "should replace everything in PDF files" do + + + def pdf_replacement_test(use_ghostscript_compression) + config = MySociety::Config.load_default() + previous = config['USE_GHOSTSCRIPT_COMPRESSION'] + config['USE_GHOSTSCRIPT_COMPRESSION'] = use_ghostscript_compression orig_pdf = load_file_fixture('tfl.pdf') pdf = orig_pdf.dup @@ -175,6 +184,15 @@ describe IncomingMessage, " when censoring data" do masked_text = IncomingMessage._get_attachment_text_internal_one_file('application/pdf', pdf) masked_text.should_not match(/foi@tfl.gov.uk/) masked_text.should match(/xxx@xxx.xxx.xx/) + config['USE_GHOSTSCRIPT_COMPRESSION'] = previous + end + + it "should replace everything in PDF files using pdftk" do + pdf_replacement_test(false) + end + + it "should replace everything in PDF files using ghostscript" do + pdf_replacement_test(true) end it "should not produce zero length output if pdftk silently fails" do @@ -190,6 +208,13 @@ describe IncomingMessage, " when censoring data" do data.should == "There was a mouse called Jarlsberg, he wished that he was yellow." end + it "should apply hard-coded privacy rules to HTML files" do + domain = MySociety::Config.get('DOMAIN') + data = "http://#{domain}/c/cheese" + @im.html_mask_stuff!(data) + data.should == "[WDTK login link]" + end + it "should apply censor rules to From: addresses" do mock_mail = mock('Email object') mock_mail.stub!(:from_name_if_present).and_return("Stilton Mouse") @@ -204,7 +229,7 @@ end describe IncomingMessage, " when censoring whole users" do fixtures :incoming_messages, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :users - before do + before(:each) do @test_data = "There was a mouse called Stilton, he wished that he was blue." @im = incoming_messages(:useless_incoming_message) diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index d0b0e0e32..b82052a0f 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(:each) 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/outgoing_message_spec.rb b/spec/models/outgoing_message_spec.rb index a9ef57b4f..1956c4d73 100644 --- a/spec/models/outgoing_message_spec.rb +++ b/spec/models/outgoing_message_spec.rb @@ -1,7 +1,10 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe OutgoingMessage, " when making an outgoing message" do + fixtures :outgoing_messages, :info_requests, :incoming_messages, :public_bodies, :public_body_translations + before do + @om = outgoing_messages(:useless_outgoing_message) @outgoing_message = OutgoingMessage.new({ :status => 'ready', :message_type => 'initial_request', @@ -27,6 +30,10 @@ describe OutgoingMessage, " when making an outgoing message" do it "should include email addresses in outgoing messages" do @outgoing_message.body.should include("foo@bar.com") end + + it "should work out a salutation" do + @om.get_salutation.should == "Dear Geraldine Quango," + end end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 5bbf03d27..3d00d37fb 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -210,6 +210,20 @@ describe PublicBody, "when searching" do body.id.should == 3 body.class.to_s.should == 'PublicBody' end + + it "should cope with same url_name across multiple locales" do + PublicBody.with_locale(:es) do + # use the unique spanish name to retrieve and edit + body = PublicBody.find_by_url_name_with_historic('etgq') + body.short_name = 'tgq' # Same as english version + body.save! + + # now try to retrieve it + body = PublicBody.find_by_url_name_with_historic('tgq') + body.id.should == public_bodies(:geraldine_public_body).id + body.name.should == "El A Geraldine Quango" + end + end end describe PublicBody, " when loading CSV files" do @@ -240,7 +254,56 @@ describe PublicBody, " when loading CSV files" do PublicBody.count.should == original_count + 3 end -end + it "should handle a field list and fields out of order" do + original_count = PublicBody.count + + csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv") + errors, notes = PublicBody.import_csv(csv_contents, 'fake', true, 'someadmin') # true means dry run + errors.should == [] + notes.size.should == 3 + notes.should == ["line 2: new authority 'North West Fake Authority' with email north_west_foi@localhost", + "line 3: new authority 'Scottish Fake Authority' with email scottish_foi@localhost", + "line 4: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost"] + + PublicBody.count.should == original_count + end + it "should create bodies with names in multiple locales" do + original_count = PublicBody.count + csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv") + errors, notes = PublicBody.import_csv(csv_contents, 'fake', false, 'someadmin', ['es']) + errors.should == [] + notes.size.should == 6 + notes.should == [ + "line 2: new authority 'North West Fake Authority' with email north_west_foi@localhost", + "line 2: (aka 'Autoridad del Nordeste' in locale es)", + "line 3: new authority 'Scottish Fake Authority' with email scottish_foi@localhost", + "line 3: (aka 'Autoridad Escocesa' in locale es)", + "line 4: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost", + "line 4: (aka 'Autoridad Irlandesa' in locale es)"] + + PublicBody.count.should == original_count + 3 + + # XXX Not sure why trying to do a PublicBody.with_locale fails here. Seems related to + # the way categories are loaded every time from the PublicBody class. For now we just + # test some translation was done. + body = PublicBody.find_by_name('North West Fake Authority') + body.translated_locales.map{|l|l.to_s}.sort.should == ["en", "es"] + end + + it "should not fail if a locale is not found in the input file" do + original_count = PublicBody.count + + csv_contents = load_file_fixture("fake-authority-type-with-field-names.csv") + errors, notes = PublicBody.import_csv(csv_contents, 'fake', true, 'someadmin', ['xx']) # true means dry run + errors.should == [] + notes.size.should == 3 + notes.should == ["line 2: new authority 'North West Fake Authority' with email north_west_foi@localhost", + "line 3: new authority 'Scottish Fake Authority' with email scottish_foi@localhost", + "line 4: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost"] + + PublicBody.count.should == original_count + end +end 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/xapian_spec.rb b/spec/models/xapian_spec.rb index 37e68b145..0c6fa6bb6 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -34,7 +34,7 @@ describe User, " when indexing users with Xapian" do end describe PublicBody, " when indexing public bodies with Xapian" do - fixtures :public_bodies, :public_body_translations, :incoming_messages, :outgoing_messages, :raw_emails, :comments + fixtures :public_bodies, :public_body_translations, :incoming_messages, :outgoing_messages, :raw_emails, :comments, :info_requests before(:each) do load_raw_emails_data(raw_emails) end @@ -74,6 +74,10 @@ end describe PublicBody, " when indexing requests by body they are to" do fixtures :public_bodies, :public_body_translations, :info_request_events, :info_requests, :raw_emails, :comments + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should find requests to the body" do rebuild_xapian_index xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_from:tgq", 'created_at', true, nil, 100, 1) |