65 lines
2.6 KiB
Erlang
65 lines
2.6 KiB
Erlang
-module(logic_auth_tests).
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
-define(JWT_SECRET, <<"test-user-secret-key-32-byt!">>).
|
|
-define(ADMIN_JWT_SECRET, <<"test-admin-secret-key-32-b">>).
|
|
|
|
%% ------------------------------------------------------------------
|
|
%% Фикстуры
|
|
%% ------------------------------------------------------------------
|
|
setup() ->
|
|
application:set_env(eventhub, jwt_secret, ?JWT_SECRET),
|
|
application:set_env(eventhub, admin_jwt_secret, ?ADMIN_JWT_SECRET),
|
|
{ok, _} = application:ensure_all_started(jose),
|
|
ok.
|
|
|
|
cleanup(_) ->
|
|
application:unset_env(eventhub, jwt_secret),
|
|
application:unset_env(eventhub, admin_jwt_secret),
|
|
application:stop(jose).
|
|
|
|
%% ------------------------------------------------------------------
|
|
%% Тесты
|
|
%% ------------------------------------------------------------------
|
|
logic_auth_test_() ->
|
|
[
|
|
{"Password hash test", fun test_password_hash/0},
|
|
{setup, fun setup/0, fun cleanup/1, [
|
|
{"JWT generate and verify test", fun test_jwt/0},
|
|
{"JWT expired test", fun test_jwt_expired/0},
|
|
{"Refresh token test", fun test_refresh_token/0}
|
|
]}
|
|
].
|
|
|
|
%% ── Хеширование паролей (остаётся в logic_auth) ──────────────────
|
|
test_password_hash() ->
|
|
Password = <<"secret123">>,
|
|
{ok, Hash} = logic_auth:hash_password(Password),
|
|
?assert(is_binary(Hash)),
|
|
{ok, true} = logic_auth:verify_password(Password, Hash),
|
|
{ok, false} = logic_auth:verify_password(<<"wrong">>, Hash).
|
|
|
|
%% ── JWT тесты (перенесены в auth) ─────────────────────────────────
|
|
test_jwt() ->
|
|
UserId = <<"user123">>,
|
|
Role = <<"user">>,
|
|
Token = auth:generate_user_token(UserId, Role),
|
|
?assert(is_binary(Token)),
|
|
{ok, ReturnedUserId, ReturnedRole} = auth:verify_user_token(Token),
|
|
?assertEqual(UserId, ReturnedUserId),
|
|
?assertEqual(Role, ReturnedRole),
|
|
% Проверка невалидного токена
|
|
{error, invalid_token} = auth:verify_user_token(<<"invalid.token.here">>).
|
|
|
|
test_jwt_expired() ->
|
|
% Тест на истечение срока пока пропущен, так как требует мока времени
|
|
ok.
|
|
|
|
%% ── Refresh token (перенесён в auth) ────────────────────────────
|
|
test_refresh_token() ->
|
|
{Token, ExpiresAt} = auth:generate_refresh_token(<<"user123">>),
|
|
?assert(is_binary(Token)),
|
|
?assert(size(Token) >= 32),
|
|
?assert(is_integer(ExpiresAt)),
|
|
Now = os:system_time(second),
|
|
?assert(ExpiresAt > Now). |