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
+88 -160
View File
@@ -2,186 +2,114 @@
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
-define(TABLES, [admin, admin_audit, 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_ticket, [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_ticket),
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_ticket_by_id_test_() ->
{setup, fun setup/0, fun cleanup/1, [
{"GET /admin/tickets/:id success", fun test_get/0},
{"GET /admin/tickets/:id not found", fun test_get_not_found/0},
{"GET /admin/tickets/:id forbidden", fun test_get_forbidden/0},
{"PUT /admin/tickets/:id success", fun test_update/0},
{"PUT /admin/tickets/:id not found", fun test_update_not_found/0},
{"PUT /admin/tickets/:id bad JSON", fun test_update_bad_json/0},
{"DELETE /admin/tickets/:id success", fun test_delete/0},
{"DELETE /admin/tickets/:id not found", fun test_delete_not_found/0},
{"POST /admin/tickets/:id method not allowed", fun test_wrong_method/0}
{foreach, fun setup/0, fun cleanup/1, [
{"GET success", fun test_get/0},
{"GET not found", fun test_get_not_found/0},
{"PUT resolve", fun test_resolve/0},
{"DELETE success", fun test_delete/0},
{"DELETE not found", fun test_delete_not_found/0},
{"GET unauthorized", fun test_unauthorized/0},
{"PATCH method not allowed", fun test_wrong_method/0}
]}.
%% GET успех
seed_ticket() ->
{ok, Ticket} = core_ticket:create(<<"u1">>, <<"hash_x">>, <<"boom">>, <<"stack">>),
Ticket.
test_get() ->
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, _) -> <<"t1">> end),
Ticket = #ticket{
id = <<"t1">>,
error_hash = <<"hash">>,
error_message = <<"msg">>,
stacktrace = <<"trace">>,
context = <<"ctx">>,
count = 5,
first_seen = {{2026,4,27},{12,0,0}},
last_seen = {{2026,4,27},{13,0,0}},
status = open,
assigned_to = <<"dev1">>,
resolution_note = undefined
},
ok = meck:expect(core_ticket, get_by_id,
fun(<<"t1">>) -> {ok, Ticket} end),
{ok, _, _} = admin_handler_ticket_by_id:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
Ticket = seed_ticket(),
Id = Ticket#ticket.id,
{Status, _, Body} = eh_test_support:call(admin_handler_ticket_by_id, #{
method => <<"GET">>,
path => <<"/v1/admin/tickets/", Id/binary>>,
bindings => #{id => Id},
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
#{<<"id">> := <<"t1">>, <<"error_message">> := <<"msg">>, <<"count">> := 5} = jsx:decode(RespBody, [return_maps]).
Result = jsx:decode(Body, [return_maps]),
?assertEqual(Id, maps:get(<<"id">>, Result)),
?assertEqual(<<"boom">>, maps:get(<<"error_message">>, Result)).
%% GET – не найдено
test_get_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, _) -> <<"t99">> end),
ok = meck:expect(core_ticket, get_by_id,
fun(_) -> {error, not_found} end),
{ok, _, _} = admin_handler_ticket_by_id:init(req, []),
{Status, _, _, _} = erase(test_reply),
{Status, _, _} = eh_test_support:call(admin_handler_ticket_by_id, #{
method => <<"GET">>,
path => <<"/v1/admin/tickets/missing">>,
bindings => #{id => <<"missing">>},
auth => ?ADMIN_ID
}),
?assertEqual(404, Status).
%% GET запрещён
test_get_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_ticket_by_id:init(req, []),
{Status, _, _, _} = erase(test_reply),
?assertEqual(403, Status).
%% PUT успех
test_update() ->
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, _) -> <<"t1">> end),
ok = meck:expect(cowboy_req, read_body,
fun(Req) -> {ok, jsx:encode(#{<<"status">> => <<"closed">>, <<"assigned_to">> => <<"adm2">>}), Req} end),
Updated = #ticket{id = <<"t1">>, status = closed, assigned_to = <<"adm2">>},
ok = meck:expect(core_ticket, update_ticket,
fun(<<"t1">>, _) -> {ok, Updated} end),
{ok, _, _} = admin_handler_ticket_by_id:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
test_resolve() ->
Ticket = seed_ticket(),
Id = Ticket#ticket.id,
{Status, _, Body} = eh_test_support:call(admin_handler_ticket_by_id, #{
method => <<"PUT">>,
path => <<"/v1/admin/tickets/", Id/binary>>,
bindings => #{id => Id},
auth => ?ADMIN_ID,
body => jsx:encode(#{
<<"status">> => <<"resolved">>,
<<"resolution_note">> => <<"fixed">>
})
}),
?assertEqual(200, Status),
#{<<"status">> := <<"closed">>, <<"assigned_to">> := <<"adm2">>} = jsx:decode(RespBody, [return_maps]).
Result = jsx:decode(Body, [return_maps]),
?assertEqual(<<"resolved">>, maps:get(<<"status">>, Result)),
?assertEqual(<<"fixed">>, maps:get(<<"resolution_note">>, Result)).
%% PUT – не найдено
test_update_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, _) -> <<"t99">> end),
ok = meck:expect(cowboy_req, read_body,
fun(Req) -> {ok, jsx:encode(#{<<"status">> => <<"closed">>}), Req} end),
ok = meck:expect(core_ticket, update_ticket,
fun(_, _) -> {error, not_found} end),
{ok, _, _} = admin_handler_ticket_by_id:init(req, []),
{Status, _, _, _} = erase(test_reply),
?assertEqual(404, Status).
%% PUT – невалидный JSON
test_update_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, _) -> <<"t1">> end),
ok = meck:expect(cowboy_req, read_body,
fun(Req) -> {ok, <<"not json">>, Req} end),
{ok, _, _} = admin_handler_ticket_by_id:init(req, []),
{Status, _, _, _} = erase(test_reply),
?assertEqual(400, Status).
%% DELETE успех
test_delete() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"DELETE">> 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, _) -> <<"t1">> end),
ok = meck:expect(core_ticket, delete_ticket,
fun(<<"t1">>) -> {ok, deleted} end),
{ok, _, _} = admin_handler_ticket_by_id:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
Ticket = seed_ticket(),
Id = Ticket#ticket.id,
{Status, _, Body} = eh_test_support:call(admin_handler_ticket_by_id, #{
method => <<"DELETE">>,
path => <<"/v1/admin/tickets/", Id/binary>>,
bindings => #{id => Id},
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
#{<<"status">> := <<"deleted">>} = jsx:decode(RespBody, [return_maps]).
#{<<"status">> := <<"deleted">>} = jsx:decode(Body, [return_maps]),
?assertEqual({error, not_found}, core_ticket:get_by_id(Id)).
%% DELETE – не найдено
test_delete_not_found() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"DELETE">> 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, _) -> <<"t99">> end),
ok = meck:expect(core_ticket, delete_ticket,
fun(_) -> {error, not_found} end),
{ok, _, _} = admin_handler_ticket_by_id:init(req, []),
{Status, _, _, _} = erase(test_reply),
{Status, _, _} = eh_test_support:call(admin_handler_ticket_by_id, #{
method => <<"DELETE">>,
path => <<"/v1/admin/tickets/missing">>,
bindings => #{id => <<"missing">>},
auth => ?ADMIN_ID
}),
?assertEqual(404, Status).
%% Неверный метод
test_unauthorized() ->
{Status, _, _} = eh_test_support:call(admin_handler_ticket_by_id, #{
method => <<"GET">>,
path => <<"/v1/admin/tickets/x">>,
bindings => #{id => <<"x">>},
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_ticket_by_id: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_ticket_by_id, #{
method => <<"PATCH">>,
path => <<"/v1/admin/tickets/x">>,
bindings => #{id => <<"x">>},
auth => ?ADMIN_ID
}),
?assertEqual(405, Status).