discourse/spec/components/email_sender_spec.rb

111 lines
2.6 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
require 'spec_helper'
require 'email_sender'
describe EmailSender do
it "doesn't deliver mail when the message is nil" do
Mail::Message.any_instance.expects(:deliver).never
EmailSender.new(nil, :hello).send
end
it "doesn't deliver when the to address is nil" do
message = Mail::Message.new(body: 'hello')
message.expects(:deliver).never
EmailSender.new(message, :hello).send
end
it "doesn't deliver when the body is nil" do
message = Mail::Message.new(to: 'eviltrout@test.domain')
message.expects(:deliver).never
EmailSender.new(message, :hello).send
end
context 'with a valid message' do
2013-02-26 00:42:20 +08:00
let(:message) do
2013-02-06 03:16:51 +08:00
message = Mail::Message.new to: 'eviltrout@test.domain',
body: '**hello**'
message.stubs(:deliver)
message
end
let(:email_sender) { EmailSender.new(message, :valid_type) }
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
context 'email parts' do
before { email_sender.send }
2013-02-06 03:16:51 +08:00
2013-02-26 00:42:20 +08:00
it 'makes the message multipart' do
2013-02-06 03:16:51 +08:00
message.should be_multipart
end
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-06 03:16:51 +08:00
end
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-06 03:16:51 +08:00
end
it 'converts the html part to html' do
expect(message.html_part.body.to_s).to eq(
"<p><strong>hello</strong></p>"
)
end
2013-02-06 03:16:51 +08:00
end
end
context 'with a user' do
2013-02-26 00:42:20 +08:00
let(:message) do
2013-02-06 03:16:51 +08:00
message = Mail::Message.new to: 'eviltrout@test.domain', body: 'test body'
message.stubs(:deliver)
message
end
let(:user) { Fabricate(:user) }
let(:email_sender) { EmailSender.new(message, :valid_type, user) }
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