2018-12-14 05:06:12 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-24 15:16:22 -05:00
|
|
|
require_relative "../test_helper"
|
2018-11-25 14:30:05 -05:00
|
|
|
|
2019-01-12 21:38:26 -05:00
|
|
|
require "action_mailbox/relayer"
|
2018-11-25 14:30:05 -05:00
|
|
|
|
|
|
|
module ActionMailbox
|
2019-01-12 21:38:26 -05:00
|
|
|
class RelayerTest < ActiveSupport::TestCase
|
|
|
|
URL = "https://example.com/rails/action_mailbox/relay/inbound_emails"
|
2018-11-25 14:30:05 -05:00
|
|
|
INGRESS_PASSWORD = "secret"
|
|
|
|
|
|
|
|
setup do
|
2019-01-12 21:38:26 -05:00
|
|
|
@relayer = ActionMailbox::Relayer.new(url: URL, password: INGRESS_PASSWORD)
|
2018-11-25 14:30:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "successfully relaying an email" do
|
|
|
|
stub_request(:post, URL).to_return status: 204
|
|
|
|
|
|
|
|
result = @relayer.relay(file_fixture("welcome.eml").read)
|
2019-01-12 21:38:26 -05:00
|
|
|
assert_equal "2.0.0", result.status_code
|
|
|
|
assert_equal "Successfully relayed message to ingress", result.message
|
2018-11-25 14:30:05 -05:00
|
|
|
assert result.success?
|
|
|
|
assert_not result.failure?
|
|
|
|
|
|
|
|
assert_requested :post, URL, body: file_fixture("welcome.eml").read,
|
2018-11-25 21:35:27 -05:00
|
|
|
basic_auth: [ "actionmailbox", INGRESS_PASSWORD ],
|
2019-01-12 21:38:26 -05:00
|
|
|
headers: { "Content-Type" => "message/rfc822", "User-Agent" => /\AAction Mailbox relayer v\d+\./ }
|
2018-11-25 14:30:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unsuccessfully relaying with invalid credentials" do
|
|
|
|
stub_request(:post, URL).to_return status: 401
|
|
|
|
|
|
|
|
result = @relayer.relay(file_fixture("welcome.eml").read)
|
2019-01-12 21:38:26 -05:00
|
|
|
assert_equal "4.7.0", result.status_code
|
|
|
|
assert_equal "Invalid credentials for ingress", result.message
|
2018-11-25 14:30:05 -05:00
|
|
|
assert_not result.success?
|
|
|
|
assert result.failure?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unsuccessfully relaying due to an unspecified server error" do
|
|
|
|
stub_request(:post, URL).to_return status: 500
|
|
|
|
|
|
|
|
result = @relayer.relay(file_fixture("welcome.eml").read)
|
2019-01-12 21:38:26 -05:00
|
|
|
assert_equal "4.0.0", result.status_code
|
|
|
|
assert_equal "HTTP 500", result.message
|
2018-11-25 14:30:05 -05:00
|
|
|
assert_not result.success?
|
|
|
|
assert result.failure?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unsuccessfully relaying due to a gateway timeout" do
|
|
|
|
stub_request(:post, URL).to_return status: 504
|
|
|
|
|
|
|
|
result = @relayer.relay(file_fixture("welcome.eml").read)
|
2019-01-12 21:38:26 -05:00
|
|
|
assert_equal "4.0.0", result.status_code
|
|
|
|
assert_equal "HTTP 504", result.message
|
2018-11-25 14:30:05 -05:00
|
|
|
assert_not result.success?
|
|
|
|
assert result.failure?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unsuccessfully relaying due to ECONNRESET" do
|
|
|
|
stub_request(:post, URL).to_raise Errno::ECONNRESET.new
|
|
|
|
|
|
|
|
result = @relayer.relay(file_fixture("welcome.eml").read)
|
2019-01-12 21:38:26 -05:00
|
|
|
assert_equal "4.4.2", result.status_code
|
|
|
|
assert_equal "Network error relaying to ingress: Connection reset by peer", result.message
|
2018-11-25 14:30:05 -05:00
|
|
|
assert_not result.success?
|
|
|
|
assert result.failure?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "unsuccessfully relaying due to connection failure" do
|
|
|
|
stub_request(:post, URL).to_raise SocketError.new("Failed to open TCP connection to example.com:443")
|
|
|
|
|
|
|
|
result = @relayer.relay(file_fixture("welcome.eml").read)
|
2019-01-12 21:38:26 -05:00
|
|
|
assert_equal "4.4.2", result.status_code
|
|
|
|
assert_equal "Network error relaying to ingress: Failed to open TCP connection to example.com:443", result.message
|
2018-11-25 14:30:05 -05:00
|
|
|
assert_not result.success?
|
|
|
|
assert result.failure?
|
|
|
|
end
|
|
|
|
|
2018-12-03 22:15:13 -05:00
|
|
|
test "unsuccessfully relaying due to client-side timeout" do
|
|
|
|
stub_request(:post, URL).to_timeout
|
|
|
|
|
|
|
|
result = @relayer.relay(file_fixture("welcome.eml").read)
|
2019-01-12 21:38:26 -05:00
|
|
|
assert_equal "4.4.2", result.status_code
|
|
|
|
assert_equal "Timed out relaying to ingress", result.message
|
2018-12-03 22:15:13 -05:00
|
|
|
assert_not result.success?
|
|
|
|
assert result.failure?
|
|
|
|
end
|
|
|
|
|
2018-11-25 14:30:05 -05:00
|
|
|
test "unsuccessfully relaying due to an unhandled exception" do
|
|
|
|
stub_request(:post, URL).to_raise StandardError.new("Something went wrong")
|
|
|
|
|
|
|
|
result = @relayer.relay(file_fixture("welcome.eml").read)
|
2019-01-12 21:38:26 -05:00
|
|
|
assert_equal "4.0.0", result.status_code
|
|
|
|
assert_equal "Error relaying to ingress: Something went wrong", result.message
|
2018-11-25 14:30:05 -05:00
|
|
|
assert_not result.success?
|
|
|
|
assert result.failure?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|