Статистика для дашборда, расширенная #7

This commit is contained in:
2026-04-28 23:04:17 +03:00
parent c87d56bb49
commit 3da4ee28d4
11 changed files with 261 additions and 70 deletions

View File

@@ -22,15 +22,17 @@ cleanup(_) ->
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}
]}.
%% --- Суперадмин ---
%% --- Суперадмин (без дат) ---
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),
@@ -41,9 +43,26 @@ test_superadmin() ->
{ok, _, _} = admin_handler_stats:init(req, []),
?assertEqual(200, erase(test_reply)).
%% --- Суперадмин (с датами) ---
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_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),
@@ -57,6 +76,7 @@ test_moderator() ->
%% --- Поддержка ---
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),