-module(admin_handler_reports_tests). -include_lib("eunit/include/eunit.hrl"). -include("records.hrl"). -define(TABLES, [admin, admin_audit, report, ticket]). -define(ADMIN_ID, <<"adm_test_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_reports_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"GET list – success", fun test_list/0}, {"GET list – filter by status", fun test_list_filter/0}, {"GET list – unauthorized", fun test_list_unauthorized/0}, {"POST – method not allowed", fun test_wrong_method/0} ]}. test_list() -> {ok, _} = core_report:create(<<"u1">>, calendar, <<"cal1">>, <<"spam">>), {ok, _} = core_report:create(<<"u2">>, event, <<"ev1">>, <<"abuse">>), {Status, _, Body} = eh_test_support:call(admin_handler_reports, #{ method => <<"GET">>, path => <<"/v1/admin/reports">>, auth => ?ADMIN_ID }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(2, length(Result)). test_list_filter() -> {ok, _} = core_report:create(<<"u1">>, calendar, <<"cal1">>, <<"spam">>), {ok, R} = core_report:create(<<"u2">>, event, <<"ev1">>, <<"abuse">>), {ok, _} = core_report:update_status(R#report.id, reviewed, ?ADMIN_ID), {Status, _, Body} = eh_test_support:call(admin_handler_reports, #{ method => <<"GET">>, path => <<"/v1/admin/reports">>, auth => ?ADMIN_ID, qs => [{<<"status">>, <<"pending">>}] }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(1, length(Result)), [Only] = Result, ?assertEqual(<<"pending">>, maps:get(<<"status">>, Only)). test_list_unauthorized() -> {Status, _, _} = eh_test_support:call(admin_handler_reports, #{ method => <<"GET">>, path => <<"/v1/admin/reports">>, auth => none }), ?assertEqual(401, Status). test_wrong_method() -> {Status, _, _} = eh_test_support:call(admin_handler_reports, #{ method => <<"POST">>, path => <<"/v1/admin/reports">>, auth => ?ADMIN_ID }), ?assertEqual(405, Status).