Rewrite and expand EUnit suite; run unit tests in CI from shared test image. Refs EventHub/EventHubBack#36 [skip ci]

This commit is contained in:
2026-07-17 15:16:46 +03:00
parent 9d2780fef4
commit 4fa802702f
54 changed files with 4240 additions and 2281 deletions
+67 -127
View File
@@ -2,145 +2,85 @@
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
-define(TABLES, [admin, admin_audit, review, ticket]).
-define(ADMIN_ID, <<"adm_test_1">>).
setup() ->
ok = meck:new(cowboy_req, [non_strict]),
ok = meck:new(handler_auth, [non_strict]),
ok = meck:new(core_user, [non_strict]),
ok = meck:new(core_review, [non_strict]),
ok = meck:expect(cowboy_req, reply,
fun(Code, Headers, Body, Req) ->
put(test_reply, {Code, Headers, Body, Req})
end),
eh_test_support:start_mnesia(),
eh_test_support:ensure_tables(?TABLES),
eh_test_support:ensure_jwt(),
eh_test_support:seed_admin(#{id => ?ADMIN_ID}),
ok.
cleanup(_) ->
meck:unload(core_review),
meck:unload(core_user),
meck:unload(handler_auth),
meck:unload(cowboy_req).
eh_test_support:unload_cowboy(),
eh_test_support:delete_tables(?TABLES),
eh_test_support:stop_mnesia(),
ok.
admin_reviews_test_() ->
{setup, fun setup/0, fun cleanup/1, [
{"GET /admin/reviews/:id success", fun test_get_review/0},
{"GET /admin/reviews/:id not found", fun test_get_review_not_found/0},
{"GET /admin/reviews/:id forbidden", fun test_get_review_forbidden/0},
{"PUT /admin/reviews/:id success", fun test_update_review/0},
{"PUT /admin/reviews/:id not found", fun test_update_review_not_found/0},
{"PUT /admin/reviews/:id bad JSON", fun test_update_review_bad_json/0},
{"DELETE /admin/reviews/:id method not allowed", fun test_wrong_method/0}
{foreach, fun setup/0, fun cleanup/1, [
{"GET list success", fun test_list/0},
{"PATCH bulk success", fun test_bulk/0},
{"PATCH bulk missing reason", fun test_bulk_missing/0},
{"GET unauthorized", fun test_unauthorized/0},
{"POST method not allowed", fun test_wrong_method/0}
]}.
%% GET успех
test_get_review() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"adm1">>, Req} end),
AdminUser = #user{id = <<"adm1">>, role = admin},
ok = meck:expect(core_user, get_by_id,
fun(<<"adm1">>) -> {ok, AdminUser} end),
ok = meck:expect(cowboy_req, binding,
fun(id, _) -> <<"rv1">> end),
Review = #review{
id = <<"rv1">>,
user_id = <<"u1">>,
target_type = <<"event">>,
target_id = <<"e1">>,
rating = 5,
comment = <<"Great!">>,
status = <<"active">>,
created_at = {{2026,4,26},{12,0,0}},
updated_at = {{2026,4,26},{12,0,0}}
},
ok = meck:expect(core_review, get_by_id,
fun(<<"rv1">>) -> {ok, Review} end),
{ok, _, _} = admin_handler_reviews:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
seed_review() ->
{ok, Review} = core_review:create(<<"u1">>, calendar, <<"cal1">>, 5, <<"great">>),
Review.
test_list() ->
_ = seed_review(),
{ok, _} = core_review:create(<<"u2">>, event, <<"ev1">>, 3, <<"ok">>),
{Status, _, Body} = eh_test_support:call(admin_handler_reviews, #{
method => <<"GET">>,
path => <<"/v1/admin/reviews">>,
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
#{<<"id">> := <<"rv1">>, <<"comment">> := <<"Great!">>, <<"rating">> := 5} = jsx:decode(RespBody, [return_maps]).
Result = jsx:decode(Body, [return_maps]),
?assertEqual(2, length(Result)).
%% GET – не найдено
test_get_review_not_found() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"adm1">>, Req} end),
AdminUser = #user{id = <<"adm1">>, role = admin},
ok = meck:expect(core_user, get_by_id,
fun(<<"adm1">>) -> {ok, AdminUser} end),
ok = meck:expect(cowboy_req, binding,
fun(id, _) -> <<"rv99">> end),
ok = meck:expect(core_review, get_by_id,
fun(_) -> {error, not_found} end),
{ok, _, _} = admin_handler_reviews:init(req, []),
{Status, _, _, _} = erase(test_reply),
?assertEqual(404, Status).
%% GET запрещён
test_get_review_forbidden() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {error, 403, <<"Admin access required">>, Req} end),
{ok, _, _} = admin_handler_reviews:init(req, []),
{Status, _, _, _} = erase(test_reply),
?assertEqual(403, Status).
%% PUT успех
test_update_review() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"PUT">> end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"adm1">>, Req} end),
AdminUser = #user{id = <<"adm1">>, role = admin},
ok = meck:expect(core_user, get_by_id,
fun(<<"adm1">>) -> {ok, AdminUser} end),
ok = meck:expect(cowboy_req, binding,
fun(id, _) -> <<"rv1">> end),
ok = meck:expect(cowboy_req, read_body,
fun(Req) -> {ok, jsx:encode(#{<<"status">> => <<"hidden">>}), Req} end),
Updated = #review{id = <<"rv1">>, status = <<"hidden">>},
ok = meck:expect(core_review, update_status,
fun(<<"rv1">>, <<"hidden">>) -> {ok, Updated} end),
{ok, _, _} = admin_handler_reviews:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
test_bulk() ->
Review = seed_review(),
Id = Review#review.id,
{Status, _, Body} = eh_test_support:call(admin_handler_reviews, #{
method => <<"PATCH">>,
path => <<"/v1/admin/reviews">>,
auth => ?ADMIN_ID,
body => jsx:encode(#{
<<"operations">> => [#{<<"id">> => Id, <<"status">> => <<"hidden">>}],
<<"reason">> => <<"moderation">>
})
}),
?assertEqual(200, Status),
#{<<"status">> := <<"hidden">>} = jsx:decode(RespBody, [return_maps]).
#{<<"updated_count">> := 1} = jsx:decode(Body, [return_maps]),
Audits = core_admin_audit:list(),
?assert(length(Audits) >= 1).
%% PUT – не найдено
test_update_review_not_found() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"PUT">> end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"adm1">>, Req} end),
AdminUser = #user{id = <<"adm1">>, role = admin},
ok = meck:expect(core_user, get_by_id,
fun(<<"adm1">>) -> {ok, AdminUser} end),
ok = meck:expect(cowboy_req, binding,
fun(id, _) -> <<"rv99">> end),
ok = meck:expect(cowboy_req, read_body,
fun(Req) -> {ok, jsx:encode(#{<<"status">> => <<"hidden">>}), Req} end),
ok = meck:expect(core_review, update_status,
fun(_, _) -> {error, not_found} end),
{ok, _, _} = admin_handler_reviews:init(req, []),
{Status, _, _, _} = erase(test_reply),
?assertEqual(404, Status).
%% PUT – невалидный JSON
test_update_review_bad_json() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"PUT">> end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"adm1">>, Req} end),
AdminUser = #user{id = <<"adm1">>, role = admin},
ok = meck:expect(core_user, get_by_id,
fun(<<"adm1">>) -> {ok, AdminUser} end),
ok = meck:expect(cowboy_req, binding,
fun(id, _) -> <<"rv1">> end),
ok = meck:expect(cowboy_req, read_body,
fun(Req) -> {ok, <<"bad json">>, Req} end),
{ok, _, _} = admin_handler_reviews:init(req, []),
{Status, _, _, _} = erase(test_reply),
test_bulk_missing() ->
{Status, _, _} = eh_test_support:call(admin_handler_reviews, #{
method => <<"PATCH">>,
path => <<"/v1/admin/reviews">>,
auth => ?ADMIN_ID,
body => jsx:encode(#{<<"operations">> => []})
}),
?assertEqual(400, Status).
%% Неверный метод
test_unauthorized() ->
{Status, _, _} = eh_test_support:call(admin_handler_reviews, #{
method => <<"GET">>,
path => <<"/v1/admin/reviews">>,
auth => none
}),
?assertEqual(401, Status).
test_wrong_method() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"DELETE">> end),
{ok, _, _} = admin_handler_reviews:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
?assertEqual(405, Status),
#{<<"error">> := <<"Method not allowed">>} = jsx:decode(RespBody, [return_maps]).
{Status, _, _} = eh_test_support:call(admin_handler_reviews, #{
method => <<"POST">>,
path => <<"/v1/admin/reviews">>,
auth => ?ADMIN_ID
}),
?assertEqual(405, Status).