diff options
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/alaveteli_text_masker_spec.rb | 146 | ||||
-rw-r--r-- | spec/lib/mail_handler/backends/mail_backend_spec.rb | 145 | ||||
-rw-r--r-- | spec/lib/mail_handler/mail_handler_spec.rb | 6 |
3 files changed, 297 insertions, 0 deletions
diff --git a/spec/lib/alaveteli_text_masker_spec.rb b/spec/lib/alaveteli_text_masker_spec.rb new file mode 100644 index 000000000..1a4782a83 --- /dev/null +++ b/spec/lib/alaveteli_text_masker_spec.rb @@ -0,0 +1,146 @@ +# -*- coding: utf-8 -*- +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe AlaveteliTextMasker do + include AlaveteliTextMasker + + describe :apply_masks! do + + describe 'when applying censor rules' do + + before do + @cheese_censor_rule = FactoryGirl.build(:censor_rule, :text => 'Stilton', + :replacement => 'Jarlsberg') + @colour_censor_rule = FactoryGirl.build(:censor_rule, :text => 'blue', + :replacement => 'yellow') + @regex_censor_rule = FactoryGirl.build(:censor_rule, :text => 'm[a-z][a-z][a-z]e', + :replacement => 'cat', + :regexp => true) + @censor_rules = [@cheese_censor_rule, @colour_censor_rule, @regex_censor_rule] + end + + it "should do nothing to a JPEG" do + data = "There was a mouse called Stilton, he wished that he was blue." + apply_masks!(data, "image/jpeg", :censor_rules => @censor_rules) + data.should == "There was a mouse called Stilton, he wished that he was blue." + end + + it "should replace censor text in Word documents" do + data = "There was a mouse called Stilton, he wished that he was blue." + apply_masks!(data, "application/vnd.ms-word", :censor_rules => @censor_rules) + data.should == "There was a xxxxx called xxxxxxx, he wished that he was xxxx." + end + + it 'should handle multibyte characters correctly' do + data = 'á mouse' + @regex_censor_rule.text = 'á' + apply_masks!(data, "application/octet-stream", :censor_rules => @censor_rules).should == 'x mouse' + end + + it "should apply censor rules to HTML files" do + data = "There was a mouse called Stilton, he wished that he was blue." + apply_masks!(data, 'text/html', :censor_rules => @censor_rules) + data.should == "There was a cat called Jarlsberg, he wished that he was yellow." + end + + end + + it "should replace ASCII email addresses in Word documents" do + data = "His email was foo@bar.com" + expected = "His email was xxx@xxx.xxx" + apply_masks!(data, "application/vnd.ms-word") + data.should == expected + end + + + it "should replace UCS-2 addresses in Word documents" do + data = "His email was f\000o\000o\000@\000b\000a\000r\000.\000c\000o\000m\000, indeed" + apply_masks!(data, "application/vnd.ms-word") + data.should == "His email was x\000x\000x\000@\000x\000x\000x\000.\000x\000x\000x\000, indeed" + end + + 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 + + orig_text = MailHandler.get_attachment_text_one_file('application/pdf', pdf) + orig_text.should match(/foi@tfl.gov.uk/) + + apply_masks!(pdf, "application/pdf") + + masked_text = MailHandler.get_attachment_text_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 + orig_pdf = load_file_fixture('psni.pdf') + pdf = orig_pdf.dup + apply_masks!(pdf, "application/pdf") + pdf.should_not == "" + end + + it "should apply hard-coded privacy rules to HTML files" do + data = "http://test.host/c/cheese" + apply_masks!(data, 'text/html') + data.should == "[Alaveteli login link]" + end + + it 'should replace a simple email address' do + expected = "the address is [email address]" + apply_masks!("the address is test@example.com", 'text/html', {}).should == expected + end + + it 'should replace a mobile phone number prefixed with "Mobile"' do + expected = "the mobile is [mobile number]" + apply_masks!("the mobile is Mobile 55555 555555", 'text/html', {}).should == expected + end + + it 'should replace a mobile phone number prefixed with "Mob Tel"' do + expected = "the mobile is [mobile number]" + apply_masks!("the mobile is Mob Tel: 55555 555 555", 'text/html', {}).should == expected + end + + it 'should replace a mobile phone number prefixed with "Mob/Fax:"' do + expected = "the mobile is [mobile number]" + apply_masks!("the mobile is Mob/Fax: 55555 555555", 'text/html', {}).should == expected + end + + it "should replace an Alaveteli login link" do + expected = "the login link is [Alaveteli login link]" + apply_masks!("the login link is http://test.host/c/ekfmsdfkm", 'text/html', {}).should == expected + end + + it "should replace a https Alaveteli login link" do + expected = "the login link is [Alaveteli login link]" + apply_masks!("the login link is https://test.host/c/ekfmsdfkm", 'text/html', {}).should == expected + end + + it "should apply censor rules to text" do + censor_rule = FactoryGirl.build(:censor_rule, :text => 'mouse', :replacement => 'cat') + expected = "here is a cat" + apply_masks!("here is a mouse", 'text/html', {:censor_rules => [ censor_rule ]}).should == expected + end + + it 'should apply extra masks to text' do + mask = {:to_replace => 'mouse', :replacement => 'cat'} + expected = "here is a cat" + apply_masks!("here is a mouse", 'text/html', {:masks => [ mask ]}).should == expected + end + + end + +end + diff --git a/spec/lib/mail_handler/backends/mail_backend_spec.rb b/spec/lib/mail_handler/backends/mail_backend_spec.rb new file mode 100644 index 000000000..588033faf --- /dev/null +++ b/spec/lib/mail_handler/backends/mail_backend_spec.rb @@ -0,0 +1,145 @@ +# coding: utf-8 +require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper') + +describe MailHandler::Backends::MailBackend do + include MailHandler + include MailHandler::Backends::MailBackend + + describe :backend do + + it 'should return the name of the backend' do + backend.should == 'Mail' + end + + end + + describe :mail_from_raw_email do + + it 'returns a new mail instance of the email' do + raw_mail = load_file_fixture('raw_emails/1.email') + expected = Mail.read_from_string(raw_mail) + mail_from_raw_email(raw_mail).should == expected + end + + end + + describe :get_part_file_name do + + it 'returns the part file name' do + mail = get_fixture_mail('document-pdf.email') + part = mail.attachments.first + get_part_file_name(part).should == 'tiny-example.pdf' + end + + it 'returns nil if there is no file name' do + mail = get_fixture_mail('document-pdf.email') + part = mail.parts.first + get_part_file_name(part).should be_nil + end + + end + + describe :get_part_body do + + it 'returns the body of a part' do + expected = <<-DOC +Here's a PDF attachement which has a document/pdf content-type, +when it really should be application/pdf.\n +DOC + mail = get_fixture_mail('document-pdf.email') + part = mail.parts.first + get_part_body(part).should == expected + end + + end + + describe :first_from do + + it 'finds the first from field' do + mail = get_fixture_mail('raw_emails/1.email') + expected = Mail::Address.new('FOI Person <foiperson@localhost>').to_s + first_from(mail).to_s.should == expected + end + + end + + describe :get_from_address do + + it 'finds the first address' do + mail = get_fixture_mail('raw_emails/1.email') + get_from_address(mail).should == 'foiperson@localhost' + end + + end + + describe :get_from_name do + + it 'finds the first from name' do + mail = get_fixture_mail('raw_emails/1.email') + get_from_name(mail).should == 'FOI Person' + end + + end + + describe :get_all_addresses do + + it 'returns all addresses present in an email' do + mail = get_fixture_mail('raw_emails/1.email') + mail.cc = 'bob@example.com' + mail['envelope-to'] = 'bob@example.net' + expected = %w(bob@localhost bob@example.com bob@example.net) + get_all_addresses(mail).should == expected + end + + end + + describe :empty_return_path? do + + it 'is false if the return path is nil' do + mail = Mail.new + empty_return_path?(mail).should be_false + end + + it 'is false if the return path has some data' do + mail = Mail.new + mail['return-path'] = 'xyz' + empty_return_path?(mail).should be_false + end + + it 'is true if the return path is blank' do + mail = Mail.new + mail['return-path'] = '' + empty_return_path?(mail).should be_true + end + + end + + describe :get_auto_submitted do + + it 'returns the auto-submitted attribute' do + mail = Mail.new + mail['auto-submitted'] = 'xyz' + get_auto_submitted(mail).should == 'xyz' + end + + it 'returns nil if there is no auto-submitted attribute' do + mail = Mail.new + get_auto_submitted(mail).should be_nil + end + + end + + describe :expand_and_normalize_parts do + + context 'when given a multipart message' do + + it 'should return a Mail::PartsList' do + mail = get_fixture_mail('incoming-request-oft-attachments.email') + expand_and_normalize_parts(mail, mail).class.should == Mail::PartsList + end + + end + + end + +end diff --git a/spec/lib/mail_handler/mail_handler_spec.rb b/spec/lib/mail_handler/mail_handler_spec.rb index ffc40ced9..e7ad93300 100644 --- a/spec/lib/mail_handler/mail_handler_spec.rb +++ b/spec/lib/mail_handler/mail_handler_spec.rb @@ -346,6 +346,12 @@ describe 'when getting attachment attributes' do attributes[0][:body].is_utf8?.should == true end + it 'should get multiple attachments from a multipart mail with text and HTML alternatives, which should be UTF-8' do + mail = get_fixture_mail('apple-mail-with-attachments.email') + attributes = MailHandler.get_attachment_attributes(mail) + attributes.length.should == 7 + end + it 'should expand a mail attached as text' do # Note that this spec will only pass using Tmail in the timezone set as datetime headers # are rendered out in the local time - using the Mail gem this is not necessary |