diff --git a/spec/fixtures/emails/valid_incoming.cooked b/spec/fixtures/emails/valid_incoming.cooked
new file mode 100644
index 00000000000..2bf35825373
--- /dev/null
+++ b/spec/fixtures/emails/valid_incoming.cooked
@@ -0,0 +1,5 @@
+
Hey folks,
+
+I was thinking. Wouldn't it be great if we could post topics via email? Yes it would!
+
+Jakie
diff --git a/spec/fixtures/emails/valid_incoming.eml b/spec/fixtures/emails/valid_incoming.eml
index 5e8db53319e..5e03d1dc223 100644
--- a/spec/fixtures/emails/valid_incoming.eml
+++ b/spec/fixtures/emails/valid_incoming.eml
@@ -4,8 +4,8 @@ Received: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for ; Thu, 13 Jun 2013 14:03:48 -0700
Received: by 10.0.0.1 with HTTP; Thu, 13 Jun 2013 14:03:48 -0700
Date: Thu, 13 Jun 2013 17:03:48 -0400
-From: Jake the Dog
-To:
+From: Jake the Dog
+To: Foo Discourse
Message-ID:
Subject: We should have a post-by-email-feature.
Mime-Version: 1.0
diff --git a/spec/fixtures/emails/valid_reply.cooked b/spec/fixtures/emails/valid_reply.cooked
new file mode 100644
index 00000000000..d622863da83
--- /dev/null
+++ b/spec/fixtures/emails/valid_reply.cooked
@@ -0,0 +1,3 @@
+I could not disagree more. I am obviously biased but adventure time is the greatest show ever created. Everyone should watch it.
+
+- Jake out
diff --git a/spec/fixtures/emails/valid_reply.eml b/spec/fixtures/emails/valid_reply.eml
index 1e696389954..ec387c8d535 100644
--- a/spec/fixtures/emails/valid_reply.eml
+++ b/spec/fixtures/emails/valid_reply.eml
@@ -4,8 +4,8 @@ Received: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b
Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for ; Thu, 13 Jun 2013 14:03:48 -0700
Received: by 10.0.0.1 with HTTP; Thu, 13 Jun 2013 14:03:48 -0700
Date: Thu, 13 Jun 2013 17:03:48 -0400
-From: Jake the Dog
-To: reply+59d8df8370b7e95c5a49fbf86aeb2c93@appmail.adventuretime.ooo
+From: Jake the Dog
+To: reply+59d8df8370b7e95c5a49fbf86aeb2c93@discourse.example.com
Message-ID:
Subject: re: [Discourse Meta] eviltrout posted in 'Adventure Time Sux'
Mime-Version: 1.0
diff --git a/spec/jobs/poll_mailbox_spec.rb b/spec/jobs/poll_mailbox_spec.rb
index 3adcb78c735..90c8ec9bea9 100644
--- a/spec/jobs/poll_mailbox_spec.rb
+++ b/spec/jobs/poll_mailbox_spec.rb
@@ -41,6 +41,93 @@ describe Jobs::PollMailbox do
end
+ # 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
+
+ describe "processing email B" do
+ 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
+
+ describe "valid incoming email" do
+ let(:email) { MockPop3EmailObject.new fixture_file('emails/valid_incoming.eml')}
+ let(:expected_post) { fixture_file('emails/valid_incoming.cooked') }
+
+ it "posts a new topic with the correct content" do
+
+ 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
+
+ describe "valid reply" do
+ 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',
+ post: first_post,
+ topic: topic)
+ end
+
+ pending "creates a new post with the correct content" do
+ RejectionMailer.expects(:send_rejection).never
+ Discourse.expects(:handle_exception).never
+
+ poller.handle_mail(email)
+
+ new_post = Post.where(topic: topic, post_number: 2)
+ assert new_post.present?
+
+ assert_equal expected_post.strip, new_post.cooked.strip
+
+ assert email.deleted?
+ end
+ end
+
+
+ end
+
describe "processing email" do
let!(:receiver) { mock }