104 lines
3.2 KiB
Erlang
104 lines
3.2 KiB
Erlang
-module(admin_handler_tickets_tests).
|
||
-include_lib("eunit/include/eunit.hrl").
|
||
-include("records.hrl").
|
||
|
||
-define(TABLES, [admin, admin_audit, ticket]).
|
||
-define(ADMIN_ID, <<"adm_test_1">>).
|
||
|
||
setup() ->
|
||
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(_) ->
|
||
eh_test_support:unload_cowboy(),
|
||
eh_test_support:delete_tables(?TABLES),
|
||
eh_test_support:stop_mnesia(),
|
||
ok.
|
||
|
||
admin_tickets_test_() ->
|
||
{foreach, fun setup/0, fun cleanup/1, [
|
||
{"GET list – success", fun test_list/0},
|
||
{"GET list – filter by status", fun test_list_filter/0},
|
||
{"PUT update – success", fun test_update/0},
|
||
{"PUT update – not found", fun test_update_not_found/0},
|
||
{"GET – unauthorized", fun test_unauthorized/0},
|
||
{"POST – method not allowed", fun test_wrong_method/0}
|
||
]}.
|
||
|
||
seed_ticket() ->
|
||
{ok, Ticket} = core_ticket:create(<<"u1">>, <<"hash_a">>, <<"error A">>, <<"stack">>),
|
||
Ticket.
|
||
|
||
test_list() ->
|
||
_ = seed_ticket(),
|
||
{ok, _} = core_ticket:create(<<"u2">>, <<"hash_b">>, <<"error B">>, <<"stack">>),
|
||
{Status, _, Body} = eh_test_support:call(admin_handler_tickets, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/tickets">>,
|
||
auth => ?ADMIN_ID
|
||
}),
|
||
?assertEqual(200, Status),
|
||
Result = jsx:decode(Body, [return_maps]),
|
||
?assertEqual(2, length(Result)).
|
||
|
||
test_list_filter() ->
|
||
Ticket = seed_ticket(),
|
||
{ok, _} = logic_ticket:update_status(?ADMIN_ID, Ticket#ticket.id, in_progress),
|
||
{ok, _} = core_ticket:create(<<"u2">>, <<"hash_b">>, <<"error B">>, <<"stack">>),
|
||
{Status, _, Body} = eh_test_support:call(admin_handler_tickets, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/tickets">>,
|
||
auth => ?ADMIN_ID,
|
||
qs => [{<<"status">>, <<"open">>}]
|
||
}),
|
||
?assertEqual(200, Status),
|
||
Result = jsx:decode(Body, [return_maps]),
|
||
?assertEqual(1, length(Result)),
|
||
[Only] = Result,
|
||
?assertEqual(<<"open">>, maps:get(<<"status">>, Only)).
|
||
|
||
test_update() ->
|
||
Ticket = seed_ticket(),
|
||
Id = Ticket#ticket.id,
|
||
{Status, _, Body} = eh_test_support:call(admin_handler_tickets, #{
|
||
method => <<"PUT">>,
|
||
path => <<"/v1/admin/tickets/", Id/binary>>,
|
||
bindings => #{id => Id},
|
||
auth => ?ADMIN_ID,
|
||
body => jsx:encode(#{<<"status">> => <<"in_progress">>})
|
||
}),
|
||
?assertEqual(200, Status),
|
||
Result = jsx:decode(Body, [return_maps]),
|
||
?assertEqual(<<"in_progress">>, maps:get(<<"status">>, Result)),
|
||
Audits = core_admin_audit:list(),
|
||
?assert(length(Audits) >= 1).
|
||
|
||
test_update_not_found() ->
|
||
{Status, _, _} = eh_test_support:call(admin_handler_tickets, #{
|
||
method => <<"PUT">>,
|
||
path => <<"/v1/admin/tickets/missing">>,
|
||
bindings => #{id => <<"missing">>},
|
||
auth => ?ADMIN_ID,
|
||
body => jsx:encode(#{<<"status">> => <<"closed">>})
|
||
}),
|
||
?assertEqual(404, Status).
|
||
|
||
test_unauthorized() ->
|
||
{Status, _, _} = eh_test_support:call(admin_handler_tickets, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/tickets">>,
|
||
auth => none
|
||
}),
|
||
?assertEqual(401, Status).
|
||
|
||
test_wrong_method() ->
|
||
{Status, _, _} = eh_test_support:call(admin_handler_tickets, #{
|
||
method => <<"POST">>,
|
||
path => <<"/v1/admin/tickets">>,
|
||
auth => ?ADMIN_ID
|
||
}),
|
||
?assertEqual(405, Status).
|