60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
|
|
import httpx
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
|
||
|
|
def test_register_creates_account(client: httpx.Client):
|
||
|
|
response = client.post(
|
||
|
|
"/auth/register",
|
||
|
|
json={"email": "newuser@example.com", "password": "securepassword123"},
|
||
|
|
)
|
||
|
|
|
||
|
|
assert response.status_code == 201
|
||
|
|
body = response.json()
|
||
|
|
assert body["email"] == "newuser@example.com"
|
||
|
|
assert "id" in body
|
||
|
|
|
||
|
|
|
||
|
|
def test_register_duplicate_email_returns_409(client: httpx.Client):
|
||
|
|
payload = {"email": "duplicate@example.com", "password": "securepassword123"}
|
||
|
|
client.post("/auth/register", json=payload)
|
||
|
|
|
||
|
|
response = client.post("/auth/register", json=payload)
|
||
|
|
|
||
|
|
assert response.status_code == 409
|
||
|
|
|
||
|
|
|
||
|
|
def test_login_returns_token(client: httpx.Client):
|
||
|
|
credentials = {"email": "loginuser@example.com", "password": "securepassword123"}
|
||
|
|
client.post("/auth/register", json=credentials)
|
||
|
|
|
||
|
|
response = client.post("/auth/login", json=credentials)
|
||
|
|
|
||
|
|
assert response.status_code == 200
|
||
|
|
body = response.json()
|
||
|
|
assert "access_token" in body
|
||
|
|
assert body["token_type"] == "bearer"
|
||
|
|
assert len(body["access_token"]) > 0
|
||
|
|
|
||
|
|
|
||
|
|
def test_login_wrong_password_returns_401(client: httpx.Client):
|
||
|
|
client.post(
|
||
|
|
"/auth/register",
|
||
|
|
json={"email": "wrongpass@example.com", "password": "correctpassword"},
|
||
|
|
)
|
||
|
|
|
||
|
|
response = client.post(
|
||
|
|
"/auth/login",
|
||
|
|
json={"email": "wrongpass@example.com", "password": "wrongpassword"},
|
||
|
|
)
|
||
|
|
|
||
|
|
assert response.status_code == 401
|
||
|
|
|
||
|
|
|
||
|
|
def test_login_unknown_email_returns_401(client: httpx.Client):
|
||
|
|
response = client.post(
|
||
|
|
"/auth/login",
|
||
|
|
json={"email": "nobody@example.com", "password": "doesntmatter"},
|
||
|
|
)
|
||
|
|
|
||
|
|
assert response.status_code == 401
|