BigW Consortium Gitlab

reply_by_email_spec.rb 2.08 KB
Newer Older
1 2 3 4 5 6
require "spec_helper"

describe Gitlab::ReplyByEmail do
  describe "self.enabled?" do
    context "when reply by email is enabled" do
      before do
7
        stub_reply_by_email_setting(enabled: true)
8 9 10 11
      end

      context "when the address is valid" do
        before do
12
          stub_reply_by_email_setting(address: "replies+%{reply_key}@example.com")
13 14 15 16 17 18 19 20 21
        end

        it "returns true" do
          expect(described_class.enabled?).to be_truthy
        end
      end

      context "when the address is invalid" do
        before do
22
          stub_reply_by_email_setting(address: "replies@example.com")
23 24 25 26 27 28 29 30 31 32
        end

        it "returns false" do
          expect(described_class.enabled?).to be_falsey
        end
      end
    end

    context "when reply by email is disabled" do
      before do
33
        stub_reply_by_email_setting(enabled: false)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
      end

      it "returns false" do
        expect(described_class.enabled?).to be_falsey
      end
    end
  end

  describe "self.reply_key" do
    context "when enabled" do
      before do
        allow(described_class).to receive(:enabled?).and_return(true)
      end

      it "returns a random hex" do
        key = described_class.reply_key
        key2 = described_class.reply_key

        expect(key).not_to eq(key2)
      end
    end

    context "when disabled" do
      before do
        allow(described_class).to receive(:enabled?).and_return(false)
      end

      it "returns nil" do
        expect(described_class.reply_key).to be_nil
      end
    end
  end

  context "self.reply_address" do
    before do
69
      stub_reply_by_email_setting(address: "replies+%{reply_key}@example.com")
70 71 72 73 74 75 76 77 78
    end

    it "returns the address with an interpolated reply key" do
      expect(described_class.reply_address("key")).to eq("replies+key@example.com")
    end
  end

  context "self.reply_key_from_address" do
    before do
79
      stub_reply_by_email_setting(address: "replies+%{reply_key}@example.com")
80 81 82 83 84 85 86
    end

    it "returns reply key" do
      expect(described_class.reply_key_from_address("replies+key@example.com")).to eq("key")
    end
  end
end