-module(admin_handler_events_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_1">>). 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_events_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"GET list – success", {timeout, 60, fun test_list/0}}, {"GET list – filter by title", {timeout, 60, fun test_list_filter/0}}, {"GET – unauthorized", {timeout, 60, fun test_unauthorized/0}}, {"POST – method not allowed", {timeout, 60, fun test_wrong_method/0}} ]}. event_start() -> {{Y, M, D}, _} = eh_test_support:days_from_now(7), {{Y, M, D}, {12, 0, 0}}. seed_events() -> {ok, Cal} = core_calendar:create(?OWNER_ID, <<"Cal">>, <<"">>, auto), CalId = Cal#calendar.id, Start = event_start(), {ok, E1} = core_event:create(CalId, <<"Meetup">>, Start, 60), {ok, E2} = core_event:create(CalId, <<"Workshop">>, Start, 90), {E1, E2}. test_list() -> seed_events(), {Status, _, Body} = eh_test_support:call(admin_handler_events, #{ method => <<"GET">>, path => <<"/v1/admin/events">>, auth => ?ADMIN_ID }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(2, length(Result)). test_list_filter() -> seed_events(), {Status, _, Body} = eh_test_support:call(admin_handler_events, #{ method => <<"GET">>, path => <<"/v1/admin/events">>, auth => ?ADMIN_ID, qs => [{<<"title">>, <<"Meetup">>}] }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(1, length(Result)), [Only] = Result, ?assertEqual(<<"Meetup">>, maps:get(<<"title">>, Only)). test_unauthorized() -> {Status, _, _} = eh_test_support:call(admin_handler_events, #{ method => <<"GET">>, path => <<"/v1/admin/events">>, auth => none }), ?assertEqual(401, Status). test_wrong_method() -> {Status, _, _} = eh_test_support:call(admin_handler_events, #{ method => <<"POST">>, path => <<"/v1/admin/events">>, auth => ?ADMIN_ID }), ?assertEqual(405, Status).