2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
2013-06-10 15:33:37 -04:00
|
|
|
require 'email/sender'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-10 15:33:37 -04:00
|
|
|
describe Email::Sender do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it "doesn't deliver mail when the message is nil" do
|
|
|
|
Mail::Message.any_instance.expects(:deliver).never
|
2013-06-10 15:33:37 -04:00
|
|
|
Email::Sender.new(nil, :hello).send
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't deliver when the to address is nil" do
|
|
|
|
message = Mail::Message.new(body: 'hello')
|
|
|
|
message.expects(:deliver).never
|
2013-06-10 15:33:37 -04:00
|
|
|
Email::Sender.new(message, :hello).send
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't deliver when the body is nil" do
|
|
|
|
message = Mail::Message.new(to: 'eviltrout@test.domain')
|
|
|
|
message.expects(:deliver).never
|
2013-06-10 15:33:37 -04:00
|
|
|
Email::Sender.new(message, :hello).send
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a valid message' do
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
let(:message) do
|
2013-02-05 14:16:51 -05:00
|
|
|
message = Mail::Message.new to: 'eviltrout@test.domain',
|
|
|
|
body: '**hello**'
|
|
|
|
message.stubs(:deliver)
|
|
|
|
message
|
|
|
|
end
|
|
|
|
|
2013-06-10 15:33:37 -04:00
|
|
|
let(:email_sender) { Email::Sender.new(message, :valid_type) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'calls deliver' do
|
|
|
|
message.expects(:deliver).once
|
|
|
|
email_sender.send
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'email logs' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
email_sender.send
|
|
|
|
@email_log = EmailLog.last
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates an email log' do
|
|
|
|
@email_log.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the correct type' do
|
|
|
|
@email_log.email_type.should == 'valid_type'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has the correct to_address' do
|
|
|
|
@email_log.to_address.should == 'eviltrout@test.domain'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has no user_id' do
|
|
|
|
@email_log.user_id.should be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-29 12:37:31 -04:00
|
|
|
context 'email parts' do
|
|
|
|
before { email_sender.send }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'makes the message multipart' do
|
2013-02-05 14:16:51 -05:00
|
|
|
message.should be_multipart
|
|
|
|
end
|
|
|
|
|
2013-05-29 12:37:31 -04:00
|
|
|
it 'sets the correct content type for the plain text part' do
|
|
|
|
expect(message.text_part.content_type).to eq 'text/plain; charset=UTF-8'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-29 12:37:31 -04:00
|
|
|
it 'sets the correct content type for the html part' do
|
|
|
|
expect(message.html_part.content_type).to eq 'text/html; charset=UTF-8'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-29 12:37:31 -04:00
|
|
|
it 'converts the html part to html' do
|
2013-06-03 16:12:24 -04:00
|
|
|
expect(message.html_part.body.to_s).to match("<p><strong>hello</strong></p>")
|
2013-05-29 12:37:31 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a user' do
|
2013-02-25 11:42:20 -05:00
|
|
|
let(:message) do
|
2013-02-05 14:16:51 -05:00
|
|
|
message = Mail::Message.new to: 'eviltrout@test.domain', body: 'test body'
|
|
|
|
message.stubs(:deliver)
|
|
|
|
message
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
2013-06-10 15:33:37 -04:00
|
|
|
let(:email_sender) { Email::Sender.new(message, :valid_type, user) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
email_sender.send
|
|
|
|
@email_log = EmailLog.last
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should have the current user_id' do
|
|
|
|
@email_log.user_id.should == user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|