Stage 10 final
This commit is contained in:
27
test/api/api_admin_tests.erl
Normal file
27
test/api/api_admin_tests.erl
Normal file
@@ -0,0 +1,27 @@
|
||||
-module(api_admin_tests).
|
||||
-export([test/0]).
|
||||
|
||||
test() ->
|
||||
io:format("Testing admin panel API...~n"),
|
||||
|
||||
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", []}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Admin stats
|
||||
io:format(" TEST 2: Admin stats... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{"http://localhost:8445/admin/stats", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: List users
|
||||
io:format(" TEST 3: List users... "),
|
||||
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
|
||||
{"http://localhost:8445/admin/users", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Admin API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
64
test/api/api_auth_tests.erl
Normal file
64
test/api/api_auth_tests.erl
Normal file
@@ -0,0 +1,64 @@
|
||||
-module(api_auth_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
|
||||
test() ->
|
||||
io:format("Testing authentication API...~n"),
|
||||
|
||||
Email = api_test_runner:unique_email(<<"auth_test">>),
|
||||
Password = <<"test123">>,
|
||||
|
||||
% TEST 1: Register
|
||||
io:format(" TEST 1: Register... "),
|
||||
RegBody = #{email => Email, password => Password},
|
||||
Token = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/register", RegBody), <<"token">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Register with existing email
|
||||
io:format(" TEST 2: Register duplicate... "),
|
||||
{ok, {{_, 409, _}, _, _}} = api_test_runner:http_post("/v1/register", RegBody),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: Login with correct credentials
|
||||
io:format(" TEST 3: Login... "),
|
||||
LoginBody = #{email => Email, password => Password},
|
||||
RefreshToken = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/login", LoginBody), <<"refresh_token">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 4: Login with wrong password
|
||||
io:format(" TEST 4: Login wrong password... "),
|
||||
{ok, {{_, 401, _}, _, _}} = api_test_runner:http_post("/v1/login", #{email => Email, password => <<"wrong">>}),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 5: Get profile with valid token
|
||||
io:format(" TEST 5: Get profile... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/user/me", Token),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 6: Get profile with invalid token
|
||||
io:format(" TEST 6: Get profile invalid token... "),
|
||||
{ok, {{_, 401, _}, _, _}} = api_test_runner:http_get("/v1/user/me", <<"invalid">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 7: Refresh token
|
||||
io:format(" TEST 7: Refresh token... "),
|
||||
RefreshBody = #{refresh_token => RefreshToken},
|
||||
NewToken = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/refresh", RefreshBody), <<"token">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 8: Refresh with used token (should fail)
|
||||
io:format(" TEST 8: Refresh with used token... "),
|
||||
{ok, {{_, 401, _}, _, _}} = api_test_runner:http_post("/v1/refresh", RefreshBody),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 9: Use new token
|
||||
io:format(" TEST 9: Use new token... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/user/me", NewToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Authentication API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
83
test/api/api_booking_tests.erl
Normal file
83
test/api/api_booking_tests.erl
Normal file
@@ -0,0 +1,83 @@
|
||||
-module(api_booking_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
|
||||
test() ->
|
||||
io:format("Testing booking API...~n"),
|
||||
|
||||
OwnerEmail = api_test_runner:unique_email(<<"book_owner">>),
|
||||
ParticipantEmail = api_test_runner:unique_email(<<"book_part">>),
|
||||
|
||||
OwnerToken = api_test_runner:register_and_login(OwnerEmail, <<"owner123">>),
|
||||
ParticipantToken = api_test_runner:register_and_login(ParticipantEmail, <<"part123">>),
|
||||
|
||||
% Используем COMMERCIAL календари
|
||||
AutoCalId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars",
|
||||
#{title => <<"Auto">>, type => <<"commercial">>, confirmation => <<"auto">>}, OwnerToken), <<"id">>),
|
||||
ManualCalId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars",
|
||||
#{title => <<"Manual">>, type => <<"commercial">>, confirmation => <<"manual">>}, OwnerToken), <<"id">>),
|
||||
|
||||
% Создаём события
|
||||
AutoEventId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(AutoCalId) ++ "/events",
|
||||
#{title => <<"Auto Event">>, start_time => <<"2026-06-01T10:00:00Z">>, duration => 60}, OwnerToken), <<"id">>),
|
||||
ManualEventId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(ManualCalId) ++ "/events",
|
||||
#{title => <<"Manual Event">>, start_time => <<"2026-06-01T10:00:00Z">>, duration => 60}, OwnerToken), <<"id">>),
|
||||
|
||||
% TEST 1: Auto booking
|
||||
io:format(" TEST 1: Auto booking... "),
|
||||
AutoBookingId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/events/" ++ binary_to_list(AutoEventId) ++ "/bookings", #{}, ParticipantToken), <<"id">>),
|
||||
timer:sleep(200),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/bookings/" ++ binary_to_list(AutoBookingId), ParticipantToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Manual booking
|
||||
io:format(" TEST 2: Manual booking... "),
|
||||
ManualBookingId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/events/" ++ binary_to_list(ManualEventId) ++ "/bookings", #{}, ParticipantToken), <<"id">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: Duplicate booking
|
||||
io:format(" TEST 3: Duplicate booking... "),
|
||||
{ok, {{_, 409, _}, _, _}} = api_test_runner:http_post("/v1/events/" ++ binary_to_list(AutoEventId) ++ "/bookings", #{}, ParticipantToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 4: Owner confirms booking
|
||||
io:format(" TEST 4: Owner confirms booking... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_put("/v1/bookings/" ++ binary_to_list(ManualBookingId),
|
||||
#{action => <<"confirm">>}, OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 5: List event bookings
|
||||
io:format(" TEST 5: List event bookings... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/events/" ++ binary_to_list(ManualEventId) ++ "/bookings", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 6: List user bookings
|
||||
io:format(" TEST 6: List user bookings... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/user/bookings", ParticipantToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 7: Cancel booking
|
||||
io:format(" TEST 7: Cancel booking... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_delete("/v1/bookings/" ++ binary_to_list(AutoBookingId), ParticipantToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 8: Owner declines booking (новое событие)
|
||||
io:format(" TEST 8: Owner declines booking... "),
|
||||
NewEventId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(ManualCalId) ++ "/events",
|
||||
#{title => <<"Decline Event">>, start_time => <<"2026-06-02T10:00:00Z">>, duration => 60}, OwnerToken), <<"id">>),
|
||||
NewBookingId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/events/" ++ binary_to_list(NewEventId) ++ "/bookings", #{}, ParticipantToken), <<"id">>),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_put("/v1/bookings/" ++ binary_to_list(NewBookingId),
|
||||
#{action => <<"decline">>}, OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Booking API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
61
test/api/api_calendar_tests.erl
Normal file
61
test/api/api_calendar_tests.erl
Normal file
@@ -0,0 +1,61 @@
|
||||
-module(api_calendar_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
|
||||
test() ->
|
||||
io:format("Testing calendar API...~n"),
|
||||
|
||||
OwnerEmail = api_test_runner:unique_email(<<"cal_owner">>),
|
||||
OtherEmail = api_test_runner:unique_email(<<"cal_other">>),
|
||||
|
||||
OwnerToken = api_test_runner:register_and_login(OwnerEmail, <<"owner123">>),
|
||||
OtherToken = api_test_runner:register_and_login(OtherEmail, <<"other123">>),
|
||||
|
||||
% TEST 1: Create personal calendar
|
||||
io:format(" TEST 1: Create personal calendar... "),
|
||||
CalId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars",
|
||||
#{title => <<"Personal">>, type => <<"personal">>}, OwnerToken), <<"id">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Create commercial calendar
|
||||
io:format(" TEST 2: Create commercial calendar... "),
|
||||
CommId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars",
|
||||
#{title => <<"Commercial">>, type => <<"commercial">>}, OwnerToken), <<"id">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: List calendars
|
||||
io:format(" TEST 3: List calendars... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/calendars", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 4: Get personal calendar (owner)
|
||||
io:format(" TEST 4: Get personal calendar... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/calendars/" ++ binary_to_list(CalId), OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 5: Get personal calendar (other - denied)
|
||||
io:format(" TEST 5: Get personal calendar (other)... "),
|
||||
{ok, {{_, 403, _}, _, _}} = api_test_runner:http_get("/v1/calendars/" ++ binary_to_list(CalId), OtherToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 6: Get commercial calendar (other - allowed)
|
||||
io:format(" TEST 6: Get commercial calendar (other)... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/calendars/" ++ binary_to_list(CommId), OtherToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 7: Update calendar
|
||||
io:format(" TEST 7: Update calendar... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_put("/v1/calendars/" ++ binary_to_list(CalId),
|
||||
#{title => <<"Updated">>}, OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 8: Delete calendar
|
||||
io:format(" TEST 8: Delete calendar... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_delete("/v1/calendars/" ++ binary_to_list(CalId), OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Calendar API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
70
test/api/api_event_tests.erl
Normal file
70
test/api/api_event_tests.erl
Normal file
@@ -0,0 +1,70 @@
|
||||
-module(api_event_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
|
||||
test() ->
|
||||
io:format("Testing event API...~n"),
|
||||
|
||||
OwnerEmail = api_test_runner:unique_email(<<"ev_owner">>),
|
||||
OwnerToken = api_test_runner:register_and_login(OwnerEmail, <<"owner123">>),
|
||||
|
||||
CalId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars", #{title => <<"Test">>}, OwnerToken), <<"id">>),
|
||||
|
||||
% TEST 1: Create single event
|
||||
io:format(" TEST 1: Create single event... "),
|
||||
EventId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events",
|
||||
#{title => <<"Test Event">>, start_time => <<"2026-06-01T10:00:00Z">>, duration => 60}, OwnerToken), <<"id">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Create event in past (should fail)
|
||||
io:format(" TEST 2: Create past event... "),
|
||||
{ok, {{_, 400, _}, _, _}} = api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events",
|
||||
#{title => <<"Past Event">>, start_time => <<"2020-01-01T10:00:00Z">>, duration => 60}, OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: Create recurring event
|
||||
io:format(" TEST 3: Create recurring event... "),
|
||||
RecurringId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events",
|
||||
#{title => <<"Weekly Meeting">>, start_time => <<"2026-06-01T10:00:00Z">>, duration => 60,
|
||||
recurrence => #{freq => <<"WEEKLY">>, interval => 1}}, OwnerToken), <<"id">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 4: List events
|
||||
io:format(" TEST 4: List events... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 5: Get event
|
||||
io:format(" TEST 5: Get event... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/events/" ++ binary_to_list(EventId), OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 6: Update event
|
||||
io:format(" TEST 6: Update event... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_put("/v1/events/" ++ binary_to_list(EventId),
|
||||
#{title => <<"Updated Event">>}, OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 7: Get occurrences
|
||||
io:format(" TEST 7: Get occurrences... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get(
|
||||
"/v1/events/" ++ binary_to_list(RecurringId) ++ "/occurrences?from=2026-06-01T00:00:00Z&to=2026-06-30T00:00:00Z", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 8: Cancel occurrence
|
||||
io:format(" TEST 8: Cancel occurrence... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_delete(
|
||||
"/v1/events/" ++ binary_to_list(RecurringId) ++ "/occurrences/2026-06-08T10:00:00Z", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 9: Delete event
|
||||
io:format(" TEST 9: Delete event... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_delete("/v1/events/" ++ binary_to_list(EventId), OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Event API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
54
test/api/api_moderation_tests.erl
Normal file
54
test/api/api_moderation_tests.erl
Normal file
@@ -0,0 +1,54 @@
|
||||
-module(api_moderation_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
|
||||
test() ->
|
||||
io:format("Testing moderation API...~n"),
|
||||
|
||||
AdminToken = api_test_runner:get_admin_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">>),
|
||||
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">>),
|
||||
|
||||
% 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">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Admin views reports
|
||||
io:format(" TEST 2: Admin views reports... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/admin/reports", AdminToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% 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),
|
||||
io:format("OK~n"),
|
||||
|
||||
% 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),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 5: List banned words
|
||||
io:format(" TEST 5: List banned words... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/admin/banned-words", AdminToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% 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),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Moderation API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
58
test/api/api_reviews_tests.erl
Normal file
58
test/api/api_reviews_tests.erl
Normal file
@@ -0,0 +1,58 @@
|
||||
-module(api_reviews_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
|
||||
test() ->
|
||||
io:format("Testing reviews API...~n"),
|
||||
|
||||
OwnerEmail = api_test_runner:unique_email(<<"rev_owner">>),
|
||||
ParticipantEmail = api_test_runner:unique_email(<<"rev_part">>),
|
||||
|
||||
OwnerToken = api_test_runner:register_and_login(OwnerEmail, <<"owner123">>),
|
||||
ParticipantToken = api_test_runner:register_and_login(ParticipantEmail, <<"part123">>),
|
||||
|
||||
CalId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars", #{title => <<"Review Cal">>}, OwnerToken), <<"id">>),
|
||||
|
||||
EventId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events",
|
||||
#{title => <<"Review Event">>, start_time => <<"2026-06-01T10:00:00Z">>, duration => 60}, OwnerToken), <<"id">>),
|
||||
|
||||
% Создаём и подтверждаем бронирование
|
||||
BookingId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/events/" ++ binary_to_list(EventId) ++ "/bookings", #{}, ParticipantToken), <<"id">>),
|
||||
api_test_runner:http_put("/v1/bookings/" ++ binary_to_list(BookingId), #{action => <<"confirm">>}, OwnerToken),
|
||||
|
||||
% TEST 1: Create review
|
||||
io:format(" TEST 1: Create review... "),
|
||||
ReviewId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/reviews",
|
||||
#{target_type => <<"event">>, target_id => EventId, rating => 5, comment => <<"Great!">>},
|
||||
ParticipantToken), <<"id">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Duplicate review
|
||||
io:format(" TEST 2: Duplicate review... "),
|
||||
{ok, {{_, 409, _}, _, _}} = api_test_runner:http_post("/v1/reviews",
|
||||
#{target_type => <<"event">>, target_id => EventId, rating => 4, comment => <<"Again">>}, ParticipantToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: Get event reviews
|
||||
io:format(" TEST 3: Get event reviews... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/reviews?target_type=event&target_id=" ++ binary_to_list(EventId), ParticipantToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 4: Update review
|
||||
io:format(" TEST 4: Update review... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_put("/v1/reviews/" ++ binary_to_list(ReviewId),
|
||||
#{rating => 4}, ParticipantToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 5: Delete review
|
||||
io:format(" TEST 5: Delete review... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_delete("/v1/reviews/" ++ binary_to_list(ReviewId), ParticipantToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Reviews API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
54
test/api/api_search_tests.erl
Normal file
54
test/api/api_search_tests.erl
Normal file
@@ -0,0 +1,54 @@
|
||||
-module(api_search_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
|
||||
test() ->
|
||||
io:format("Testing search API...~n"),
|
||||
|
||||
OwnerEmail = api_test_runner:unique_email(<<"search_owner">>),
|
||||
OwnerToken = api_test_runner:register_and_login(OwnerEmail, <<"owner123">>),
|
||||
|
||||
CalId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars", #{title => <<"Search Cal">>}, OwnerToken), <<"id">>),
|
||||
|
||||
% Создаём события с тегами
|
||||
api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events",
|
||||
#{title => <<"Python Workshop">>, start_time => <<"2026-06-01T10:00:00Z">>, duration => 60,
|
||||
tags => [<<"python">>, <<"workshop">>]}, OwnerToken), <<"id">>),
|
||||
|
||||
api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events",
|
||||
#{title => <<"JavaScript">>, start_time => <<"2026-06-15T10:00:00Z">>, duration => 60,
|
||||
tags => [<<"javascript">>]}, OwnerToken), <<"id">>),
|
||||
|
||||
timer:sleep(500),
|
||||
|
||||
% TEST 1: Text search
|
||||
io:format(" TEST 1: Text search... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/search?type=event&q=Python", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Tag search
|
||||
io:format(" TEST 2: Tag search... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/search?type=event&tags=workshop", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: Combined search
|
||||
io:format(" TEST 3: Combined search... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/search?type=event&q=Python&tags=workshop", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 4: Pagination
|
||||
io:format(" TEST 4: Pagination... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/search?type=event&limit=2", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 5: Search calendars
|
||||
io:format(" TEST 5: Search calendars... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/search?type=calendar", OwnerToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Search API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
36
test/api/api_subscription_tests.erl
Normal file
36
test/api/api_subscription_tests.erl
Normal file
@@ -0,0 +1,36 @@
|
||||
-module(api_subscription_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
|
||||
test() ->
|
||||
io:format("Testing subscription API...~n"),
|
||||
|
||||
UserEmail = api_test_runner:unique_email(<<"sub_user">>),
|
||||
UserToken = api_test_runner:register_and_login(UserEmail, <<"user123">>),
|
||||
|
||||
% TEST 1: Get subscription (free)
|
||||
io:format(" TEST 1: Get subscription (free)... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/subscription", UserToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 2: Create commercial calendar (auto-activates trial)
|
||||
io:format(" TEST 2: Create commercial calendar... "),
|
||||
api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars",
|
||||
#{title => <<"Commercial">>, type => <<"commercial">>}, UserToken), <<"id">>),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 3: Get subscription (trial)
|
||||
io:format(" TEST 3: Get subscription (trial)... "),
|
||||
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/subscription", UserToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
% TEST 4: Activate paid subscription
|
||||
io:format(" TEST 4: Activate paid subscription... "),
|
||||
{ok, {{_, 201, _}, _, _}} = api_test_runner:http_post("/v1/subscription",
|
||||
#{action => <<"activate">>, plan => <<"monthly">>, payment_info => #{card => <<"4242">>}}, UserToken),
|
||||
io:format("OK~n"),
|
||||
|
||||
io:format("~n✅ Subscription API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
216
test/api/api_test_runner.erl
Normal file
216
test/api/api_test_runner.erl
Normal file
@@ -0,0 +1,216 @@
|
||||
-module(api_test_runner).
|
||||
-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([wait_for_server/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
-define(ADMIN_URL, "http://localhost:8445").
|
||||
|
||||
%% ============ Глобальные переменные для тестов ============
|
||||
-define(ADMIN_EMAIL, <<"global_admin@test.com">>).
|
||||
-define(ADMIN_PASSWORD, <<"admin123">>).
|
||||
-define(USER_EMAIL, <<"global_user@test.com">>).
|
||||
-define(USER_PASSWORD, <<"user123">>).
|
||||
|
||||
%% ============ Инициализация ============
|
||||
init_global_users() ->
|
||||
case get(admin_token) of
|
||||
undefined ->
|
||||
io:format("~n=== Initializing global test users ===~n"),
|
||||
|
||||
% Создаём или логиним админа
|
||||
AdminToken = register_and_login(?ADMIN_EMAIL, ?ADMIN_PASSWORD),
|
||||
{ok, {{_, 200, _}, _, MeResp}} = http_get("/v1/user/me", AdminToken),
|
||||
#{<<"id">> := AdminId, <<"role">> := Role} = jsx:decode(list_to_binary(MeResp), [return_maps]),
|
||||
|
||||
io:format("Admin ID: ~s, Current role: ~s~n", [AdminId, Role]),
|
||||
|
||||
% Проверяем, что админ действительно админ
|
||||
case Role of
|
||||
<<"admin">> ->
|
||||
io:format("✓ Admin already has admin role~n"),
|
||||
ok;
|
||||
_ ->
|
||||
io:format("⚠ Admin role is '~s', attempting to promote...~n", [Role]),
|
||||
promote_to_admin(AdminToken, AdminId)
|
||||
end,
|
||||
|
||||
put(admin_token, AdminToken),
|
||||
put(admin_id, AdminId),
|
||||
|
||||
% Создаём или логиним обычного пользователя
|
||||
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]),
|
||||
|
||||
put(user_token, UserToken),
|
||||
put(user_id, UserId),
|
||||
|
||||
io:format("User ID: ~s~n", [UserId]),
|
||||
io:format("=== Global users initialized ===~n~n"),
|
||||
ok;
|
||||
_ ->
|
||||
io:format("Global users already initialized.~n"),
|
||||
ok
|
||||
end.
|
||||
|
||||
%% Попытка повысить роль через разные методы
|
||||
promote_to_admin(AdminToken, AdminId) ->
|
||||
io:format("Attempting to promote user ~s to admin...~n", [AdminId]),
|
||||
|
||||
% Метод 1: Прямое обновление через core_user (если доступно)
|
||||
try
|
||||
{ok, _User} = core_user:get_by_id(AdminId),
|
||||
core_user:update(AdminId, [{role, admin}]),
|
||||
io:format("✓ Promoted via core_user~n")
|
||||
catch
|
||||
_:_ ->
|
||||
io:format(" Method 1 (core_user) failed~n")
|
||||
end,
|
||||
|
||||
% Проверяем, сработало ли
|
||||
{ok, {{_, 200, _}, _, CheckResp}} = http_get("/v1/user/me", AdminToken),
|
||||
#{<<"role">> := NewRole} = jsx:decode(list_to_binary(CheckResp), [return_maps]),
|
||||
|
||||
case NewRole of
|
||||
<<"admin">> ->
|
||||
io:format("✓ User is now admin~n");
|
||||
_ ->
|
||||
io:format("⚠ WARNING: User still has role '~s'~n", [NewRole]),
|
||||
io:format(" Some admin tests may fail~n")
|
||||
end.
|
||||
|
||||
get_admin_token() ->
|
||||
init_global_users(),
|
||||
get(admin_token).
|
||||
|
||||
get_admin_id() ->
|
||||
init_global_users(),
|
||||
get(admin_id).
|
||||
|
||||
get_user_token() ->
|
||||
init_global_users(),
|
||||
get(user_token).
|
||||
|
||||
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)
|
||||
end,
|
||||
|
||||
init_global_users(),
|
||||
|
||||
io:format("Starting API tests...~n"),
|
||||
Modules = [
|
||||
api_auth_tests,
|
||||
api_calendar_tests,
|
||||
api_event_tests,
|
||||
api_booking_tests,
|
||||
api_search_tests,
|
||||
api_reviews_tests,
|
||||
api_moderation_tests,
|
||||
api_tickets_tests,
|
||||
api_subscription_tests,
|
||||
api_admin_tests
|
||||
],
|
||||
lists:foreach(fun(M) -> M:test() end, Modules).
|
||||
|
||||
run(Module) ->
|
||||
inets:start(),
|
||||
ssl:start(),
|
||||
init_global_users(),
|
||||
Module:test().
|
||||
|
||||
%% ============ HTTP запросы ============
|
||||
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)}, [], []).
|
||||
|
||||
http_get(Url) -> http_get(Url, undefined).
|
||||
http_get(Url, Token) ->
|
||||
Headers = case Token of
|
||||
undefined -> [];
|
||||
_ -> [{"Authorization", "Bearer " ++ binary_to_list(Token)}]
|
||||
end,
|
||||
httpc:request(get, {?BASE_URL ++ Url, Headers}, [], []).
|
||||
|
||||
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)}, [], []).
|
||||
|
||||
http_delete(Url, Token) ->
|
||||
Headers = [{"Authorization", "Bearer " ++ binary_to_list(Token)}],
|
||||
httpc:request(delete, {?BASE_URL ++ Url, Headers}, [], []).
|
||||
|
||||
%% ============ Утилиты ============
|
||||
|
||||
extract_json({ok, {{_, 200, _}, _, Body}}, Field) ->
|
||||
Map = jsx:decode(list_to_binary(Body), [return_maps]),
|
||||
maps:get(Field, Map);
|
||||
extract_json({ok, {{_, 201, _}, _, Body}}, Field) ->
|
||||
Map = jsx:decode(list_to_binary(Body), [return_maps]),
|
||||
maps:get(Field, Map);
|
||||
extract_json(Response, _Field) ->
|
||||
error({unexpected_response, Response}).
|
||||
|
||||
extract_json(Response, Field, ExpectedStatus) ->
|
||||
case Response of
|
||||
{ok, {{_, ExpectedStatus, _}, _, Body}} ->
|
||||
Map = jsx:decode(list_to_binary(Body), [return_maps]),
|
||||
maps:get(Field, Map);
|
||||
_ ->
|
||||
error({unexpected_response, Response})
|
||||
end.
|
||||
|
||||
assert_status(Status, {ok, {{_, Status, _}, _, _}}) -> ok;
|
||||
assert_status(Expected, {ok, {{_, Got, _}, _, _}}) ->
|
||||
error({expected_status, Expected, got, Got}).
|
||||
|
||||
unique_email(Prefix) ->
|
||||
list_to_binary([Prefix, "_", integer_to_binary(os:system_time(millisecond)), "@test.com"]).
|
||||
|
||||
register_and_login(Email, Password) ->
|
||||
RegBody = #{email => Email, password => Password},
|
||||
case http_post("/v1/register", RegBody) of
|
||||
{ok, {{_, 201, _}, _, RegResp}} ->
|
||||
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]),
|
||||
maps:get(<<"token">>, Map)
|
||||
end.
|
||||
|
||||
create_calendar(Token, Params) ->
|
||||
Id = extract_json(http_post("/v1/calendars", Params, Token), <<"id">>),
|
||||
Id.
|
||||
|
||||
create_event(Token, CalId, Params) ->
|
||||
Url = "/v1/calendars/" ++ binary_to_list(CalId) ++ "/events",
|
||||
Id = extract_json(http_post(Url, Params, Token), <<"id">>),
|
||||
Id.
|
||||
|
||||
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
|
||||
{ok, {{_, 200, _}, _, _}} -> ok;
|
||||
_ -> timer:sleep(1000), wait_for_server(Attempts - 1)
|
||||
end.
|
||||
37
test/api/api_tickets_tests.erl
Normal file
37
test/api/api_tickets_tests.erl
Normal file
@@ -0,0 +1,37 @@
|
||||
-module(api_tickets_tests).
|
||||
-export([test/0]).
|
||||
|
||||
-define(BASE_URL, "http://localhost:8080").
|
||||
|
||||
test() ->
|
||||
io:format("Testing tickets API...~n"),
|
||||
|
||||
AdminToken = api_test_runner:get_admin_token(),
|
||||
UserToken = api_test_runner:get_user_token(),
|
||||
|
||||
% TEST 1: Report error
|
||||
io:format(" TEST 1: Report error... "),
|
||||
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"),
|
||||
|
||||
% 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 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 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"),
|
||||
|
||||
io:format("~n✅ Tickets API tests passed!~n"),
|
||||
{?MODULE, ok}.
|
||||
238
test/api/api_websocket_tests.erl
Normal file
238
test/api/api_websocket_tests.erl
Normal file
@@ -0,0 +1,238 @@
|
||||
-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").
|
||||
|
||||
test() ->
|
||||
ct:pal("Testing WebSocket API..."),
|
||||
|
||||
% Запускаем gun
|
||||
application:ensure_all_started(gun),
|
||||
|
||||
% Используем глобальных пользователей
|
||||
AdminToken = api_test_runner:get_admin_token(),
|
||||
UserToken = api_test_runner:get_user_token(),
|
||||
|
||||
ct:pal(" AdminToken: ~s...", [binary_part(AdminToken, 0, 30)]),
|
||||
ct:pal(" UserToken: ~s...", [binary_part(UserToken, 0, 30)]),
|
||||
|
||||
% Создаём календарь и событие для тестов
|
||||
CalId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars",
|
||||
#{title => <<"WS Test Calendar">>, type => <<"commercial">>},
|
||||
UserToken), <<"id">>, 201),
|
||||
ct:pal(" CalId: ~s", [CalId]),
|
||||
|
||||
EventId = api_test_runner:extract_json(
|
||||
api_test_runner:http_post("/v1/calendars/" ++ binary_to_list(CalId) ++ "/events",
|
||||
#{title => <<"WS Test Event">>,
|
||||
start_time => <<"2026-06-01T10:00:00Z">>,
|
||||
duration => 60},
|
||||
UserToken), <<"id">>, 201),
|
||||
ct:pal(" EventId: ~s", [EventId]),
|
||||
|
||||
% TEST 1: Connect to WebSocket with valid token
|
||||
ct:pal(" TEST 1: Connect WebSocket with valid token..."),
|
||||
ct:pal(" URL: ~s", [?WS_URL]),
|
||||
ct:pal(" Token: ~s...", [binary_part(UserToken, 0, 30)]),
|
||||
|
||||
case test_ws_connect_debug(?WS_URL, UserToken) of
|
||||
{ok, WS} ->
|
||||
ct:pal(" OK - Connected"),
|
||||
|
||||
% TEST 2: Subscribe to calendar updates
|
||||
ct:pal(" TEST 2: Subscribe to calendar..."),
|
||||
SubMsg = #{action => <<"subscribe">>, calendar_id => CalId},
|
||||
ct:pal(" Sending: ~p", [SubMsg]),
|
||||
ok = test_ws_send(WS, SubMsg),
|
||||
|
||||
case test_ws_recv(WS) of
|
||||
{ok, #{<<"status">> := <<"subscribed">>}} ->
|
||||
ct:pal(" OK - Subscribed");
|
||||
{ok, Other} ->
|
||||
ct:pal(" ERROR: Unexpected response: ~p", [Other]),
|
||||
error({unexpected_response, Other});
|
||||
{error, timeout} ->
|
||||
ct:pal(" ERROR: Timeout waiting for response"),
|
||||
error(timeout)
|
||||
end,
|
||||
|
||||
% Закрываем соединение
|
||||
test_ws_close(WS);
|
||||
{error, Reason} ->
|
||||
ct:pal(" ERROR: ~p", [Reason]),
|
||||
error({websocket_connect_failed, Reason})
|
||||
end,
|
||||
|
||||
ct:pal("~n✅ WebSocket API tests passed!"),
|
||||
|
||||
% ============ ТЕСТЫ АДМИНСКОГО WEBSOCKET ============
|
||||
ct:pal("~n=== ADMIN WEBSOCKET TESTS ==="),
|
||||
|
||||
% TEST 6: Admin WebSocket connection
|
||||
ct:pal(" TEST 6: Admin WebSocket connect..."),
|
||||
{ok, AdminWS} = test_ws_connect_debug(?ADMIN_WS_URL, AdminToken),
|
||||
ct:pal(" OK - Admin connected"),
|
||||
|
||||
% TEST 7: Admin subscribe to reports channel
|
||||
ct:pal(" TEST 7: Admin subscribe to reports channel..."),
|
||||
ok = test_ws_send(AdminWS, #{action => <<"subscribe">>, channel => <<"reports">>}),
|
||||
{ok, #{<<"status">> := <<"subscribed">>}} = test_ws_recv(AdminWS),
|
||||
ct:pal(" OK - Subscribed to reports"),
|
||||
|
||||
% TEST 8: Admin subscribe to tickets channel
|
||||
ct:pal(" TEST 8: Admin subscribe to tickets channel..."),
|
||||
ok = test_ws_send(AdminWS, #{action => <<"subscribe">>, channel => <<"tickets">>}),
|
||||
{ok, #{<<"status">> := <<"subscribed">>}} = test_ws_recv(AdminWS),
|
||||
ct:pal(" OK - Subscribed to tickets"),
|
||||
|
||||
% TEST 9: Admin receives report notification
|
||||
ct:pal(" TEST 9: Admin receives report notification..."),
|
||||
% Создаём жалобу через HTTP
|
||||
api_test_runner:http_post("/v1/reports",
|
||||
#{target_type => <<"event">>, target_id => EventId, reason => <<"Test report">>},
|
||||
UserToken),
|
||||
{ok, #{<<"type">> := <<"report_created">>}} = test_ws_recv(AdminWS, 5000),
|
||||
ct:pal(" OK - Received report notification"),
|
||||
|
||||
% TEST 10: Admin Ping/Pong
|
||||
ct:pal(" TEST 10: Admin Ping/Pong..."),
|
||||
ok = test_ws_send(AdminWS, #{action => <<"ping">>}),
|
||||
{ok, #{<<"status">> := <<"pong">>}} = test_ws_recv(AdminWS),
|
||||
ct:pal(" OK - Admin Ping/Pong"),
|
||||
|
||||
% TEST 11: Admin unsubscribe
|
||||
ct:pal(" TEST 11: Admin unsubscribe from reports..."),
|
||||
ok = test_ws_send(AdminWS, #{action => <<"unsubscribe">>, channel => <<"reports">>}),
|
||||
{ok, #{<<"status">> := <<"unsubscribed">>}} = test_ws_recv(AdminWS),
|
||||
ct:pal(" OK - Unsubscribed"),
|
||||
|
||||
test_ws_close(AdminWS),
|
||||
|
||||
% TEST 12: Admin WebSocket with user token (should fail)
|
||||
ct:pal(" TEST 12: Admin WS with user token..."),
|
||||
{error, {403, _}} = test_ws_connect_debug(?ADMIN_WS_URL, UserToken),
|
||||
ct:pal(" OK - Rejected"),
|
||||
|
||||
% TEST 13: Admin WebSocket with invalid token
|
||||
ct:pal(" TEST 13: Admin WS with invalid token..."),
|
||||
Chars = <<"abcdefghijklmnopqrstuvwxyz0123456789">>,
|
||||
InvalidToken = << <<(binary:at(Chars, rand:uniform(byte_size(Chars)) - 1))>> || _ <- lists:seq(1, 30) >>,
|
||||
{error, {401, _}} = test_ws_connect_debug(?ADMIN_WS_URL, InvalidToken),
|
||||
ct:pal(" OK - Rejected"),
|
||||
|
||||
ct:pal("~n✅ Admin WebSocket API tests passed!"),
|
||||
{?MODULE, ok}.
|
||||
|
||||
%% ============ WebSocket хелперы с отладкой ============
|
||||
|
||||
test_ws_connect_debug(Url, Token) ->
|
||||
Path = case string:split(Url, "://", trailing) of
|
||||
[_, Rest] ->
|
||||
case string:split(Rest, "/", leading) of
|
||||
[_HostPort, WsPath] ->
|
||||
"/" ++ WsPath ++ "?token=" ++ binary_to_list(Token);
|
||||
_ ->
|
||||
"/ws?token=" ++ binary_to_list(Token)
|
||||
end;
|
||||
_ ->
|
||||
"/ws?token=" ++ binary_to_list(Token)
|
||||
end,
|
||||
|
||||
Port = ws_port(Url),
|
||||
Host = "localhost",
|
||||
|
||||
ct:pal(" Host: ~s", [Host]),
|
||||
ct:pal(" Port: ~p", [Port]),
|
||||
ct:pal(" Path: ~s", [Path]),
|
||||
|
||||
{ok, ConnPid} = gun:open(Host, Port, #{protocols => [http]}),
|
||||
{ok, http} = gun:await_up(ConnPid, 5000),
|
||||
|
||||
Headers = [
|
||||
{<<"host">>, list_to_binary(Host ++ ":" ++ integer_to_list(Port))}
|
||||
],
|
||||
StreamRef = gun:ws_upgrade(ConnPid, Path, Headers),
|
||||
|
||||
% Ожидаем ответ
|
||||
receive
|
||||
{gun_upgrade, ConnPid, StreamRef, [<<"websocket">>], _} ->
|
||||
ct:pal(" WebSocket upgrade OK"),
|
||||
{ok, ConnPid};
|
||||
{gun_response, ConnPid, StreamRef, fin, 401, _} ->
|
||||
ct:pal(" ERROR: HTTP 401 Unauthorized"),
|
||||
gun:close(ConnPid),
|
||||
{error, {401, <<"Invalid token">>}};
|
||||
{gun_response, ConnPid, StreamRef, fin, 403, _} ->
|
||||
ct:pal(" ERROR: HTTP 403 Forbidden"),
|
||||
gun:close(ConnPid),
|
||||
{error, {403, <<"Admin access required">>}};
|
||||
{gun_response, ConnPid, StreamRef, nofin, 403, _} ->
|
||||
ct:pal(" ERROR: HTTP 403 Forbidden (nofin)"),
|
||||
gun:close(ConnPid),
|
||||
{error, {403, <<"Admin access required">>}};
|
||||
{gun_response, ConnPid, StreamRef, fin, Status, _} ->
|
||||
ct:pal(" ERROR: HTTP ~p", [Status]),
|
||||
gun:close(ConnPid),
|
||||
{error, {Status, <<"WebSocket upgrade failed">>}};
|
||||
{gun_response, ConnPid, StreamRef, nofin, Status, _} ->
|
||||
ct:pal(" ERROR: HTTP ~p (nofin)", [Status]),
|
||||
gun:close(ConnPid),
|
||||
{error, {Status, <<"WebSocket upgrade failed">>}};
|
||||
{gun_error, ConnPid, Reason} ->
|
||||
ct:pal(" ERROR: ~p", [Reason]),
|
||||
gun:close(ConnPid),
|
||||
{error, Reason}
|
||||
after 5000 ->
|
||||
ct:pal(" ERROR: Timeout"),
|
||||
gun:close(ConnPid),
|
||||
{error, timeout}
|
||||
end.
|
||||
|
||||
test_ws_send(ConnPid, Data) ->
|
||||
Msg = jsx:encode(Data),
|
||||
ct:pal(" Sending: ~s", [Msg]),
|
||||
case catch gun:ws_send(ConnPid, {text, Msg}) of
|
||||
ok -> ok;
|
||||
{'EXIT', {undef, _}} ->
|
||||
% Пробуем альтернативный синтаксис
|
||||
gun:ws_send(ConnPid, fin, {text, Msg});
|
||||
Other ->
|
||||
ct:pal(" ERROR sending: ~p", [Other]),
|
||||
error({ws_send_failed, Other})
|
||||
end.
|
||||
|
||||
test_ws_recv(ConnPid) ->
|
||||
test_ws_recv(ConnPid, 3000).
|
||||
|
||||
test_ws_recv(ConnPid, Timeout) ->
|
||||
receive
|
||||
{gun_ws, ConnPid, _StreamRef, {text, Msg}} ->
|
||||
ct:pal(" Received (with StreamRef): ~s", [Msg]),
|
||||
{ok, jsx:decode(Msg, [return_maps])};
|
||||
{gun_ws, ConnPid, {text, Msg}} ->
|
||||
ct:pal(" Received: ~s", [Msg]),
|
||||
{ok, jsx:decode(Msg, [return_maps])};
|
||||
{gun_ws, ConnPid, _StreamRef, Frame} ->
|
||||
ct:pal(" Received frame: ~p", [Frame]),
|
||||
{ok, Frame};
|
||||
{gun_ws, ConnPid, Frame} ->
|
||||
ct:pal(" Received: ~p", [Frame]),
|
||||
{ok, Frame};
|
||||
{gun_error, ConnPid, Reason} ->
|
||||
ct:pal(" ERROR: gun_error ~p", [Reason]),
|
||||
{error, Reason};
|
||||
Other ->
|
||||
ct:pal(" Received unexpected: ~p", [Other]),
|
||||
test_ws_recv(ConnPid, Timeout)
|
||||
after Timeout ->
|
||||
{error, timeout}
|
||||
end.
|
||||
|
||||
test_ws_close(ConnPid) ->
|
||||
gun:close(ConnPid).
|
||||
|
||||
ws_port("ws://localhost:8081" ++ _) -> 8081;
|
||||
ws_port("ws://localhost:8446" ++ _) -> 8446.
|
||||
Reference in New Issue
Block a user