-module(admin_handler_moderation_tests). -include_lib("eunit/include/eunit.hrl"). -include("records.hrl"). -define(TABLES, [admin, admin_audit, user, calendar, ticket]). -define(ADMIN_ID, <<"adm_test_1">>). -define(USER_ID, <<"user_mod_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 = mnesia:dirty_write(eh_test_support:make_user(#{ id => ?USER_ID, email => <<"user@test.local">>, status => active })), ok. cleanup(_) -> eh_test_support:unload_cowboy(), eh_test_support:delete_tables(?TABLES), eh_test_support:stop_mnesia(), ok. admin_moderation_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"PUT block user – success", fun test_block_user/0}, {"PUT block user – not found", fun test_block_user_not_found/0}, {"PUT freeze calendar – success", fun test_freeze_calendar/0}, {"PUT – missing action", fun test_missing_action/0}, {"PUT – invalid target", fun test_invalid_target/0}, {"PUT – unauthorized", fun test_unauthorized/0}, {"GET – method not allowed", fun test_wrong_method/0} ]}. test_block_user() -> {Status, _, Body} = eh_test_support:call(admin_handler_moderation, #{ method => <<"PUT">>, path => <<"/v1/admin/user/", ?USER_ID/binary>>, bindings => #{target_type => <<"user">>, id => ?USER_ID}, auth => ?ADMIN_ID, body => jsx:encode(#{<<"action">> => <<"block">>, <<"reason">> => <<"spam">>}) }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(?USER_ID, maps:get(<<"id">>, Result)), ?assertEqual(<<"blocked">>, maps:get(<<"status">>, Result)), Audits = core_admin_audit:list(), ?assert(length(Audits) >= 1). test_block_user_not_found() -> {Status, _, _} = eh_test_support:call(admin_handler_moderation, #{ method => <<"PUT">>, path => <<"/v1/admin/user/missing">>, bindings => #{target_type => <<"user">>, id => <<"missing">>}, auth => ?ADMIN_ID, body => jsx:encode(#{<<"action">> => <<"block">>, <<"reason">> => <<"x">>}) }), ?assertEqual(404, Status). test_freeze_calendar() -> {ok, Cal} = core_calendar:create(?USER_ID, <<"Cal">>, <<"desc">>, auto), CalId = Cal#calendar.id, {Status, _, Body} = eh_test_support:call(admin_handler_moderation, #{ method => <<"PUT">>, path => <<"/v1/admin/calendar/", CalId/binary>>, bindings => #{target_type => <<"calendar">>, id => CalId}, auth => ?ADMIN_ID, body => jsx:encode(#{<<"action">> => <<"freeze">>, <<"reason">> => <<"policy">>}) }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(CalId, maps:get(<<"id">>, Result)), ?assertEqual(<<"frozen">>, maps:get(<<"status">>, Result)). test_missing_action() -> {Status, _, _} = eh_test_support:call(admin_handler_moderation, #{ method => <<"PUT">>, path => <<"/v1/admin/user/", ?USER_ID/binary>>, bindings => #{target_type => <<"user">>, id => ?USER_ID}, auth => ?ADMIN_ID, body => jsx:encode(#{<<"reason">> => <<"x">>}) }), ?assertEqual(400, Status). test_invalid_target() -> {Status, _, _} = eh_test_support:call(admin_handler_moderation, #{ method => <<"PUT">>, path => <<"/v1/admin/unknown/x">>, bindings => #{target_type => <<"unknown">>, id => <<"x">>}, auth => ?ADMIN_ID, body => jsx:encode(#{<<"action">> => <<"block">>}) }), ?assertEqual(400, Status). test_unauthorized() -> {Status, _, _} = eh_test_support:call(admin_handler_moderation, #{ method => <<"PUT">>, path => <<"/v1/admin/user/", ?USER_ID/binary>>, bindings => #{target_type => <<"user">>, id => ?USER_ID}, auth => none, body => jsx:encode(#{<<"action">> => <<"block">>}) }), ?assertEqual(401, Status). test_wrong_method() -> {Status, _, _} = eh_test_support:call(admin_handler_moderation, #{ method => <<"GET">>, path => <<"/v1/admin/user/", ?USER_ID/binary>>, bindings => #{target_type => <<"user">>, id => ?USER_ID}, auth => ?ADMIN_ID }), ?assertEqual(405, Status).