Перенести все админские эндпоинты на порт 8445 и добавить отдельную авторизацию для админов. Часть 2. Final #3

This commit is contained in:
2026-04-28 12:42:10 +03:00
parent 4ed6a961ab
commit 7ea4efd7d9
38 changed files with 1252 additions and 1124 deletions

View File

@@ -29,6 +29,7 @@ admin_reports_test_() ->
{"POST /admin/reports method not allowed", fun test_wrong_method/0}
]}.
%% GET успех
test_list_reports() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(handler_auth, authenticate,
@@ -42,17 +43,18 @@ test_list_reports() ->
target_type = <<"event">>,
target_id = <<"e1">>,
reason = <<"spam">>,
status = <<"new">>,
status = pending,
created_at = {{2026,4,26},{12,0,0}},
resolved_at = undefined
},
ok = meck:expect(core_report, list_reports, fun() -> [Report] end),
% list_all возвращает {ok, List}
ok = meck:expect(core_report, list_all, fun() -> {ok, [Report]} end),
{ok, _, _} = admin_handler_reports:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
?assertEqual(200, Status),
[#{<<"id">> := <<"r1">>, <<"target_type">> := <<"event">>, <<"status">> := <<"new">>}]
= jsx:decode(RespBody, [return_maps]).
[#{<<"id">> := <<"r1">>, <<"status">> := <<"pending">>}] = jsx:decode(RespBody, [return_maps]).
%% GET запрещён
test_list_reports_forbidden() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(handler_auth, authenticate,
@@ -62,6 +64,7 @@ test_list_reports_forbidden() ->
?assertEqual(403, Status),
#{<<"error">> := <<"Admin access required">>} = jsx:decode(RespBody, [return_maps]).
%% PUT успех
test_update_report() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"PUT">> end),
ok = meck:expect(handler_auth, authenticate,
@@ -73,14 +76,16 @@ test_update_report() ->
fun(id, _) -> <<"r1">> end),
ok = meck:expect(cowboy_req, read_body,
fun(Req) -> {ok, jsx:encode(#{<<"status">> => <<"reviewed">>}), Req} end),
Updated = #report{id = <<"r1">>, status = <<"reviewed">>},
Updated = #report{id = <<"r1">>, status = reviewed},
% обработчик передаёт бинарный статус, поэтому мок ожидает строку
ok = meck:expect(core_report, update_status,
fun(<<"r1">>, <<"reviewed">>) -> {ok, Updated} end),
fun(<<"r1">>, <<"reviewed">>, <<"adm1">>) -> {ok, Updated} end),
{ok, _, _} = admin_handler_reports:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
?assertEqual(200, Status),
#{<<"status">> := <<"reviewed">>} = jsx:decode(RespBody, [return_maps]).
%% PUT невалидный JSON
test_update_report_bad_json() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"PUT">> end),
ok = meck:expect(handler_auth, authenticate,
@@ -93,9 +98,10 @@ test_update_report_bad_json() ->
ok = meck:expect(cowboy_req, read_body,
fun(Req) -> {ok, <<"bad json">>, Req} end),
{ok, _, _} = admin_handler_reports:init(req, []),
{Status, _, _, _} = erase(test_reply), %% исправлено: четыре элемента
{Status, _, _, _} = erase(test_reply),
?assertEqual(400, Status).
%% PUT не найдено
test_update_report_not_found() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"PUT">> end),
ok = meck:expect(handler_auth, authenticate,
@@ -108,11 +114,12 @@ test_update_report_not_found() ->
ok = meck:expect(cowboy_req, read_body,
fun(Req) -> {ok, jsx:encode(#{<<"status">> => <<"reviewed">>}), Req} end),
ok = meck:expect(core_report, update_status,
fun(_, _) -> {error, not_found} end),
fun(<<"r99">>, <<"reviewed">>, <<"adm1">>) -> {error, not_found} end),
{ok, _, _} = admin_handler_reports:init(req, []),
{Status, _, _, _} = erase(test_reply), %% исправлено: четыре элемента
{Status, _, _, _} = erase(test_reply),
?assertEqual(404, Status).
%% Неправильный метод
test_wrong_method() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"POST">> end),
{ok, _, _} = admin_handler_reports:init(req, []),