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:
@@ -0,0 +1,153 @@
|
||||
-module(admin_handler_admins_by_id_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-define(TABLES, [admin, admin_audit, ticket]).
|
||||
-define(ADMIN_ID, <<"adm_test_1">>).
|
||||
-define(TARGET_ID, <<"adm_target_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 = mnesia:dirty_write(eh_test_support:make_admin(#{
|
||||
id => ?TARGET_ID,
|
||||
email => <<"target@test.local">>,
|
||||
role => admin,
|
||||
nickname => <<"target">>
|
||||
})),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
eh_test_support:unload_cowboy(),
|
||||
eh_test_support:delete_tables(?TABLES),
|
||||
eh_test_support:stop_mnesia(),
|
||||
ok.
|
||||
|
||||
admin_admins_by_id_test_() ->
|
||||
{foreach, fun setup/0, fun cleanup/1, [
|
||||
{"GET – success", {timeout, 60, fun test_get/0}},
|
||||
{"GET – not found", {timeout, 60, fun test_get_not_found/0}},
|
||||
{"GET – unauthorized", {timeout, 60, fun test_unauthorized/0}},
|
||||
{"PUT – update nickname", {timeout, 60, fun test_update/0}},
|
||||
{"PUT – not found", {timeout, 60, fun test_update_not_found/0}},
|
||||
{"PUT – forbidden for non-superadmin", {timeout, 60, fun test_update_forbidden/0}},
|
||||
{"DELETE – success", {timeout, 60, fun test_delete/0}},
|
||||
{"DELETE – not found", {timeout, 60, fun test_delete_not_found/0}},
|
||||
{"DELETE – forbidden for non-superadmin", {timeout, 60, fun test_delete_forbidden/0}},
|
||||
{"PATCH – method not allowed", {timeout, 60, fun test_wrong_method/0}}
|
||||
]}.
|
||||
|
||||
test_get() ->
|
||||
{Status, _, Body} = eh_test_support:call(admin_handler_admins_by_id, #{
|
||||
method => <<"GET">>,
|
||||
path => <<"/v1/admin/admins/", ?TARGET_ID/binary>>,
|
||||
bindings => #{id => ?TARGET_ID},
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(200, Status),
|
||||
Result = jsx:decode(Body, [return_maps]),
|
||||
?assertEqual(?TARGET_ID, maps:get(<<"id">>, Result)),
|
||||
?assertEqual(<<"target@test.local">>, maps:get(<<"email">>, Result)).
|
||||
|
||||
test_get_not_found() ->
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_admins_by_id, #{
|
||||
method => <<"GET">>,
|
||||
path => <<"/v1/admin/admins/missing">>,
|
||||
bindings => #{id => <<"missing">>},
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(404, Status).
|
||||
|
||||
test_unauthorized() ->
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_admins_by_id, #{
|
||||
method => <<"GET">>,
|
||||
path => <<"/v1/admin/admins/", ?TARGET_ID/binary>>,
|
||||
bindings => #{id => ?TARGET_ID},
|
||||
auth => none
|
||||
}),
|
||||
?assertEqual(401, Status).
|
||||
|
||||
test_update() ->
|
||||
{Status, _, Body} = eh_test_support:call(admin_handler_admins_by_id, #{
|
||||
method => <<"PUT">>,
|
||||
path => <<"/v1/admin/admins/", ?TARGET_ID/binary>>,
|
||||
bindings => #{id => ?TARGET_ID},
|
||||
auth => ?ADMIN_ID,
|
||||
body => jsx:encode(#{<<"nickname">> => <<"updated">>})
|
||||
}),
|
||||
?assertEqual(200, Status),
|
||||
Result = jsx:decode(Body, [return_maps]),
|
||||
?assertEqual(<<"updated">>, maps:get(<<"nickname">>, Result)),
|
||||
Audits = core_admin_audit:list(),
|
||||
?assert(length(Audits) >= 1).
|
||||
|
||||
test_update_not_found() ->
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_admins_by_id, #{
|
||||
method => <<"PUT">>,
|
||||
path => <<"/v1/admin/admins/missing">>,
|
||||
bindings => #{id => <<"missing">>},
|
||||
auth => ?ADMIN_ID,
|
||||
body => jsx:encode(#{<<"nickname">> => <<"x">>})
|
||||
}),
|
||||
?assertEqual(404, Status).
|
||||
|
||||
test_update_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_admins_by_id, #{
|
||||
method => <<"PUT">>,
|
||||
path => <<"/v1/admin/admins/", ?TARGET_ID/binary>>,
|
||||
bindings => #{id => ?TARGET_ID},
|
||||
auth => ?PLAIN_ID,
|
||||
body => jsx:encode(#{<<"nickname">> => <<"nope">>})
|
||||
}),
|
||||
?assertEqual(403, Status).
|
||||
|
||||
test_delete() ->
|
||||
{Status, _, Body} = eh_test_support:call(admin_handler_admins_by_id, #{
|
||||
method => <<"DELETE">>,
|
||||
path => <<"/v1/admin/admins/", ?TARGET_ID/binary>>,
|
||||
bindings => #{id => ?TARGET_ID},
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(200, Status),
|
||||
#{<<"status">> := <<"deleted">>} = jsx:decode(Body, [return_maps]),
|
||||
?assertMatch({error, not_found}, core_admin:get_by_id(?TARGET_ID)).
|
||||
|
||||
test_delete_not_found() ->
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_admins_by_id, #{
|
||||
method => <<"DELETE">>,
|
||||
path => <<"/v1/admin/admins/missing">>,
|
||||
bindings => #{id => <<"missing">>},
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(404, Status).
|
||||
|
||||
test_delete_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_admins_by_id, #{
|
||||
method => <<"DELETE">>,
|
||||
path => <<"/v1/admin/admins/", ?TARGET_ID/binary>>,
|
||||
bindings => #{id => ?TARGET_ID},
|
||||
auth => ?PLAIN_ID
|
||||
}),
|
||||
?assertEqual(403, Status).
|
||||
|
||||
test_wrong_method() ->
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_admins_by_id, #{
|
||||
method => <<"PATCH">>,
|
||||
path => <<"/v1/admin/admins/", ?TARGET_ID/binary>>,
|
||||
bindings => #{id => ?TARGET_ID},
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(405, Status).
|
||||
Reference in New Issue
Block a user