From e44936f3ed4799714fc36a342c1dbab1d50f0ffc Mon Sep 17 00:00:00 2001
From: Douwe Maan <douwe@gitlab.com>
Date: Thu, 20 Aug 2015 11:47:09 -0700
Subject: [PATCH] Test EmailReceiverWorker.

---
 spec/fixtures/emails/valid_incoming.eml    | 10 ++---
 spec/workers/email_receiver_worker_spec.rb | 51 ++++++++++++++++++++++
 2 files changed, 56 insertions(+), 5 deletions(-)
 create mode 100644 spec/workers/email_receiver_worker_spec.rb

diff --git a/spec/fixtures/emails/valid_incoming.eml b/spec/fixtures/emails/valid_incoming.eml
index 5e8db53319e..409711ab417 100644
--- a/spec/fixtures/emails/valid_incoming.eml
+++ b/spec/fixtures/emails/valid_incoming.eml
@@ -1,11 +1,11 @@
-Return-Path: <FROM>
+Return-Path: <from@example.com>
 Received: from iceking.adventuretime.ooo ([unix socket]) by iceking (Cyrus v2.2.13-Debian-2.2.13-19+squeeze3) with LMTPA; Thu, 13 Jun 2013 17:03:50 -0400
-Received: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <TO>; Thu, 13 Jun 2013 17:03:50 -0400
-Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <TO>; Thu, 13 Jun 2013 14:03:48 -0700
+Received: from mail-ie0-x234.google.com (mail-ie0-x234.google.com [IPv6:2607:f8b0:4001:c03::234]) by iceking.adventuretime.ooo (8.14.3/8.14.3/Debian-9.4) with ESMTP id r5DL3nFJ016967 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=NOT) for <to@example.com>; Thu, 13 Jun 2013 17:03:50 -0400
+Received: by mail-ie0-f180.google.com with SMTP id f4so21977375iea.25 for <to@example.com>; 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 <FROM>
-To: <TO>
+From: Jake the Dog <from@example.com>
+To: <to@example.com>
 Message-ID: <CADkmRc+rNGAGGbV2iE5p918UVy4UyJqVcXRO2=otppgzduJSg@mail.gmail.com>
 Subject: We should have a post-by-email-feature.
 Mime-Version: 1.0
diff --git a/spec/workers/email_receiver_worker_spec.rb b/spec/workers/email_receiver_worker_spec.rb
new file mode 100644
index 00000000000..109a7b3eb1d
--- /dev/null
+++ b/spec/workers/email_receiver_worker_spec.rb
@@ -0,0 +1,51 @@
+require "spec_helper"
+
+describe EmailReceiverWorker do
+  def fixture_file(filename)
+    return '' if filename.blank?
+    file_path = File.expand_path(Rails.root + 'spec/fixtures/' + filename)
+    File.read(file_path)
+  end
+
+  let(:raw_message) { fixture_file('emails/valid_incoming.eml') }
+
+  context "when reply by email is enabled" do
+    before do
+      allow(Gitlab::ReplyByEmail).to receive(:enabled?).and_return(true)
+    end
+
+    it "calls the email receiver" do
+      expect(Gitlab::Email::Receiver).to receive(:new).with(raw_message).and_call_original
+      expect_any_instance_of(Gitlab::Email::Receiver).to receive(:execute)
+
+      described_class.new.perform(raw_message)
+    end
+
+    context "when an error occurs" do
+      before do
+        allow_any_instance_of(Gitlab::Email::Receiver).to receive(:execute).and_raise(Gitlab::Email::Receiver::EmptyEmailError)
+      end
+
+      it "sends out a rejection email" do
+        described_class.new.perform(raw_message)
+
+        email = ActionMailer::Base.deliveries.last
+        expect(email).not_to be_nil
+        expect(email.to).to eq(["from@example.com"])
+        expect(email.subject).to include("Rejected")
+      end
+    end
+  end
+
+  context "when reply by email is disabled" do
+    before do
+      allow(Gitlab::ReplyByEmail).to receive(:enabled?).and_return(false)
+    end
+
+    it "doesn't call the email receiver" do
+      expect(Gitlab::Email::Receiver).not_to receive(:new)
+
+      described_class.new.perform(raw_message)
+    end
+  end
+end
-- 
GitLab