Переделать связь нод в кластере на автоматическое обнаружение #9

This commit is contained in:
2026-05-01 22:30:40 +03:00
parent 1787b0f8a3
commit f36dd3bbc1
25 changed files with 870 additions and 332 deletions

View File

@@ -1,173 +1,203 @@
-module(api_admin_tests).
-include_lib("eunit/include/eunit.hrl").
-export([test/0]).
test() ->
io:format("Testing admin panel API...~n"),
AdminURL = "http://localhost:8445",
%% Учётные данные по умолчанию (используются, если словарь процесса пуст)
-define(FALLBACK_ADMIN_SUPER_EMAIL, <<"superadmin@eventhub.local">>).
-define(FALLBACK_ADMIN_SUPER_PASSWORD, <<"123456">>).
-define(FALLBACK_ADMIN_MODER_EMAIL, <<"moderator@eventhub.local">>).
-define(FALLBACK_ADMIN_MODER_PASSWORD, <<"123456">>).
-define(FALLBACK_ADMIN_SUPPORT_EMAIL, <<"support@eventhub.local">>).
-define(FALLBACK_ADMIN_SUPPORT_PASSWORD,<<"123456">>).
% Получаем admin-токен через test runner (уже проверенный)
test() ->
ct:pal("Testing admin panel API...~n"),
AdminURL = api_test_runner:get_admin_url(),
UserURL = api_test_runner:get_base_url(),
% Получаем токен суперадмина (уже проинициализирован в api_test_runner)
AdminToken = api_test_runner:get_admin_token(),
%% TEST 1: Admin healthcheck (public)
io:format(" TEST 1: Admin healthcheck... "),
ct:pal(" TEST 1: Admin healthcheck... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(get, {AdminURL ++ "/v1/admin/health", []}, [], []),
io:format("OK~n"),
ct:pal("OK~n"),
%% TEST 2: Admin login (дополнительная проверка)
io:format(" TEST 2: Admin login (attempt)... "),
LoginBody = jsx:encode(#{<<"email">> => <<"admin@eventhub.local">>, <<"password">> => <<"123456">>}),
ct:pal(" TEST 2: Admin login (attempt)... "),
% Теперь используем суперадмина, который гарантированно создан
LoginBody = jsx:encode(#{<<"email">> => ?FALLBACK_ADMIN_SUPER_EMAIL, <<"password">> => ?FALLBACK_ADMIN_SUPER_PASSWORD}),
case httpc:request(post, {AdminURL ++ "/v1/admin/login", [], "application/json", LoginBody}, [], []) of
{ok, {{_, 200, _}, _, _}} ->
io:format("OK (logged in)~n");
ct:pal("OK (logged in)~n");
_ ->
io:format("SKIPPED (credentials not found, using runner token)~n")
ct:pal("SKIPPED (credentials not found, using runner token)~n")
end,
%% TEST 3: Admin stats (superadmin)
io:format(" TEST 3: Admin stats (superadmin)... "),
LoginBody = jsx:encode(#{<<"email">> => <<"admin@eventhub.local">>, <<"password">> => <<"123456">>}),
{ok, {{_, 200, _}, _, LoginResp}} = httpc:request(post,
{AdminURL ++ "/v1/admin/login", [], "application/json", LoginBody}, [], []),
#{<<"token">> := SuperToken} = jsx:decode(list_to_binary(LoginResp), [return_maps]),
% Без дат
ct:pal(" TEST 3: Admin stats (superadmin)... "),
{ok, {{_, 200, _}, _, StatsResp1}} = httpc:request(get,
{AdminURL ++ "/v1/admin/stats", [{"Authorization", "Bearer " ++ binary_to_list(SuperToken)}]}, [], []),
{AdminURL ++ "/v1/admin/stats", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
Stats1 = jsx:decode(list_to_binary(StatsResp1), [return_maps]),
io:format(" OK (keys: ~p)~n", [maps:keys(Stats1)]),
% С датами
{ok, {{_, 200, _}, _, StatsResp2}} = httpc:request(get,
{AdminURL ++ "/v1/admin/stats?from=2026-01-01T00:00:00&to=2026-12-31T23:59:59",
[{"Authorization", "Bearer " ++ binary_to_list(SuperToken)}]}, [], []),
Stats2 = jsx:decode(list_to_binary(StatsResp2), [return_maps]),
io:format(" (with dates, keys: ~p)~n", [maps:keys(Stats2)]),
ct:pal(" OK (keys: ~p)~n", [maps:keys(Stats1)]),
%% TEST 4: List users
io:format(" TEST 4: List users... "),
ct:pal(" TEST 4: List users... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
{AdminURL ++ "/v1/admin/users", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
io:format("OK~n"),
ct:pal("OK~n"),
%% TEST 5: Get user by ID
io:format(" TEST 5: Get user by ID... "),
ct:pal(" 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"),
ct:pal("OK~n"),
%% TEST 6: List reports
io:format(" TEST 6: List reports... "),
ct:pal(" TEST 6: List reports... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
{AdminURL ++ "/v1/admin/reports", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
io:format("OK~n"),
ct:pal("OK~n"),
%% TEST 7: List banned words
io:format(" TEST 7: List banned words... "),
%% ── TEST 7: Full moderation flow (create event, report, resolve) ──
ct:pal(" TEST 7: Moderation flow... "),
% Создаём календарь и событие от имени пользователя
UserToken = api_test_runner:get_user_token(),
CalId = api_test_runner:create_calendar(UserToken, #{title => <<"ModerationTest">>}),
EventId = api_test_runner:create_event(UserToken, CalId, #{
title => <<"Event to report">>,
start_time => api_SUITE:future_date(),
duration => 60
}),
% Подаём жалобу на это событие
CreateBody = jsx:encode(#{
<<"target_type">> => <<"event">>,
<<"target_id">> => EventId,
<<"reason">> => <<"Inappropriate content">>
}),
{ok, {{_, 201, _}, _, CreateResp}} = httpc:request(post,
{UserURL ++ "/v1/reports",
[{"Authorization", "Bearer " ++ binary_to_list(UserToken)}],
"application/json", CreateBody}, [], []),
#{<<"id">> := ReportId} = jsx:decode(list_to_binary(CreateResp), [return_maps]),
% Администратор изменяет статус жалобы
EditBody = jsx:encode(#{
<<"status">> => <<"reviewed">>,
<<"reason">> => <<"Issue resolved">>
}),
{ok, {{_, 200, _}, _, _}} = httpc:request(put,
{AdminURL ++ "/v1/admin/reports/" ++ binary_to_list(ReportId),
[{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}],
"application/json", EditBody}, [], []),
ct:pal("OK~n"),
%% TEST 8: List banned words
ct:pal(" TEST 8: List banned words... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
{AdminURL ++ "/v1/admin/banned-words", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
io:format("OK~n"),
ct:pal("OK~n"),
%% TEST 8: Add banned word
io:format(" TEST 8: Add banned word... "),
%% TEST 9: Add banned word
ct:pal(" TEST 9: 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"),
ct:pal("OK~n"),
%% TEST 9: Delete banned word
io:format(" TEST 9: Delete banned word... "),
%% TEST 10: Delete banned word
ct:pal(" TEST 10: 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"),
ct:pal("OK~n"),
%% TEST 10: List tickets
io:format(" TEST 10: List tickets... "),
%% TEST 11: List tickets
ct:pal(" TEST 11: List tickets... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
{AdminURL ++ "/v1/admin/tickets", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
io:format("OK~n"),
ct:pal("OK~n"),
%% TEST 11: Create ticket
io:format(" TEST 11: Create ticket... "),
%% TEST 12: Create ticket
ct:pal(" TEST 12: 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"),
ct:pal("OK~n"),
%% TEST 12: Get ticket by ID
io:format(" TEST 12: Get ticket by ID... "),
%% TEST 13: Get ticket by ID
ct:pal(" TEST 13: 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"),
ct:pal("OK~n"),
%% TEST 13: Update ticket
io:format(" TEST 13: Update ticket... "),
%% TEST 14: Update ticket
ct:pal(" TEST 14: 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"),
ct:pal("OK~n"),
%% TEST 14: Delete ticket
io:format(" TEST 14: Delete ticket... "),
%% TEST 15: Delete ticket
ct:pal(" TEST 15: 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"),
ct:pal("OK~n"),
%% TEST 15: Ticket stats
io:format(" TEST 15: Ticket stats... "),
%% TEST 16: Ticket stats
ct:pal(" TEST 16: Ticket stats... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
{AdminURL ++ "/v1/admin/tickets/stats", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
io:format("OK~n"),
ct:pal("OK~n"),
%% TEST 16: List subscriptions
io:format(" TEST 16: List subscriptions... "),
%% TEST 17: List subscriptions
ct:pal(" TEST 17: List subscriptions... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
{AdminURL ++ "/v1/admin/subscriptions", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
io:format("OK~n"),
ct:pal("OK~n"),
%% TEST 17: Create subscription
io:format(" TEST 17: Create subscription... "),
%% TEST 18: Create subscription
ct:pal(" TEST 18: 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"),
ct:pal("OK~n"),
%% TEST 18: Get subscription by ID
io:format(" TEST 18: Get subscription by ID... "),
%% TEST 19: Get subscription by ID
ct:pal(" TEST 19: 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"),
ct:pal("OK~n"),
%% TEST 19: Update subscription
io:format(" TEST 19: Update subscription... "),
%% TEST 20: Update subscription
ct:pal(" TEST 20: 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"),
ct:pal("OK~n"),
%% TEST 20: Delete subscription
io:format(" TEST 20: Delete subscription... "),
%% TEST 21: Delete subscription
ct:pal(" TEST 21: 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"),
ct:pal("OK~n"),
%% TEST 21: Moderation - block user
io:format(" TEST 21: Moderation - block user... "),
ModBody = jsx:encode(#{<<"action">> => <<"block">>}),
%% TEST 22: Moderation - block user
ct:pal(" TEST 22: Moderation - block user... "),
ModBody = jsx:encode(#{<<"action">> => <<"block">>, <<"reason">> => <<"test">>}),
{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"),
ct:pal("OK~n"),
%% TEST 22: Moderation - unblock user
io:format(" TEST 22: Moderation - unblock user... "),
UnblockBody = jsx:encode(#{<<"action">> => <<"unblock">>}),
%% TEST 23: Moderation - unblock user
ct:pal(" TEST 23: Moderation - unblock user... "),
UnblockBody = jsx:encode(#{<<"action">> => <<"unblock">>, <<"reason">> => <<"restore">>}),
{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"),
ct:pal("OK~n"),
io:format("~n✅ Admin API tests passed!~n"),
ct:pal("~n✅ Admin API tests passed!~n"),
{?MODULE, ok}.

View File

@@ -1,7 +1,7 @@
-module(api_auth_tests).
-export([test/0]).
-define(BASE_URL, "http://localhost:8080").
-define(BASE_URL, api_test_runner:get_base_url()).
test() ->
io:format("Testing authentication API...~n"),

View File

@@ -1,7 +1,7 @@
-module(api_booking_tests).
-export([test/0]).
-define(BASE_URL, "http://localhost:8080").
-define(BASE_URL, api_test_runner:get_base_url()).
test() ->
io:format("Testing booking API...~n"),

View File

@@ -1,7 +1,7 @@
-module(api_calendar_tests).
-export([test/0]).
-define(BASE_URL, "http://localhost:8080").
-define(BASE_URL, api_test_runner:get_base_url()).
test() ->
io:format("Testing calendar API...~n"),

View File

@@ -1,7 +1,7 @@
-module(api_event_tests).
-export([test/0]).
-define(BASE_URL, "http://localhost:8080").
-define(BASE_URL, api_test_runner:get_base_url()).
test() ->
io:format("Testing event API...~n"),

View File

@@ -1,8 +1,8 @@
-module(api_moderation_tests).
-export([test/0]).
-define(BASE_URL, "http://localhost:8080").
-define(ADMIN_BASE_URL, "http://localhost:8445").
-define(BASE_URL, api_test_runner:get_base_url()).
-define(ADMIN_BASE_URL, api_test_runner:get_admin_url()).
test() ->
io:format("Testing moderation API...~n"),

View File

@@ -1,7 +1,7 @@
-module(api_reviews_tests).
-export([test/0]).
-define(BASE_URL, "http://localhost:8080").
-define(BASE_URL, api_test_runner:get_base_url()).
test() ->
io:format("Testing reviews API...~n"),

View File

@@ -1,7 +1,7 @@
-module(api_search_tests).
-export([test/0]).
-define(BASE_URL, "http://localhost:8080").
-define(BASE_URL, api_test_runner:get_base_url()).
test() ->
io:format("Testing search API...~n"),

View File

@@ -1,7 +1,7 @@
-module(api_subscription_tests).
-export([test/0]).
-define(BASE_URL, "http://localhost:8080").
-define(BASE_URL, api_test_runner:get_base_url()).
test() ->
io:format("Testing subscription API...~n"),

View File

@@ -1,67 +1,132 @@
-module(api_test_runner).
-include("records.hrl").
-export([run_all/0, run/1]).
-export([http_post/2, http_post/3, http_get/1, http_get/2, http_put/3, http_delete/2]).
-export([extract_json/2, extract_json/3, assert_status/2]).
-export([unique_email/1, register_and_login/2, create_calendar/2, create_event/3]).
-export([get_admin_token/0, get_admin_id/0, get_user_token/0, get_user_id/0]).
-export([get_admin_token/0, get_admin_id/0, get_user_token/0, get_user_id/0, get_admin_url/0, get_base_url/0, get_admin_ws_url/0, get_base_ws_url/0]).
-export([wait_for_server/0]).
-define(BASE_URL, "http://localhost:8080").
-define(ADMIN_URL, "http://localhost:8445").
-define(BASE_URL, base_url()).
-define(ADMIN_URL, admin_base_url()).
%% ============ Глобальные переменные для тестов ============
-define(ADMIN_EMAIL, <<"admin@eventhub.local">>).
-define(ADMIN_PASSWORD, <<"123456">>).
-define(USER_EMAIL, <<"global_user@test.com">>).
-define(USER_PASSWORD, <<"user123">>).
%% Учётные данные по умолчанию (используются в локальном режиме, если словарь пуст)
-define(FALLBACK_ADMIN_EMAIL, <<"admin@eventhub.local">>).
-define(FALLBACK_ADMIN_PASSWORD, <<"123456">>).
-define(USER_EMAIL, <<"global_user@test.com">>).
-define(USER_PASSWORD, <<"user123">>).
%% ------------------------------------------------------------------
%% Выбор базовых URL в зависимости от режима запуска
%% ------------------------------------------------------------------
base_url() ->
case os:getenv("CT_MODE", "local") of
"remote" -> os:getenv("API_HOST", "http://localhost:8080");
_ -> "http://localhost:8080"
end.
base_ws_url() ->
case os:getenv("CT_MODE", "local") of
"remote" -> os:getenv("WS_HOST", "ws://localhost:8081");
_ -> "ws://localhost:8081"
end.
admin_base_url() ->
case os:getenv("CT_MODE", "local") of
"remote" -> os:getenv("ADMIN_API_HOST", "http://localhost:8445");
_ -> "http://localhost:8445"
end.
admin_ws_url() ->
case os:getenv("CT_MODE", "local") of
"remote" -> os:getenv("ADMIN_WS_HOST", "ws://localhost:8446");
_ -> "ws://localhost:8446"
end.
%% ------------------------------------------------------------------
%% Инициализация глобальных тестовых пользователей
%% ------------------------------------------------------------------
init_global_urls() ->
put(admin_url, admin_base_url()),
put(admin_ws_url, admin_ws_url()),
put(base_url, base_url()),
put(base_ws_url, base_ws_url()).
%% ============ Инициализация ============
init_global_users() ->
case get(admin_token) of
undefined ->
io:format("~n=== Initializing global test users ===~n"),
ct:pal("~n=== Initializing global test users ===~n"),
% ---------- АДМИНИСТРАТОР ----------
% Проверяем, существует ли админ в таблице admin
case core_admin:get_by_email(?ADMIN_EMAIL) of
{ok, Admin} ->
io:format("Admin already exists: ~s~n", [Admin#admin.id]),
ok;
{error, not_found} ->
% Создаём суперадмина напрямую
{ok, Admin} = core_admin:create(?ADMIN_EMAIL, ?ADMIN_PASSWORD, superadmin),
io:format("Admin created: ~s~n", [Admin#admin.id])
end,
%% 1. Администратор
AdminEmail = get(admin_super_email),
AdminPassword = get(admin_super_password),
AdminToken =
if
AdminEmail =/= undefined, AdminPassword =/= undefined ->
%% Учётные данные переданы из api_SUITE (remoteрежим) просто логинимся
login_admin(AdminEmail, AdminPassword);
true ->
%% Локальный режим: админы уже есть, логинимся под суперадмином
login_admin(?FALLBACK_ADMIN_EMAIL, ?FALLBACK_ADMIN_PASSWORD)
end,
% Логинимся через админский API
LoginBody = jsx:encode(#{<<"email">> => ?ADMIN_EMAIL, <<"password">> => ?ADMIN_PASSWORD}),
{ok, {{_, 200, _}, _, LoginResp}} = httpc:request(post,
{?ADMIN_URL ++ "/v1/admin/login", [], "application/json", LoginBody}, [], []),
#{<<"token">> := AdminToken, <<"user">> := #{<<"id">> := AdminId}} =
jsx:decode(list_to_binary(LoginResp), [return_maps]),
%% Получаем ID администратора через /v1/admin/me
MeUrl = ?ADMIN_URL ++ "/v1/admin/me",
{ok, {{_, 200, _}, _, MeBody}} = httpc:request(get,
{MeUrl, [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, ssl_opts(), []),
#{<<"id">> := AdminId} = jsx:decode(list_to_binary(MeBody), [return_maps]),
put(admin_token, AdminToken),
put(admin_id, AdminId),
% ---------- ПОЛЬЗОВАТЕЛЬ ----------
%% 2. Обычный пользователь
UserToken = register_and_login(?USER_EMAIL, ?USER_PASSWORD),
{ok, {{_, 200, _}, _, UserMeResp}} = http_get("/v1/user/me", UserToken),
#{<<"id">> := UserId} = jsx:decode(list_to_binary(UserMeResp), [return_maps]),
{ok, {{_, 200, _}, _, UserMeBody}} = http_get("/v1/user/me", UserToken),
#{<<"id">> := UserId} = jsx:decode(list_to_binary(UserMeBody), [return_maps]),
put(user_token, UserToken),
put(user_id, UserId),
io:format("Admin ID: ~s, User ID: ~s~n", [AdminId, UserId]),
io:format("=== Global users initialized ===~n~n"),
ct:pal("Admin ID: ~s, User ID: ~s~n", [AdminId, UserId]),
ct:pal("=== Global users initialized ===~n~n"),
ok;
_ ->
io:format("Global users already initialized.~n"),
ct:pal("Global users already initialized.~n"),
ok
end.
%% ------------------------------------------------------------------
%% Вход администратора (используется, когда учётки уже известны)
%% ------------------------------------------------------------------
login_admin(Email, Password) ->
ct:pal("Admin url: ~s~n", [?ADMIN_URL]),
ct:pal("Admin: ~s, password: ~s~n", [Email, Password]),
LoginBody = jsx:encode(#{<<"email">> => Email, <<"password">> => Password}),
ct:pal("url: ~s, body: ~s~n", [?ADMIN_URL ++ "/v1/admin/login", LoginBody]),
{ok, {{_, _, _}, _, LoginResp}} = httpc:request(post,
{?ADMIN_URL ++ "/v1/admin/login", [], "application/json", LoginBody}, ssl_opts(), []),
ct:pal("LoginResp: ~s~n", [LoginResp]),
#{<<"token">> := Token} = jsx:decode(list_to_binary(LoginResp), [return_maps]),
Token.
%% ------------------------------------------------------------------
%% Остальные функции (без изменений, только используют ?BASE_URL / ?ADMIN_URL)
%% ------------------------------------------------------------------
get_admin_url() ->
init_global_urls(),
get(admin_url).
get_admin_ws_url() ->
init_global_urls(),
get(admin_ws_url).
get_base_url() ->
init_global_urls(),
get(base_url).
get_base_ws_url() ->
init_global_urls(),
get(base_ws_url).
get_admin_token() ->
init_global_users(),
get(admin_token).
@@ -78,19 +143,18 @@ get_user_id() ->
init_global_users(),
get(user_id).
%% ============ Главные функции запуска ============
run_all() ->
inets:start(),
ssl:start(),
case wait_for_server() of
ok -> ok;
{error, _} -> io:format("❌ Server is not running!~n"), exit(server_not_running)
{error, _} -> ct:pal("❌ Server is not running!~n"), exit(server_not_running)
end,
init_global_users(),
io:format("Starting API tests...~n"),
ct:pal("Starting API tests...~n"),
Modules = [
api_auth_tests,
api_calendar_tests,
@@ -111,14 +175,17 @@ run(Module) ->
init_global_users(),
Module:test().
%% ============ HTTP запросы ============
%% ── HTTPзапросы ─────────────────────────────────────────
ssl_opts() ->
[{ssl, [{verify, verify_none}]}].
http_post(Url, Body) -> http_post(Url, Body, undefined).
http_post(Url, Body, Token) ->
Headers = case Token of
undefined -> [{"Content-Type", "application/json"}];
_ -> [{"Content-Type", "application/json"}, {"Authorization", "Bearer " ++ binary_to_list(Token)}]
end,
httpc:request(post, {?BASE_URL ++ Url, Headers, "application/json", jsx:encode(Body)}, [], []).
httpc:request(post, {?BASE_URL ++ Url, Headers, "application/json", jsx:encode(Body)}, ssl_opts(), []).
http_get(Url) -> http_get(Url, undefined).
http_get(Url, Token) ->
@@ -126,18 +193,17 @@ http_get(Url, Token) ->
undefined -> [];
_ -> [{"Authorization", "Bearer " ++ binary_to_list(Token)}]
end,
httpc:request(get, {?BASE_URL ++ Url, Headers}, [], []).
httpc:request(get, {?BASE_URL ++ Url, Headers}, ssl_opts(), []).
http_put(Url, Body, Token) ->
Headers = [{"Content-Type", "application/json"}, {"Authorization", "Bearer " ++ binary_to_list(Token)}],
httpc:request(put, {?BASE_URL ++ Url, Headers, "application/json", jsx:encode(Body)}, [], []).
httpc:request(put, {?BASE_URL ++ Url, Headers, "application/json", jsx:encode(Body)}, ssl_opts(), []).
http_delete(Url, Token) ->
Headers = [{"Authorization", "Bearer " ++ binary_to_list(Token)}],
httpc:request(delete, {?BASE_URL ++ Url, Headers}, [], []).
%% ============ Утилиты ============
httpc:request(delete, {?BASE_URL ++ Url, Headers}, ssl_opts(), []).
%% ── Вспомогательные функции ──────────────────────────────
extract_json({ok, {{_, 200, _}, _, Body}}, Field) ->
Map = jsx:decode(list_to_binary(Body), [return_maps]),
maps:get(Field, Map);
@@ -170,7 +236,6 @@ register_and_login(Email, Password) ->
Map = jsx:decode(list_to_binary(RegResp), [return_maps]),
maps:get(<<"token">>, Map);
{ok, {{_, 409, _}, _, _}} ->
% Уже существует - логинимся
LoginBody = #{email => Email, password => Password},
{ok, {{_, 200, _}, _, LoginResp}} = http_post("/v1/login", LoginBody),
Map = jsx:decode(list_to_binary(LoginResp), [return_maps]),
@@ -189,7 +254,7 @@ create_event(Token, CalId, Params) ->
wait_for_server() -> wait_for_server(30).
wait_for_server(0) -> {error, timeout};
wait_for_server(Attempts) ->
case httpc:request(get, {?BASE_URL ++ "/health", []}, [], [{timeout, 1000}]) of
case httpc:request(get, {?BASE_URL ++ "/health", []}, ssl_opts(), [{timeout, 1000}]) of
{ok, {{_, 200, _}, _, _}} -> ok;
_ -> timer:sleep(1000), wait_for_server(Attempts - 1)
end.

View File

@@ -1,8 +1,8 @@
-module(api_tickets_tests).
-export([test/0]).
-define(ADMIN_BASE_URL, "http://localhost:8445").
-define(BASE_URL, "http://localhost:8080").
-define(ADMIN_BASE_URL, api_test_runner:get_admin_url()).
-define(BASE_URL, api_test_runner:get_base_url()).
test() ->
io:format("Testing tickets API...~n"),

View File

@@ -1,9 +1,9 @@
-module(api_websocket_tests).
-export([test/0]).
-define(BASE_URL, "http://localhost:8080").
-define(WS_URL, "ws://localhost:8081/ws").
-define(ADMIN_WS_URL, "ws://localhost:8446/admin/ws").
-define(BASE_URL, api_test_runner:get_base_url()).
-define(WS_URL, api_test_runner:get_base_ws_url() ++ "/ws").
-define(ADMIN_WS_URL, api_test_runner:get_admin_ws_url() ++ "/admin/ws").
test() ->
ct:pal("Testing WebSocket API..."),
@@ -141,14 +141,24 @@ test_ws_connect_debug(Url, Token) ->
"/ws?token=" ++ binary_to_list(Token)
end,
Port = ws_port(Url),
Host = "localhost",
{ok, Port} = extract_port(Url),
{ok, Host} = extract_host(Url),
Opts = case Port of
443 ->
#{
protocols => [http],
transport => tls,
tls_opts => [{verify, verify_none}]
};
_ -> #{ protocols => [http] }
end,
ct:pal(" Host: ~s", [Host]),
ct:pal(" Port: ~p", [Port]),
ct:pal(" Path: ~s", [Path]),
{ok, ConnPid} = gun:open(Host, Port, #{protocols => [http]}),
{ok, ConnPid} = gun:open(Host, Port, Opts),
{ok, http} = gun:await_up(ConnPid, 5000),
Headers = [
@@ -234,5 +244,40 @@ test_ws_recv(ConnPid, Timeout) ->
test_ws_close(ConnPid) ->
gun:close(ConnPid).
ws_port("ws://localhost:8081" ++ _) -> 8081;
ws_port("ws://localhost:8446" ++ _) -> 8446.
%% ========== URL parsing helpers ==========
normalize_scheme(S) when is_binary(S) -> S;
normalize_scheme(S) when is_list(S) -> list_to_binary(S);
normalize_scheme(S) when is_atom(S) -> atom_to_binary(S, utf8);
normalize_scheme(_) -> <<"unknown">>.
extract_host(Url) ->
try
Parsed = uri_string:parse(Url),
#{scheme := SchemeRaw, host := Host} = Parsed,
Scheme = normalize_scheme(SchemeRaw),
if Scheme =:= <<"ws">>; Scheme =:= <<"wss">> -> ok;
true -> throw({invalid_scheme, SchemeRaw})
end,
HostStr = if is_binary(Host) -> binary_to_list(Host); true -> Host end,
{ok, HostStr}
catch
Class:Reason:Stacktrace ->
{error, {parse_error, {Class, Reason}, Stacktrace}}
end.
extract_port(Url) ->
try
Parsed = uri_string:parse(Url),
#{scheme := SchemeRaw} = Parsed,
Scheme = normalize_scheme(SchemeRaw),
DefaultPort = if Scheme =:= <<"ws">> -> 80; Scheme =:= <<"wss">> -> 443; true -> throw({invalid_scheme, SchemeRaw}) end,
case maps:find(port, Parsed) of
{ok, P} when is_integer(P) -> {ok, P};
{ok, P} -> {ok, try list_to_integer(binary_to_list(normalize_scheme(P))) catch _:_ -> DefaultPort end};
error -> {ok, DefaultPort}
end
catch
Class:Reason:Stacktrace ->
{error, {parse_error, {Class, Reason}, Stacktrace}}
end.

View File

@@ -2,97 +2,129 @@
-include_lib("common_test/include/ct.hrl").
-export([all/0, init_per_suite/1, end_per_suite/1]).
-export([auth_test/1, calendar_test/1, event_test/1, booking_test/1]).
-export([search_test/1, reviews_test/1, moderation_test/1]).
-export([tickets_test/1, subscription_test/1, admin_test/1]).
-export([websocket_test/1]).
-export([auth_test/1, calendar_test/1, event_test/1, booking_test/1,
search_test/1, reviews_test/1, moderation_test/1, tickets_test/1,
subscription_test/1, admin_test/1, websocket_test/1]).
-export([future_date/0]).
all() -> [
auth_test,
calendar_test,
event_test,
booking_test,
search_test,
reviews_test,
moderation_test,
tickets_test,
subscription_test,
admin_test,
websocket_test
].
all() ->
[
auth_test,
calendar_test,
event_test,
booking_test,
search_test,
reviews_test,
moderation_test,
tickets_test,
subscription_test,
admin_test,
websocket_test
].
init_per_suite(Config) ->
% Очищаем Mnesia перед тестами
io:format("~n=== Cleaning Mnesia for fresh test run ===~n"),
os:cmd("rm -rf Mnesia.* 2>/dev/null || true"),
timer:sleep(2000),
% Запускаем сервер
io:format("Starting server...~n"),
{ok, _Apps} = application:ensure_all_started(eventhub),
ct:pal("Start Api Testing ~n"),
Mode = os:getenv("CT_MODE", "local"),
ct:pal(" Mode: ~s", [Mode]),
AdminURL = os:getenv("ADMIN_API_HOST"),
ct:pal(" AdminURL: ~s", [AdminURL]),
AdminWsURL = os:getenv("ADMIN_WS_HOST"),
ct:pal(" AdminWsURL: ~s", [AdminWsURL]),
UserURL = os:getenv("API_HOST"),
ct:pal(" UserURL: ~s", [UserURL]),
UserWsURL = os:getenv("WS_HOST"),
ct:pal(" UserWsURL: ~s", [UserWsURL]),
% Компилируем модули из test/api/
code:add_patha("_build/test/lib/eventhub/ebin"),
code:add_patha("test/api"),
case Mode of
"remote" ->
inets:start(),
ssl:start(),
% Отключаем авто-редирект и проверку сертификатов
httpc:set_options([
{autoredirect, false},
{ssl, [{verify, verify_none}]}
]),
wait_for_server(),
timer:sleep(1000),
% Извлекаем учётные данные администраторов из переменных окружения
% и сохраняем их в словаре процесса для api_test_runner
put(admin_super_email,
list_to_binary(os:getenv("ADMIN_SUPER_EMAIL", "superadmin@eventhub.local"))),
put(admin_super_password,
list_to_binary(os:getenv("ADMIN_SUPER_PASSWORD", "123456"))),
put(admin_moder_email,
list_to_binary(os:getenv("ADMIN_MODER_EMAIL", "moderator@eventhub.local"))),
put(admin_moder_password,
list_to_binary(os:getenv("ADMIN_MODER_PASSWORD", "123456"))),
put(admin_support_email,
list_to_binary(os:getenv("ADMIN_SUPPORT_EMAIL", "support@eventhub.local"))),
put(admin_support_password,
list_to_binary(os:getenv("ADMIN_SUPPORT_PASSWORD", "123456"))),
Config;
_ ->
application:ensure_all_started(eventhub),
timer:sleep(3000),
check_admins(),
Config
end.
% Компилируем все файлы в test/api/
compile_api_modules(),
inets:start(),
ssl:start(),
%% Perform healthcheck (simplified)
Url = "http://localhost:8080",
case httpc:request(get, {Url ++ "/health", []}, [], []) of
{ok, {{_Version, 200, _Reason}, _Headers, _Body}} ->
ok; %% Healthcheck passed
_Error ->
ct:log("Healthcheck failed for: ~p", [Url]),
error(healthcheck_failed)
end_per_suite(Config) ->
Mode = os:getenv("CT_MODE", "local"),
case Mode of
"remote" ->
ok;
_ ->
application:stop(eventhub)
end,
Config.
end_per_suite(_Config) ->
application:stop(eventhub),
ok.
%% ── Тестовые обёртки ──────────────────────────────────
auth_test(_) -> api_auth_tests:test().
calendar_test(_) -> api_calendar_tests:test().
event_test(_) -> api_event_tests:test().
booking_test(_) -> api_booking_tests:test().
search_test(_) -> api_search_tests:test().
reviews_test(_) -> api_reviews_tests:test().
moderation_test(_) -> api_moderation_tests:test().
tickets_test(_) -> api_tickets_tests:test().
subscription_test(_) -> api_subscription_tests:test().
admin_test(_) -> api_admin_tests:test().
websocket_test(_) -> api_websocket_tests:test().
compile_api_modules() ->
Files = filelib:wildcard("test/api/*.erl"),
lists:foreach(fun(File) ->
compile:file(File, [report, {outdir, "test/api"}])
end, Files),
code:add_patha("test/api").
%% @doc Проверка наличия администраторов (только в remoteрежиме)
%% Если таблица admin пуста роняем тест явно, чтобы не гадать.
check_admins() ->
case core_admin:list_all() of
[] ->
ct:fail("No admins found in remote cluster. Run init_default_admins first.");
Admins ->
ct:pal("Admins present: ~p", [length(Admins)])
end.
%% ============ ТЕСТЫ-ПРОКСИ ============
%% @doc Ожидание доступности healthcheck-эндпоинта (/health)
wait_for_server() ->
URL = case os:getenv("API_HOST") of
false -> "http://localhost:8080/health";
Host -> Host ++ "/health"
end,
wait_for_server(URL, 30).
auth_test(_Config) ->
api_auth_tests:test().
wait_for_server(URL, 0) ->
ct:fail("Healthcheck ~s not responding after 30 seconds", [URL]);
wait_for_server(URL, Attempts) ->
case httpc:request(get, {URL, []}, [{timeout, 2000}, {ssl, [{verify, verify_none}]}], []) of
{ok, {{_, 200, _}, _, _}} ->
ct:pal("Healthcheck OK", []);
_ ->
timer:sleep(1000),
wait_for_server(URL, Attempts - 1)
end.
calendar_test(_Config) ->
api_calendar_tests:test().
event_test(_Config) ->
api_event_tests:test().
booking_test(_Config) ->
api_booking_tests:test().
search_test(_Config) ->
api_search_tests:test().
reviews_test(_Config) ->
api_reviews_tests:test().
moderation_test(_Config) ->
api_moderation_tests:test().
tickets_test(_Config) ->
api_tickets_tests:test().
subscription_test(_Config) ->
api_subscription_tests:test().
admin_test(_Config) ->
api_admin_tests:test().
websocket_test(_Config) ->
api_websocket_tests:test().
future_date() ->
Now = calendar:universal_time(),
Tomorrow = calendar:gregorian_seconds_to_datetime(
calendar:datetime_to_gregorian_seconds(Now) + 86400
),
{{Y, M, D}, {H, Min, S}} = Tomorrow,
iolist_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0BZ",
[Y, M, D, H, Min, S])).