feat(email): HTTP API transport via Resend (fallback SMTP).
Outbound SMTP ports are often blocked on VPS; EMAIL_TRANSPORT=http_api sends via Resend HTTPS (Brevo optional). OpenSMTPd/SMTP remain available.
This commit is contained in:
@@ -8,25 +8,35 @@ logic_email_test_() ->
|
||||
{"invite and reset urls", fun test_invite_reset/0},
|
||||
{"deliver error still ok", fun test_deliver_error/0},
|
||||
{"html template placeholders filled", fun test_template_fill/0},
|
||||
{"booking reminder deliver", fun test_reminder_deliver/0}
|
||||
{"booking reminder deliver", fun test_reminder_deliver/0},
|
||||
{"http_api mock deliver", fun test_http_api_deliver/0},
|
||||
{"http_api error still ok", fun test_http_api_error/0}
|
||||
]}.
|
||||
|
||||
setup() ->
|
||||
application:unset_env(eventhub, smtp_host),
|
||||
application:unset_env(eventhub, smtp_deliver),
|
||||
application:unset_env(eventhub, email_transport),
|
||||
application:unset_env(eventhub, email_api_key),
|
||||
application:unset_env(eventhub, email_http_deliver),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
application:unset_env(eventhub, smtp_host),
|
||||
application:unset_env(eventhub, smtp_deliver),
|
||||
application:unset_env(eventhub, email_transport),
|
||||
application:unset_env(eventhub, email_api_key),
|
||||
application:unset_env(eventhub, email_http_deliver),
|
||||
ok.
|
||||
|
||||
test_log_only() ->
|
||||
application:unset_env(eventhub, smtp_host),
|
||||
application:set_env(eventhub, email_transport, log),
|
||||
?assertEqual(ok, logic_email:send_verification_email(<<"a@b.c">>, <<"tok">>)).
|
||||
|
||||
test_verify_deliver() ->
|
||||
Self = self(),
|
||||
application:set_env(eventhub, email_transport, smtp),
|
||||
application:set_env(eventhub, smtp_host, "smtp.test"),
|
||||
application:set_env(eventhub, smtp_deliver,
|
||||
fun(From, To, Raw, Opts) ->
|
||||
@@ -50,6 +60,7 @@ test_verify_deliver() ->
|
||||
|
||||
test_invite_reset() ->
|
||||
Self = self(),
|
||||
application:set_env(eventhub, email_transport, smtp),
|
||||
application:set_env(eventhub, smtp_host, <<"smtp.test">>),
|
||||
application:set_env(eventhub, smtp_deliver,
|
||||
fun(_From, To, Raw, _Opts) ->
|
||||
@@ -70,6 +81,7 @@ test_invite_reset() ->
|
||||
end.
|
||||
|
||||
test_deliver_error() ->
|
||||
application:set_env(eventhub, email_transport, smtp),
|
||||
application:set_env(eventhub, smtp_host, "smtp.test"),
|
||||
application:set_env(eventhub, smtp_deliver, fun(_, _, _, _) -> {error, boom} end),
|
||||
?assertEqual(ok, logic_email:send_verification_email(<<"a@b.c">>, <<"t">>)),
|
||||
@@ -85,6 +97,7 @@ test_template_fill() ->
|
||||
|
||||
test_reminder_deliver() ->
|
||||
Self = self(),
|
||||
application:set_env(eventhub, email_transport, smtp),
|
||||
application:set_env(eventhub, smtp_host, "smtp.test"),
|
||||
application:set_env(eventhub, smtp_deliver,
|
||||
fun(_From, To, Raw, _Opts) ->
|
||||
@@ -102,3 +115,33 @@ test_reminder_deliver() ->
|
||||
after 1000 ->
|
||||
error(timeout)
|
||||
end.
|
||||
|
||||
test_http_api_deliver() ->
|
||||
Self = self(),
|
||||
application:set_env(eventhub, email_transport, http_api),
|
||||
application:set_env(eventhub, email_api_key, <<"test-key">>),
|
||||
application:set_env(eventhub, email_http_deliver,
|
||||
fun(Key, From, To, Subject, Plain, Html) ->
|
||||
Self ! {http, Key, From, To, Subject, Plain, Html},
|
||||
ok
|
||||
end),
|
||||
?assertEqual(ok, logic_email:send_verification_email(<<"user@ex.com">>, <<"abc123">>)),
|
||||
receive
|
||||
{http, Key, From, To, Subject, Plain, Html} ->
|
||||
?assertEqual(<<"test-key">>, Key),
|
||||
?assertEqual(<<"noreply@calentiq.com">>, From),
|
||||
?assertEqual(<<"user@ex.com">>, To),
|
||||
?assert(binary:match(Subject, <<"CalenTIQ">>) =/= nomatch),
|
||||
?assert(binary:match(Plain, <<"/verify?token=abc123">>) =/= nomatch),
|
||||
?assert(binary:match(Html, <<"/verify?token=abc123">>) =/= nomatch)
|
||||
after 1000 ->
|
||||
error(timeout)
|
||||
end.
|
||||
|
||||
test_http_api_error() ->
|
||||
application:set_env(eventhub, email_transport, http_api),
|
||||
application:set_env(eventhub, email_api_key, <<"k">>),
|
||||
application:set_env(eventhub, email_http_deliver, fun(_, _, _, _, _, _) -> {error, boom} end),
|
||||
?assertEqual(ok, logic_email:send_verification_email(<<"a@b.c">>, <<"t">>)),
|
||||
application:set_env(eventhub, email_http_deliver, fun(_, _, _, _, _, _) -> error(crash) end),
|
||||
?assertEqual(ok, logic_email:send_password_reset(<<"a@b.c">>, <<"t">>)).
|
||||
|
||||
Reference in New Issue
Block a user