REFACTOR: poll mailbox specs
This commit is contained in:
parent
ce40a27274
commit
c3a1c867dd
|
@ -24,16 +24,15 @@ module Jobs
|
||||||
Email::Receiver.new(mail_string).process
|
Email::Receiver.new(mail_string).process
|
||||||
rescue Email::Receiver::UserNotSufficientTrustLevelError
|
rescue Email::Receiver::UserNotSufficientTrustLevelError
|
||||||
# inform the user about the rejection
|
# inform the user about the rejection
|
||||||
@message = Mail::Message.new(mail_string)
|
message = Mail::Message.new(mail_string)
|
||||||
clientMessage = RejectionMailer.send_trust_level(@message.from, @message.body)
|
client_message = RejectionMailer.send_trust_level(message.from, message.body)
|
||||||
email_sender = Email::Sender.new(clientMessage, :email_reject_trust_level)
|
Email::Sender.new(client_message, :email_reject_trust_level).send
|
||||||
email_sender.send
|
|
||||||
rescue Email::Receiver::ProcessingError
|
rescue Email::Receiver::ProcessingError
|
||||||
# all other ProcessingErrors are ok to be dropped
|
# all other ProcessingErrors are ok to be dropped
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
# Inform Admins about error
|
# inform admins about the error
|
||||||
GroupMessage.create(Group[:admins].name, :email_error_notification,
|
data = { limit_once_per: false, message_params: { source: mail, error: e }}
|
||||||
{limit_once_per: false, message_params: {source: mail, error: e}})
|
GroupMessage.create(Group[:admins].name, :email_error_notification, data)
|
||||||
ensure
|
ensure
|
||||||
mail.delete
|
mail.delete
|
||||||
end
|
end
|
||||||
|
@ -51,6 +50,10 @@ module Jobs
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
rescue Net::POPAuthenticationError => e
|
||||||
|
# inform admins about the error (1 message per hour to prevent too much SPAM)
|
||||||
|
data = { limit_once_per: 1.hour, message_params: { error: e }}
|
||||||
|
GroupMessage.create(Group[:admins].name, :email_error_notification, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,109 +0,0 @@
|
||||||
# -*- encoding : utf-8 -*-
|
|
||||||
|
|
||||||
require 'spec_helper'
|
|
||||||
require 'email/receiver'
|
|
||||||
require 'jobs/scheduled/poll_mailbox'
|
|
||||||
require 'email/message_builder'
|
|
||||||
|
|
||||||
describe Jobs::PollMailbox do
|
|
||||||
|
|
||||||
describe "processing email" do
|
|
||||||
|
|
||||||
let!(:poller) { Jobs::PollMailbox.new }
|
|
||||||
let!(:receiver) { mock }
|
|
||||||
let!(:email_string) { "EMAIL AS A STRING" }
|
|
||||||
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
|
|
||||||
|
|
||||||
before do
|
|
||||||
receiver.expects(:process).raises(Email::Receiver::UserNotSufficientTrustLevelError)
|
|
||||||
email.expects(:delete)
|
|
||||||
|
|
||||||
Mail::Message.expects(:new).with(email_string).returns(email)
|
|
||||||
|
|
||||||
email.expects(:from)
|
|
||||||
email.expects(:body)
|
|
||||||
|
|
||||||
clientMessage = mock
|
|
||||||
senderMock = mock
|
|
||||||
RejectionMailer.expects(:send_trust_level).returns(clientMessage)
|
|
||||||
Email::Sender.expects(:new).with(
|
|
||||||
clientMessage, :email_reject_trust_level).returns(senderMock)
|
|
||||||
senderMock.expects(:send)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "sends a reply and deletes the email" do
|
|
||||||
poller.handle_mail(email)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "raises error" do
|
|
||||||
|
|
||||||
it "deletes email on ProcessingError" do
|
|
||||||
receiver.expects(:process).raises(Email::Receiver::ProcessingError)
|
|
||||||
email.expects(:delete)
|
|
||||||
|
|
||||||
poller.handle_mail(email)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "deletes email on EmailUnparsableError" do
|
|
||||||
receiver.expects(:process).raises(Email::Receiver::EmailUnparsableError)
|
|
||||||
email.expects(:delete)
|
|
||||||
|
|
||||||
poller.handle_mail(email)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "deletes email on EmptyEmailError" do
|
|
||||||
receiver.expects(:process).raises(Email::Receiver::EmptyEmailError)
|
|
||||||
email.expects(:delete)
|
|
||||||
|
|
||||||
poller.handle_mail(email)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "deletes email on UserNotFoundError" do
|
|
||||||
receiver.expects(:process).raises(Email::Receiver::UserNotFoundError)
|
|
||||||
email.expects(:delete)
|
|
||||||
|
|
||||||
poller.handle_mail(email)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "deletes email on EmailLogNotFound" do
|
|
||||||
receiver.expects(:process).raises(Email::Receiver::EmailLogNotFound)
|
|
||||||
email.expects(:delete)
|
|
||||||
|
|
||||||
poller.handle_mail(email)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
it "informs admins on any other error" do
|
|
||||||
receiver.expects(:process).raises(TypeError)
|
|
||||||
email.expects(:delete)
|
|
||||||
GroupMessage.expects(:create) do |args|
|
|
||||||
args[0].should eq "admins"
|
|
||||||
args[1].shouled eq :email_error_notification
|
|
||||||
args[2].message_params.source.should eq email
|
|
||||||
args[2].message_params.error.should_be instance_of(TypeError)
|
|
||||||
end
|
|
||||||
|
|
||||||
poller.handle_mail(email)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
|
@ -3,12 +3,14 @@ require_dependency 'jobs/regular/process_post'
|
||||||
|
|
||||||
describe Jobs::PollMailbox do
|
describe Jobs::PollMailbox do
|
||||||
|
|
||||||
|
let!(:poller) { Jobs::PollMailbox.new }
|
||||||
|
|
||||||
let(:poller) { Jobs::PollMailbox.new }
|
describe ".execute" do
|
||||||
|
|
||||||
it "does no polling if pop3s_polling_enabled is false" do
|
it "does no polling if pop3s_polling_enabled is false" do
|
||||||
SiteSetting.expects(:pop3s_polling_enabled?).returns(false)
|
SiteSetting.expects(:pop3s_polling_enabled?).returns(false)
|
||||||
poller.expects(:poll_pop3s).never
|
poller.expects(:poll_pop3s).never
|
||||||
|
|
||||||
poller.execute({})
|
poller.execute({})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -17,8 +19,100 @@ describe Jobs::PollMailbox do
|
||||||
it "calls poll_pop3s" do
|
it "calls poll_pop3s" do
|
||||||
SiteSetting.expects(:pop3s_polling_enabled?).returns(true)
|
SiteSetting.expects(:pop3s_polling_enabled?).returns(true)
|
||||||
poller.expects(:poll_pop3s).once
|
poller.expects(:poll_pop3s).once
|
||||||
|
|
||||||
poller.execute({})
|
poller.execute({})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".poll_pop3s" do
|
||||||
|
|
||||||
|
it "informs admins on pop authentication error" do
|
||||||
|
error = Net::POPAuthenticationError.new
|
||||||
|
data = { limit_once_per: 1.hour, message_params: { error: error }}
|
||||||
|
|
||||||
|
Net::POP3.expects(:start).raises(error)
|
||||||
|
GroupMessage.expects(:create).with("admins", :email_error_notification, data)
|
||||||
|
|
||||||
|
poller.poll_pop3s
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "processing email" do
|
||||||
|
|
||||||
|
let!(:receiver) { mock }
|
||||||
|
let!(:email_string) { "EMAIL AS A STRING" }
|
||||||
|
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)
|
||||||
|
|
||||||
|
Mail::Message.expects(:new).with(email_string).returns(email)
|
||||||
|
|
||||||
|
email.expects(:from)
|
||||||
|
email.expects(:body)
|
||||||
|
|
||||||
|
client_message = mock
|
||||||
|
sender = mock
|
||||||
|
|
||||||
|
RejectionMailer.expects(:send_trust_level).returns(client_message)
|
||||||
|
Email::Sender.expects(:new).with(client_message, :email_reject_trust_level).returns(sender)
|
||||||
|
sender.expects(:send)
|
||||||
|
|
||||||
|
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,
|
||||||
|
].each do |exception|
|
||||||
|
|
||||||
|
it "deletes email on #{exception}" do
|
||||||
|
receiver.expects(:process).raises(exception)
|
||||||
|
email.expects(:delete)
|
||||||
|
|
||||||
|
poller.handle_mail(email)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
it "informs admins on any other error" do
|
||||||
|
error = TypeError.new
|
||||||
|
data = { limit_once_per: false, message_params: { source: email, error: error }}
|
||||||
|
|
||||||
|
receiver.expects(:process).raises(error)
|
||||||
|
email.expects(:delete)
|
||||||
|
GroupMessage.expects(:create).with("admins", :email_error_notification, data)
|
||||||
|
|
||||||
|
poller.handle_mail(email)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue