Files
EventHubBack/test/unit/logic_specialist_invite_tests.erl
T
aleksey 571f04737d
CI / test (push) Successful in 6m47s
CI / deploy-ift (push) Successful in 3m23s
CI / e2e-ift (push) Successful in 1m58s
CI / deploy-stage (push) Successful in 2m1s
CI / e2e-stage (push) Successful in 1m13s
feat: specialist_invite API + user lookup. Fixes EventHub/EventHubBack#55
2026-07-22 18:52:35 +03:00

95 lines
4.0 KiB
Erlang
Executable File

-module(logic_specialist_invite_tests).
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
-define(TABLES, [user, calendar, calendar_specialist, specialist_invite, subscription, notification]).
setup() ->
eh_test_support:start_mnesia(),
eh_test_support:ensure_tables(?TABLES),
ok.
cleanup(_) ->
eh_test_support:delete_tables(?TABLES),
eh_test_support:stop_mnesia(),
ok.
logic_specialist_invite_test_() ->
{foreach, fun setup/0, fun cleanup/1, [
{"invite by user_id and accept", fun test_invite_accept/0},
{"invite by email unknown user", fun test_invite_email/0},
{"decline invite", fun test_decline/0},
{"duplicate pending", fun test_duplicate_pending/0},
{"lookup typeahead", fun test_lookup/0}
]}.
seed_owner_commercial() ->
Id = base64:encode(crypto:strong_rand_bytes(12), #{padding => false}),
Owner = eh_test_support:make_user(#{
id => Id, email => <<"owner-", Id/binary, "@ex.com">>, status => active}),
OwnerId = Owner#user.id,
mnesia:dirty_write(Owner),
{ok, _} = core_subscription:create(OwnerId, monthly, true),
{ok, Cal} = core_calendar:create(OwnerId, <<"Studio">>, <<>>, manual, commercial),
{OwnerId, Cal#calendar.id}.
make_active_user(Email, Nick) ->
Id = base64:encode(crypto:strong_rand_bytes(12), #{padding => false}),
U = eh_test_support:make_user(#{
id => Id, email => Email, status => active, nickname => Nick}),
mnesia:dirty_write(U),
U.
test_invite_accept() ->
{OwnerId, CalId} = seed_owner_commercial(),
Spec = make_active_user(<<"spec@ex.com">>, <<"speccy">>),
SpecId = Spec#user.id,
{ok, Inv} = logic_specialist_invite:create(OwnerId, CalId, #{user_id => SpecId},
#{name => <<"Doc">>, specialization => [<<"yoga">>]}),
?assertEqual(pending, Inv#specialist_invite.status),
?assertEqual(SpecId, Inv#specialist_invite.invitee_user_id),
{ok, Incoming} = logic_specialist_invite:list_incoming(SpecId),
?assertEqual(1, length(Incoming)),
{ok, Inv2, Specialist} = logic_specialist_invite:accept(SpecId, Inv#specialist_invite.id),
?assertEqual(accepted, Inv2#specialist_invite.status),
?assertEqual(active, Specialist#calendar_specialist.status),
?assert(core_calendar_specialist:is_active_specialist(CalId, SpecId)).
test_invite_email() ->
{OwnerId, CalId} = seed_owner_commercial(),
{ok, Inv} = logic_specialist_invite:create(OwnerId, CalId,
#{email => <<"new@ex.com">>}, #{name => <<"New">>}),
?assertEqual(<<>>, Inv#specialist_invite.invitee_user_id),
?assertEqual(<<"new@ex.com">>, Inv#specialist_invite.invitee_email),
User = make_active_user(<<"new@ex.com">>, <<"newbie">>),
{ok, _, Spec} = logic_specialist_invite:accept(User#user.id, Inv#specialist_invite.id),
?assertEqual(User#user.id, Spec#calendar_specialist.user_id).
test_decline() ->
{OwnerId, CalId} = seed_owner_commercial(),
Spec = make_active_user(<<"d@ex.com">>, <<"dee">>),
{ok, Inv} = logic_specialist_invite:create(OwnerId, CalId, #{user_id => Spec#user.id}, #{}),
{ok, Inv2} = logic_specialist_invite:decline(Spec#user.id, Inv#specialist_invite.id),
?assertEqual(declined, Inv2#specialist_invite.status),
?assertNot(core_calendar_specialist:is_active_specialist(CalId, Spec#user.id)).
test_duplicate_pending() ->
{OwnerId, CalId} = seed_owner_commercial(),
Spec = make_active_user(<<"dup@ex.com">>, <<"dup">>),
{ok, _} = logic_specialist_invite:create(OwnerId, CalId, #{user_id => Spec#user.id}, #{}),
?assertEqual({error, already_pending},
logic_specialist_invite:create(OwnerId, CalId, #{user_id => Spec#user.id}, #{})).
test_lookup() ->
_ = make_active_user(<<"alice@ex.com">>, <<"alice">>),
_ = make_active_user(<<"bob@ex.com">>, <<"bobby">>),
{ok, Exact} = logic_user_lookup:lookup(<<"alice@ex.com">>),
?assertEqual(1, length(Exact)),
[A] = Exact,
?assertEqual(<<"alice@ex.com">>, maps:get(email, A)),
{ok, Pref} = logic_user_lookup:lookup(<<"bob">>),
?assert(length(Pref) >= 1),
[B | _] = Pref,
?assertEqual(<<"b***@ex.com">>, maps:get(email, B)),
?assertEqual({error, bad_request}, logic_user_lookup:lookup(<<"x">>)).