DEV: Refactor test to not call private method.

This commit is contained in:
Guo Xiang Tan 2018-08-21 14:29:58 +08:00
parent 48f499b324
commit 7dcc69aef4
3 changed files with 18 additions and 20 deletions

View File

@ -1,6 +1,7 @@
module Email module Email
class Processor class Processor
attr_reader :receiver
def initialize(mail, retry_on_rate_limit = true) def initialize(mail, retry_on_rate_limit = true)
@mail = mail @mail = mail

View File

@ -2,44 +2,40 @@ require "rails_helper"
require "email/processor" require "email/processor"
describe Email::Processor do describe Email::Processor do
after do
$redis.flushall
end
let(:from) { "foo@bar.com" } let(:from) { "foo@bar.com" }
context "when reply via email is too short" do context "when reply via email is too short" do
let(:mail) { file_from_fixtures("email_reply_3.eml", "emails").read } let(:mail) { file_from_fixtures("chinese_reply.eml", "emails").read }
let(:post) { Fabricate(:post) } let(:post) { Fabricate(:post) }
let!(:user) { Fabricate(:user, email: "three@foo.com") } let(:user) { Fabricate(:user, email: 'discourse@bar.com') }
let!(:email_log) do
Fabricate(:email_log, let!(:post_reply_key) do
message_id: "35@foo.bar.mail", # don't change, based on fixture file "email_reply_3.eml" Fabricate(:post_reply_key,
email_type: "user_posted", user: user,
post_id: post.id, post: post,
to_address: "asdas@dasdfd.com" reply_key: '4f97315cc828096c9cb34c6f1a0d6fe8'
) )
end end
before do before do
SiteSetting.email_in = true
SiteSetting.reply_by_email_address = "reply+%{reply_key}@bar.com"
SiteSetting.min_post_length = 1000 SiteSetting.min_post_length = 1000
SiteSetting.find_related_post_with_key = false
end end
it "rejects reply and sends an email with custom error message" do it "rejects reply and sends an email with custom error message" do
begin
receiver = Email::Receiver.new(mail)
receiver.process!
rescue Email::Receiver::TooShortPost => e
error = e
end
expect(error.class).to eq(Email::Receiver::TooShortPost)
processor = Email::Processor.new(mail) processor = Email::Processor.new(mail)
processor.process! processor.process!
rejection_raw = processor.send(:handle_failure, mail, error).body.raw_source rejection_raw = ActionMailer::Base.deliveries.first.body.raw_source
count = SiteSetting.min_post_length count = SiteSetting.min_post_length
destination = receiver.mail.to destination = processor.receiver.mail.to
former_title = receiver.mail.subject former_title = processor.receiver.mail.subject
expect(rejection_raw.gsub(/\r/, "")).to eq( expect(rejection_raw.gsub(/\r/, "")).to eq(
I18n.t("system_messages.email_reject_post_too_short.text_body_template", I18n.t("system_messages.email_reject_post_too_short.text_body_template",

View File

@ -133,6 +133,7 @@ RSpec.configure do |config|
end end
unfreeze_time unfreeze_time
ActionMailer::Base.deliveries.clear
raise if ActiveRecord::Base.connection_pool.stat[:busy] > 1 raise if ActiveRecord::Base.connection_pool.stat[:busy] > 1
end end