-module(api_booking_tests). -export([test/0]). -define(BASE_URL, api_test_runner:get_base_url()). 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}.