diff options
author | Louise Crow <louise.crow@gmail.com> | 2015-05-28 17:26:41 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2015-06-04 12:18:47 +0100 |
commit | 772e05925b1a4f6e3769254c33b2bf9114651bc6 (patch) | |
tree | 8a11831a1f3dd5ac95f6a4abae0c20421379b63b | |
parent | a048c2d1af31006363712f0c348e3bee5ca45ac6 (diff) |
Address#to_s changes the input passed to it - dup before calling.
Otherwise UTF-8 encoded strings will be returned as ASCII-8BIT.
-rw-r--r-- | lib/mail_handler/backends/mail_backend.rb | 6 | ||||
-rw-r--r-- | spec/lib/mail_handler/backends/mail_backend_spec.rb | 162 |
2 files changed, 165 insertions, 3 deletions
diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index 190e79e97..c8066f1fa 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -358,11 +358,11 @@ module MailHandler raise "invalid email " + email + " passed to address_from_name_and_email" end if name.nil? - return Mail::Address.new(email).to_s + return Mail::Address.new(email.dup).to_s end address = Mail::Address.new - address.display_name = name - address.address = email + address.display_name = name.dup + address.address = email.dup address.to_s 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..eb1d4b167 --- /dev/null +++ b/spec/lib/mail_handler/backends/mail_backend_spec.rb @@ -0,0 +1,162 @@ +# 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 + + describe :address_from_name_and_email do + + it 'returns an address string' do + expected = 'Test User <test@example.com>' + address_from_name_and_email('Test User', 'test@example.com').should == expected + end + + it 'does not change the name passed to it' do + original = "brønn" + name = original.dup + address_from_name_and_email(name, 'test@example.com') + name.should == original + end + + end + + +end |