2013-06-13 18:11:10 -04:00
|
|
|
require 'spec_helper'
|
2013-10-01 03:04:02 -04:00
|
|
|
require_dependency 'jobs/regular/process_post'
|
2013-06-13 18:11:10 -04:00
|
|
|
|
|
|
|
describe Jobs::PollMailbox do
|
|
|
|
|
2014-04-09 13:26:19 -04:00
|
|
|
let!(:poller) { Jobs::PollMailbox.new }
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2014-04-09 13:26:19 -04:00
|
|
|
describe ".execute" do
|
|
|
|
|
|
|
|
it "does no polling if pop3s_polling_enabled is false" do
|
|
|
|
SiteSetting.expects(:pop3s_polling_enabled?).returns(false)
|
|
|
|
poller.expects(:poll_pop3s).never
|
|
|
|
|
|
|
|
poller.execute({})
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with pop3s_polling_enabled" do
|
|
|
|
|
|
|
|
it "calls poll_pop3s" do
|
|
|
|
SiteSetting.expects(:pop3s_polling_enabled?).returns(true)
|
|
|
|
poller.expects(:poll_pop3s).once
|
|
|
|
|
|
|
|
poller.execute({})
|
|
|
|
end
|
|
|
|
end
|
2013-06-13 18:11:10 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-04-09 13:26:19 -04:00
|
|
|
describe ".poll_pop3s" do
|
|
|
|
|
2014-06-30 18:16:16 -04:00
|
|
|
it "logs an error on pop authentication error" do
|
2014-04-09 13:26:19 -04:00
|
|
|
error = Net::POPAuthenticationError.new
|
|
|
|
data = { limit_once_per: 1.hour, message_params: { error: error }}
|
|
|
|
|
|
|
|
Net::POP3.expects(:start).raises(error)
|
2014-06-30 18:16:16 -04:00
|
|
|
|
|
|
|
Discourse.expects(:handle_exception)
|
2014-04-09 13:26:19 -04:00
|
|
|
|
|
|
|
poller.poll_pop3s
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-08-01 14:03:03 -04:00
|
|
|
# Testing mock for the email objects that you get
|
|
|
|
# from Net::POP3.start { |pop| pop.mails }
|
|
|
|
class MockPop3EmailObject
|
|
|
|
def initialize(mail_string)
|
|
|
|
@message = mail_string
|
|
|
|
@delete_called = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def pop
|
|
|
|
@message
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
|
|
|
@delete_called += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# call 'assert email.deleted?' at the end of the test
|
|
|
|
def deleted?
|
|
|
|
@delete_called == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-01 14:03:55 -04:00
|
|
|
def expect_success
|
2014-08-01 15:40:28 -04:00
|
|
|
poller.expects(:handle_failure).never
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_exception(clazz)
|
|
|
|
poller.expects(:handle_failure).with(anything, instance_of(clazz))
|
2014-08-01 14:03:55 -04:00
|
|
|
end
|
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
describe "processing emails" do
|
2014-08-01 14:03:03 -04:00
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.email_in = true
|
2014-08-01 15:40:28 -04:00
|
|
|
SiteSetting.reply_by_email_address = "reply+%{reply_key}@appmail.adventuretime.ooo"
|
|
|
|
category.email_in = 'incoming+amazing@appmail.adventuretime.ooo'
|
2014-08-01 14:03:03 -04:00
|
|
|
category.save
|
|
|
|
user.change_trust_level! :regular
|
|
|
|
user.username = 'Jake'
|
2014-08-01 15:40:28 -04:00
|
|
|
user.email = 'jake@adventuretime.ooo'
|
2014-08-01 14:03:03 -04:00
|
|
|
user.save
|
|
|
|
end
|
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
describe "a valid incoming email" do
|
2014-08-01 15:40:28 -04:00
|
|
|
let(:email) {
|
|
|
|
# this string replacing is kinda dumb
|
|
|
|
str = fixture_file('emails/valid_incoming.eml')
|
|
|
|
str = str.gsub("FROM", 'jake@adventuretime.ooo').gsub("TO", 'incoming+amazing@appmail.adventuretime.ooo')
|
|
|
|
MockPop3EmailObject.new str
|
|
|
|
}
|
2014-08-01 14:03:03 -04:00
|
|
|
let(:expected_post) { fixture_file('emails/valid_incoming.cooked') }
|
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
it "posts a new topic with the correct content" do
|
2014-08-01 14:03:55 -04:00
|
|
|
expect_success
|
2014-08-01 14:03:03 -04:00
|
|
|
|
|
|
|
poller.handle_mail(email)
|
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
topic = Topic.where(category: category).where.not(id: category.topic_id).last
|
|
|
|
topic.should be_present
|
|
|
|
topic.title.should == "We should have a post-by-email-feature"
|
|
|
|
|
2014-08-01 14:03:03 -04:00
|
|
|
post = topic.posts.first
|
2014-08-01 15:40:28 -04:00
|
|
|
post.cooked.strip.should == expected_post.strip
|
2014-08-01 14:03:03 -04:00
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
email.should be_deleted
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with insufficient trust" do
|
|
|
|
before do
|
|
|
|
user.change_trust_level! :newuser
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a UserNotSufficientTrustLevelError" do
|
|
|
|
expect_exception Email::Receiver::UserNotSufficientTrustLevelError
|
|
|
|
|
|
|
|
poller.handle_mail(email)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "posts the topic if allow_strangers is true" do
|
|
|
|
begin
|
|
|
|
category.email_in_allow_strangers = true
|
|
|
|
category.save
|
|
|
|
|
|
|
|
expect_success
|
|
|
|
poller.handle_mail(email)
|
|
|
|
topic = Topic.where(category: category).where.not(id: category.topic_id).last
|
|
|
|
topic.should be_present
|
|
|
|
topic.title.should == "We should have a post-by-email-feature"
|
|
|
|
ensure
|
|
|
|
category.email_in_allow_strangers = false
|
|
|
|
category.save
|
|
|
|
end
|
|
|
|
end
|
2014-08-01 14:03:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
describe "a valid reply" do
|
2014-08-01 14:03:03 -04:00
|
|
|
let(:email) { MockPop3EmailObject.new fixture_file('emails/valid_reply.eml')}
|
|
|
|
let(:expected_post) { fixture_file('emails/valid_reply.cooked')}
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:first_post) { Fabricate(:post, topic: topic, post_number: 1)}
|
|
|
|
|
|
|
|
before do
|
|
|
|
first_post.save
|
|
|
|
EmailLog.create(to_address: 'jake@email.example.com',
|
|
|
|
email_type: 'user_posted',
|
|
|
|
reply_key: '59d8df8370b7e95c5a49fbf86aeb2c93',
|
2014-08-01 14:38:44 -04:00
|
|
|
user: user,
|
2014-08-01 14:03:03 -04:00
|
|
|
post: first_post,
|
|
|
|
topic: topic)
|
|
|
|
end
|
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
it "creates a new post" do
|
2014-08-01 14:03:55 -04:00
|
|
|
expect_success
|
2014-08-01 14:03:03 -04:00
|
|
|
|
|
|
|
poller.handle_mail(email)
|
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
new_post = Post.find_by(topic: topic, post_number: 2)
|
2014-08-01 14:03:03 -04:00
|
|
|
assert new_post.present?
|
|
|
|
assert_equal expected_post.strip, new_post.cooked.strip
|
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
email.should be_deleted
|
2014-08-01 14:03:03 -04:00
|
|
|
end
|
|
|
|
|
2014-08-13 14:06:17 -04:00
|
|
|
it "works with multiple To addresses" do
|
|
|
|
email = MockPop3EmailObject.new fixture_file('emails/multiple_destinations.eml')
|
|
|
|
expect_success
|
|
|
|
|
|
|
|
poller.handle_mail(email)
|
|
|
|
|
|
|
|
new_post = Post.find_by(topic: topic, post_number: 2)
|
|
|
|
assert new_post.present?
|
|
|
|
assert_equal expected_post.strip, new_post.cooked.strip
|
|
|
|
|
|
|
|
email.should be_deleted
|
|
|
|
end
|
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
describe "with the wrong reply key" do
|
2014-08-13 14:06:17 -04:00
|
|
|
let(:email) { MockPop3EmailObject.new fixture_file('emails/wrong_reply_key.eml') }
|
2014-08-01 14:38:44 -04:00
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
it "raises an EmailLogNotFound error" do
|
|
|
|
expect_exception Email::Receiver::EmailLogNotFound
|
2014-08-01 14:03:03 -04:00
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
poller.handle_mail(email)
|
|
|
|
email.should be_deleted
|
|
|
|
end
|
2014-08-01 14:38:44 -04:00
|
|
|
end
|
|
|
|
end
|
2014-08-01 14:03:03 -04:00
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
describe "in failure conditions" do
|
2014-04-09 13:26:19 -04:00
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
it "a valid reply without an email log raises an EmailLogNotFound error" do
|
|
|
|
email = MockPop3EmailObject.new fixture_file('emails/valid_reply.eml')
|
|
|
|
expect_exception Email::Receiver::EmailLogNotFound
|
2014-04-09 13:26:19 -04:00
|
|
|
|
|
|
|
poller.handle_mail(email)
|
2014-08-01 15:40:28 -04:00
|
|
|
email.should be_deleted
|
2014-04-09 13:26:19 -04:00
|
|
|
end
|
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
it "a no content reply raises an EmailUnparsableError" do
|
|
|
|
email = MockPop3EmailObject.new fixture_file('emails/no_content_reply.eml')
|
|
|
|
expect_exception Email::Receiver::EmailUnparsableError
|
2014-04-09 13:26:19 -04:00
|
|
|
|
|
|
|
poller.handle_mail(email)
|
2014-08-01 15:40:28 -04:00
|
|
|
email.should be_deleted
|
2014-04-09 13:26:19 -04:00
|
|
|
end
|
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
it "a fully empty email raises an EmptyEmailError" do
|
|
|
|
email = MockPop3EmailObject.new fixture_file('emails/empty.eml')
|
|
|
|
expect_exception Email::Receiver::EmptyEmailError
|
2014-04-09 13:26:19 -04:00
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
poller.handle_mail(email)
|
|
|
|
email.should be_deleted
|
2014-04-09 13:26:19 -04:00
|
|
|
end
|
|
|
|
|
2014-08-01 15:40:28 -04:00
|
|
|
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|