refactor: remove legacy user registration from auth_service

- Remove unused register_user() method and helper methods
- Remove legacy UserRegister schema (customer registration uses CustomerService)
- Remove wrapper methods that just delegated to auth_manager
- Simplify auth_service to focus on login and vendor access control
- Clean up tests to match simplified service

The only registration path is now /api/v1/shop/auth/register for customers,
which uses CustomerService.register_customer().

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-06 19:55:23 +01:00
parent b31fd41423
commit 0bfdf331d6
5 changed files with 77 additions and 611 deletions

View File

@@ -1,4 +1,10 @@
# tests/integration/api/v1/test_auth_endpoints.py
"""Integration tests for authentication endpoints.
Note: User registration is handled per-context:
- Customers: /api/v1/shop/auth/register (CustomerService)
- Admin/Vendor users: Created by admin or via team invites
"""
from datetime import UTC, datetime, timedelta
import pytest
@@ -9,78 +15,10 @@ from jose import jwt
@pytest.mark.api
@pytest.mark.auth
class TestAuthenticationAPI:
def test_register_user_success(self, client, db):
"""Test successful user registration"""
response = client.post(
"/api/v1/auth/register",
json={
"email": "newuser@example.com",
"username": "newuser",
"password": "securepass123",
},
)
assert response.status_code == 200
data = response.json()
assert data["email"] == "newuser@example.com"
assert data["username"] == "newuser"
assert data["role"] == "user"
assert data["is_active"] is True
assert "hashed_password" not in data
def test_register_user_duplicate_email(self, client, test_user):
"""Test registration with duplicate email"""
response = client.post(
"/api/v1/auth/register",
json={
"email": test_user.email, # Same as test_user
"username": "newuser",
"password": "securepass123",
},
)
assert response.status_code == 409
data = response.json()
assert data["error_code"] == "USER_ALREADY_EXISTS"
assert "Email already registered" in data["message"]
assert data["details"]["field"] == "email"
def test_register_user_duplicate_username(self, client, test_user):
"""Test registration with duplicate username"""
response = client.post(
"/api/v1/auth/register",
json={
"email": "new@example.com",
"username": test_user.username, # Same as test_user
"password": "securepass123",
},
)
assert response.status_code == 409
data = response.json()
assert data["error_code"] == "USER_ALREADY_EXISTS"
assert "Username already taken" in data["message"]
assert data["details"]["field"] == "username"
def test_register_user_validation_error(self, client):
"""Test registration with invalid data"""
response = client.post(
"/api/v1/auth/register",
json={
"email": "invalid-email", # Invalid email format
"username": "", # Empty username
"password": "123", # Too short password
},
)
assert response.status_code == 422
data = response.json()
assert data["error_code"] == "VALIDATION_ERROR"
assert "Request validation failed" in data["message"]
assert "validation_errors" in data["details"]
"""Test authentication endpoints for admin/vendor login."""
def test_login_success(self, client, test_user):
"""Test successful login"""
"""Test successful login."""
response = client.post(
"/api/v1/auth/login",
json={"username": test_user.username, "password": "testpass123"},
@@ -95,7 +33,7 @@ class TestAuthenticationAPI:
assert data["user"]["email"] == test_user.email
def test_login_wrong_password(self, client, test_user):
"""Test login with wrong password"""
"""Test login with wrong password."""
response = client.post(
"/api/v1/auth/login",
json={"username": test_user.username, "password": "wrongpassword"},
@@ -107,7 +45,7 @@ class TestAuthenticationAPI:
assert "Incorrect username or password" in data["message"]
def test_login_nonexistent_user(self, client, db):
"""Test login with nonexistent user"""
"""Test login with nonexistent user."""
response = client.post(
"/api/v1/auth/login",
json={"username": "nonexistent", "password": "password123"},
@@ -119,7 +57,7 @@ class TestAuthenticationAPI:
assert "Incorrect username or password" in data["message"]
def test_login_inactive_user(self, client, db, test_user):
"""Test login with inactive user account"""
"""Test login with inactive user account."""
# Manually deactivate the user for this test
original_status = test_user.is_active
test_user.is_active = False
@@ -142,7 +80,7 @@ class TestAuthenticationAPI:
db.commit()
def test_login_validation_error(self, client):
"""Test login with invalid request format"""
"""Test login with invalid request format."""
response = client.post(
"/api/v1/auth/login",
json={
@@ -157,7 +95,7 @@ class TestAuthenticationAPI:
assert "Request validation failed" in data["message"]
def test_get_current_user_info(self, client, auth_headers, test_user):
"""Test getting current user info"""
"""Test getting current user info."""
response = client.get("/api/v1/auth/me", headers=auth_headers)
assert response.status_code == 200
@@ -168,7 +106,7 @@ class TestAuthenticationAPI:
assert data["is_active"] is True
def test_get_current_user_without_auth(self, client):
"""Test getting current user without authentication"""
"""Test getting current user without authentication."""
response = client.get("/api/v1/auth/me")
assert response.status_code == 401
@@ -177,7 +115,7 @@ class TestAuthenticationAPI:
assert "Authorization header required" in data["message"]
def test_get_current_user_invalid_token(self, client):
"""Test getting current user with invalid token"""
"""Test getting current user with invalid token."""
response = client.get(
"/api/v1/auth/me", headers={"Authorization": "Bearer invalid_token_here"}
)
@@ -187,9 +125,7 @@ class TestAuthenticationAPI:
assert data["error_code"] == "INVALID_TOKEN"
def test_get_current_user_expired_token(self, client, test_user, auth_manager):
"""Test getting current user with expired token"""
# Create an expired token by mocking the expiration
"""Test getting current user with expired token."""
# Create token that expired 1 hour ago
expired_payload = {
"sub": str(test_user.id),
@@ -212,49 +148,14 @@ class TestAuthenticationAPI:
data = response.json()
assert data["error_code"] == "TOKEN_EXPIRED"
def test_user_registration_flow(self, client, db):
"""Test complete user registration and login flow"""
# Register new user
register_response = client.post(
"/api/v1/auth/register",
json={
"email": "flowtest@example.com",
"username": "flowtest",
"password": "securepass123",
},
)
assert register_response.status_code == 200
user_data = register_response.json()
assert user_data["email"] == "flowtest@example.com"
assert user_data["username"] == "flowtest"
# Login with new user
login_response = client.post(
"/api/v1/auth/login",
json={"username": "flowtest", "password": "securepass123"},
)
assert login_response.status_code == 200
login_data = login_response.json()
assert "access_token" in login_data
assert login_data["user"]["username"] == "flowtest"
# Use token to get user info
headers = {"Authorization": f"Bearer {login_data['access_token']}"}
me_response = client.get("/api/v1/auth/me", headers=headers)
assert me_response.status_code == 200
me_data = me_response.json()
assert me_data["username"] == "flowtest"
assert me_data["email"] == "flowtest@example.com"
@pytest.mark.unit
@pytest.mark.auth
class TestAuthManager:
"""Unit tests for AuthManager."""
def test_hash_password(self, auth_manager):
"""Test password hashing"""
"""Test password hashing."""
password = "testpassword123"
hashed = auth_manager.hash_password(password)
@@ -263,7 +164,7 @@ class TestAuthManager:
assert hashed.startswith("$") # bcrypt hash format
def test_verify_password(self, auth_manager):
"""Test password verification"""
"""Test password verification."""
password = "testpassword123"
hashed = auth_manager.hash_password(password)
@@ -271,7 +172,7 @@ class TestAuthManager:
assert auth_manager.verify_password("wrongpassword", hashed) is False
def test_create_access_token(self, auth_manager, test_user):
"""Test JWT token creation"""
"""Test JWT token creation."""
token_data = auth_manager.create_access_token(test_user)
assert "access_token" in token_data
@@ -281,7 +182,7 @@ class TestAuthManager:
assert token_data["expires_in"] > 0
def test_verify_token_valid(self, auth_manager, test_user):
"""Test JWT token verification with valid token"""
"""Test JWT token verification with valid token."""
token_data = auth_manager.create_access_token(test_user)
token = token_data["access_token"]
@@ -293,14 +194,14 @@ class TestAuthManager:
assert verified_data["role"] == test_user.role
def test_verify_token_invalid(self, auth_manager):
"""Test JWT token verification with invalid token"""
"""Test JWT token verification with invalid token."""
from app.exceptions.auth import InvalidTokenException
with pytest.raises(InvalidTokenException):
auth_manager.verify_token("invalid_token_here")
def test_authenticate_user_success(self, auth_manager, db, test_user):
"""Test user authentication with valid credentials"""
"""Test user authentication with valid credentials."""
user = auth_manager.authenticate_user(db, test_user.username, "testpass123")
assert user is not None
@@ -308,13 +209,13 @@ class TestAuthManager:
assert user.username == test_user.username
def test_authenticate_user_wrong_password(self, auth_manager, db, test_user):
"""Test user authentication with wrong password"""
"""Test user authentication with wrong password."""
user = auth_manager.authenticate_user(db, test_user.username, "wrongpassword")
assert user is None
def test_authenticate_user_nonexistent(self, auth_manager, db):
"""Test user authentication with nonexistent user"""
"""Test user authentication with nonexistent user."""
user = auth_manager.authenticate_user(db, "nonexistent", "password")
assert user is None