require 'spec_helper' describe Postmark::MailMessageConverter do subject { Postmark::MailMessageConverter } let(:mail_message) do Mail.new do from "sheldon@bigbangtheory.com" to "lenard@bigbangtheory.com" subject "Hello!" body "Hello Sheldon!" end end let(:mail_html_message) do mail = Mail.new do from "sheldon@bigbangtheory.com" to "lenard@bigbangtheory.com" subject "Hello!" content_type 'text/html; charset=UTF-8' body "Hello Sheldon!" end end let(:tagged_mail_message) do Mail.new do from "sheldon@bigbangtheory.com" to "lenard@bigbangtheory.com" subject "Hello!" body "Hello Sheldon!" tag "sheldon" end end let(:mail_message_without_body) do Mail.new do from "sheldon@bigbangtheory.com" to "lenard@bigbangtheory.com" subject "Hello!" end end let(:mail_multipart_message) do mail = Mail.new do from "sheldon@bigbangtheory.com" to "lenard@bigbangtheory.com" subject "Hello!" text_part do body "Hello Sheldon!" end html_part do body "Hello Sheldon!" end end end let(:mail_message_with_attachment) do Mail.new do from "sheldon@bigbangtheory.com" to "lenard@bigbangtheory.com" subject "Hello!" body "Hello Sheldon!" add_file empty_gif_path end end let(:mail_message_with_named_addresses) do Mail.new do from "Sheldon " to "\"Leonard Hofstadter\" " subject "Hello!" body "Hello Sheldon!" reply_to 'Penny "The Neighbor" ' end end it 'converts plain text messages correctly' do subject.new(mail_message).run.should == { "From" => "sheldon@bigbangtheory.com", "Subject" => "Hello!", "TextBody" => "Hello Sheldon!", "To" => "lenard@bigbangtheory.com"} end it 'converts tagged text messages correctly' do subject.new(tagged_mail_message).run.should == { "From" => "sheldon@bigbangtheory.com", "Subject" => "Hello!", "TextBody" => "Hello Sheldon!", "Tag" => "sheldon", "To"=>"lenard@bigbangtheory.com"} end it 'converts plain text messages without body correctly' do subject.new(mail_message_without_body).run.should == { "From" => "sheldon@bigbangtheory.com", "Subject" => "Hello!", "To" => "lenard@bigbangtheory.com"} end it 'converts html messages correctly' do subject.new(mail_html_message).run.should == { "From" => "sheldon@bigbangtheory.com", "Subject" => "Hello!", "HtmlBody" => "Hello Sheldon!", "To" => "lenard@bigbangtheory.com"} end it 'converts multipart messages correctly' do subject.new(mail_multipart_message).run.should == { "From" => "sheldon@bigbangtheory.com", "Subject" => "Hello!", "HtmlBody" => "Hello Sheldon!", "TextBody" => "Hello Sheldon!", "To" => "lenard@bigbangtheory.com"} end it 'converts messages with attachments correctly' do subject.new(mail_message_with_attachment).run.should == { "From" => "sheldon@bigbangtheory.com", "Subject" => "Hello!", "Attachments" => [{"Name"=>"empty.gif", "Content"=>encoded_empty_gif_data, "ContentType"=>"image/gif"}], "TextBody"=>"Hello Sheldon!", "To"=>"lenard@bigbangtheory.com"} end it 'converts messages with named addresses correctly' do subject.new(mail_message_with_named_addresses).run.should == { "From" => "Sheldon ", "Subject" => "Hello!", "TextBody" => "Hello Sheldon!", "To" => "Leonard Hofstadter ", "ReplyTo" => "\"Penny \\\"The Neighbor\\\"\" " } end context 'when bcc is empty' do it 'excludes bcc from message' do mail_message.bcc = nil mail_message.to_postmark_hash.keys.should_not include('Bcc') end end context 'when cc is empty' do it 'excludes cc from message' do mail_message.cc = nil mail_message.to_postmark_hash.keys.should_not include('Cc') end end end