Перенести все админские эндпоинты на порт 8445 и добавить отдельную авторизацию для админов. Часть 1
This commit is contained in:
@@ -2,106 +2,88 @@
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-define(JWT_SECRET, <<"test-user-secret-key-32-byt!">>).
|
||||
-define(ADMIN_JWT_SECRET, <<"test-admin-secret-key-32-b">>).
|
||||
|
||||
setup() ->
|
||||
mnesia:start(),
|
||||
mnesia:create_table(user, [{attributes, record_info(fields, user)}, {ram_copies, [node()]}]),
|
||||
mnesia:create_table(calendar, [{attributes, record_info(fields, calendar)}, {ram_copies, [node()]}]),
|
||||
mnesia:create_table(event, [{attributes, record_info(fields, event)}, {ram_copies, [node()]}]),
|
||||
mnesia:create_table(booking, [{attributes, record_info(fields, booking)}, {ram_copies, [node()]}]),
|
||||
mnesia:create_table(review, [{attributes, record_info(fields, review)}, {ram_copies, [node()]}]),
|
||||
mnesia:create_table(report, [{attributes, record_info(fields, report)}, {ram_copies, [node()]}]),
|
||||
mnesia:create_table(ticket, [{attributes, record_info(fields, ticket)}, {ram_copies, [node()]}]),
|
||||
mnesia:create_table(subscription, [{attributes, record_info(fields, subscription)}, {ram_copies, [node()]}]),
|
||||
ok = meck:new(cowboy_req, [non_strict]),
|
||||
ok = meck:new(handler_auth, [non_strict]),
|
||||
ok = meck:new(core_user, [non_strict]),
|
||||
ok = meck:new(mnesia, [non_strict]),
|
||||
ok = meck:expect(mnesia, dirty_match_object, fun(_) -> [] end),
|
||||
application:set_env(eventhub, jwt_secret, ?JWT_SECRET),
|
||||
application:set_env(eventhub, admin_jwt_secret, ?ADMIN_JWT_SECRET),
|
||||
{ok, _} = application:ensure_all_started(jose),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
mnesia:delete_table(subscription),
|
||||
mnesia:delete_table(ticket),
|
||||
mnesia:delete_table(report),
|
||||
mnesia:delete_table(review),
|
||||
mnesia:delete_table(booking),
|
||||
mnesia:delete_table(event),
|
||||
mnesia:delete_table(calendar),
|
||||
mnesia:delete_table(user),
|
||||
mnesia:stop(),
|
||||
ok.
|
||||
application:unset_env(eventhub, jwt_secret),
|
||||
application:unset_env(eventhub, admin_jwt_secret),
|
||||
application:stop(jose),
|
||||
meck:unload(mnesia),
|
||||
meck:unload(core_user),
|
||||
meck:unload(handler_auth),
|
||||
meck:unload(cowboy_req).
|
||||
|
||||
admin_stats_test_() ->
|
||||
{foreach,
|
||||
fun setup/0,
|
||||
fun cleanup/1,
|
||||
[
|
||||
{"Count users", fun test_count_users/0},
|
||||
{"Count calendars", fun test_count_calendars/0},
|
||||
{"Count events", fun test_count_events/0},
|
||||
{"Count bookings", fun test_count_bookings/0},
|
||||
{"Count reviews", fun test_count_reviews/0},
|
||||
{"Count reports", fun test_count_reports/0},
|
||||
{"Count tickets", fun test_count_tickets/0},
|
||||
{"Count subscriptions", fun test_count_subscriptions/0}
|
||||
]}.
|
||||
{setup, fun setup/0, fun cleanup/1, [
|
||||
{"GET /admin/stats with admin role returns 200 and dashboard data",
|
||||
fun test_stats_admin/0},
|
||||
{"GET /admin/stats with non-admin role returns 403",
|
||||
fun test_stats_forbidden/0},
|
||||
{"POST /admin/stats returns 405",
|
||||
fun test_stats_wrong_method/0},
|
||||
{"Count functions return 0 with empty DB",
|
||||
fun test_count_functions/0}
|
||||
]}.
|
||||
|
||||
create_test_user() ->
|
||||
UserId = base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}),
|
||||
User = #user{id = UserId, email = <<UserId/binary, "@test.com">>, password_hash = <<"hash">>,
|
||||
role = user, status = active, created_at = calendar:universal_time(), updated_at = calendar:universal_time()},
|
||||
mnesia:dirty_write(User),
|
||||
UserId.
|
||||
%% ── Успешный GET с ролью админа ────────────────────────────
|
||||
test_stats_admin() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
|
||||
ok = meck:expect(handler_auth, authenticate,
|
||||
fun(Req) -> {ok, <<"adm1">>, Req} end),
|
||||
% Администратор с ролью superadmin
|
||||
AdminUser = #user{id = <<"adm1">>, role = superadmin, _ = '_'},
|
||||
ok = meck:expect(core_user, get_by_id,
|
||||
fun(<<"adm1">>) -> {ok, AdminUser} end),
|
||||
ok = meck:expect(cowboy_req, reply,
|
||||
fun(Code, Headers, Body, Req) ->
|
||||
put(test_reply, {Code, Headers, Body, Req})
|
||||
end),
|
||||
{ok, _, _} = admin_handler_stats:init(req, []),
|
||||
{Status, _, RespBody, _} = erase(test_reply),
|
||||
?assertEqual(200, Status),
|
||||
Stats = jsx:decode(RespBody, [return_maps]),
|
||||
?assert(is_map_key(<<"users">>, Stats)),
|
||||
?assert(is_map_key(<<"events">>, Stats)).
|
||||
|
||||
test_count_users() ->
|
||||
%% ── Обычный пользователь получает 403 ─────────────────────
|
||||
test_stats_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 = meck:expect(cowboy_req, reply,
|
||||
fun(Code, Headers, Body, Req) ->
|
||||
put(test_reply, {Code, Headers, Body, Req})
|
||||
end),
|
||||
{ok, _, _} = admin_handler_stats:init(req, []),
|
||||
{Status, _, RespBody, _} = erase(test_reply),
|
||||
?assertEqual(403, Status),
|
||||
?assertEqual(#{<<"error">> => <<"Admin access required">>}, jsx:decode(RespBody, [return_maps])).
|
||||
|
||||
%% ── Неверный метод ──────────────────────────────────────
|
||||
test_stats_wrong_method() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"POST">> end),
|
||||
ok = meck:expect(cowboy_req, reply,
|
||||
fun(Code, Headers, Body, Req) ->
|
||||
put(test_reply, {Code, Headers, Body, Req})
|
||||
end),
|
||||
{ok, _, _} = admin_handler_stats:init(req, []),
|
||||
{Status, _, RespBody, _} = erase(test_reply),
|
||||
?assertEqual(405, Status),
|
||||
?assertEqual(#{<<"error">> => <<"Method not allowed">>}, jsx:decode(RespBody, [return_maps])).
|
||||
|
||||
%% ── Функции подсчёта (мок mnesia) ──────────────────────
|
||||
test_count_functions() ->
|
||||
?assertEqual(0, admin_handler_stats:count_users()),
|
||||
create_test_user(),
|
||||
create_test_user(),
|
||||
?assertEqual(2, admin_handler_stats:count_users()).
|
||||
|
||||
test_count_calendars() ->
|
||||
?assertEqual(0, admin_handler_stats:count_calendars()),
|
||||
UserId = create_test_user(),
|
||||
core_calendar:create(UserId, <<"Cal1">>, <<"">>, manual),
|
||||
core_calendar:create(UserId, <<"Cal2">>, <<"">>, auto),
|
||||
?assertEqual(2, admin_handler_stats:count_calendars()).
|
||||
|
||||
test_count_events() ->
|
||||
?assertEqual(0, admin_handler_stats:count_events()),
|
||||
UserId = create_test_user(),
|
||||
{ok, Cal} = core_calendar:create(UserId, <<"Cal">>, <<"">>, manual),
|
||||
core_event:create(Cal#calendar.id, <<"Ev1">>, {{2026,6,1},{10,0,0}}, 60),
|
||||
core_event:create(Cal#calendar.id, <<"Ev2">>, {{2026,6,2},{10,0,0}}, 60),
|
||||
?assertEqual(2, admin_handler_stats:count_events()).
|
||||
|
||||
test_count_bookings() ->
|
||||
?assertEqual(0, admin_handler_stats:count_bookings()),
|
||||
UserId = create_test_user(),
|
||||
ParticipantId = create_test_user(),
|
||||
{ok, Cal} = core_calendar:create(UserId, <<"Cal">>, <<"">>, manual),
|
||||
{ok, Ev} = core_event:create(Cal#calendar.id, <<"Ev">>, {{2026,6,1},{10,0,0}}, 60),
|
||||
core_booking:create(Ev#event.id, ParticipantId),
|
||||
core_booking:create(Ev#event.id, ParticipantId),
|
||||
?assertEqual(2, admin_handler_stats:count_bookings()).
|
||||
|
||||
test_count_reviews() ->
|
||||
?assertEqual(0, admin_handler_stats:count_reviews()),
|
||||
UserId = create_test_user(),
|
||||
core_review:create(UserId, calendar, <<"cal1">>, 5, <<"Great">>),
|
||||
core_review:create(UserId, event, <<"ev1">>, 4, <<"Good">>),
|
||||
?assertEqual(2, admin_handler_stats:count_reviews()).
|
||||
|
||||
test_count_reports() ->
|
||||
?assertEqual(0, admin_handler_stats:count_reports()),
|
||||
UserId = create_test_user(),
|
||||
core_report:create(UserId, event, <<"ev1">>, <<"Bad">>),
|
||||
core_report:create(UserId, calendar, <<"cal1">>, <<"Spam">>),
|
||||
?assertEqual(2, admin_handler_stats:count_reports()).
|
||||
|
||||
test_count_tickets() ->
|
||||
?assertEqual(0, admin_handler_stats:count_tickets()),
|
||||
core_ticket:create_or_update(<<"Error1">>, <<"">>, #{}),
|
||||
core_ticket:create_or_update(<<"Error2">>, <<"">>, #{}),
|
||||
?assertEqual(2, admin_handler_stats:count_tickets()).
|
||||
|
||||
test_count_subscriptions() ->
|
||||
?assertEqual(0, admin_handler_stats:count_subscriptions()),
|
||||
UserId = create_test_user(),
|
||||
core_subscription:create(UserId, trial, false),
|
||||
core_subscription:create(UserId, monthly, true),
|
||||
?assertEqual(2, admin_handler_stats:count_subscriptions()).
|
||||
?assertEqual(0, admin_handler_stats:count_events()).
|
||||
Reference in New Issue
Block a user