2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
2013-06-11 03:33:37 +08:00
|
|
|
require 'email/sender'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-11 03:33:37 +08:00
|
|
|
describe Email::Sender do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-08-23 17:07:37 +08:00
|
|
|
it "doesn't deliver mail when mails are disabled" do
|
|
|
|
SiteSetting.expects(:disable_emails).returns(true)
|
2014-10-15 15:04:47 +08:00
|
|
|
Mail::Message.any_instance.expects(:deliver_now).never
|
2014-08-23 17:07:37 +08:00
|
|
|
message = Mail::Message.new(to: "hello@world.com" , body: "hello")
|
|
|
|
Email::Sender.new(message, :hello).send
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
it "doesn't deliver mail when the message is nil" do
|
2014-10-15 15:04:47 +08:00
|
|
|
Mail::Message.any_instance.expects(:deliver_now).never
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.new(nil, :hello).send
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't deliver when the to address is nil" do
|
|
|
|
message = Mail::Message.new(body: 'hello')
|
2014-10-15 15:04:47 +08:00
|
|
|
message.expects(:deliver_now).never
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.new(message, :hello).send
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't deliver when the body is nil" do
|
|
|
|
message = Mail::Message.new(to: 'eviltrout@test.domain')
|
2014-10-15 15:04:47 +08:00
|
|
|
message.expects(:deliver_now).never
|
2013-06-11 03:33:37 +08:00
|
|
|
Email::Sender.new(message, :hello).send
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 23:48:40 +08:00
|
|
|
context "host_for" do
|
|
|
|
it "defaults to localhost" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Email::Sender.host_for(nil)).to eq("localhost")
|
2013-07-03 02:13:46 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 23:48:40 +08:00
|
|
|
it "returns localhost for a weird host" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Email::Sender.host_for("this is not a real host")).to eq("localhost")
|
2013-07-03 02:13:46 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 23:48:40 +08:00
|
|
|
it "parses hosts from urls" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Email::Sender.host_for("http://meta.discourse.org")).to eq("meta.discourse.org")
|
2013-07-03 02:13:46 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 23:48:40 +08:00
|
|
|
it "downcases hosts" do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(Email::Sender.host_for("http://ForumSite.com")).to eq("forumsite.com")
|
2013-07-03 02:13:46 +08:00
|
|
|
end
|
|
|
|
|
2013-07-08 23:48:40 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'with a valid message' do
|
|
|
|
|
2013-06-13 22:56:16 +08:00
|
|
|
let(:reply_key) { "abcd" * 8 }
|
|
|
|
|
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**'
|
2014-10-15 15:04:47 +08:00
|
|
|
message.stubs(:deliver_now)
|
2013-02-06 03:16:51 +08:00
|
|
|
message
|
|
|
|
end
|
|
|
|
|
2013-06-11 03:33:37 +08:00
|
|
|
let(:email_sender) { Email::Sender.new(message, :valid_type) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
it 'calls deliver' do
|
2014-10-15 15:04:47 +08:00
|
|
|
message.expects(:deliver_now).once
|
2013-02-06 03:16:51 +08:00
|
|
|
email_sender.send
|
|
|
|
end
|
|
|
|
|
2014-10-09 02:09:21 +08:00
|
|
|
context "adds a List-ID header to identify the forum" do
|
|
|
|
before do
|
|
|
|
message.header['X-Discourse-Topic-Id'] = 5577
|
|
|
|
end
|
|
|
|
|
|
|
|
When { email_sender.send }
|
|
|
|
Then { expect(message.header['List-ID']).to be_present }
|
|
|
|
end
|
2013-07-03 02:13:46 +08:00
|
|
|
|
2015-01-28 17:12:49 +08:00
|
|
|
context "adds a Message-ID header even when topic id is not present" do
|
|
|
|
When { email_sender.send }
|
|
|
|
Then { expect(message.header['Message-ID']).to be_present }
|
|
|
|
end
|
|
|
|
|
2014-10-09 03:57:30 +08:00
|
|
|
context "adds Precedence header" do
|
|
|
|
before do
|
|
|
|
message.header['X-Discourse-Topic-Id'] = 5577
|
|
|
|
end
|
|
|
|
|
|
|
|
When { email_sender.send }
|
|
|
|
Then { expect(message.header['Precedence']).to be_present }
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context 'email logs' do
|
2013-06-13 22:56:16 +08:00
|
|
|
let(:email_log) { EmailLog.last }
|
|
|
|
|
|
|
|
When { email_sender.send }
|
|
|
|
Then { expect(email_log).to be_present }
|
|
|
|
Then { expect(email_log.email_type).to eq('valid_type') }
|
|
|
|
Then { expect(email_log.to_address).to eq('eviltrout@test.domain') }
|
|
|
|
Then { expect(email_log.reply_key).to be_blank }
|
|
|
|
Then { expect(email_log.user_id).to be_blank }
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
context "email log with a post id and topic id" do
|
|
|
|
before do
|
2013-06-19 03:54:02 +08:00
|
|
|
message.header['X-Discourse-Post-Id'] = 3344
|
|
|
|
message.header['X-Discourse-Topic-Id'] = 5577
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:email_log) { EmailLog.last }
|
|
|
|
When { email_sender.send }
|
|
|
|
Then { expect(email_log.post_id).to eq(3344) }
|
|
|
|
Then { expect(email_log.topic_id).to eq(5577) }
|
2013-07-08 23:48:40 +08:00
|
|
|
Then { expect(message.header['In-Reply-To']).to be_present }
|
|
|
|
Then { expect(message.header['References']).to be_present }
|
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
end
|
|
|
|
|
2013-06-13 22:56:16 +08:00
|
|
|
context "email log with a reply key" do
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
2013-06-19 03:54:02 +08:00
|
|
|
message.header['X-Discourse-Reply-Key'] = reply_key
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-06-13 22:56:16 +08:00
|
|
|
let(:email_log) { EmailLog.last }
|
|
|
|
When { email_sender.send }
|
|
|
|
Then { expect(email_log.reply_key).to eq(reply_key) }
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-06-13 22:56:16 +08:00
|
|
|
context 'email parts' do
|
|
|
|
When { email_sender.send }
|
|
|
|
Then { expect(message).to be_multipart }
|
|
|
|
Then { expect(message.text_part.content_type).to eq('text/plain; charset=UTF-8') }
|
|
|
|
Then { expect(message.html_part.content_type).to eq('text/html; charset=UTF-8') }
|
|
|
|
Then { expect(message.html_part.body.to_s).to match("<p><strong>hello</strong></p>") }
|
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'
|
2014-10-15 15:04:47 +08:00
|
|
|
message.stubs(:deliver_now)
|
2013-02-06 03:16:51 +08:00
|
|
|
message
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
2013-06-11 03:33:37 +08:00
|
|
|
let(:email_sender) { Email::Sender.new(message, :valid_type, user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
email_sender.send
|
|
|
|
@email_log = EmailLog.last
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should have the current user_id' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(@email_log.user_id).to eq(user.id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|