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
+69 -51
View File
@@ -1,65 +1,83 @@
-module(admin_handler_users_tests).
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
-define(TABLES, [admin, admin_audit, user, ticket]).
-define(ADMIN_ID, <<"adm_test_1">>).
setup() ->
ok = meck:new(cowboy_req, [non_strict]),
ok = meck:new(handler_auth, [non_strict]), % вместо auth
ok = meck:new(core_user, [non_strict]),
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_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_users_test_() ->
{setup, fun setup/0, fun cleanup/1, [
{"GET /admin/users with valid admin token returns 200 and list of users", fun test_list_users/0},
{"GET /admin/users with non-admin token returns 403", fun test_list_users_forbidden/0},
{"POST /admin/users returns 405", fun test_wrong_method/0}
{foreach, fun setup/0, fun cleanup/1, [
{"GET list success", fun test_list/0},
{"GET list filter by status", fun test_list_filter/0},
{"GET unauthorized", fun test_unauthorized/0},
{"POST method not allowed", fun test_wrong_method/0}
]}.
test_list_users() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
ok = meck:expect(handler_auth, authenticate,
fun(Req) -> {ok, <<"admin1">>, Req} end),
User = #{
id => <<"user1">>,
email => <<"user@test.com">>,
role => <<"user">>,
status => <<"active">>,
created_at => {{2025,4,27},{12,0,0}},
updated_at => {{2025,4,27},{12,30,0}}
},
ok = meck:expect(core_user, list_users, fun() -> {ok, [User]} end),
ok = meck:expect(cowboy_req, reply, fun(Code, Headers, Body, Req) ->
put(test_reply, {Code, Headers, Body, Req})
end),
{ok, _, _} = admin_handler_users:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
?assertEqual(200, Status),
Users = jsx:decode(RespBody, [return_maps]),
?assertEqual(1, length(Users)),
?assertEqual(<<"user1">>, maps:get(<<"id">>, hd(Users))).
seed_users() ->
ok = mnesia:dirty_write(eh_test_support:make_user(#{
id => <<"u1">>,
email => <<"alice@test.local">>,
nickname => <<"alice">>,
status => active
})),
ok = mnesia:dirty_write(eh_test_support:make_user(#{
id => <<"u2">>,
email => <<"bob@test.local">>,
nickname => <<"bob">>,
status => frozen
})),
ok.
test_list_users_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 = meck:expect(cowboy_req, reply, fun(Code, Headers, Body, Req) ->
put(test_reply, {Code, Headers, Body, Req})
end),
{ok, _, _} = admin_handler_users:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
?assertEqual(403, Status),
?assertEqual(#{<<"error">> => <<"Admin access required">>}, jsx:decode(RespBody, [return_maps])).
test_list() ->
seed_users(),
{Status, _, Body} = eh_test_support:call(admin_handler_users, #{
method => <<"GET">>,
path => <<"/v1/admin/users">>,
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual(2, length(Result)).
test_list_filter() ->
seed_users(),
{Status, _, Body} = eh_test_support:call(admin_handler_users, #{
method => <<"GET">>,
path => <<"/v1/admin/users">>,
auth => ?ADMIN_ID,
qs => [{<<"status">>, <<"active">>}]
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual(1, length(Result)),
[Only] = Result,
?assertEqual(<<"alice@test.local">>, maps:get(<<"email">>, Only)).
test_unauthorized() ->
{Status, _, _} = eh_test_support:call(admin_handler_users, #{
method => <<"GET">>,
path => <<"/v1/admin/users">>,
auth => none
}),
?assertEqual(401, Status).
test_wrong_method() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"POST">> end),
ok = meck:expect(cowboy_req, reply, fun(Code, Headers, Body, Req) ->
put(test_reply, {Code, Headers, Body, Req})
end),
{ok, _, _} = admin_handler_users:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
?assertEqual(405, Status),
?assertEqual(#{<<"error">> => <<"Method not allowed">>}, jsx:decode(RespBody, [return_maps])).
{Status, _, _} = eh_test_support:call(admin_handler_users, #{
method => <<"POST">>,
path => <<"/v1/admin/users">>,
auth => ?ADMIN_ID
}),
?assertEqual(405, Status).