2013-11-25 11:29:37 -05:00
|
|
|
# -*- encoding : utf-8 -*-
|
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'email/receiver'
|
|
|
|
|
|
|
|
describe Email::Receiver do
|
|
|
|
|
2013-06-13 18:11:10 -04:00
|
|
|
before do
|
2014-07-31 04:46:02 -04:00
|
|
|
SiteSetting.reply_by_email_address = "reply+%{reply_key}@appmail.adventuretime.ooo"
|
|
|
|
SiteSetting.email_in = false
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
2013-06-10 16:46:08 -04:00
|
|
|
|
2013-06-13 18:11:10 -04:00
|
|
|
describe 'invalid emails' do
|
2014-02-28 07:05:09 -05:00
|
|
|
it "raises EmptyEmailError if the message is blank" do
|
|
|
|
expect { Email::Receiver.new("").process }.to raise_error(Email::Receiver::EmptyEmailError)
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
2013-06-10 16:46:08 -04:00
|
|
|
|
2014-07-11 15:26:40 -04:00
|
|
|
it "raises EmptyEmailError if the message is not an email" do
|
2014-02-28 07:05:09 -05:00
|
|
|
expect { Email::Receiver.new("asdf" * 30).process}.to raise_error(Email::Receiver::EmptyEmailError)
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
2014-07-11 15:26:40 -04:00
|
|
|
|
2014-07-17 12:43:13 -04:00
|
|
|
it "raises EmailUnparsableError if there is no reply content" do
|
|
|
|
expect { Email::Receiver.new(fixture_file("emails/no_content_reply.eml")).process}.to raise_error(Email::Receiver::EmailUnparsableError)
|
2014-07-11 15:26:40 -04:00
|
|
|
end
|
2013-06-13 18:11:10 -04:00
|
|
|
end
|
|
|
|
|
2013-06-20 12:38:03 -04:00
|
|
|
describe "with multipart" do
|
2014-07-11 15:26:40 -04:00
|
|
|
let(:reply_below) { fixture_file("emails/multipart.eml") }
|
2013-06-20 12:38:03 -04:00
|
|
|
let(:receiver) { Email::Receiver.new(reply_below) }
|
|
|
|
|
2013-07-24 14:22:32 -04:00
|
|
|
it "processes correctly" do
|
2014-07-11 15:26:40 -04:00
|
|
|
expect { receiver.process}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2013-06-20 12:38:03 -04:00
|
|
|
expect(receiver.body).to eq(
|
|
|
|
"So presumably all the quoted garbage and my (proper) signature will get
|
|
|
|
stripped from my reply?")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "html only" do
|
2014-07-11 15:26:40 -04:00
|
|
|
let(:reply_below) { fixture_file("emails/html_only.eml") }
|
2013-06-20 12:38:03 -04:00
|
|
|
let(:receiver) { Email::Receiver.new(reply_below) }
|
|
|
|
|
2013-07-24 14:22:32 -04:00
|
|
|
it "processes correctly" do
|
2014-07-11 15:26:40 -04:00
|
|
|
expect { receiver.process}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2013-06-20 12:38:03 -04:00
|
|
|
expect(receiver.body).to eq("The EC2 instance - I've seen that there tends to be odd and " +
|
|
|
|
"unrecommended settings on the Bitnami installs that I've checked out.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-24 14:22:32 -04:00
|
|
|
describe "it supports a dutch reply" do
|
2014-07-11 15:26:40 -04:00
|
|
|
let(:dutch) { fixture_file("emails/dutch.eml") }
|
2013-07-24 14:22:32 -04:00
|
|
|
let(:receiver) { Email::Receiver.new(dutch) }
|
|
|
|
|
|
|
|
it "processes correctly" do
|
2014-07-11 15:26:40 -04:00
|
|
|
expect { receiver.process}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2013-07-24 14:22:32 -04:00
|
|
|
expect(receiver.body).to eq("Dit is een antwoord in het Nederlands.")
|
|
|
|
end
|
2013-08-21 16:54:01 -04:00
|
|
|
end
|
|
|
|
|
2013-11-04 15:30:24 -05:00
|
|
|
describe "It supports a non english reply" do
|
2014-07-11 15:26:40 -04:00
|
|
|
let(:hebrew) { fixture_file("emails/hebrew.eml") }
|
2013-11-04 15:30:24 -05:00
|
|
|
let(:receiver) { Email::Receiver.new(hebrew) }
|
|
|
|
|
|
|
|
it "processes correctly" do
|
|
|
|
I18n.expects(:t).with('user_notifications.previous_discussion').returns('כלטוב')
|
2014-07-11 15:26:40 -04:00
|
|
|
expect { receiver.process}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2013-11-04 15:30:24 -05:00
|
|
|
expect(receiver.body).to eq("שלום")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-16 21:24:32 -05:00
|
|
|
describe "It supports a non UTF-8 reply" do
|
2014-07-11 15:26:40 -04:00
|
|
|
let(:big5) { fixture_file("emails/big5.eml") }
|
2014-01-16 21:24:32 -05:00
|
|
|
let(:receiver) { Email::Receiver.new(big5) }
|
|
|
|
|
|
|
|
it "processes correctly" do
|
|
|
|
I18n.expects(:t).with('user_notifications.previous_discussion').returns('媽!我上電視了!')
|
2014-07-11 15:26:40 -04:00
|
|
|
expect { receiver.process}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2014-01-16 21:24:32 -05:00
|
|
|
expect(receiver.body).to eq("媽!我上電視了!")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-21 16:54:01 -04:00
|
|
|
describe "via" do
|
2014-07-11 15:26:40 -04:00
|
|
|
let(:wrote) { fixture_file("emails/via_line.eml") }
|
2013-08-21 16:54:01 -04:00
|
|
|
let(:receiver) { Email::Receiver.new(wrote) }
|
|
|
|
|
|
|
|
it "removes via lines if we know them" do
|
2014-07-11 15:26:40 -04:00
|
|
|
expect { receiver.process}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2013-08-21 16:54:01 -04:00
|
|
|
expect(receiver.body).to eq("Hello this email has content!")
|
|
|
|
end
|
2013-07-24 14:22:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "if wrote is on a second line" do
|
2014-07-11 15:26:40 -04:00
|
|
|
let(:wrote) { fixture_file("emails/multiline_wrote.eml") }
|
2013-07-24 14:22:32 -04:00
|
|
|
let(:receiver) { Email::Receiver.new(wrote) }
|
|
|
|
|
|
|
|
it "processes correctly" do
|
2014-07-11 15:26:40 -04:00
|
|
|
expect { receiver.process}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2013-07-24 14:22:32 -04:00
|
|
|
expect(receiver.body).to eq("Thanks!")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "remove previous discussion" do
|
2014-07-11 15:26:40 -04:00
|
|
|
let(:previous) { fixture_file("emails/previous.eml") }
|
2013-07-24 14:22:32 -04:00
|
|
|
let(:receiver) { Email::Receiver.new(previous) }
|
|
|
|
|
|
|
|
it "processes correctly" do
|
2014-07-11 15:26:40 -04:00
|
|
|
expect { receiver.process}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2013-07-24 14:22:32 -04:00
|
|
|
expect(receiver.body).to eq("This will not include the previous discussion that is present in this email.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-25 14:05:14 -04:00
|
|
|
describe "multiple paragraphs" do
|
2014-07-11 15:26:40 -04:00
|
|
|
let(:paragraphs) { fixture_file("emails/paragraphs.eml") }
|
2013-06-25 14:05:14 -04:00
|
|
|
let(:receiver) { Email::Receiver.new(paragraphs) }
|
|
|
|
|
2013-07-24 14:22:32 -04:00
|
|
|
it "processes correctly" do
|
2014-07-11 15:26:40 -04:00
|
|
|
expect { receiver.process}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2013-06-25 14:05:14 -04:00
|
|
|
expect(receiver.body).to eq(
|
|
|
|
"Is there any reason the *old* candy can't be be kept in silos while the new candy
|
|
|
|
is imported into *new* silos?
|
|
|
|
|
|
|
|
The thing about candy is it stays delicious for a long time -- we can just keep
|
|
|
|
it there without worrying about it too much, imo.
|
|
|
|
|
|
|
|
Thanks for listening.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
def fill_email(mail, from, to, body = nil, subject = nil)
|
|
|
|
result = mail.gsub("FROM", from).gsub("TO", to)
|
|
|
|
if body
|
|
|
|
result.gsub!(/Hey.*/m, body)
|
|
|
|
end
|
|
|
|
if subject
|
|
|
|
result.sub!(/We .*/, subject)
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_email(opts)
|
|
|
|
incoming_email = fixture_file("emails/valid_incoming.eml")
|
|
|
|
email = fill_email(incoming_email, opts[:from], opts[:to], opts[:body], opts[:subject])
|
|
|
|
Email::Receiver.new(email).process
|
|
|
|
end
|
|
|
|
|
2013-06-13 18:11:10 -04:00
|
|
|
describe "with a valid email" do
|
|
|
|
let(:reply_key) { "59d8df8370b7e95c5a49fbf86aeb2c93" }
|
2014-07-31 04:46:02 -04:00
|
|
|
let(:to) { SiteSetting.reply_by_email_address.gsub("%{reply_key}", reply_key) }
|
|
|
|
|
|
|
|
let(:valid_reply) {
|
|
|
|
reply = fixture_file("emails/valid_reply.eml")
|
|
|
|
to = SiteSetting.reply_by_email_address.gsub("%{reply_key}", reply_key)
|
|
|
|
fill_email(reply, "test@test.com", to)
|
|
|
|
}
|
|
|
|
|
2013-06-13 18:11:10 -04:00
|
|
|
let(:receiver) { Email::Receiver.new(valid_reply) }
|
2014-07-31 04:46:02 -04:00
|
|
|
let(:post) { create_post }
|
|
|
|
let(:user) { post.user }
|
|
|
|
let(:email_log) { EmailLog.new(reply_key: reply_key,
|
|
|
|
post_id: post.id,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
user_id: post.user_id,
|
|
|
|
post: post,
|
|
|
|
user: user,
|
|
|
|
email_type: 'test',
|
|
|
|
to_address: 'test@test.com'
|
|
|
|
) }
|
2013-06-13 18:11:10 -04:00
|
|
|
let(:reply_body) {
|
|
|
|
"I could not disagree more. I am obviously biased but adventure time is the
|
|
|
|
greatest show ever created. Everyone should watch it.
|
|
|
|
|
|
|
|
- Jake out" }
|
|
|
|
|
|
|
|
describe "with an email log" do
|
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
it "extracts data" do
|
|
|
|
expect{ receiver.process }.to raise_error(Email::Receiver::EmailLogNotFound)
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
email_log.save!
|
|
|
|
receiver.process
|
2013-06-13 18:11:10 -04:00
|
|
|
|
|
|
|
expect(receiver.body).to eq(reply_body)
|
|
|
|
expect(receiver.email_log).to eq(email_log)
|
|
|
|
expect(receiver.reply_key).to eq(reply_key)
|
|
|
|
|
2014-07-11 15:26:40 -04:00
|
|
|
attachment_email = fixture_file("emails/attachment.eml")
|
2014-07-31 04:46:02 -04:00
|
|
|
attachment_email = fill_email(attachment_email, "test@test.com", to)
|
2014-03-28 09:57:12 -04:00
|
|
|
r = Email::Receiver.new(attachment_email)
|
|
|
|
expect { r.process }.to_not raise_error
|
2014-04-14 16:55:57 -04:00
|
|
|
expect(r.body).to match(/here is an image attachment\n<img src='\/uploads\/default\/\d+\/\w{16}\.png' width='289' height='126'>\n/)
|
2014-03-28 09:57:12 -04:00
|
|
|
end
|
2014-04-14 16:55:57 -04:00
|
|
|
|
2014-03-28 09:57:12 -04:00
|
|
|
end
|
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
|
|
|
|
2014-02-27 07:44:21 -05:00
|
|
|
describe "processes an email to a category" do
|
|
|
|
before do
|
2014-07-31 04:46:02 -04:00
|
|
|
SiteSetting.email_in = true
|
2014-02-27 07:44:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
it "correctly can target categories" do
|
|
|
|
to = "some@email.com"
|
2014-02-27 07:44:21 -05:00
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
Fabricate(:category, email_in_allow_strangers: false, email_in: to)
|
|
|
|
SiteSetting.email_in_min_trust = TrustLevel.levels[:elder].to_s
|
2014-02-27 07:44:21 -05:00
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
# no email in for user
|
|
|
|
expect{
|
|
|
|
process_email(from: "cobb@dob.com", to: "invalid@address.com")
|
|
|
|
}.to raise_error(Email::Receiver::EmailLogNotFound)
|
2014-02-27 07:44:21 -05:00
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
# valid target invalid user
|
|
|
|
expect{
|
|
|
|
process_email(from: "cobb@dob.com", to: to)
|
|
|
|
}.to raise_error(Email::Receiver::UserNotFoundError)
|
2014-02-27 07:44:21 -05:00
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
# untrusted
|
|
|
|
user = Fabricate(:user)
|
|
|
|
expect{
|
|
|
|
process_email(from: user.email, to: to)
|
|
|
|
}.to raise_error(Email::Receiver::UserNotSufficientTrustLevelError)
|
2014-02-27 07:44:21 -05:00
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
# trusted
|
|
|
|
user.trust_level = 4
|
|
|
|
user.save
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
process_email(from: user.email, to: to)
|
|
|
|
user.posts.count.should == 1
|
2014-02-24 11:36:53 -05:00
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
# email too short
|
|
|
|
message = nil
|
|
|
|
begin
|
|
|
|
process_email(from: user.email, to: to, body: "x", subject: "this is my new topic title")
|
|
|
|
rescue Email::Receiver::InvalidPost => e
|
|
|
|
message = e.message
|
2014-02-24 11:36:53 -05:00
|
|
|
end
|
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
e.message.should include("too short")
|
2014-02-24 11:36:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2014-02-27 10:36:33 -05:00
|
|
|
|
|
|
|
describe "processes an unknown email sender to category" do
|
|
|
|
before do
|
2014-07-31 04:46:02 -04:00
|
|
|
SiteSetting.email_in = true
|
2014-02-27 10:36:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
it "rejects anon email" do
|
|
|
|
Fabricate(:category, email_in_allow_strangers: false, email_in: "bob@bob.com")
|
|
|
|
expect { process_email(from: "test@test.com", to: "bob@bob.com") }.to raise_error(Email::Receiver::UserNotFoundError)
|
2014-02-27 10:36:33 -05:00
|
|
|
end
|
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
it "creates a topic for allowed category" do
|
|
|
|
Fabricate(:category, email_in_allow_strangers: true, email_in: "bob@bob.com")
|
|
|
|
process_email(from: "test@test.com", to: "bob@bob.com")
|
2014-02-27 10:36:33 -05:00
|
|
|
|
2014-07-31 04:46:02 -04:00
|
|
|
# This is the current implementation but it is wrong, it should register an account
|
|
|
|
Discourse.system_user.posts.order("id desc").limit(1).pluck(:raw).first.should include("Hey folks")
|
2014-02-27 10:36:33 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|