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,121 @@
|
||||
-module(core_admin_audit_tests).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-define(TABLES, [admin_audit]).
|
||||
|
||||
setup() ->
|
||||
eh_test_support:start_mnesia(),
|
||||
eh_test_support:ensure_tables(?TABLES),
|
||||
ok.
|
||||
|
||||
cleanup(_) ->
|
||||
eh_test_support:clear_tables(?TABLES),
|
||||
eh_test_support:delete_tables(?TABLES),
|
||||
eh_test_support:stop_mnesia(),
|
||||
ok.
|
||||
|
||||
core_admin_audit_test_() ->
|
||||
{foreach, fun setup/0, fun cleanup/1, [
|
||||
{"Log/7 without reason", fun test_log7/0},
|
||||
{"Log/8 with reason", fun test_log8/0},
|
||||
{"List all", fun test_list_all/0},
|
||||
{"List with filters", fun test_list_filters/0},
|
||||
{"Count actions by admin", fun test_count_actions/0}
|
||||
]}.
|
||||
|
||||
test_log7() ->
|
||||
{ok, Entry} = core_admin_audit:log(
|
||||
<<"adm1">>, <<"a@test.local">>, admin,
|
||||
<<"create">>, <<"user">>, <<"u1">>, <<"127.0.0.1">>
|
||||
),
|
||||
?assertEqual(<<"adm1">>, Entry#admin_audit.admin_id),
|
||||
?assertEqual(<<"a@test.local">>, Entry#admin_audit.email),
|
||||
?assertEqual(admin, Entry#admin_audit.role),
|
||||
?assertEqual(<<"create">>, Entry#admin_audit.action),
|
||||
?assertEqual(<<"user">>, Entry#admin_audit.entity_type),
|
||||
?assertEqual(<<"u1">>, Entry#admin_audit.entity_id),
|
||||
?assertEqual(<<"127.0.0.1">>, Entry#admin_audit.ip),
|
||||
?assertEqual(<<>>, Entry#admin_audit.reason),
|
||||
?assert(is_binary(Entry#admin_audit.id)).
|
||||
|
||||
test_log8() ->
|
||||
{ok, Entry} = core_admin_audit:log(
|
||||
<<"adm1">>, <<"a@test.local">>, moderator,
|
||||
<<"block">>, <<"user">>, <<"u2">>, <<"10.0.0.1">>, <<"abuse">>
|
||||
),
|
||||
?assertEqual(<<"block">>, Entry#admin_audit.action),
|
||||
?assertEqual(<<"abuse">>, Entry#admin_audit.reason),
|
||||
?assertEqual(moderator, Entry#admin_audit.role).
|
||||
|
||||
test_list_all() ->
|
||||
?assertEqual([], core_admin_audit:list()),
|
||||
{ok, _} = core_admin_audit:log(
|
||||
<<"adm1">>, <<"a@test.local">>, admin,
|
||||
<<"create">>, <<"user">>, <<"u1">>, <<"127.0.0.1">>
|
||||
),
|
||||
{ok, _} = core_admin_audit:log(
|
||||
<<"adm2">>, <<"b@test.local">>, admin,
|
||||
<<"update">>, <<"event">>, <<"e1">>, <<"127.0.0.1">>, <<"fix">>
|
||||
),
|
||||
?assertEqual(2, length(core_admin_audit:list())).
|
||||
|
||||
test_list_filters() ->
|
||||
{ok, _} = core_admin_audit:log(
|
||||
<<"adm1">>, <<"a@test.local">>, admin,
|
||||
<<"create">>, <<"user">>, <<"u1">>, <<"127.0.0.1">>
|
||||
),
|
||||
{ok, _} = core_admin_audit:log(
|
||||
<<"adm1">>, <<"a@test.local">>, admin,
|
||||
<<"block">>, <<"user">>, <<"u2">>, <<"127.0.0.1">>, <<"spam">>
|
||||
),
|
||||
{ok, _} = core_admin_audit:log(
|
||||
<<"adm2">>, <<"b@test.local">>, support,
|
||||
<<"create">>, <<"ticket">>, <<"t1">>, <<"10.0.0.1">>
|
||||
),
|
||||
|
||||
ByAdmin = core_admin_audit:list([{admin_id, <<"adm1">>}]),
|
||||
?assertEqual(2, length(ByAdmin)),
|
||||
?assert(lists:all(fun(E) -> E#admin_audit.admin_id =:= <<"adm1">> end, ByAdmin)),
|
||||
|
||||
ByAction = core_admin_audit:list([{action, <<"create">>}]),
|
||||
?assertEqual(2, length(ByAction)),
|
||||
|
||||
Combined = core_admin_audit:list([
|
||||
{admin_id, <<"adm1">>},
|
||||
{action, <<"block">>}
|
||||
]),
|
||||
?assertEqual(1, length(Combined)),
|
||||
[Only] = Combined,
|
||||
?assertEqual(<<"block">>, Only#admin_audit.action),
|
||||
|
||||
FarFuture = {{2099, 1, 1}, {0, 0, 0}},
|
||||
?assertEqual([], core_admin_audit:list([{date_from, FarFuture}])),
|
||||
|
||||
FarPast = {{1970, 1, 1}, {0, 0, 0}},
|
||||
?assertEqual(3, length(core_admin_audit:list([{date_from, FarPast}]))),
|
||||
|
||||
BeforeAll = {{1970, 1, 1}, {0, 0, 0}},
|
||||
?assertEqual([], core_admin_audit:list([{date_to, BeforeAll}])).
|
||||
|
||||
test_count_actions() ->
|
||||
{ok, _} = core_admin_audit:log(
|
||||
<<"adm1">>, <<"a@test.local">>, admin,
|
||||
<<"create">>, <<"user">>, <<"u1">>, <<"127.0.0.1">>
|
||||
),
|
||||
{ok, _} = core_admin_audit:log(
|
||||
<<"adm1">>, <<"a@test.local">>, admin,
|
||||
<<"create">>, <<"event">>, <<"e1">>, <<"127.0.0.1">>
|
||||
),
|
||||
{ok, _} = core_admin_audit:log(
|
||||
<<"adm1">>, <<"a@test.local">>, admin,
|
||||
<<"block">>, <<"user">>, <<"u2">>, <<"127.0.0.1">>
|
||||
),
|
||||
{ok, _} = core_admin_audit:log(
|
||||
<<"adm2">>, <<"b@test.local">>, admin,
|
||||
<<"create">>, <<"user">>, <<"u3">>, <<"127.0.0.1">>
|
||||
),
|
||||
?assertEqual(2, core_admin_audit:count_actions_by_admin(<<"adm1">>, <<"create">>)),
|
||||
?assertEqual(1, core_admin_audit:count_actions_by_admin(<<"adm1">>, <<"block">>)),
|
||||
?assertEqual(0, core_admin_audit:count_actions_by_admin(<<"adm1">>, <<"delete">>)),
|
||||
?assertEqual(1, core_admin_audit:count_actions_by_admin(<<"adm2">>, <<"create">>)).
|
||||
Reference in New Issue
Block a user