-module(logic_owner_specialist_tests). -include_lib("eunit/include/eunit.hrl"). -include("records.hrl"). -define(TABLES, [user, calendar, calendar_specialist, subscription, event]). 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_owner_specialist_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"create commercial ensures owner specialist active", fun test_ensure_on_create/0}, {"ensure is idempotent", fun test_ensure_idempotent/0}, {"DELETE owner specialist protected", fun test_remove_owner_forbidden/0}, {"owner can update name/status", fun test_owner_update/0}, {"specialist_id=owner ok when active, invalid when inactive", fun test_owner_specialist_id/0} ]}. seed_owner() -> Id = base64:encode(crypto:strong_rand_bytes(12), #{padding => false}), Owner = eh_test_support:make_user(#{ id => Id, email => <<"owner-", Id/binary, "@ex.com">>, nickname => <<"SoloNick">>, status => active}), mnesia:dirty_write(Owner), {ok, _} = logic_subscription:start_trial(Id), Id. test_ensure_on_create() -> OwnerId = seed_owner(), {ok, Cal} = logic_calendar:create_calendar(OwnerId, <<"Studio">>, <<>>, manual, commercial), ?assert(core_calendar_specialist:is_active_specialist(Cal#calendar.id, OwnerId)), {ok, Spec} = core_calendar_specialist:get_by_calendar_and_user(Cal#calendar.id, OwnerId), ?assertEqual(<<"SoloNick">>, Spec#calendar_specialist.name), ?assertEqual(active, Spec#calendar_specialist.status). test_ensure_idempotent() -> OwnerId = seed_owner(), {ok, Cal} = logic_calendar:create_calendar(OwnerId, <<"Studio">>, <<>>, manual, commercial), ?assertEqual(ok, logic_calendar_specialist:ensure_owner_specialist(Cal)), Specs = core_calendar_specialist:list_by_calendar(Cal#calendar.id), OwnerSpecs = [S || S <- Specs, S#calendar_specialist.user_id =:= OwnerId], ?assertEqual(1, length(OwnerSpecs)). test_remove_owner_forbidden() -> OwnerId = seed_owner(), {ok, Cal} = logic_calendar:create_calendar(OwnerId, <<"Studio">>, <<>>, manual, commercial), ?assertEqual({error, owner_specialist_protected}, logic_calendar_specialist:remove(OwnerId, Cal#calendar.id, OwnerId)), ?assert(core_calendar_specialist:is_active_specialist(Cal#calendar.id, OwnerId)). test_owner_update() -> OwnerId = seed_owner(), {ok, Cal} = logic_calendar:create_calendar(OwnerId, <<"Studio">>, <<>>, manual, commercial), {ok, Updated} = logic_calendar_specialist:update(OwnerId, Cal#calendar.id, OwnerId, [{name, <<"Master">>}, {specialization, [<<"cut">>]}, {status, inactive}]), ?assertEqual(<<"Master">>, Updated#calendar_specialist.name), ?assertEqual([<<"cut">>], Updated#calendar_specialist.specialization), ?assertEqual(inactive, Updated#calendar_specialist.status), ?assertNot(core_calendar_specialist:is_active_specialist(Cal#calendar.id, OwnerId)), {ok, ActiveAgain} = logic_calendar_specialist:update(OwnerId, Cal#calendar.id, OwnerId, [{status, active}]), ?assertEqual(active, ActiveAgain#calendar_specialist.status). test_owner_specialist_id() -> OwnerId = seed_owner(), {ok, Cal} = logic_calendar:create_calendar(OwnerId, <<"Studio">>, <<>>, manual, commercial), Start = eh_test_support:future_start(), {ok, Event} = logic_event:create_event(OwnerId, Cal#calendar.id, <<"Slot">>, Start, 60), {ok, WithOwner} = logic_event:update_event(OwnerId, Event#event.id, [{specialist_id, OwnerId}]), ?assertEqual(OwnerId, WithOwner#event.specialist_id), {ok, _} = logic_calendar_specialist:update(OwnerId, Cal#calendar.id, OwnerId, [{status, inactive}]), ?assertMatch({error, invalid_specialist}, logic_event:update_event(OwnerId, Event#event.id, [{specialist_id, OwnerId}])).