feat(email): SMTP via gen_smtp and CalenTIQ HTML templates. Refs EventHub/EventHubBack#68

This commit is contained in:
2026-08-14 18:51:09 +03:00
parent 3d7ae0d372
commit 69ee037d45
9 changed files with 369 additions and 6 deletions
+83
View File
@@ -0,0 +1,83 @@
-module(logic_email_tests).
-include_lib("eunit/include/eunit.hrl").
logic_email_test_() ->
{foreach, fun setup/0, fun cleanup/1, [
{"log-only without host", fun test_log_only/0},
{"mock deliver gets CalenTIQ + verify url", fun test_verify_deliver/0},
{"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}
]}.
setup() ->
application:unset_env(eventhub, smtp_host),
application:unset_env(eventhub, smtp_deliver),
ok.
cleanup(_) ->
application:unset_env(eventhub, smtp_host),
application:unset_env(eventhub, smtp_deliver),
ok.
test_log_only() ->
application:unset_env(eventhub, smtp_host),
?assertEqual(ok, logic_email:send_verification_email(<<"a@b.c">>, <<"tok">>)).
test_verify_deliver() ->
Self = self(),
application:set_env(eventhub, smtp_host, "smtp.test"),
application:set_env(eventhub, smtp_deliver,
fun(From, To, Raw, Opts) ->
Self ! {smtp, From, To, Raw, Opts},
{ok, mock}
end),
?assertEqual(ok, logic_email:send_verification_email(<<"user@ex.com">>, <<"abc123">>)),
receive
{smtp, From, To, Raw, Opts} ->
?assertEqual(<<"noreply@calentiq.com">>, From),
?assertEqual(<<"user@ex.com">>, To),
?assertEqual("smtp.test", proplists:get_value(relay, Opts)),
?assert(binary:match(Raw, <<"multipart/alternative">>) =/= nomatch),
?assert(binary:match(Raw, <<"text/html">>) =/= nomatch),
?assert(binary:match(Raw, <<"CalenTIQ">>) =/= nomatch),
?assert(binary:match(Raw, <<"/verify?token=abc123">>) =/= nomatch),
?assert(binary:match(Raw, <<"#FF6A3D">>) =/= nomatch)
after 1000 ->
error(timeout)
end.
test_invite_reset() ->
Self = self(),
application:set_env(eventhub, smtp_host, <<"smtp.test">>),
application:set_env(eventhub, smtp_deliver,
fun(_From, To, Raw, _Opts) ->
Self ! {smtp, To, Raw},
ok
end),
ok = logic_email:send_specialist_invite(<<"i@ex.com">>, <<"invtok">>),
receive
{smtp, <<"i@ex.com">>, Raw1} ->
?assert(binary:match(Raw1, <<"/invites?token=invtok">>) =/= nomatch)
after 1000 -> error(timeout)
end,
ok = logic_email:send_password_reset(<<"r@ex.com">>, <<"rst">>),
receive
{smtp, <<"r@ex.com">>, Raw2} ->
?assert(binary:match(Raw2, <<"/reset-password?token=rst">>) =/= nomatch)
after 1000 -> error(timeout)
end.
test_deliver_error() ->
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">>)),
application:set_env(eventhub, smtp_deliver, fun(_, _, _, _) -> error(crash) end),
?assertEqual(ok, logic_email:send_password_reset(<<"a@b.c">>, <<"t">>)).
test_template_fill() ->
{Sub, Plain, Html} = logic_email_templates:render(verify, <<"https://x/verify?token=z">>),
?assert(binary:match(Sub, <<"CalenTIQ">>) =/= nomatch),
?assert(binary:match(Plain, <<"https://x/verify?token=z">>) =/= nomatch),
?assert(binary:match(Html, <<"{{">>) =:= nomatch),
?assert(binary:match(Html, <<"https://x/verify?token=z">>) =/= nomatch).