Перенести все админские эндпоинты на порт 8445 и добавить отдельную авторизацию для админов. Часть 2. Final #3
This commit is contained in:
@@ -3,24 +3,154 @@
|
||||
|
||||
test() ->
|
||||
io:format("Testing admin panel API...~n"),
|
||||
AdminURL = "http://localhost:8445",
|
||||
|
||||
% Получаем admin-токен через test runner (уже проверенный)
|
||||
AdminToken = api_test_runner:get_admin_token(),
|
||||
|
||||
% TEST 1: Admin healthcheck
|
||||
io:format(" TEST 1: Admin healthcheck... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get, {"http://localhost:8445/admin/health", []}, [], []),
|
||||
%% TEST 1: Admin healthcheck (public)
|
||||
io:format(" TEST 1: Admin healthcheck... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get, {AdminURL ++ "/v1/admin/health", []}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Admin stats
|
||||
io:format(" TEST 2: Admin stats... "),
|
||||
%% TEST 2: Admin login (дополнительная проверка)
|
||||
io:format(" TEST 2: Admin login (attempt)... "),
|
||||
LoginBody = jsx:encode(#{<<"email">> => <<"global_admin@test.com">>, <<"password">> => <<"admin123">>}),
|
||||
case httpc:request(post, {AdminURL ++ "/v1/admin/login", [], "application/json", LoginBody}, [], []) of
|
||||
{ok, {{_, 200, _}, _, _}} ->
|
||||
io:format("OK (logged in)~n");
|
||||
_ ->
|
||||
io:format("SKIPPED (credentials not found, using runner token)~n")
|
||||
end,
|
||||
|
||||
%% TEST 3: Admin stats
|
||||
io:format(" TEST 3: Admin stats... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{"http://localhost:8445/admin/stats", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
{AdminURL ++ "/v1/admin/stats", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: List users
|
||||
io:format(" TEST 3: List users... "),
|
||||
%% TEST 4: List users
|
||||
io:format(" TEST 4: List users... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{"http://localhost:8445/admin/users", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
{AdminURL ++ "/v1/admin/users", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 5: Get user by ID
|
||||
io:format(" TEST 5: Get user by ID... "),
|
||||
UserId = api_test_runner:get_user_id(),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{AdminURL ++ "/v1/admin/users/" ++ binary_to_list(UserId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 6: List reports
|
||||
io:format(" TEST 6: List reports... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{AdminURL ++ "/v1/admin/reports", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 7: List banned words
|
||||
io:format(" TEST 7: List banned words... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{AdminURL ++ "/v1/admin/banned-words", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 8: Add banned word
|
||||
io:format(" TEST 8: Add banned word... "),
|
||||
BannedWordBody = jsx:encode(#{<<"word">> => <<"test_banned_word">>}),
|
||||
{ok, {{_, 201, _}, _, _}} = httpc:request(post,
|
||||
{AdminURL ++ "/v1/admin/banned-words", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}], "application/json", BannedWordBody}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 9: Delete banned word
|
||||
io:format(" TEST 9: Delete banned word... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(delete,
|
||||
{AdminURL ++ "/v1/admin/banned-words/test_banned_word", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 10: List tickets
|
||||
io:format(" TEST 10: List tickets... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{AdminURL ++ "/v1/admin/tickets", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 11: Create ticket
|
||||
io:format(" TEST 11: Create ticket... "),
|
||||
TicketBody = jsx:encode(#{<<"error_message">> => <<"Test error">>, <<"stacktrace">> => <<"trace">>}),
|
||||
{ok, {{_, 201, _}, _, TicketResp}} = httpc:request(post,
|
||||
{AdminURL ++ "/v1/admin/tickets", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}], "application/json", TicketBody}, [], []),
|
||||
#{<<"id">> := TicketId} = jsx:decode(list_to_binary(TicketResp), [return_maps]),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 12: Get ticket by ID
|
||||
io:format(" TEST 12: Get ticket by ID... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{AdminURL ++ "/v1/admin/tickets/" ++ binary_to_list(TicketId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 13: Update ticket
|
||||
io:format(" TEST 13: Update ticket... "),
|
||||
UpdateTicketBody = jsx:encode(#{<<"status">> => <<"closed">>}),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(put,
|
||||
{AdminURL ++ "/v1/admin/tickets/" ++ binary_to_list(TicketId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}], "application/json", UpdateTicketBody}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 14: Delete ticket
|
||||
io:format(" TEST 14: Delete ticket... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(delete,
|
||||
{AdminURL ++ "/v1/admin/tickets/" ++ binary_to_list(TicketId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 15: Ticket stats
|
||||
io:format(" TEST 15: Ticket stats... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{AdminURL ++ "/v1/admin/tickets/stats", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 16: List subscriptions
|
||||
io:format(" TEST 16: List subscriptions... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{AdminURL ++ "/v1/admin/subscriptions", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 17: Create subscription
|
||||
io:format(" TEST 17: Create subscription... "),
|
||||
SubBody = jsx:encode(#{<<"user_id">> => UserId, <<"plan">> => <<"monthly">>}),
|
||||
{ok, {{_, 201, _}, _, SubResp}} = httpc:request(post,
|
||||
{AdminURL ++ "/v1/admin/subscriptions", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}], "application/json", SubBody}, [], []),
|
||||
#{<<"id">> := SubId} = jsx:decode(list_to_binary(SubResp), [return_maps]),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 18: Get subscription by ID
|
||||
io:format(" TEST 18: Get subscription by ID... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{AdminURL ++ "/v1/admin/subscriptions/" ++ binary_to_list(SubId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 19: Update subscription
|
||||
io:format(" TEST 19: Update subscription... "),
|
||||
UpdateSubBody = jsx:encode(#{<<"status">> => <<"cancelled">>}),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(put,
|
||||
{AdminURL ++ "/v1/admin/subscriptions/" ++ binary_to_list(SubId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}], "application/json", UpdateSubBody}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 20: Delete subscription
|
||||
io:format(" TEST 20: Delete subscription... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(delete,
|
||||
{AdminURL ++ "/v1/admin/subscriptions/" ++ binary_to_list(SubId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 21: Moderation - block user
|
||||
io:format(" TEST 21: Moderation - block user... "),
|
||||
ModBody = jsx:encode(#{<<"action">> => <<"block">>}),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(put,
|
||||
{AdminURL ++ "/v1/admin/user/" ++ binary_to_list(UserId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}], "application/json", ModBody}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
%% TEST 22: Moderation - unblock user
|
||||
io:format(" TEST 22: Moderation - unblock user... "),
|
||||
UnblockBody = jsx:encode(#{<<"action">> => <<"unblock">>}),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(put,
|
||||
{AdminURL ++ "/v1/admin/user/" ++ binary_to_list(UserId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}], "application/json", UnblockBody}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Admin API tests passed!~n"),
|
||||
|
||||
@@ -2,52 +2,70 @@
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
-define(ADMIN_BASE_URL, "http://localhost:8445").
|
||||
|
||||
test() ->
|
||||
io:format("Testing moderation API...~n"),
|
||||
|
||||
AdminToken = api_test_runner:get_admin_token(),
|
||||
UserToken = api_test_runner:get_user_token(),
|
||||
UserToken = api_test_runner:get_user_token(),
|
||||
|
||||
% Создаём календарь и событие
|
||||
CalId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars", #{title => <<"Mod Cal">>}, UserToken), <<"id">>),
|
||||
%% Создаём календарь и событие через пользовательский API
|
||||
CalId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars", #{title => <<"Mod Cal">>}, UserToken),
|
||||
<<"id">>),
|
||||
EventId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events",
|
||||
#{title => <<"Mod Event">>, start_time => <<"2026-06-01T10:00:00Z">>, duration => 60}, UserToken), <<"id">>),
|
||||
#{title => <<"Mod Event">>,
|
||||
start_time => <<"2026-06-01T10:00:00Z">>,
|
||||
duration => 60},
|
||||
UserToken),
|
||||
<<"id">>),
|
||||
|
||||
% TEST 1: Create report
|
||||
%% TEST 1: Create report (пользователь)
|
||||
io:format(" TEST 1: Create report... "),
|
||||
ReportId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/reports",
|
||||
#{target_type => <<"event">>, target_id => EventId, reason => <<"Inappropriate">>}, UserToken), <<"id">>),
|
||||
#{target_type => <<"event">>,
|
||||
target_id => EventId,
|
||||
reason => <<"Inappropriate">>},
|
||||
UserToken),
|
||||
<<"id">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Admin views reports
|
||||
%% TEST 2: Admin views reports (через админский URL, прямой httpc)
|
||||
io:format(" TEST 2: Admin views reports... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/admin/reports", AdminToken),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/reports", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: Admin resolves report
|
||||
%% TEST 3: Admin resolves report
|
||||
io:format(" TEST 3: Admin resolves report... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_put("/v1/admin/reports/" ++ binary_to_list(ReportId),
|
||||
#{action => <<"review">>}, AdminToken),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(put,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/reports/" ++ binary_to_list(ReportId),
|
||||
[{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}],
|
||||
"application/json",
|
||||
jsx:encode(#{status => <<"reviewed">>})}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 4: Add banned word
|
||||
%% TEST 4: Add banned word (админ)
|
||||
io:format(" TEST 4: Add banned word... "),
|
||||
{ok, {{_, 201, _}, _, _}} = api_test_runner:http_post("/v1/admin/banned-words",
|
||||
#{word => <<"badword">>}, AdminToken),
|
||||
{ok, {{_, 201, _}, _, _}} = httpc:request(post,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/banned-words",
|
||||
[{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}],
|
||||
"application/json",
|
||||
jsx:encode(#{<<"word">> => <<"badword">>})}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 5: List banned words
|
||||
%% TEST 5: List banned words (админ)
|
||||
io:format(" TEST 5: List banned words... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/admin/banned-words", AdminToken),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/banned-words", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 6: Remove banned word
|
||||
%% TEST 6: Remove banned word (админ)
|
||||
io:format(" TEST 6: Remove banned word... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_delete("/v1/admin/banned-words/badword", AdminToken),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(delete,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/banned-words/badword", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Moderation API tests passed!~n"),
|
||||
|
||||
@@ -1,37 +1,78 @@
|
||||
-module(api_tickets_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
-define(ADMIN_BASE_URL, "http://localhost:8445").
|
||||
|
||||
test() ->
|
||||
io:format("Testing tickets API...~n"),
|
||||
|
||||
Token = api_test_runner:get_user_token(),
|
||||
AdminToken = api_test_runner:get_admin_token(),
|
||||
UserToken = api_test_runner:get_user_token(),
|
||||
|
||||
% TEST 1: Report error
|
||||
io:format(" TEST 1: Report error... "),
|
||||
%% TEST 1: Create ticket (user)
|
||||
io:format(" TEST 1: Create ticket...~n"),
|
||||
io:format(" POST /v1/tickets~n"),
|
||||
TicketId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/tickets",
|
||||
#{error_message => <<"Test bug">>, stacktrace => <<"line 1">>}, UserToken), <<"id">>),
|
||||
io:format("OK~n"),
|
||||
#{error_message => <<"Bug">>,
|
||||
stacktrace => <<"Something broke">>},
|
||||
Token),
|
||||
<<"id">>),
|
||||
io:format(" OK~n"),
|
||||
|
||||
% TEST 2: Admin views tickets
|
||||
io:format(" TEST 2: Admin views tickets... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/admin/tickets", AdminToken),
|
||||
io:format("OK~n"),
|
||||
%% TEST 2: Get my tickets (user)
|
||||
io:format(" TEST 2: Get my tickets...~n"),
|
||||
io:format(" GET /v1/tickets~n"),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/tickets", Token),
|
||||
io:format(" OK~n"),
|
||||
|
||||
% TEST 3: Update ticket status
|
||||
io:format(" TEST 3: Update ticket status... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_put("/v1/admin/tickets/" ++ binary_to_list(TicketId),
|
||||
#{action => <<"status">>, status => <<"in_progress">>}, AdminToken),
|
||||
io:format("OK~n"),
|
||||
%% TEST 3: Get single ticket (user)
|
||||
io:format(" TEST 3: Get single ticket...~n"),
|
||||
io:format(" GET /v1/tickets/~s~n", [TicketId]),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get(
|
||||
"/v1/tickets/" ++ binary_to_list(TicketId),
|
||||
Token),
|
||||
io:format(" OK~n"),
|
||||
|
||||
% TEST 4: Close ticket
|
||||
io:format(" TEST 4: Close ticket... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_put("/v1/admin/tickets/" ++ binary_to_list(TicketId),
|
||||
#{action => <<"close">>}, AdminToken),
|
||||
io:format("OK~n"),
|
||||
%% TEST 4: Admin lists all tickets
|
||||
io:format(" TEST 4: Admin lists all tickets...~n"),
|
||||
io:format(" GET ~s/v1/admin/tickets~n", [?ADMIN_BASE_URL]),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/tickets", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format(" OK~n"),
|
||||
|
||||
%% TEST 5: Admin updates ticket status
|
||||
io:format(" TEST 5: Admin updates ticket status...~n"),
|
||||
io:format(" PUT ~s/v1/admin/tickets/~s~n", [?ADMIN_BASE_URL, TicketId]),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(put,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/tickets/" ++ binary_to_list(TicketId),
|
||||
[{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}],
|
||||
"application/json",
|
||||
jsx:encode(#{status => <<"in_progress">>})}, [], []),
|
||||
io:format(" OK~n"),
|
||||
|
||||
%% TEST 6: Admin assigns ticket
|
||||
io:format(" TEST 6: Admin assigns ticket...~n"),
|
||||
io:format(" PUT ~s/v1/admin/tickets/~s~n", [?ADMIN_BASE_URL, TicketId]),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(put,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/tickets/" ++ binary_to_list(TicketId),
|
||||
[{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}],
|
||||
"application/json",
|
||||
jsx:encode(#{assigned_to => AdminToken})}, [], []),
|
||||
io:format(" OK~n"),
|
||||
|
||||
%% TEST 7: Admin views ticket stats
|
||||
io:format(" TEST 7: Admin views ticket stats...~n"),
|
||||
io:format(" GET ~s/v1/admin/tickets/stats~n", [?ADMIN_BASE_URL]),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/tickets/stats", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format(" OK~n"),
|
||||
|
||||
%% TEST 8: Admin deletes ticket
|
||||
io:format(" TEST 8: Admin deletes ticket...~n"),
|
||||
io:format(" DELETE ~s/v1/admin/tickets/~s~n", [?ADMIN_BASE_URL, TicketId]),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(delete,
|
||||
{?ADMIN_BASE_URL ++ "/v1/admin/tickets/" ++ binary_to_list(TicketId), [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format(" OK~n"),
|
||||
|
||||
io:format("~n✅ Tickets API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
Reference in New Issue
Block a user