-module(admin_handler_event_by_id_tests). -include_lib("eunit/include/eunit.hrl"). -include("records.hrl"). -define(TABLES, [admin, admin_audit, calendar, event, ticket]). -define(ADMIN_ID, <<"adm_test_1">>). -define(OWNER_ID, <<"owner_evt_by_id">>). setup() -> eh_test_support:start_mnesia(), eh_test_support:ensure_tables(?TABLES), eh_test_support:ensure_jwt(), eh_test_support:seed_admin(#{id => ?ADMIN_ID}), ok. cleanup(_) -> eh_test_support:unload_cowboy(), eh_test_support:delete_tables(?TABLES), eh_test_support:stop_mnesia(), ok. admin_event_by_id_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"GET – success", {timeout, 60, fun test_get/0}}, {"GET – not found", {timeout, 60, fun test_get_not_found/0}}, {"PUT – update title/status", {timeout, 60, fun test_update/0}}, {"PUT – not found", {timeout, 60, fun test_update_not_found/0}}, {"DELETE – soft delete", {timeout, 60, fun test_delete/0}}, {"DELETE – not found", {timeout, 60, fun test_delete_not_found/0}}, {"GET – unauthorized", {timeout, 60, fun test_unauthorized/0}}, {"PATCH – method not allowed", {timeout, 60, fun test_wrong_method/0}} ]}. seed_event() -> {ok, Cal} = core_calendar:create(?OWNER_ID, <<"Cal">>, <<"">>, auto), {ok, Event} = core_event:create( Cal#calendar.id, <<"Target Event">>, eh_test_support:future_start(), 60 ), Event. test_get() -> Event = seed_event(), EventId = Event#event.id, {Status, _, Body} = eh_test_support:call(admin_handler_event_by_id, #{ method => <<"GET">>, path => <<"/v1/admin/events/", EventId/binary>>, bindings => #{id => EventId}, auth => ?ADMIN_ID }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(EventId, maps:get(<<"id">>, Result)), ?assertEqual(<<"Target Event">>, maps:get(<<"title">>, Result)). test_get_not_found() -> {Status, _, _} = eh_test_support:call(admin_handler_event_by_id, #{ method => <<"GET">>, path => <<"/v1/admin/events/missing">>, bindings => #{id => <<"missing">>}, auth => ?ADMIN_ID }), ?assertEqual(404, Status). test_update() -> Event = seed_event(), EventId = Event#event.id, {Status, _, Body} = eh_test_support:call(admin_handler_event_by_id, #{ method => <<"PUT">>, path => <<"/v1/admin/events/", EventId/binary>>, bindings => #{id => EventId}, auth => ?ADMIN_ID, body => jsx:encode(#{ <<"title">> => <<"Updated Event">>, <<"status">> => <<"cancelled">> }) }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(<<"Updated Event">>, maps:get(<<"title">>, Result)), ?assertEqual(<<"cancelled">>, maps:get(<<"status">>, Result)), Audits = core_admin_audit:list(), ?assert(length(Audits) >= 1). test_update_not_found() -> {Status, _, _} = eh_test_support:call(admin_handler_event_by_id, #{ method => <<"PUT">>, path => <<"/v1/admin/events/missing">>, bindings => #{id => <<"missing">>}, auth => ?ADMIN_ID, body => jsx:encode(#{<<"title">> => <<"X">>}) }), ?assertEqual(404, Status). test_delete() -> Event = seed_event(), EventId = Event#event.id, {Status, _, Body} = eh_test_support:call(admin_handler_event_by_id, #{ method => <<"DELETE">>, path => <<"/v1/admin/events/", EventId/binary>>, bindings => #{id => EventId}, auth => ?ADMIN_ID }), ?assertEqual(200, Status), #{<<"status">> := <<"deleted">>} = jsx:decode(Body, [return_maps]), {ok, Deleted} = core_event:get_by_id(EventId), ?assertEqual(deleted, Deleted#event.status), Audits = core_admin_audit:list(), ?assert(length(Audits) >= 1). test_delete_not_found() -> {Status, _, _} = eh_test_support:call(admin_handler_event_by_id, #{ method => <<"DELETE">>, path => <<"/v1/admin/events/missing">>, bindings => #{id => <<"missing">>}, auth => ?ADMIN_ID }), ?assertEqual(404, Status). test_unauthorized() -> Event = seed_event(), EventId = Event#event.id, {Status, _, _} = eh_test_support:call(admin_handler_event_by_id, #{ method => <<"GET">>, path => <<"/v1/admin/events/", EventId/binary>>, bindings => #{id => EventId}, auth => none }), ?assertEqual(401, Status). test_wrong_method() -> Event = seed_event(), EventId = Event#event.id, {Status, _, _} = eh_test_support:call(admin_handler_event_by_id, #{ method => <<"PATCH">>, path => <<"/v1/admin/events/", EventId/binary>>, bindings => #{id => EventId}, auth => ?ADMIN_ID }), ?assertEqual(405, Status).