Files
EventHubBack/test/api/api_moderation_tests.erl

72 lines
2.7 KiB
Erlang
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-module(api_moderation_tests).
-export([test/0]).
-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"),
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, _}, _, _}} = httpc:request(get,
{?ADMIN_BASE_URL ++ "/v1/admin/reports", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
io:format("OK~n"),
%% TEST 3: Admin resolves report с reason
io:format(" TEST 3: Admin resolves report... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(put,
{?ADMIN_BASE_URL ++ "/v1/admin/reports/" ++ binary_to_list(ReportId),
[{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}],
"application/json",
jsx:encode(#{status => <<"reviewed">>, reason => <<"Resolved by moderator">>})}, [], []),
io:format("OK~n"),
%% TEST 4: Add banned word
io:format(" TEST 4: Add banned word... "),
{ok, {{_, 201, _}, _, _}} = httpc:request(post,
{?ADMIN_BASE_URL ++ "/v1/admin/banned-words",
[{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}],
"application/json",
jsx:encode(#{<<"word">> => <<"badword">>})}, [], []),
io:format("OK~n"),
%% TEST 5: List banned words
io:format(" TEST 5: List banned words... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(get,
{?ADMIN_BASE_URL ++ "/v1/admin/banned-words", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
io:format("OK~n"),
%% TEST 6: Remove banned word
io:format(" TEST 6: Remove banned word... "),
{ok, {{_, 200, _}, _, _}} = httpc:request(delete,
{?ADMIN_BASE_URL ++ "/v1/admin/banned-words/badword", [{"Authorization", "Bearer " ++ binary_to_list(AdminToken)}]}, [], []),
io:format("OK~n"),
io:format("~n✅ Moderation API tests passed!~n"),
{?MODULE, ok}.