64 lines
2.2 KiB
Erlang
64 lines
2.2 KiB
Erlang
-module(api_auth_tests).
|
|
-export([test/0]).
|
|
|
|
-define(BASE_URL, "http://localhost:8080").
|
|
|
|
test() ->
|
|
io:format("Testing authentication API...~n"),
|
|
|
|
Email = api_test_runner:unique_email(<<"auth_test">>),
|
|
Password = <<"test123">>,
|
|
|
|
% TEST 1: Register
|
|
io:format(" TEST 1: Register... "),
|
|
RegBody = #{email => Email, password => Password},
|
|
Token = api_test_runner:extract_json(
|
|
api_test_runner:http_post("/v1/register", RegBody), <<"token">>),
|
|
io:format("OK~n"),
|
|
|
|
% TEST 2: Register with existing email
|
|
io:format(" TEST 2: Register duplicate... "),
|
|
{ok, {{_, 409, _}, _, _}} = api_test_runner:http_post("/v1/register", RegBody),
|
|
io:format("OK~n"),
|
|
|
|
% TEST 3: Login with correct credentials
|
|
io:format(" TEST 3: Login... "),
|
|
LoginBody = #{email => Email, password => Password},
|
|
RefreshToken = api_test_runner:extract_json(
|
|
api_test_runner:http_post("/v1/login", LoginBody), <<"refresh_token">>),
|
|
io:format("OK~n"),
|
|
|
|
% TEST 4: Login with wrong password
|
|
io:format(" TEST 4: Login wrong password... "),
|
|
{ok, {{_, 401, _}, _, _}} = api_test_runner:http_post("/v1/login", #{email => Email, password => <<"wrong">>}),
|
|
io:format("OK~n"),
|
|
|
|
% TEST 5: Get profile with valid token
|
|
io:format(" TEST 5: Get profile... "),
|
|
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/user/me", Token),
|
|
io:format("OK~n"),
|
|
|
|
% TEST 6: Get profile with invalid token
|
|
io:format(" TEST 6: Get profile invalid token... "),
|
|
{ok, {{_, 401, _}, _, _}} = api_test_runner:http_get("/v1/user/me", <<"invalid">>),
|
|
io:format("OK~n"),
|
|
|
|
% TEST 7: Refresh token
|
|
io:format(" TEST 7: Refresh token... "),
|
|
RefreshBody = #{refresh_token => RefreshToken},
|
|
NewToken = api_test_runner:extract_json(
|
|
api_test_runner:http_post("/v1/refresh", RefreshBody), <<"token">>),
|
|
io:format("OK~n"),
|
|
|
|
% TEST 8: Refresh with used token (should fail)
|
|
io:format(" TEST 8: Refresh with used token... "),
|
|
{ok, {{_, 401, _}, _, _}} = api_test_runner:http_post("/v1/refresh", RefreshBody),
|
|
io:format("OK~n"),
|
|
|
|
% TEST 9: Use new token
|
|
io:format(" TEST 9: Use new token... "),
|
|
{ok, {{_, 200, _}, _, _}} = api_test_runner:http_get("/v1/user/me", NewToken),
|
|
io:format("OK~n"),
|
|
|
|
io:format("~n✅ Authentication API tests passed!~n"),
|
|
{?MODULE, ok}. |