From 1708f4a4c3f27e5bbbbb5818623dc1eb6688d2f0 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Wed, 3 Aug 2011 15:11:28 +0100 Subject: Log the reason why an incoming mail is routed to the holding pen, and display it to administrators. Closes #107. --- spec/models/request_mailer_spec.rb | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'spec/models') 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 -- cgit v1.2.3 From 99548c87d201871a150e395c334e07149651996a Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Mon, 8 Aug 2011 11:54:27 +0100 Subject: Guess holding pen emails based on having a correct hash and an incorrect id. Closes #117 --- spec/models/info_request_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'spec/models') diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index d0b0e0e32..ba80256ab 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -2,6 +2,40 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe InfoRequest do + describe "guessing a request from an email" do + fixtures :info_requests, :public_bodies, :incoming_messages, :raw_emails + + before do + @im = incoming_messages(:useless_incoming_message) + load_raw_emails_data(raw_emails) + end + + it 'should compute a hash' do + @info_request = InfoRequest.new(:title => "testing", + :public_body => public_bodies(:geraldine_public_body), + :user_id => 1) + @info_request.save! + @info_request.idhash.should_not == nil + end + + it 'should find a request based on an email with an intact id and a broken hash' do + ir = info_requests(:fancy_dog_request) + id = ir.id + @im.mail.to = "request-#{id}-asdfg@example.com" + guessed = InfoRequest.guess_by_incoming_email(@im) + guessed[0].idhash.should == ir.idhash + end + + it 'should find a request based on an email with a broken id and an intact hash' do + ir = info_requests(:fancy_dog_request) + idhash = ir.idhash + @im.mail.to = "request-123ab-#{idhash}@example.com" + guessed = InfoRequest.guess_by_incoming_email(@im) + guessed[0].id.should == ir.id + end + + end + describe "making up the URL title" do before do @info_request = InfoRequest.new -- cgit v1.2.3 From 6e57ba619e11ee76a29eda12e7cfef1f9aefba8e Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Wed, 10 Aug 2011 16:19:48 +0100 Subject: Fix up missing fixtures (causing failures dependent on order the tests were funr). Also tidy up raw_email setup code to match everywhere. --- spec/models/incoming_message_spec.rb | 6 +++--- spec/models/info_request_spec.rb | 2 +- spec/models/xapian_spec.rb | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index 42ea748fd..70d92356f 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 @@ -112,7 +112,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) @@ -204,7 +204,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 ba80256ab..b82052a0f 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -5,7 +5,7 @@ describe InfoRequest do describe "guessing a request from an email" do fixtures :info_requests, :public_bodies, :incoming_messages, :raw_emails - before do + before(:each) do @im = incoming_messages(:useless_incoming_message) load_raw_emails_data(raw_emails) end 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) -- cgit v1.2.3 From 79a2047f04aad0d04b56aa8f81152e868b4e3951 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Wed, 10 Aug 2011 16:58:12 +0100 Subject: Apply censor rules even when pdftk refuses to compress. Also provide for alternative to pdftk for compression. Closes #123. --- spec/models/incoming_message_spec.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index 70d92356f..5fcc534ca 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -160,10 +160,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 +177,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 -- cgit v1.2.3 From b4af048ec41048c1dd05a6b5445d5e357ba4444a Mon Sep 17 00:00:00 2001 From: David Cabo Date: Tue, 9 Aug 2011 17:29:27 +0200 Subject: Handle optional field list on CSV import, needed for enabling multiple locales --- spec/models/public_body_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'spec/models') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 5bbf03d27..e532d0d50 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -242,5 +242,18 @@ describe PublicBody, " when loading CSV files" do end end +describe PublicBody, " when loading CSV files with field names and fields out of order" do + it "should do a dry run successfully" 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 1: new authority 'North West Fake Authority' with email north_west_foi@localhost", + "line 2: new authority 'Scottish Fake Authority' with email scottish_foi@localhost", + "line 3: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost"] + PublicBody.count.should == original_count + end +end -- cgit v1.2.3 From 4713ad8cfde9b37befdf8c9cac3f26da0997786a Mon Sep 17 00:00:00 2001 From: David Cabo Date: Tue, 9 Aug 2011 18:52:15 +0200 Subject: Create public bodies in multiple locales when importing from CSV --- spec/models/public_body_spec.rb | 48 +++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) (limited to 'spec/models') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index e532d0d50..83075e4af 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -240,19 +240,55 @@ describe PublicBody, " when loading CSV files" do PublicBody.count.should == original_count + 3 end -end -describe PublicBody, " when loading CSV files with field names and fields out of order" do - it "should do a dry run successfully" do + 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 1: new authority 'North West Fake Authority' with email north_west_foi@localhost", - "line 2: new authority 'Scottish Fake Authority' with email scottish_foi@localhost", - "line 3: new authority 'Fake Authority of Northern Ireland' with email ni_foi@localhost"] + 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.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 -- cgit v1.2.3 From af4d10fa5d98112c0d841d2dbea639147b3992d6 Mon Sep 17 00:00:00 2001 From: David Cabo Date: Fri, 12 Aug 2011 10:58:04 +0200 Subject: make CSV import test deterministic by sorting results --- spec/models/public_body_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 83075e4af..ec84cbe65 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -276,7 +276,7 @@ describe PublicBody, " when loading CSV files" do # 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.should == [:en, :es] + 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 -- cgit v1.2.3 From 7705f6754184d471fb5da38c5d8671a7737bcdd6 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Thu, 11 Aug 2011 17:54:29 +0100 Subject: Remove more whatdotheyknow references. Closes #113 (I think). --- spec/models/incoming_message_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index 5fcc534ca..ad7aa8d43 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -201,6 +201,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") -- cgit v1.2.3 From dbac4121cae0d620c7c11c6731fe5344ff1c677e Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Fri, 12 Aug 2011 16:06:28 +0100 Subject: Force elinks to assume UTF8 character set for its input (used when making plain text versions of HTML email) --- spec/models/incoming_message_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'spec/models') diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index ad7aa8d43..183a258af 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -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 = "foo ë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 -- cgit v1.2.3 From f6516d55f75752bf1eb7773f0a09cc6200bc9fad Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Mon, 15 Aug 2011 12:12:43 +0100 Subject: Internationalise the salution, making sure it's OK to miss out the public body name as they might in French. Fixes #134. --- spec/models/outgoing_message_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'spec/models') 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 -- cgit v1.2.3 From 95e357b02b75de103d93180e3a33417f0c83dfb3 Mon Sep 17 00:00:00 2001 From: David Cabo Date: Tue, 16 Aug 2011 03:50:11 +0200 Subject: Don't fail if a body has the same url_name in several locales (closes #139) --- spec/models/public_body_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'spec/models') diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index ec84cbe65..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 -- cgit v1.2.3