aboutsummaryrefslogtreecommitdiffstats
path: root/actionmailbox/test/controllers/ingresses/mailgun/inbound_emails_controller_test.rb
blob: 3f225b895322ad9e83319e3f05d67c2ad60d5fa3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

require "test_helper"

ENV["MAILGUN_INGRESS_API_KEY"] = "tbsy84uSV1Kt3ZJZELY2TmShPRs91E3yL4tzf96297vBCkDWgL"

class ActionMailbox::Ingresses::Mailgun::InboundEmailsControllerTest < ActionDispatch::IntegrationTest
  setup { ActionMailbox.ingress = :mailgun }

  test "receiving an inbound email from Mailgun" do
    assert_difference -> { ActionMailbox::InboundEmail.count }, +1 do
      travel_to "2018-10-09 15:15:00 EDT"
      post rails_mailgun_inbound_emails_url, params: {
        timestamp: 1539112500,
        token: "7VwW7k6Ak7zcTwoSoNm7aTtbk1g67MKAnsYLfUB7PdszbgR5Xi",
        signature: "ef24c5225322217bb065b80bb54eb4f9206d764e3e16abab07f0a64d1cf477cc",
        "body-mime" => file_fixture("../files/welcome.eml").read
      }
    end

    assert_response :no_content

    inbound_email = ActionMailbox::InboundEmail.last
    assert_equal file_fixture("../files/welcome.eml").read, inbound_email.raw_email.download
    assert_equal "0CB459E0-0336-41DA-BC88-E6E28C697DDB@37signals.com", inbound_email.message_id
  end

  test "rejecting a delayed inbound email from Mailgun" do
    assert_no_difference -> { ActionMailbox::InboundEmail.count } do
      travel_to "2018-10-09 15:26:00 EDT"
      post rails_mailgun_inbound_emails_url, params: {
        timestamp: 1539112500,
        token: "7VwW7k6Ak7zcTwoSoNm7aTtbk1g67MKAnsYLfUB7PdszbgR5Xi",
        signature: "ef24c5225322217bb065b80bb54eb4f9206d764e3e16abab07f0a64d1cf477cc",
        "body-mime" => file_fixture("../files/welcome.eml").read
      }
    end

    assert_response :unauthorized
  end

  test "rejecting a forged inbound email from Mailgun" do
    assert_no_difference -> { ActionMailbox::InboundEmail.count } do
      travel_to "2018-10-09 15:15:00 EDT"
      post rails_mailgun_inbound_emails_url, params: {
        timestamp: 1539112500,
        token: "Zx8mJBiGmiiyyfWnho3zKyjCg2pxLARoCuBM7X9AKCioShGiMX",
        signature: "ef24c5225322217bb065b80bb54eb4f9206d764e3e16abab07f0a64d1cf477cc",
        "body-mime" => file_fixture("../files/welcome.eml").read
      }
    end

    assert_response :unauthorized
  end

  test "raising when the configured Mailgun API key is nil" do
    switch_key_to nil do
      assert_raises ArgumentError do
        travel_to "2018-10-09 15:15:00 EDT"
        post rails_mailgun_inbound_emails_url, params: {
          timestamp: 1539112500,
          token: "7VwW7k6Ak7zcTwoSoNm7aTtbk1g67MKAnsYLfUB7PdszbgR5Xi",
          signature: "ef24c5225322217bb065b80bb54eb4f9206d764e3e16abab07f0a64d1cf477cc",
          "body-mime" => file_fixture("../files/welcome.eml").read
        }
      end
    end
  end

  test "raising when the configured Mailgun API key is blank" do
    switch_key_to "" do
      assert_raises ArgumentError do
        travel_to "2018-10-09 15:15:00 EDT"
        post rails_mailgun_inbound_emails_url, params: {
          timestamp: 1539112500,
          token: "7VwW7k6Ak7zcTwoSoNm7aTtbk1g67MKAnsYLfUB7PdszbgR5Xi",
          signature: "ef24c5225322217bb065b80bb54eb4f9206d764e3e16abab07f0a64d1cf477cc",
          "body-mime" => file_fixture("../files/welcome.eml").read
        }
      end
    end
  end

  private
    def switch_key_to(new_key)
      previous_key, ENV["MAILGUN_INGRESS_API_KEY"] = ENV["MAILGUN_INGRESS_API_KEY"], new_key
      yield
    ensure
      ENV["MAILGUN_INGRESS_API_KEY"] = previous_key
    end
end