-module(admin_handler_reviews_tests). -include_lib("eunit/include/eunit.hrl"). -include("records.hrl"). -define(TABLES, [admin, admin_audit, review, 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_reviews_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"GET list – success", fun test_list/0}, {"PATCH bulk – success", fun test_bulk/0}, {"PATCH bulk – missing reason", fun test_bulk_missing/0}, {"GET – unauthorized", fun test_unauthorized/0}, {"POST – method not allowed", fun test_wrong_method/0} ]}. seed_review() -> {ok, Review} = core_review:create(<<"u1">>, calendar, <<"cal1">>, 5, <<"great">>), Review. test_list() -> _ = seed_review(), {ok, _} = core_review:create(<<"u2">>, event, <<"ev1">>, 3, <<"ok">>), {Status, _, Body} = eh_test_support:call(admin_handler_reviews, #{ method => <<"GET">>, path => <<"/v1/admin/reviews">>, auth => ?ADMIN_ID }), ?assertEqual(200, Status), Result = jsx:decode(Body, [return_maps]), ?assertEqual(2, length(Result)). test_bulk() -> Review = seed_review(), Id = Review#review.id, {Status, _, Body} = eh_test_support:call(admin_handler_reviews, #{ method => <<"PATCH">>, path => <<"/v1/admin/reviews">>, auth => ?ADMIN_ID, body => jsx:encode(#{ <<"operations">> => [#{<<"id">> => Id, <<"status">> => <<"hidden">>}], <<"reason">> => <<"moderation">> }) }), ?assertEqual(200, Status), #{<<"updated_count">> := 1} = jsx:decode(Body, [return_maps]), Audits = core_admin_audit:list(), ?assert(length(Audits) >= 1). test_bulk_missing() -> {Status, _, _} = eh_test_support:call(admin_handler_reviews, #{ method => <<"PATCH">>, path => <<"/v1/admin/reviews">>, auth => ?ADMIN_ID, body => jsx:encode(#{<<"operations">> => []}) }), ?assertEqual(400, Status). test_unauthorized() -> {Status, _, _} = eh_test_support:call(admin_handler_reviews, #{ method => <<"GET">>, path => <<"/v1/admin/reviews">>, auth => none }), ?assertEqual(401, Status). test_wrong_method() -> {Status, _, _} = eh_test_support:call(admin_handler_reviews, #{ method => <<"POST">>, path => <<"/v1/admin/reviews">>, auth => ?ADMIN_ID }), ?assertEqual(405, Status).