Расширена работа с бан словами #22
This commit is contained in:
@@ -4,24 +4,26 @@
|
||||
%%% Покрывает эндпоинты:
|
||||
%%% GET /v1/admin/banned-words
|
||||
%%% POST /v1/admin/banned-words
|
||||
%%% PUT /v1/admin/banned-words/:word
|
||||
%%% DELETE /v1/admin/banned-words/:word
|
||||
%%% POST /v1/admin/banned-words/batch
|
||||
%%%
|
||||
%%% Проверяет:
|
||||
%%% - получение списка слов
|
||||
%%% - добавление нового слова
|
||||
%%% - редактирование существующего слова
|
||||
%%% - удаление слова
|
||||
%%% - массовую загрузку списком
|
||||
%%% - пагинацию
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(admin_banned_words_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-export([test/0]).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Главная тестовая функция
|
||||
%%%===================================================================
|
||||
|
||||
-spec test() -> ok.
|
||||
test() ->
|
||||
ct:pal("=== Admin Banned Words Tests ==="),
|
||||
@@ -36,9 +38,13 @@ test() ->
|
||||
|
||||
test_list_words(Token, Word1, Word2),
|
||||
test_add_word(Token),
|
||||
test_update_word(Token, Word1),
|
||||
test_batch_add_words(Token),
|
||||
test_words_pagination(Token),
|
||||
test_delete_word(Token, Word1),
|
||||
test_delete_word(Token, Word2),
|
||||
test_update_word_conflict(Token),
|
||||
test_batch_add_invalid(Token),
|
||||
test_batch_add_duplicates(Token),
|
||||
|
||||
ct:pal("=== All admin banned words tests passed ==="),
|
||||
ok.
|
||||
@@ -69,6 +75,39 @@ test_add_word(Token) ->
|
||||
?assert(lists:any(fun(W) -> maps:get(<<"word">>, W) =:= NewWord end, Words)),
|
||||
ct:pal(" OK").
|
||||
|
||||
%% @doc PUT /v1/admin/banned-words/:word – редактирование слова.
|
||||
-spec test_update_word(binary(), binary()) -> ok.
|
||||
test_update_word(Token, Word) ->
|
||||
ct:pal(" TEST: Update a banned word"),
|
||||
NewWord = <<Word/binary, "_updated">>,
|
||||
Result = api_test_runner:admin_put(<<"/v1/admin/banned-words/", Word/binary>>, Token, #{<<"word">> => NewWord}),
|
||||
?assertEqual(<<"updated">>, maps:get(<<"status">>, Result)),
|
||||
% Проверяем, что старое слово отсутствует, а новое присутствует
|
||||
Words = api_test_runner:admin_get(<<"/v1/admin/banned-words">>, Token),
|
||||
?assertNot(lists:any(fun(W) -> maps:get(<<"word">>, W) =:= Word end, Words)),
|
||||
?assert(lists:any(fun(W) -> maps:get(<<"word">>, W) =:= NewWord end, Words)),
|
||||
ct:pal(" OK").
|
||||
|
||||
%% @doc POST /v1/admin/banned-words/batch – массовая загрузка.
|
||||
-spec test_batch_add_words(binary()) -> ok.
|
||||
test_batch_add_words(Token) ->
|
||||
ct:pal(" TEST: Batch add banned words"),
|
||||
Unique = integer_to_binary(erlang:system_time()),
|
||||
Words = [
|
||||
<<"batch1_", Unique/binary>>,
|
||||
<<"batch2_", Unique/binary>>,
|
||||
<<"batch3_", Unique/binary>>
|
||||
],
|
||||
Body = jsx:encode(#{<<"words">> => Words}),
|
||||
{ok, 200, _, RespBody} = api_test_runner:admin_request(post, <<"/v1/admin/banned-words/batch">>, Token, Body),
|
||||
Result = jsx:decode(list_to_binary(RespBody), [return_maps]),
|
||||
?assertEqual(3, maps:get(<<"added">>, Result)),
|
||||
?assertEqual(0, maps:get(<<"skipped">>, Result)),
|
||||
% Проверяем наличие всех слов
|
||||
AllWords = api_test_runner:admin_get(<<"/v1/admin/banned-words">>, Token),
|
||||
[?assert(lists:any(fun(W) -> maps:get(<<"word">>, W) =:= Word end, AllWords)) || Word <- Words],
|
||||
ct:pal(" OK").
|
||||
|
||||
%% @doc DELETE /v1/admin/banned-words/:word – удаление слова.
|
||||
-spec test_delete_word(binary(), binary()) -> ok.
|
||||
test_delete_word(Token, Word) ->
|
||||
@@ -91,4 +130,48 @@ test_words_pagination(Token) ->
|
||||
Id1 = maps:get(<<"id">>, hd(Page1)),
|
||||
Id2 = maps:get(<<"id">>, hd(Page2)),
|
||||
?assertNotEqual(Id1, Id2),
|
||||
ct:pal(" OK").
|
||||
ct:pal(" OK").
|
||||
|
||||
%% @doc PUT /v1/admin/banned-words/:word – попытка переименовать в уже существующее слово.
|
||||
test_update_word_conflict(Token) ->
|
||||
ct:pal(" TEST: Update banned word to existing word"),
|
||||
Unique = integer_to_binary(erlang:system_time()),
|
||||
WordA = <<"conflictA_", Unique/binary>>,
|
||||
WordB = <<"conflictB_", Unique/binary>>,
|
||||
api_test_runner:admin_post(<<"/v1/admin/banned-words">>, Token, #{<<"word">> => WordA}),
|
||||
api_test_runner:admin_post(<<"/v1/admin/banned-words">>, Token, #{<<"word">> => WordB}),
|
||||
Path = <<"/v1/admin/banned-words/", WordA/binary>>,
|
||||
{ok, 409, _, _} = api_test_runner:admin_request(put, Path, Token, jsx:encode(#{<<"word">> => WordB})),
|
||||
ct:pal(" OK: got 409 conflict").
|
||||
|
||||
%% @doc POST /v1/admin/banned-words/batch – с невалидными элементами.
|
||||
test_batch_add_invalid(Token) ->
|
||||
ct:pal(" TEST: Batch add with invalid items"),
|
||||
Unique = integer_to_binary(erlang:system_time()),
|
||||
Words = [
|
||||
<<"valid_", Unique/binary>>,
|
||||
null,
|
||||
12345,
|
||||
<<"valid2_", Unique/binary>>
|
||||
],
|
||||
Body = jsx:encode(#{<<"words">> => Words}),
|
||||
{ok, 200, _, RespBody} = api_test_runner:admin_request(post, <<"/v1/admin/banned-words/batch">>, Token, Body),
|
||||
Result = jsx:decode(list_to_binary(RespBody), [return_maps]),
|
||||
?assertEqual(2, maps:get(<<"added">>, Result)),
|
||||
?assertEqual(0, maps:get(<<"skipped">>, Result)),
|
||||
?assertEqual(2, maps:get(<<"invalid_count">>, Result)),
|
||||
ct:pal(" OK: invalid items filtered").
|
||||
|
||||
%% @doc POST /v1/admin/banned-words/batch – с дубликатами.
|
||||
test_batch_add_duplicates(Token) ->
|
||||
ct:pal(" TEST: Batch add with duplicates"),
|
||||
Unique = integer_to_binary(erlang:system_time()),
|
||||
Word1 = <<"dup1_", Unique/binary>>,
|
||||
Word2 = <<"dup2_", Unique/binary>>,
|
||||
Words = [Word1, Word2, Word1, Word2],
|
||||
Body = jsx:encode(#{<<"words">> => Words}),
|
||||
{ok, 200, _, RespBody} = api_test_runner:admin_request(post, <<"/v1/admin/banned-words/batch">>, Token, Body),
|
||||
Result = jsx:decode(list_to_binary(RespBody), [return_maps]),
|
||||
?assertEqual(2, maps:get(<<"added">>, Result)),
|
||||
?assertEqual(2, maps:get(<<"skipped">>, Result)),
|
||||
ct:pal(" OK: duplicates handled").
|
||||
Reference in New Issue
Block a user