Рефакторинг обработчиков. Часть 3 #21

This commit is contained in:
2026-05-13 23:02:59 +03:00
parent 61bb44ab4a
commit 40806df62a
91 changed files with 6138 additions and 7150 deletions

171
test/api_users_SUITE.erl Normal file
View File

@@ -0,0 +1,171 @@
%%%-------------------------------------------------------------------
%%% @doc Common Test Suite для клиентского API.
%%%
%%% Поддерживает два режима запуска:
%%% - Локальный (CT_MODE=local или не задан): автоматически
%%% стартует EventHub, выполняет тесты и останавливает приложение.
%%% - Удалённый (CT_MODE=remote): подключается к уже работающему
%%% кластеру по URL, определённым в переменных окружения
%%% (API_HOST, ADMIN_API_HOST и т.д.).
%%%
%%% Запуск:
%%% rebar3 ct --suite=api_client_SUITE
%%% или CT_MODE=remote rebar3 ct --suite=api_client_SUITE
%%% @end
%%%-------------------------------------------------------------------
-module(api_users_SUITE).
-compile(export_all).
-include_lib("common_test/include/ct.hrl").
%%%===================================================================
%%% Common Test callbacks
%%%===================================================================
%% @doc Возвращает список тестовых кейсов.
all() ->
[
user_test_register,
user_test_login,
user_test_user_me,
user_test_calendars,
user_test_calendar_by_id,
user_test_calendar_view,
user_test_event_by_id,
user_test_events,
user_test_occurrence_cancel,
user_test_bookings,
user_test_my_bookings,
user_test_reviews,
user_test_review_by_id,
user_test_my_reviews,
user_test_search,
user_test_refresh,
user_test_reports,
user_test_tickets,
user_test_subscription,
user_test_websocket
].
%% @doc Инициализация сьюта.
%% В локальном режиме запускает приложение EventHub.
init_per_suite(Config) ->
case os:getenv("CT_MODE", "local") of
"remote" ->
ct:pal("Remote mode: assuming application is already running"),
wait_for_remote(),
[{started_by_us, false} | Config];
_ ->
case lists:keymember(eventhub, 1, application:which_applications()) of
true ->
ct:pal("Local mode: application already running"),
[{started_by_us, false} | Config];
false ->
ct:pal("Local mode: starting application..."),
%% ok = application:load(eventhub),
{ok, _} = application:ensure_all_started(eventhub),
timer:sleep(1000),
[{started_by_us, true} | Config]
end
end.
%% @doc Завершение сьюта. В локальном режиме останавливает приложение.
end_per_suite(Config) ->
case proplists:get_value(started_by_us, Config, false) andalso
os:getenv("CT_MODE", "local") =/= "remote" of
true ->
application:stop(eventhub);
false ->
ok
end,
Config.
%%%===================================================================
%%% Тестовые кейсы
%%%===================================================================
%% @doc Тесты регистрации.
user_test_register(_Config) ->
user_register_tests:test().
user_test_login(_Config) ->
user_login_tests:test().
user_test_user_me(_Config) ->
user_me_tests:test().
user_test_calendars(_Config) ->
user_calendars_tests:test().
user_test_calendar_by_id(_Config) ->
user_calendar_by_id_tests:test().
user_test_calendar_view(_Config) ->
user_calendar_view_tests:test().
user_test_events(_Config) ->
user_events_tests:test().
user_test_event_by_id(_Config) ->
user_event_by_id_tests:test().
user_test_occurrence_cancel(_Config) ->
user_occurrence_cancel_tests:test().
user_test_bookings(_Config) ->
user_bookings_tests:test().
user_test_my_bookings(_Config) ->
user_my_bookings_tests:test().
user_test_reviews(_Config) ->
user_reviews_tests:test().
user_test_review_by_id(_Config) ->
user_review_by_id_tests:test().
user_test_my_reviews(_Config) ->
user_my_reviews_tests:test().
user_test_search(_Config) ->
user_search_tests:test().
user_test_refresh(_Config) ->
user_refresh_tests:test().
user_test_reports(_Config) ->
user_reports_tests:test().
user_test_tickets(_Config) ->
user_tickets_tests:test().
user_test_subscription(_Config) ->
user_subscription_tests:test().
user_test_websocket(_Config) ->
user_websocket_tests:test().
%%%===================================================================
%%% Внутренние функции
%%%===================================================================
-spec ct_mode() -> string().
ct_mode() ->
os:getenv("CT_MODE", "local").
%% @private Ожидание доступности удалённого API.
-spec wait_for_remote() -> ok.
wait_for_remote() ->
URL = os:getenv("API_HOST", "http://localhost:8080") ++ "/health",
ct:pal("Waiting for remote API: ~s", [URL]),
wait_for_health(URL, 30).
wait_for_health(_URL, 0) ->
ct:fail("Remote API did not start within 30 seconds");
wait_for_health(URL, Retries) ->
case httpc:request(get, {URL, []}, [], []) of
{ok, {{_, 200, _}, _, _}} -> ok;
_ ->
timer:sleep(1000),
wait_for_health(URL, Retries - 1)
end.