Files
EventHubBack/test/unit/admin_handler_me_tests.erl
T

90 lines
2.7 KiB
Erlang
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-module(admin_handler_me_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,
role => admin,
nickname => <<"me-admin">>,
email => <<"me@test.local">>
}),
ok.
cleanup(_) ->
eh_test_support:unload_cowboy(),
eh_test_support:delete_tables(?TABLES),
eh_test_support:stop_mnesia(),
ok.
admin_me_test_() ->
{foreach, fun setup/0, fun cleanup/1, [
{"GET success", {timeout, 60, fun test_get/0}},
{"GET unauthorized", {timeout, 60, fun test_unauthorized/0}},
{"PUT update profile", {timeout, 60, fun test_update/0}},
{"PUT unauthorized", {timeout, 60, fun test_update_unauthorized/0}},
{"POST method not allowed", {timeout, 60, fun test_wrong_method/0}}
]}.
test_get() ->
{Status, _, Body} = eh_test_support:call(admin_handler_me, #{
method => <<"GET">>,
path => <<"/v1/admin/me">>,
auth => ?ADMIN_ID
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual(?ADMIN_ID, maps:get(<<"id">>, Result)),
?assertEqual(<<"me@test.local">>, maps:get(<<"email">>, Result)),
?assertEqual(<<"me-admin">>, maps:get(<<"nickname">>, Result)).
test_unauthorized() ->
{Status, _, _} = eh_test_support:call(admin_handler_me, #{
method => <<"GET">>,
path => <<"/v1/admin/me">>,
auth => none
}),
?assertEqual(401, Status).
test_update() ->
{Status, _, Body} = eh_test_support:call(admin_handler_me, #{
method => <<"PUT">>,
path => <<"/v1/admin/me">>,
auth => ?ADMIN_ID,
body => jsx:encode(#{
<<"nickname">> => <<"new-nick">>,
<<"timezone">> => <<"Europe/Moscow">>,
<<"language">> => <<"en">>
})
}),
?assertEqual(200, Status),
Result = jsx:decode(Body, [return_maps]),
?assertEqual(<<"new-nick">>, maps:get(<<"nickname">>, Result)),
?assertEqual(<<"Europe/Moscow">>, maps:get(<<"timezone">>, Result)),
?assertEqual(<<"en">>, maps:get(<<"language">>, Result)),
Audits = core_admin_audit:list(),
?assert(length(Audits) >= 1).
test_update_unauthorized() ->
{Status, _, _} = eh_test_support:call(admin_handler_me, #{
method => <<"PUT">>,
path => <<"/v1/admin/me">>,
auth => none,
body => jsx:encode(#{<<"nickname">> => <<"x">>})
}),
?assertEqual(401, Status).
test_wrong_method() ->
{Status, _, _} = eh_test_support:call(admin_handler_me, #{
method => <<"POST">>,
path => <<"/v1/admin/me">>,
auth => ?ADMIN_ID
}),
?assertEqual(405, Status).