aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2014-11-04 15:14:14 +0000
committerGareth Rees <gareth@mysociety.org>2014-11-04 15:14:14 +0000
commit01be1a10f03f060f6e9d30a19ef40c723117d6cb (patch)
tree1ee321352247ee78671d99c9f289474b90db0057
parente35662cd62c781b144d4154568ac7fa0b4c45d36 (diff)
Add some specs to MailBackend
-rw-r--r--spec/lib/mail_handler/backends/mail_backend_spec.rb145
1 files changed, 145 insertions, 0 deletions
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