Rewrite and expand EUnit suite; run unit tests in CI from shared test image. Refs EventHub/EventHubBack#36 [skip ci]

This commit is contained in:
2026-07-17 15:16:46 +03:00
parent 9d2780fef4
commit 4fa802702f
54 changed files with 4240 additions and 2281 deletions
+60 -86
View File
@@ -2,101 +2,75 @@
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
-define(TABLES, [admin, admin_audit, user, event, calendar, review, report,
ticket, subscription]).
-define(ADMIN_ID, <<"adm_test_1">>).
setup() ->
ok = meck:new(cowboy_req, [non_strict]),
ok = meck:new(handler_auth, [non_strict]),
ok = meck:new(admin_utils, [non_strict]),
ok = meck:new(core_admin, [non_strict]),
ok = meck:new(logic_stats, [non_strict]),
ok = meck:expect(cowboy_req, reply,
fun(Code, _, _, _) -> put(test_reply, Code) end),
eh_test_support:start_mnesia(),
eh_test_support:ensure_tables(?TABLES),
eh_test_support:ensure_jwt(),
eh_test_support:seed_admin(#{id => ?ADMIN_ID, role => admin}),
ok.
cleanup(_) ->
meck:unload(logic_stats),
meck:unload(core_admin),
meck:unload(admin_utils),
meck:unload(handler_auth),
meck:unload(cowboy_req).
eh_test_support:unload_cowboy(),
eh_test_support:delete_tables(?TABLES),
eh_test_support:stop_mnesia(),
ok.
admin_stats_test_() ->
{setup, fun setup/0, fun cleanup/1, [
{"GET /admin/stats as superadmin returns 200 with system metrics", fun test_superadmin/0},
{"GET /admin/stats as superadmin with date filter", fun test_superadmin_dates/0},
{"GET /admin/stats as moderator returns 200 with own metrics", fun test_moderator/0},
{"GET /admin/stats as support returns 200 with assigned tickets", fun test_support/0},
{"GET /admin/stats with nonadmin token returns 403", fun test_forbidden/0},
{"POST /admin/stats returns 405", fun test_wrong_method/0}
{foreach, fun setup/0, fun cleanup/1, [
{"GET stats success empty", fun test_stats_empty/0},
{"GET stats with seeded data", fun test_stats_with_data/0},
{"GET unauthorized", fun test_unauthorized/0},
{"POST method not allowed", fun test_wrong_method/0}
]}.
%% --- Суперадмин (без дат) ---
test_superadmin() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(cowboy_req, parse_qs, fun(_) -> [] end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"adm1">>, Req} end),
ok = meck:expect(admin_utils, is_admin, fun(_) -> true end),
ok = meck:expect(core_admin, get_by_id,
fun(<<"adm1">>) -> {ok, #admin{id = <<"adm1">>, role = superadmin}} end),
ok = meck:expect(logic_stats, get_stats,
fun(superadmin, _) -> #{users => 10, events => 25} end),
{ok, _, _} = admin_handler_stats:init(req, []),
?assertEqual(200, erase(test_reply)).
test_stats_empty() ->
{Status, _, Body} = eh_test_support:call(admin_handler_stats, #{
method => <<"GET">>,
path => <<"/v1/admin/stats">>,
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual(0, maps:get(<<"users_total">>, Result)),
?assertEqual(0, maps:get(<<"events_total">>, Result)),
?assertEqual(0, maps:get(<<"tickets_total">>, Result)),
?assert(is_map_key(<<"registrations_by_day">>, Result)).
%% --- Суперадмин (с датами) ---
test_superadmin_dates() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(cowboy_req, parse_qs, fun(_) ->
[{<<"from">>, <<"2026-01-01T00:00:00">>}, {<<"to">>, <<"2026-06-01T00:00:00">>}]
end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"adm1">>, Req} end),
ok = meck:expect(admin_utils, is_admin, fun(_) -> true end),
ok = meck:expect(core_admin, get_by_id,
fun(<<"adm1">>) -> {ok, #admin{id = <<"adm1">>, role = superadmin}} end),
ok = meck:expect(logic_stats, get_stats,
fun(superadmin, _, _, _) -> #{users => 10} end),
{ok, _, _} = admin_handler_stats:init(req, []),
?assertEqual(200, erase(test_reply)).
test_stats_with_data() ->
ok = mnesia:dirty_write(eh_test_support:make_user(#{
id => <<"u1">>,
email => <<"u1@test.local">>,
status => active
})),
{ok, _} = core_report:create(<<"u1">>, calendar, <<"c1">>, <<"spam">>),
{ok, _} = core_ticket:create(<<"u1">>, <<"hash1">>, <<"boom">>, <<"stack">>),
{Status, _, Body} = eh_test_support:call(admin_handler_stats, #{
method => <<"GET">>,
path => <<"/v1/admin/stats">>,
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual(1, maps:get(<<"users_total">>, Result)),
?assertEqual(1, maps:get(<<"reports_total">>, Result)),
?assertEqual(1, maps:get(<<"tickets_total">>, Result)).
%% --- Модератор ---
test_moderator() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(cowboy_req, parse_qs, fun(_) -> [] end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"mod1">>, Req} end),
ok = meck:expect(admin_utils, is_admin, fun(_) -> true end),
ok = meck:expect(core_admin, get_by_id,
fun(<<"mod1">>) -> {ok, #admin{id = <<"mod1">>, role = moderator}} end),
ok = meck:expect(logic_stats, get_stats,
fun(moderator, _) -> #{reports_reviewed => 5} end),
{ok, _, _} = admin_handler_stats:init(req, []),
?assertEqual(200, erase(test_reply)).
test_unauthorized() ->
{Status, _, _} = eh_test_support:call(admin_handler_stats, #{
method => <<"GET">>,
path => <<"/v1/admin/stats">>,
auth => none
}),
?assertEqual(401, Status).
%% --- Поддержка ---
test_support() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(cowboy_req, parse_qs, fun(_) -> [] end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"sup1">>, Req} end),
ok = meck:expect(admin_utils, is_admin, fun(_) -> true end),
ok = meck:expect(core_admin, get_by_id,
fun(<<"sup1">>) -> {ok, #admin{id = <<"sup1">>, role = support}} end),
ok = meck:expect(logic_stats, get_stats,
fun(support, _) -> #{tickets_assigned => 3} end),
{ok, _, _} = admin_handler_stats:init(req, []),
?assertEqual(200, erase(test_reply)).
%% --- Не админ ---
test_forbidden() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {error, 403, <<"Admin access required">>, Req} end),
{ok, _, _} = admin_handler_stats:init(req, []),
?assertEqual(403, erase(test_reply)).
%% --- Неверный метод ---
test_wrong_method() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"POST">> end),
{ok, _, _} = admin_handler_stats:init(req, []),
?assertEqual(405, erase(test_reply)).
{Status, _, _} = eh_test_support:call(admin_handler_stats, #{
method => <<"POST">>,
path => <<"/v1/admin/stats">>,
auth => ?ADMIN_ID
}),
?assertEqual(405, Status).