From d7df4e597995bdf0ceff7fee56fda3e36427a58b Mon Sep 17 00:00:00 2001 From: riking Date: Fri, 1 Aug 2014 11:03:03 -0700 Subject: [PATCH] Start making better-written tests for the email job --- spec/fixtures/emails/valid_incoming.cooked | 5 ++ spec/fixtures/emails/valid_incoming.eml | 4 +- spec/fixtures/emails/valid_reply.cooked | 3 + spec/fixtures/emails/valid_reply.eml | 4 +- spec/jobs/poll_mailbox_spec.rb | 87 ++++++++++++++++++++++ 5 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 spec/fixtures/emails/valid_incoming.cooked create mode 100644 spec/fixtures/emails/valid_reply.cooked diff --git a/spec/fixtures/emails/valid_incoming.cooked b/spec/fixtures/emails/valid_incoming.cooked new file mode 100644 index 000000000..2bf358253 --- /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 5e8db5331..5e03d1dc2 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 000000000..d622863da --- /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 1e6963899..ec387c8d5 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 3adcb78c7..90c8ec9be 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 }