REFACTOR: poll mailbox specs

This commit is contained in:
Régis Hanol 2014-04-09 19:26:19 +02:00
parent ce40a27274
commit c3a1c867dd
3 changed files with 114 additions and 126 deletions

View File

@ -24,16 +24,15 @@ module Jobs
Email::Receiver.new(mail_string).process
rescue Email::Receiver::UserNotSufficientTrustLevelError
# inform the user about the rejection
@message = Mail::Message.new(mail_string)
clientMessage = RejectionMailer.send_trust_level(@message.from, @message.body)
email_sender = Email::Sender.new(clientMessage, :email_reject_trust_level)
email_sender.send
message = Mail::Message.new(mail_string)
client_message = RejectionMailer.send_trust_level(message.from, message.body)
Email::Sender.new(client_message, :email_reject_trust_level).send
rescue Email::Receiver::ProcessingError
# all other ProcessingErrors are ok to be dropped
rescue StandardError => e
# Inform Admins about error
GroupMessage.create(Group[:admins].name, :email_error_notification,
{limit_once_per: false, message_params: {source: mail, error: e}})
# inform admins about the error
data = { limit_once_per: false, message_params: { source: mail, error: e }}
GroupMessage.create(Group[:admins].name, :email_error_notification, data)
ensure
mail.delete
end
@ -51,6 +50,10 @@ module Jobs
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

View File

@ -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

View File

@ -3,21 +3,115 @@ require_dependency 'jobs/regular/process_post'
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
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
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
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
it "calls poll_pop3s" do
SiteSetting.expects(:pop3s_polling_enabled?).returns(true)
poller.expects(:poll_pop3s).once
poller.execute({})
end
end