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
|
|
|
|
Jobs::PollMailbox.expects(:handle_failure).never
|
|
|
|
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
|
|
|
|
SiteSetting.reply_by_email_address = 'reply+%{reply_key}@discourse.example.com'
|
|
|
|
category.email_in = 'incoming+amazing@discourse.example.com'
|
|
|
|
category.save
|
|
|
|
user.change_trust_level! :regular
|
|
|
|
user.username = 'Jake'
|
|
|
|
user.email = 'jake@email.example.com'
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
describe "a valid incoming email" do
|
2014-08-01 14:03:03 -04:00
|
|
|
let(:email) { MockPop3EmailObject.new fixture_file('emails/valid_incoming.eml')}
|
|
|
|
let(:expected_post) { fixture_file('emails/valid_incoming.cooked') }
|
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
it "posts a new topic" do
|
2014-08-01 14:03:55 -04:00
|
|
|
expect_success
|
2014-08-01 14:03:03 -04:00
|
|
|
|
|
|
|
poller.handle_mail(email)
|
|
|
|
|
|
|
|
topic = Topic.where(category: category).where.not(id: category.topic_id).first
|
|
|
|
assert topic.present?
|
|
|
|
post = topic.posts.first
|
|
|
|
assert_equal expected_post.strip, post.cooked.strip
|
|
|
|
|
|
|
|
assert email.deleted?
|
|
|
|
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
|
|
|
|
|
|
|
|
assert email.deleted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
describe "without an email log" do
|
|
|
|
let(:email) { MockPop3EmailObject.new fixture_file('emails/valid_reply.eml')}
|
|
|
|
|
|
|
|
it "handles an EmailLogNotFound error" do
|
|
|
|
poller.expects(:handle_failure).with { |mail_string, ex| ex.is_a? Email::Receiver::EmailLogNotFound }
|
2014-08-01 14:03:03 -04:00
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
poller.handle_mail(email)
|
|
|
|
assert email.deleted?
|
|
|
|
end
|
|
|
|
end
|
2014-08-01 14:03:03 -04:00
|
|
|
end
|
|
|
|
|
2014-08-01 14:38:44 -04:00
|
|
|
describe "processing email A" do
|
2014-04-09 13:26:19 -04:00
|
|
|
|
|
|
|
let!(:receiver) { mock }
|
2014-07-17 13:04:02 -04:00
|
|
|
let!(:email_string) { fixture_file("emails/valid_incoming.eml") }
|
2014-04-09 13:26:19 -04:00
|
|
|
let!(:email) { mock }
|
|
|
|
|
|
|
|
before do
|
|
|
|
email.stubs(:pop).returns(email_string)
|
|
|
|
Email::Receiver.expects(:new).with(email_string).returns(receiver)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "all goes fine" do
|
|
|
|
|
|
|
|
it "email gets deleted" do
|
|
|
|
receiver.expects(:process)
|
|
|
|
email.expects(:delete)
|
|
|
|
|
|
|
|
poller.handle_mail(email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "raises Untrusted error" do
|
|
|
|
|
|
|
|
it "sends a reply and deletes the email" do
|
|
|
|
receiver.expects(:process).raises(Email::Receiver::UserNotSufficientTrustLevelError)
|
|
|
|
email.expects(:delete)
|
|
|
|
|
2014-06-27 15:03:07 -04:00
|
|
|
message = Mail::Message.new(email_string)
|
|
|
|
Mail::Message.expects(:new).with(email_string).returns(message)
|
2014-04-09 13:26:19 -04:00
|
|
|
|
|
|
|
client_message = mock
|
2014-06-27 15:03:07 -04:00
|
|
|
sender_object = mock
|
2014-04-09 13:26:19 -04:00
|
|
|
|
2014-06-27 15:03:07 -04:00
|
|
|
RejectionMailer.expects(:send_rejection).with(
|
2014-07-17 13:04:02 -04:00
|
|
|
message.from, message.body, message.subject, message.to, :email_reject_trust_level
|
2014-06-27 15:03:07 -04:00
|
|
|
).returns(client_message)
|
|
|
|
Email::Sender.expects(:new).with(client_message, :email_reject_trust_level).returns(sender_object)
|
|
|
|
sender_object.expects(:send)
|
2014-04-09 13:26:19 -04:00
|
|
|
|
|
|
|
poller.handle_mail(email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "raises error" do
|
|
|
|
|
|
|
|
[ Email::Receiver::ProcessingError,
|
|
|
|
Email::Receiver::EmailUnparsableError,
|
|
|
|
Email::Receiver::EmptyEmailError,
|
|
|
|
Email::Receiver::UserNotFoundError,
|
|
|
|
Email::Receiver::EmailLogNotFound,
|
2014-06-23 21:11:50 -04:00
|
|
|
ActiveRecord::Rollback,
|
|
|
|
TypeError
|
2014-04-09 13:26:19 -04:00
|
|
|
].each do |exception|
|
|
|
|
|
|
|
|
it "deletes email on #{exception}" do
|
|
|
|
receiver.expects(:process).raises(exception)
|
|
|
|
email.expects(:delete)
|
|
|
|
|
2014-06-27 15:03:07 -04:00
|
|
|
Discourse.stubs(:handle_exception)
|
|
|
|
|
2014-04-09 13:26:19 -04:00
|
|
|
poller.handle_mail(email)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|