98 lines
2.8 KiB
Erlang
98 lines
2.8 KiB
Erlang
-module(admin_handler_audit_tests).
|
||
-include_lib("eunit/include/eunit.hrl").
|
||
-include("records.hrl").
|
||
|
||
-define(TABLES, [admin, admin_audit, ticket]).
|
||
-define(ADMIN_ID, <<"adm_test_1">>).
|
||
-define(PLAIN_ID, <<"adm_plain_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, role => superadmin}),
|
||
ok.
|
||
|
||
cleanup(_) ->
|
||
eh_test_support:unload_cowboy(),
|
||
eh_test_support:delete_tables(?TABLES),
|
||
eh_test_support:stop_mnesia(),
|
||
ok.
|
||
|
||
admin_audit_test_() ->
|
||
{foreach, fun setup/0, fun cleanup/1, [
|
||
{"GET list – success", {timeout, 60, fun test_list/0}},
|
||
{"GET list – filter by action", {timeout, 60, fun test_list_filter/0}},
|
||
{"GET – unauthorized", {timeout, 60, fun test_unauthorized/0}},
|
||
{"GET – forbidden for non-superadmin", {timeout, 60, fun test_forbidden/0}},
|
||
{"POST – method not allowed", {timeout, 60, fun test_wrong_method/0}}
|
||
]}.
|
||
|
||
seed_audit() ->
|
||
{ok, _} = core_admin_audit:log(
|
||
?ADMIN_ID, <<"admin@test.local">>, superadmin,
|
||
<<"create_admin">>, <<"admin">>, <<"adm_x">>, <<"127.0.0.1">>
|
||
),
|
||
{ok, _} = core_admin_audit:log(
|
||
?ADMIN_ID, <<"admin@test.local">>, superadmin,
|
||
<<"update_me">>, <<"admin">>, ?ADMIN_ID, <<"127.0.0.1">>
|
||
),
|
||
ok.
|
||
|
||
test_list() ->
|
||
seed_audit(),
|
||
{Status, _, Body} = eh_test_support:call(admin_handler_audit, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/audit">>,
|
||
auth => ?ADMIN_ID
|
||
}),
|
||
?assertEqual(200, Status),
|
||
Result = jsx:decode(Body, [return_maps]),
|
||
?assertEqual(2, length(Result)),
|
||
[First | _] = Result,
|
||
?assert(is_map_key(<<"action">>, First)),
|
||
?assert(is_map_key(<<"admin_id">>, First)).
|
||
|
||
test_list_filter() ->
|
||
seed_audit(),
|
||
{Status, _, Body} = eh_test_support:call(admin_handler_audit, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/audit">>,
|
||
auth => ?ADMIN_ID,
|
||
qs => [{<<"action">>, <<"update_me">>}]
|
||
}),
|
||
?assertEqual(200, Status),
|
||
Result = jsx:decode(Body, [return_maps]),
|
||
?assertEqual(1, length(Result)),
|
||
[Only] = Result,
|
||
?assertEqual(<<"update_me">>, maps:get(<<"action">>, Only)).
|
||
|
||
test_unauthorized() ->
|
||
{Status, _, _} = eh_test_support:call(admin_handler_audit, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/audit">>,
|
||
auth => none
|
||
}),
|
||
?assertEqual(401, Status).
|
||
|
||
test_forbidden() ->
|
||
ok = mnesia:dirty_write(eh_test_support:make_admin(#{
|
||
id => ?PLAIN_ID,
|
||
email => <<"plain@test.local">>,
|
||
role => admin
|
||
})),
|
||
{Status, _, _} = eh_test_support:call(admin_handler_audit, #{
|
||
method => <<"GET">>,
|
||
path => <<"/v1/admin/audit">>,
|
||
auth => ?PLAIN_ID
|
||
}),
|
||
?assertEqual(403, Status).
|
||
|
||
test_wrong_method() ->
|
||
{Status, _, _} = eh_test_support:call(admin_handler_audit, #{
|
||
method => <<"POST">>,
|
||
path => <<"/v1/admin/audit">>,
|
||
auth => ?ADMIN_ID
|
||
}),
|
||
?assertEqual(405, Status).
|