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:
@@ -2,144 +2,112 @@
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-include("records.hrl").
|
||||
|
||||
-define(TABLES, [admin, admin_audit, report, ticket]).
|
||||
-define(ADMIN_ID, <<"adm_test_1">>).
|
||||
|
||||
setup() ->
|
||||
ok = meck:new(cowboy_req, [non_strict]),
|
||||
ok = meck:new(handler_auth, [non_strict]),
|
||||
ok = meck:new(core_user, [non_strict]),
|
||||
ok = meck:new(core_report, [non_strict]),
|
||||
ok = meck:expect(cowboy_req, reply,
|
||||
fun(Code, Headers, Body, Req) ->
|
||||
put(test_reply, {Code, Headers, Body, Req})
|
||||
end),
|
||||
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(_) ->
|
||||
meck:unload(core_report),
|
||||
meck:unload(core_user),
|
||||
meck:unload(handler_auth),
|
||||
meck:unload(cowboy_req).
|
||||
eh_test_support:unload_cowboy(),
|
||||
eh_test_support:delete_tables(?TABLES),
|
||||
eh_test_support:stop_mnesia(),
|
||||
ok.
|
||||
|
||||
admin_report_by_id_test_() ->
|
||||
{setup, fun setup/0, fun cleanup/1, [
|
||||
{"GET /admin/reports/:id – success", fun test_get_report/0},
|
||||
{"GET /admin/reports/:id – not found", fun test_get_report_not_found/0},
|
||||
{"GET /admin/reports/:id – forbidden", fun test_get_report_forbidden/0},
|
||||
{"PUT /admin/reports/:id – success", fun test_update_report/0},
|
||||
{"PUT /admin/reports/:id – not found", fun test_update_report_not_found/0},
|
||||
{"PUT /admin/reports/:id – bad JSON", fun test_update_report_bad_json/0},
|
||||
{"DELETE /admin/reports/:id – method not allowed", fun test_wrong_method/0}
|
||||
{foreach, fun setup/0, fun cleanup/1, [
|
||||
{"GET – success", fun test_get/0},
|
||||
{"GET – not found", fun test_get_not_found/0},
|
||||
{"PUT – update status", fun test_update/0},
|
||||
{"PUT – missing status", fun test_update_missing/0},
|
||||
{"PUT – not found", fun test_update_not_found/0},
|
||||
{"GET – unauthorized", fun test_unauthorized/0},
|
||||
{"PATCH – method not allowed", fun test_wrong_method/0}
|
||||
]}.
|
||||
|
||||
%% GET – успех
|
||||
test_get_report() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
|
||||
ok = meck:expect(handler_auth, authenticate,
|
||||
fun(Req) -> {ok, <<"adm1">>, Req} end),
|
||||
AdminUser = #user{id = <<"adm1">>, role = admin},
|
||||
ok = meck:expect(core_user, get_by_id,
|
||||
fun(<<"adm1">>) -> {ok, AdminUser} end),
|
||||
ok = meck:expect(cowboy_req, binding,
|
||||
fun(id, _) -> <<"r1">> end),
|
||||
Report = #report{
|
||||
id = <<"r1">>,
|
||||
reporter_id = <<"u1">>,
|
||||
target_type = <<"event">>,
|
||||
target_id = <<"e1">>,
|
||||
reason = <<"spam">>,
|
||||
status = pending,
|
||||
created_at = {{2026,4,26},{12,0,0}},
|
||||
resolved_at = undefined
|
||||
},
|
||||
ok = meck:expect(core_report, get_by_id,
|
||||
fun(<<"r1">>) -> {ok, Report} end),
|
||||
{ok, _, _} = admin_handler_report_by_id:init(req, []),
|
||||
{Status, _, RespBody, _} = erase(test_reply),
|
||||
?assertEqual(200, Status),
|
||||
#{<<"id">> := <<"r1">>, <<"status">> := <<"pending">>} = jsx:decode(RespBody, [return_maps]).
|
||||
seed_report() ->
|
||||
{ok, Report} = core_report:create(<<"u1">>, calendar, <<"cal1">>, <<"spam">>),
|
||||
Report.
|
||||
|
||||
%% GET – не найдено
|
||||
test_get_report_not_found() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
|
||||
ok = meck:expect(handler_auth, authenticate,
|
||||
fun(Req) -> {ok, <<"adm1">>, Req} end),
|
||||
AdminUser = #user{id = <<"adm1">>, role = admin},
|
||||
ok = meck:expect(core_user, get_by_id,
|
||||
fun(<<"adm1">>) -> {ok, AdminUser} end),
|
||||
ok = meck:expect(cowboy_req, binding,
|
||||
fun(id, _) -> <<"r99">> end),
|
||||
ok = meck:expect(core_report, get_by_id,
|
||||
fun(_) -> {error, not_found} end),
|
||||
{ok, _, _} = admin_handler_report_by_id:init(req, []),
|
||||
{Status, _, _, _} = erase(test_reply),
|
||||
test_get() ->
|
||||
Report = seed_report(),
|
||||
Id = Report#report.id,
|
||||
{Status, _, Body} = eh_test_support:call(admin_handler_report_by_id, #{
|
||||
method => <<"GET">>,
|
||||
path => <<"/v1/admin/reports/", Id/binary>>,
|
||||
bindings => #{id => Id},
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(200, Status),
|
||||
Result = jsx:decode(Body, [return_maps]),
|
||||
?assertEqual(Id, maps:get(<<"id">>, Result)),
|
||||
?assertEqual(<<"spam">>, maps:get(<<"reason">>, Result)).
|
||||
|
||||
test_get_not_found() ->
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_report_by_id, #{
|
||||
method => <<"GET">>,
|
||||
path => <<"/v1/admin/reports/missing">>,
|
||||
bindings => #{id => <<"missing">>},
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(404, Status).
|
||||
|
||||
%% GET – запрещён
|
||||
test_get_report_forbidden() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
|
||||
ok = meck:expect(handler_auth, authenticate,
|
||||
fun(Req) -> {error, 403, <<"Admin access required">>, Req} end),
|
||||
{ok, _, _} = admin_handler_report_by_id:init(req, []),
|
||||
{Status, _, _, _} = erase(test_reply),
|
||||
?assertEqual(403, Status).
|
||||
|
||||
%% PUT – успех
|
||||
test_update_report() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"PUT">> end),
|
||||
ok = meck:expect(handler_auth, authenticate,
|
||||
fun(Req) -> {ok, <<"adm1">>, Req} end),
|
||||
AdminUser = #user{id = <<"adm1">>, role = admin},
|
||||
ok = meck:expect(core_user, get_by_id,
|
||||
fun(<<"adm1">>) -> {ok, AdminUser} end),
|
||||
ok = meck:expect(cowboy_req, binding,
|
||||
fun(id, _) -> <<"r1">> end),
|
||||
ok = meck:expect(cowboy_req, read_body,
|
||||
fun(Req) -> {ok, jsx:encode(#{<<"status">> => <<"reviewed">>}), Req} end),
|
||||
Updated = #report{id = <<"r1">>, status = reviewed},
|
||||
ok = meck:expect(core_report, update_status,
|
||||
fun(<<"r1">>, reviewed, <<"adm1">>) -> {ok, Updated} end),
|
||||
{ok, _, _} = admin_handler_report_by_id:init(req, []),
|
||||
{Status, _, RespBody, _} = erase(test_reply),
|
||||
test_update() ->
|
||||
Report = seed_report(),
|
||||
Id = Report#report.id,
|
||||
{Status, _, Body} = eh_test_support:call(admin_handler_report_by_id, #{
|
||||
method => <<"PUT">>,
|
||||
path => <<"/v1/admin/reports/", Id/binary>>,
|
||||
bindings => #{id => Id},
|
||||
auth => ?ADMIN_ID,
|
||||
body => jsx:encode(#{<<"status">> => <<"reviewed">>})
|
||||
}),
|
||||
?assertEqual(200, Status),
|
||||
#{<<"status">> := <<"reviewed">>} = jsx:decode(RespBody, [return_maps]).
|
||||
Result = jsx:decode(Body, [return_maps]),
|
||||
?assertEqual(<<"reviewed">>, maps:get(<<"status">>, Result)),
|
||||
Audits = core_admin_audit:list(),
|
||||
?assert(length(Audits) >= 1).
|
||||
|
||||
%% PUT – не найдено
|
||||
test_update_report_not_found() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"PUT">> end),
|
||||
ok = meck:expect(handler_auth, authenticate,
|
||||
fun(Req) -> {ok, <<"adm1">>, Req} end),
|
||||
AdminUser = #user{id = <<"adm1">>, role = admin},
|
||||
ok = meck:expect(core_user, get_by_id,
|
||||
fun(<<"adm1">>) -> {ok, AdminUser} end),
|
||||
ok = meck:expect(cowboy_req, binding,
|
||||
fun(id, _) -> <<"r99">> end),
|
||||
ok = meck:expect(cowboy_req, read_body,
|
||||
fun(Req) -> {ok, jsx:encode(#{<<"status">> => <<"reviewed">>}), Req} end),
|
||||
ok = meck:expect(core_report, update_status,
|
||||
fun(<<"r99">>, reviewed, <<"adm1">>) -> {error, not_found} end),
|
||||
{ok, _, _} = admin_handler_report_by_id:init(req, []),
|
||||
{Status, _, _, _} = erase(test_reply),
|
||||
?assertEqual(404, Status).
|
||||
|
||||
%% PUT – невалидный JSON
|
||||
test_update_report_bad_json() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"PUT">> end),
|
||||
ok = meck:expect(handler_auth, authenticate,
|
||||
fun(Req) -> {ok, <<"adm1">>, Req} end),
|
||||
AdminUser = #user{id = <<"adm1">>, role = admin},
|
||||
ok = meck:expect(core_user, get_by_id,
|
||||
fun(<<"adm1">>) -> {ok, AdminUser} end),
|
||||
ok = meck:expect(cowboy_req, binding,
|
||||
fun(id, _) -> <<"r1">> end),
|
||||
ok = meck:expect(cowboy_req, read_body,
|
||||
fun(Req) -> {ok, <<"bad json">>, Req} end),
|
||||
{ok, _, _} = admin_handler_report_by_id:init(req, []),
|
||||
{Status, _, _, _} = erase(test_reply),
|
||||
test_update_missing() ->
|
||||
Report = seed_report(),
|
||||
Id = Report#report.id,
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_report_by_id, #{
|
||||
method => <<"PUT">>,
|
||||
path => <<"/v1/admin/reports/", Id/binary>>,
|
||||
bindings => #{id => Id},
|
||||
auth => ?ADMIN_ID,
|
||||
body => jsx:encode(#{<<"other">> => <<"x">>})
|
||||
}),
|
||||
?assertEqual(400, Status).
|
||||
|
||||
%% Неверный метод
|
||||
test_update_not_found() ->
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_report_by_id, #{
|
||||
method => <<"PUT">>,
|
||||
path => <<"/v1/admin/reports/missing">>,
|
||||
bindings => #{id => <<"missing">>},
|
||||
auth => ?ADMIN_ID,
|
||||
body => jsx:encode(#{<<"status">> => <<"dismissed">>})
|
||||
}),
|
||||
?assertEqual(404, Status).
|
||||
|
||||
test_unauthorized() ->
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_report_by_id, #{
|
||||
method => <<"GET">>,
|
||||
path => <<"/v1/admin/reports/x">>,
|
||||
bindings => #{id => <<"x">>},
|
||||
auth => none
|
||||
}),
|
||||
?assertEqual(401, Status).
|
||||
|
||||
test_wrong_method() ->
|
||||
ok = meck:expect(cowboy_req, method, fun(_) -> <<"DELETE">> end),
|
||||
{ok, _, _} = admin_handler_report_by_id:init(req, []),
|
||||
{Status, _, RespBody, _} = erase(test_reply),
|
||||
?assertEqual(405, Status),
|
||||
#{<<"error">> := <<"Method not allowed">>} = jsx:decode(RespBody, [return_maps]).
|
||||
{Status, _, _} = eh_test_support:call(admin_handler_report_by_id, #{
|
||||
method => <<"PATCH">>,
|
||||
path => <<"/v1/admin/reports/x">>,
|
||||
bindings => #{id => <<"x">>},
|
||||
auth => ?ADMIN_ID
|
||||
}),
|
||||
?assertEqual(405, Status).
|
||||
|
||||
Reference in New Issue
Block a user