require "spec_helper" describe LetterOpener::DeliveryMethod do let(:location) { File.expand_path('../../../tmp/letter_opener', __FILE__) } let(:plain_file) { Dir["#{location}/*/plain.html"].first } let(:plain) { CGI.unescape_html(File.read(plain_file)) } before do Launchy.stub(:open) FileUtils.rm_rf(location) context = self Mail.defaults do delivery_method LetterOpener::DeliveryMethod, :location => context.location end end it 'raises an exception if no location passed' do lambda { LetterOpener::DeliveryMethod.new }.should raise_exception(LetterOpener::DeliveryMethod::InvalidOption) lambda { LetterOpener::DeliveryMethod.new(location: "foo") }.should_not raise_exception end context 'integration' do before do Launchy.unstub(:open) ENV['LAUNCHY_DRY_RUN'] = 'true' end context 'normal location path' do it 'opens email' do expect { Mail.deliver do from 'Foo foo@example.com' body 'World! http://example.com' end }.not_to raise_error(Launchy::ApplicationNotFoundError) end end context 'with spaces in location path' do let(:location) { File.expand_path('../../../tmp/letter_opener with space', __FILE__) } it 'opens email' do expect { Mail.deliver do from 'Foo foo@example.com' body 'World! http://example.com' end }.not_to raise_error(Launchy::ApplicationNotFoundError) end end end context 'content' do context 'plain' do before do Launchy.should_receive(:open) Mail.deliver do from 'Foo ' sender 'Baz ' reply_to 'No Reply ' to 'Bar ' cc 'Qux ' bcc 'Qux ' subject 'Hello' body 'World! http://example.com' end end it 'creates plain html document' do File.exist?(plain_file).should be_true end it 'saves From field' do plain.should include("Foo ") end it 'saves Sender field' do plain.should include("Baz ") end it 'saves Reply-to field' do plain.should include("No Reply ") end it 'saves To field' do plain.should include("Bar ") end it 'saves Subject field' do plain.should include("Hello") end it 'saves Body with autolink' do plain.should include('World! http://example.com') end end context 'multipart' do let(:rich_file) { Dir["#{location}/*/rich.html"].first } let(:rich) { CGI.unescape_html(File.read(rich_file)) } before do Launchy.should_receive(:open) Mail.deliver do from 'foo@example.com' to 'bar@example.com' subject 'Many parts with ' text_part do body 'This is text' end html_part do content_type 'text/html; charset=UTF-8' body '

This is HTML

' end end end it 'creates plain html document' do File.exist?(plain_file).should be_true end it 'creates rich html document' do File.exist?(rich_file).should be_true end it 'shows link to rich html version' do plain.should include("View HTML version") end it 'saves text part' do plain.should include("This is text") end it 'saves html part' do rich.should include("

This is HTML

") end it 'saves escaped Subject field' do plain.should include("Many parts with ") end it 'shows subject as title' do rich.should include("Many parts with <html>") end end end context 'document with spaces in name' do let(:location) { File.expand_path('../../../tmp/letter_opener with space', __FILE__) } before do Launchy.should_receive(:open) Mail.deliver do from 'Foo ' to 'bar@example.com' subject 'Hello' body 'World!' end end it 'creates plain html document' do File.exist?(plain_file) end it 'saves From filed' do plain.should include("Foo ") end end context 'using deliver! method' do before do Launchy.should_receive(:open) Mail.new do from 'foo@example.com' to 'bar@example.com' subject 'Hello' body 'World!' end.deliver! end it 'creates plain html document' do File.exist?(plain_file).should be_true end it 'saves From field' do plain.should include("foo@example.com") end it 'saves To field' do plain.should include("bar@example.com") end it 'saves Subject field' do plain.should include("Hello") end it 'saves Body field' do plain.should include("World!") end end context 'attachments in plain text mail' do before do Mail.deliver do from 'foo@example.com' to 'bar@example.com' subject 'With attachments' text_part do body 'This is text' end attachments[File.basename(__FILE__)] = File.read(__FILE__) end end it 'creates attachments dir with attachment' do attachment = Dir["#{location}/*/attachments/#{File.basename(__FILE__)}"].first File.exists?(attachment).should be_true end it 'saves attachment name' do plain = File.read(Dir["#{location}/*/plain.html"].first) plain.should include(File.basename(__FILE__)) end end context 'attachments in rich mail' do let(:url) { mail.attachments[0].url } let!(:mail) do Mail.deliver do from 'foo@example.com' to 'bar@example.com' subject 'With attachments' attachments[File.basename(__FILE__)] = File.read(__FILE__) url = attachments[0].url html_part do content_type 'text/html; charset=UTF-8' body "Here's an image: " end end end it 'creates attachments dir with attachment' do attachment = Dir["#{location}/*/attachments/#{File.basename(__FILE__)}"].first File.exists?(attachment).should be_true end it 'replaces inline attachment urls' do text = File.read(Dir["#{location}/*/rich.html"].first) mail.parts[0].body.should include(url) text.should_not include(url) text.should include("attachments/#{File.basename(__FILE__)}") end end context 'attachments with non-word characters in the filename' do before do Mail.deliver do from 'foo@example.com' to 'bar@example.com' subject 'With attachments' text_part do body 'This is text' end attachments['non word:chars/used,01.txt'] = File.read(__FILE__) end end it 'creates attachments dir with attachment' do attachment = Dir["#{location}/*/attachments/non_word_chars_used_01.txt"].first File.exists?(attachment).should be_true end it 'saves attachment name' do plain = File.read(Dir["#{location}/*/plain.html"].first) plain.should include('non_word_chars_used_01.txt') end end context 'subjectless mail' do before do Launchy.should_receive(:open) Mail.deliver do from 'Foo foo@example.com' reply_to 'No Reply no-reply@example.com' to 'Bar bar@example.com' body 'World! http://example.com' end end it 'creates plain html document' do File.exist?(plain_file).should be_true end end end