185 lines
5.1 KiB
Python
185 lines
5.1 KiB
Python
"""
|
|
Authentication tests for Newzy application.
|
|
"""
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_client(test_db):
|
|
"""Test client with authentication setup."""
|
|
from db.session import SessionLocal
|
|
from app.models import User
|
|
|
|
db = SessionLocal()
|
|
|
|
# Create test user
|
|
test_user = User(
|
|
username="testuser",
|
|
password_hash="$a1b2c3d4e5f678901234567890123456789012345678901234567890abcdef$hash1234567890"
|
|
)
|
|
|
|
# Insert test user into database
|
|
db.add(test_user)
|
|
db.commit()
|
|
|
|
yield TestClient(app, raise_server_exceptions=True)
|
|
db.close()
|
|
|
|
|
|
@pytest.fixture
|
|
def valid_password():
|
|
"""Password for hashing tests."""
|
|
return "securepassword123"
|
|
|
|
|
|
@pytest.fixture
|
|
def hashed_password(valid_password):
|
|
"""Hashed version of test password."""
|
|
from app.auth import hash_password
|
|
return hash_password(valid_password)
|
|
|
|
|
|
def test_register_user(auth_client, valid_password):
|
|
"""Test user registration creates account."""
|
|
response = auth_client.post("/api/auth/register", json={
|
|
"username": "newuser123",
|
|
"password": valid_password
|
|
})
|
|
|
|
assert response.status_code == 201
|
|
data = response.json()
|
|
assert "token" in data
|
|
assert data["username"] == "newuser123"
|
|
|
|
|
|
def test_duplicate_username(auth_client):
|
|
"""Test duplicate username is rejected."""
|
|
# First create user
|
|
auth_client.post("/api/auth/register", json={
|
|
"username": "testuser",
|
|
"password": "securepassword123"
|
|
})
|
|
|
|
# Try to register again with same username
|
|
response = auth_client.post("/api/auth/register", json={
|
|
"username": "testuser",
|
|
"password": "anotherpassword"
|
|
})
|
|
|
|
assert response.status_code == 409
|
|
|
|
|
|
def test_invalid_username_format(auth_client):
|
|
"""Test invalid username format is rejected."""
|
|
response = auth_client.post("/api/auth/register", json={
|
|
"username": "ab", # Too short
|
|
"password": "securepassword123"
|
|
})
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
def test_login_valid_credentials(auth_client):
|
|
"""Test login with valid credentials returns token."""
|
|
from app.auth import hash_password
|
|
|
|
response = auth_client.post("/api/auth/login", json={
|
|
"username": "testuser",
|
|
"password": hash_password("securepassword123") # Use stored hash format
|
|
})
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "token" in data
|
|
assert data["username"] == "testuser"
|
|
|
|
|
|
def test_login_invalid_credentials(auth_client):
|
|
"""Test login with invalid credentials returns 401."""
|
|
response = auth_client.post("/api/auth/login", json={
|
|
"username": "testuser",
|
|
"password": "wrongpassword"
|
|
})
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_get_me_valid_token(auth_client, valid_password):
|
|
"""Test /me endpoint with valid token."""
|
|
from app.auth import hash_password, create_token
|
|
|
|
# First register to get a proper password hash
|
|
response = auth_client.post("/api/auth/register", json={
|
|
"username": "testmeuser",
|
|
"password": valid_password
|
|
})
|
|
|
|
assert response.status_code == 201
|
|
token = response.json()["token"]
|
|
|
|
# Now verify /me endpoint works
|
|
response = auth_client.get("/api/auth/me", headers={
|
|
"Authorization": f"Bearer {token}"
|
|
})
|
|
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["username"] == "testmeuser"
|
|
|
|
|
|
def test_get_me_invalid_token(auth_client):
|
|
"""Test /me endpoint with invalid token returns 401."""
|
|
response = auth_client.get("/api/auth/me", headers={
|
|
"Authorization": "Bearer invalid.token.here"
|
|
})
|
|
|
|
assert response.status_code == 401
|
|
|
|
|
|
def test_logout(auth_client, valid_password):
|
|
"""Test logout endpoint."""
|
|
from app.auth import hash_password, create_token
|
|
|
|
# Register to get token
|
|
response = auth_client.post("/api/auth/register", json={
|
|
"username": "testlogoutuser",
|
|
"password": valid_password
|
|
})
|
|
|
|
assert response.status_code == 201
|
|
token = response.json()["token"]
|
|
|
|
# Call logout
|
|
response = auth_client.post("/api/auth/logout")
|
|
assert response.status_code == 204
|
|
|
|
# Try to use token after logout (should still work since JWT is stateless)
|
|
response = auth_client.get("/api/auth/me", headers={
|
|
"Authorization": f"Bearer {token}"
|
|
})
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_password_hashing_valid(auth_client):
|
|
"""Test that password hashing works correctly."""
|
|
from app.auth import hash_password, verify_password
|
|
|
|
original_password = "mysecret123"
|
|
stored_hash = hash_password(original_password)
|
|
|
|
assert verify_password(original_password, stored_hash) is True
|
|
assert verify_password("wrongpassword", stored_hash) is False
|
|
|
|
|
|
def test_jwt_token_structure(valid_password):
|
|
"""Test JWT token has correct structure and expiry."""
|
|
from app.auth import create_token
|
|
|
|
token = create_token("testuser")
|
|
|
|
# Token should not be empty
|
|
assert token and len(token) > 0
|