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:
@@ -2,60 +2,70 @@
|
||||
-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_stats_test_() ->
|
||||
{setup, fun setup/0, fun cleanup/1, [
|
||||
{"GET /admin/tickets/stats – success", fun test_stats/0},
|
||||
{"GET /admin/tickets/stats – forbidden", fun test_stats_forbidden/0},
|
||||
{"POST /admin/tickets/stats – method not allowed", fun test_wrong_method/0}
|
||||
{foreach, fun setup/0, fun cleanup/1, [
|
||||
{"GET stats – empty", fun test_stats_empty/0},
|
||||
{"GET stats – with tickets", fun test_stats_with_data/0},
|
||||
{"GET – unauthorized", fun test_unauthorized/0},
|
||||
{"POST – method not allowed", fun test_wrong_method/0}
|
||||
]}.
|
||||
|
||||
test_stats() ->
|
||||
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),
|
||||
StatsData = #{
|
||||
open => 5,
|
||||
in_progress => 3,
|
||||
resolved => 12,
|
||||
closed => 20
|
||||
},
|
||||
ok = meck:expect(core_ticket, stats, fun() -> StatsData end),
|
||||
{ok, _, _} = admin_handler_ticket_stats:init(req, []),
|
||||
{Status, _, RespBody, _} = erase(test_reply),
|
||||
test_stats_empty() ->
|
||||
{Status, _, Body} = eh_test_support:call(admin_handler_ticket_stats, #{
|
||||
method => <<"GET">>,
|
||||
path => <<"/v1/admin/tickets/stats">>,
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(200, Status),
|
||||
#{<<"open">> := 5, <<"resolved">> := 12} = jsx:decode(RespBody, [return_maps]).
|
||||
Result = jsx:decode(Body, [return_maps]),
|
||||
?assertEqual(0, maps:get(<<"total_tickets">>, Result)),
|
||||
?assertEqual(0, maps:get(<<"open">>, Result)),
|
||||
?assertEqual(0, maps:get(<<"total_errors">>, Result)).
|
||||
|
||||
test_stats_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_stats:init(req, []),
|
||||
{Status, _, _, _} = erase(test_reply),
|
||||
?assertEqual(403, Status).
|
||||
test_stats_with_data() ->
|
||||
{ok, T1} = core_ticket:create(<<"u1">>, <<"h1">>, <<"err1">>, <<"s">>),
|
||||
{ok, _} = core_ticket:create(<<"u2">>, <<"h2">>, <<"err2">>, <<"s">>),
|
||||
{ok, _} = logic_ticket:update_status(?ADMIN_ID, T1#ticket.id, in_progress),
|
||||
{Status, _, Body} = eh_test_support:call(admin_handler_ticket_stats, #{
|
||||
method => <<"GET">>,
|
||||
path => <<"/v1/admin/tickets/stats">>,
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(200, Status),
|
||||
Result = jsx:decode(Body, [return_maps]),
|
||||
?assertEqual(2, maps:get(<<"total_tickets">>, Result)),
|
||||
?assertEqual(1, maps:get(<<"open">>, Result)),
|
||||
?assertEqual(1, maps:get(<<"in_progress">>, Result)),
|
||||
?assertEqual(2, maps:get(<<"total_errors">>, Result)).
|
||||
|
||||
test_unauthorized() ->
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_ticket_stats, #{
|
||||
method => <<"GET">>,
|
||||
path => <<"/v1/admin/tickets/stats">>,
|
||||
auth => none
|
||||
}),
|
||||
?assertEqual(401, Status).
|
||||
|
||||
test_wrong_method() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"POST">> end),
|
||||
{ok, _, _} = admin_handler_ticket_stats: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_stats, #{
|
||||
method => <<"POST">>,
|
||||
path => <<"/v1/admin/tickets/stats">>,
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(405, Status).
|
||||
|
||||
Reference in New Issue
Block a user