-module(admin_handler_ticket_by_id_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_ticket_by_id_test_() -> {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} ]}. seed_ticket() -> {ok, Ticket} = core_ticket:create(<<"u1">>, <<"hash_x">>, <<"boom">>, <<"stack">>), Ticket. test_get() -> 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), Result = jsx:decode(Body, [return_maps]), ?assertEqual(Id, maps:get(<<"id">>, Result)), ?assertEqual(<<"boom">>, maps:get(<<"error_message">>, Result)). test_get_not_found() -> {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). 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), Result = jsx:decode(Body, [return_maps]), ?assertEqual(<<"resolved">>, maps:get(<<"status">>, Result)), ?assertEqual(<<"fixed">>, maps:get(<<"resolution_note">>, Result)). test_delete() -> 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(Body, [return_maps]), ?assertEqual({error, not_found}, core_ticket:get_by_id(Id)). test_delete_not_found() -> {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() -> {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).