Skip to content

Commit e74b264

Browse files
committed
test: added basic auth tests
1 parent e7d908c commit e74b264

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

backend/tests/test_auth.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def auth_data():
6+
return {
7+
"email": "user@example.com",
8+
"password": "securepassword123",
9+
}
10+
11+
12+
def test_signup_returns_token(client, auth_data):
13+
response = client.post("/v1/auth/signup", json=auth_data)
14+
assert response.status_code == 201
15+
data = response.json()
16+
assert "access_token" in data
17+
assert data["token_type"] == "bearer"
18+
19+
20+
def test_signup_duplicate_email(client, auth_data):
21+
# Repeat signup to trigger duplicate error
22+
response = client.post("/v1/auth/signup", json=auth_data)
23+
assert response.status_code == 400
24+
assert "already exists" in response.json()["detail"].lower()
25+
26+
27+
def test_login_with_valid_credentials(client, auth_data):
28+
response = client.post("/v1/auth/login", json=auth_data)
29+
assert response.status_code == 200
30+
data = response.json()
31+
assert "access_token" in data
32+
assert data["token_type"] == "bearer"
33+
34+
35+
def test_login_with_invalid_password(client, auth_data):
36+
response = client.post(
37+
"/v1/auth/login",
38+
json={"email": auth_data["email"], "password": "wrongpassword"},
39+
)
40+
assert response.status_code == 401
41+
assert "invalid credentials" in response.json()["detail"].lower()
42+
43+
44+
def test_login_with_unknown_email(client):
45+
response = client.post(
46+
"/v1/auth/login",
47+
json={"email": "unknown@example.com", "password": "irrelevant"},
48+
)
49+
assert response.status_code == 401
50+
assert "invalid credentials" in response.json()["detail"].lower()
51+
52+
53+
def test_me_requires_auth(client):
54+
response = client.get("/v1/auth/me")
55+
assert response.status_code == 401
56+
57+
58+
def test_me_with_valid_token(auth_client, auth_data):
59+
response = auth_client.get("/v1/auth/me")
60+
assert response.status_code == 200
61+
assert response.json()["email"] == "test@example.com"
62+
assert "id" in response.json()
63+
64+
65+
def test_login_for_access_token(client, auth_data):
66+
form_data = {
67+
"username": auth_data["email"],
68+
"password": auth_data["password"],
69+
}
70+
response = client.post("/v1/auth/token", data=form_data)
71+
assert response.status_code == 200
72+
token_data = response.json()
73+
assert "access_token" in token_data
74+
assert token_data["token_type"] == "bearer"

0 commit comments

Comments
 (0)