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-09-17 20:48:32 -04:00
|
|
|
|
2018-09-28 15:19:43 -04:00
|
|
|
class RootMailbox < ActionMailbox::Base
|
2018-09-17 20:48:32 -04:00
|
|
|
def process
|
2018-09-19 20:20:09 -04:00
|
|
|
$processed_by = self.class.to_s
|
|
|
|
$processed_mail = mail
|
2018-09-17 20:48:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-19 20:20:09 -04:00
|
|
|
class FirstMailbox < RootMailbox
|
|
|
|
end
|
|
|
|
|
|
|
|
class SecondMailbox < RootMailbox
|
|
|
|
end
|
|
|
|
|
2018-10-01 17:48:44 -04:00
|
|
|
module Nested
|
|
|
|
class FirstMailbox < RootMailbox
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-20 20:16:19 -04:00
|
|
|
class FirstMailboxAddress
|
|
|
|
def match?(inbound_email)
|
|
|
|
inbound_email.mail.to.include?("replies-class@example.com")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-28 15:19:43 -04:00
|
|
|
module ActionMailbox
|
2018-09-17 20:48:32 -04:00
|
|
|
class RouterTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
2018-09-28 15:19:43 -04:00
|
|
|
@router = ActionMailbox::Router.new
|
2018-09-19 20:20:09 -04:00
|
|
|
$processed_by = $processed_mail = nil
|
2018-09-17 20:48:32 -04:00
|
|
|
end
|
|
|
|
|
2018-09-19 20:20:09 -04:00
|
|
|
test "single string route" do
|
|
|
|
@router.add_routes("first@example.com" => :first)
|
|
|
|
|
|
|
|
inbound_email = create_inbound_email_from_mail(to: "first@example.com", subject: "This is a reply")
|
|
|
|
@router.route inbound_email
|
|
|
|
assert_equal "FirstMailbox", $processed_by
|
2018-09-25 19:58:00 -04:00
|
|
|
assert_equal inbound_email.mail, $processed_mail
|
|
|
|
end
|
|
|
|
|
|
|
|
test "single string routing on cc" do
|
|
|
|
@router.add_routes("first@example.com" => :first)
|
|
|
|
|
|
|
|
inbound_email = create_inbound_email_from_mail(to: "someone@example.com", cc: "first@example.com", subject: "This is a reply")
|
|
|
|
@router.route inbound_email
|
2019-04-16 08:04:38 -04:00
|
|
|
assert_equal "FirstMailbox", $processed_by
|
|
|
|
assert_equal inbound_email.mail, $processed_mail
|
|
|
|
end
|
|
|
|
|
|
|
|
test "single string routing on bcc" do
|
|
|
|
@router.add_routes("first@example.com" => :first)
|
|
|
|
|
|
|
|
inbound_email = create_inbound_email_from_mail(to: "someone@example.com", bcc: "first@example.com", subject: "This is a reply")
|
|
|
|
@router.route inbound_email
|
2018-09-25 19:58:00 -04:00
|
|
|
assert_equal "FirstMailbox", $processed_by
|
|
|
|
assert_equal inbound_email.mail, $processed_mail
|
2018-09-19 20:20:09 -04:00
|
|
|
end
|
|
|
|
|
2018-10-03 20:44:31 -04:00
|
|
|
test "single string routing case-insensitively" do
|
|
|
|
@router.add_routes("first@example.com" => :first)
|
|
|
|
|
|
|
|
inbound_email = create_inbound_email_from_mail(to: "FIRST@example.com", subject: "This is a reply")
|
|
|
|
@router.route inbound_email
|
|
|
|
assert_equal "FirstMailbox", $processed_by
|
|
|
|
assert_equal inbound_email.mail, $processed_mail
|
|
|
|
end
|
|
|
|
|
2018-09-19 20:20:09 -04:00
|
|
|
test "multiple string routes" do
|
|
|
|
@router.add_routes("first@example.com" => :first, "second@example.com" => :second)
|
|
|
|
|
|
|
|
inbound_email = create_inbound_email_from_mail(to: "first@example.com", subject: "This is a reply")
|
|
|
|
@router.route inbound_email
|
|
|
|
assert_equal "FirstMailbox", $processed_by
|
|
|
|
assert_equal inbound_email.mail, $processed_mail
|
2018-09-19 20:08:35 -04:00
|
|
|
|
2018-09-19 20:20:09 -04:00
|
|
|
inbound_email = create_inbound_email_from_mail(to: "second@example.com", subject: "This is a reply")
|
|
|
|
@router.route inbound_email
|
|
|
|
assert_equal "SecondMailbox", $processed_by
|
|
|
|
assert_equal inbound_email.mail, $processed_mail
|
2018-09-17 20:48:32 -04:00
|
|
|
end
|
2018-10-01 17:48:44 -04:00
|
|
|
|
2018-09-20 20:16:19 -04:00
|
|
|
test "single regexp route" do
|
|
|
|
@router.add_routes(/replies-\w+@example.com/ => :first, "replies-nowhere@example.com" => :second)
|
|
|
|
|
|
|
|
inbound_email = create_inbound_email_from_mail(to: "replies-okay@example.com", subject: "This is a reply")
|
|
|
|
@router.route inbound_email
|
|
|
|
assert_equal "FirstMailbox", $processed_by
|
|
|
|
end
|
|
|
|
|
|
|
|
test "single proc route" do
|
|
|
|
@router.add_route \
|
|
|
|
->(inbound_email) { inbound_email.mail.to.include?("replies-proc@example.com") },
|
|
|
|
to: :second
|
|
|
|
|
|
|
|
@router.route create_inbound_email_from_mail(to: "replies-proc@example.com", subject: "This is a reply")
|
|
|
|
assert_equal "SecondMailbox", $processed_by
|
|
|
|
end
|
|
|
|
|
|
|
|
test "address class route" do
|
|
|
|
@router.add_route FirstMailboxAddress.new, to: :first
|
|
|
|
@router.route create_inbound_email_from_mail(to: "replies-class@example.com", subject: "This is a reply")
|
|
|
|
assert_equal "FirstMailbox", $processed_by
|
|
|
|
end
|
|
|
|
|
2018-10-01 17:48:44 -04:00
|
|
|
test "string route to nested mailbox" do
|
|
|
|
@router.add_route "first@example.com", to: "nested/first"
|
|
|
|
|
|
|
|
inbound_email = create_inbound_email_from_mail(to: "first@example.com", subject: "This is a reply")
|
|
|
|
@router.route inbound_email
|
|
|
|
assert_equal "Nested::FirstMailbox", $processed_by
|
|
|
|
end
|
|
|
|
|
2018-12-06 16:27:45 -05:00
|
|
|
test "all as the only route" do
|
|
|
|
@router.add_route :all, to: :first
|
|
|
|
@router.route create_inbound_email_from_mail(to: "replies-class@example.com", subject: "This is a reply")
|
|
|
|
assert_equal "FirstMailbox", $processed_by
|
|
|
|
end
|
|
|
|
|
|
|
|
test "all as the second route" do
|
|
|
|
@router.add_route FirstMailboxAddress.new, to: :first
|
|
|
|
@router.add_route :all, to: :second
|
|
|
|
|
|
|
|
@router.route create_inbound_email_from_mail(to: "replies-class@example.com", subject: "This is a reply")
|
|
|
|
assert_equal "FirstMailbox", $processed_by
|
|
|
|
|
|
|
|
@router.route create_inbound_email_from_mail(to: "elsewhere@example.com", subject: "This is a reply")
|
|
|
|
assert_equal "SecondMailbox", $processed_by
|
|
|
|
end
|
|
|
|
|
2018-09-20 20:16:19 -04:00
|
|
|
test "missing route" do
|
2021-06-24 06:46:21 -04:00
|
|
|
inbound_email = create_inbound_email_from_mail(to: "going-nowhere@example.com", subject: "This is a reply")
|
2018-09-28 15:19:43 -04:00
|
|
|
assert_raises(ActionMailbox::Router::RoutingError) do
|
2018-09-20 20:16:19 -04:00
|
|
|
@router.route inbound_email
|
|
|
|
end
|
2021-06-24 06:46:21 -04:00
|
|
|
assert inbound_email.bounced?
|
2018-09-20 20:16:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "invalid address" do
|
2021-06-01 11:30:13 -04:00
|
|
|
error = assert_raises(ArgumentError) do
|
2018-09-20 20:16:19 -04:00
|
|
|
@router.add_route Array.new, to: :first
|
|
|
|
end
|
2021-06-01 11:30:13 -04:00
|
|
|
assert_equal "Expected a Symbol, String, Regexp, Proc, or matchable, got []", error.message
|
2018-09-20 20:16:19 -04:00
|
|
|
end
|
2019-05-04 13:53:08 -04:00
|
|
|
|
|
|
|
test "single string mailbox_for" do
|
|
|
|
@router.add_routes("first@example.com" => :first)
|
|
|
|
|
|
|
|
inbound_email = create_inbound_email_from_mail(to: "first@example.com", subject: "This is a reply")
|
|
|
|
assert_equal FirstMailbox, @router.mailbox_for(inbound_email)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "mailbox_for with no matches" do
|
|
|
|
@router.add_routes("first@example.com" => :first)
|
|
|
|
|
|
|
|
inbound_email = create_inbound_email_from_mail(to: "second@example.com", subject: "This is a reply")
|
|
|
|
assert_nil @router.mailbox_for(inbound_email)
|
|
|
|
end
|
2018-09-17 20:48:32 -04:00
|
|
|
end
|
|
|
|
end
|