kanban-app/backend/tests/test_routes.py

115 lines
3.7 KiB
Python
Raw Permalink Normal View History

2026-02-24 14:36:31 +00:00
"""Test API routes"""
import pytest
class TestAuthRoutes:
"""Test authentication routes"""
@pytest.mark.auth
def test_register_success(self, client):
"""Test successful user registration"""
2026-02-24 16:19:15 +00:00
response = client.post(
"/api/auth/register",
json={
"email": "newuser@example.com",
"password": "password123",
"username": "newuser",
"first_name": "New",
"last_name": "User",
},
)
2026-02-24 14:36:31 +00:00
assert response.status_code == 201
data = response.get_json()
2026-02-24 16:19:15 +00:00
assert data["email"] == "newuser@example.com"
assert data["username"] == "newuser"
assert "password" not in data
assert "password_hash" not in data
2026-02-24 14:36:31 +00:00
@pytest.mark.auth
def test_register_missing_fields(self, client):
"""Test registration with missing required fields"""
2026-02-24 16:19:15 +00:00
response = client.post(
"/api/auth/register", json={"email": "newuser@example.com"}
)
2026-02-24 14:36:31 +00:00
assert response.status_code == 400
data = response.get_json()
2026-02-24 16:19:15 +00:00
assert "error" in data
2026-02-24 14:36:31 +00:00
@pytest.mark.auth
def test_register_duplicate_email(self, client, regular_user):
"""Test registration with duplicate email"""
2026-02-24 16:19:15 +00:00
response = client.post(
"/api/auth/register",
json={"email": regular_user.email, "password": "password123"},
)
2026-02-24 14:36:31 +00:00
assert response.status_code == 400
data = response.get_json()
2026-02-24 16:19:15 +00:00
assert "already exists" in data["error"].lower()
2026-02-24 14:36:31 +00:00
@pytest.mark.auth
def test_login_success(self, client, regular_user):
"""Test successful login"""
2026-02-24 16:19:15 +00:00
response = client.post(
"/api/auth/login",
json={"email": regular_user.email, "password": "password123"},
)
2026-02-24 14:36:31 +00:00
assert response.status_code == 200
data = response.get_json()
2026-02-24 16:19:15 +00:00
assert "access_token" in data
assert "refresh_token" in data
assert data["user"]["email"] == regular_user.email
2026-02-24 14:36:31 +00:00
@pytest.mark.auth
2026-02-24 16:19:15 +00:00
@pytest.mark.parametrize(
"email,password,expected_status",
[
("wrong@example.com", "password123", 401),
("user@example.com", "wrongpassword", 401),
(None, "password123", 400),
("user@example.com", None, 400),
],
)
def test_login_validation(
self, client, regular_user, email, password, expected_status
):
2026-02-24 14:36:31 +00:00
"""Test login with various invalid inputs"""
login_data = {}
if email is not None:
2026-02-24 16:19:15 +00:00
login_data["email"] = email
2026-02-24 14:36:31 +00:00
if password is not None:
2026-02-24 16:19:15 +00:00
login_data["password"] = password
2026-02-24 14:36:31 +00:00
2026-02-24 16:19:15 +00:00
response = client.post("/api/auth/login", json=login_data)
2026-02-24 14:36:31 +00:00
assert response.status_code == expected_status
@pytest.mark.auth
def test_login_inactive_user(self, client, inactive_user):
"""Test login with inactive user"""
2026-02-24 16:19:15 +00:00
response = client.post(
"/api/auth/login",
json={"email": inactive_user.email, "password": "password123"},
)
2026-02-24 14:36:31 +00:00
assert response.status_code == 401
data = response.get_json()
2026-02-24 16:19:15 +00:00
assert "inactive" in data["error"].lower()
2026-02-24 14:36:31 +00:00
@pytest.mark.auth
def test_get_current_user(self, client, auth_headers, regular_user):
"""Test getting current user"""
2026-02-24 16:19:15 +00:00
response = client.get("/api/users/me", headers=auth_headers)
2026-02-24 14:36:31 +00:00
assert response.status_code == 200
data = response.get_json()
2026-02-24 16:19:15 +00:00
assert data["email"] == regular_user.email
2026-02-24 14:36:31 +00:00
@pytest.mark.auth
def test_get_current_user_unauthorized(self, client):
"""Test getting current user without authentication"""
2026-02-24 16:19:15 +00:00
response = client.get("/api/users/me")
2026-02-24 14:36:31 +00:00
assert response.status_code == 401