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