forked from cardosofelipe/fast-next-template
Add pyproject.toml for consolidated project configuration and replace Black, isort, and Flake8 with Ruff
- Introduced `pyproject.toml` to centralize backend tool configurations (e.g., Ruff, mypy, coverage, pytest). - Replaced Black, isort, and Flake8 with Ruff for linting, formatting, and import sorting. - Updated `requirements.txt` to include Ruff and remove replaced tools. - Added `Makefile` to streamline development workflows with commands for linting, formatting, type-checking, testing, and cleanup.
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
# tests/core/test_auth.py
|
||||
import uuid
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from jose import jwt
|
||||
from pydantic import ValidationError
|
||||
|
||||
from app.core.auth import (
|
||||
verify_password,
|
||||
get_password_hash,
|
||||
TokenExpiredError,
|
||||
TokenInvalidError,
|
||||
TokenMissingClaimError,
|
||||
create_access_token,
|
||||
create_refresh_token,
|
||||
decode_token,
|
||||
get_password_hash,
|
||||
get_token_data,
|
||||
TokenExpiredError,
|
||||
TokenInvalidError,
|
||||
TokenMissingClaimError
|
||||
verify_password,
|
||||
)
|
||||
from app.core.config import settings
|
||||
|
||||
@@ -58,15 +58,13 @@ class TestTokenCreation:
|
||||
custom_claims = {
|
||||
"email": "test@example.com",
|
||||
"first_name": "Test",
|
||||
"is_superuser": True
|
||||
"is_superuser": True,
|
||||
}
|
||||
token = create_access_token(subject=user_id, claims=custom_claims)
|
||||
|
||||
# Decode token to verify claims
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
settings.SECRET_KEY,
|
||||
algorithms=[settings.ALGORITHM]
|
||||
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
|
||||
)
|
||||
|
||||
# Check standard claims
|
||||
@@ -87,9 +85,7 @@ class TestTokenCreation:
|
||||
|
||||
# Decode token to verify claims
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
settings.SECRET_KEY,
|
||||
algorithms=[settings.ALGORITHM]
|
||||
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
|
||||
)
|
||||
|
||||
# Check standard claims
|
||||
@@ -105,23 +101,18 @@ class TestTokenCreation:
|
||||
expires = timedelta(minutes=5)
|
||||
|
||||
# Create token with specific expiration
|
||||
token = create_access_token(
|
||||
subject=user_id,
|
||||
expires_delta=expires
|
||||
)
|
||||
token = create_access_token(subject=user_id, expires_delta=expires)
|
||||
|
||||
# Decode token
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
settings.SECRET_KEY,
|
||||
algorithms=[settings.ALGORITHM]
|
||||
token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM]
|
||||
)
|
||||
|
||||
# Get actual expiration time from token
|
||||
expiration = datetime.fromtimestamp(payload["exp"], tz=timezone.utc)
|
||||
expiration = datetime.fromtimestamp(payload["exp"], tz=UTC)
|
||||
|
||||
# Calculate expected expiration (approximately)
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
expected_expiration = now + expires
|
||||
|
||||
# Difference should be small (less than 1 second)
|
||||
@@ -148,7 +139,7 @@ class TestTokenDecoding:
|
||||
user_id = str(uuid.uuid4())
|
||||
|
||||
# Create a token that's already expired by directly manipulating the payload
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
expired_time = now - timedelta(hours=1) # 1 hour in the past
|
||||
|
||||
# Create the expired token manually
|
||||
@@ -157,13 +148,11 @@ class TestTokenDecoding:
|
||||
"exp": int(expired_time.timestamp()), # Set expiration in the past
|
||||
"iat": int(now.timestamp()),
|
||||
"jti": str(uuid.uuid4()),
|
||||
"type": "access"
|
||||
"type": "access",
|
||||
}
|
||||
|
||||
expired_token = jwt.encode(
|
||||
payload,
|
||||
settings.SECRET_KEY,
|
||||
algorithm=settings.ALGORITHM
|
||||
payload, settings.SECRET_KEY, algorithm=settings.ALGORITHM
|
||||
)
|
||||
|
||||
# Attempting to decode should raise TokenExpiredError
|
||||
@@ -180,20 +169,16 @@ class TestTokenDecoding:
|
||||
def test_decode_token_with_missing_sub(self):
|
||||
"""Test that a token without 'sub' claim raises TokenMissingClaimError"""
|
||||
# Create a token without a subject
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
payload = {
|
||||
"exp": int((now + timedelta(minutes=30)).timestamp()),
|
||||
"iat": int(now.timestamp()),
|
||||
"jti": str(uuid.uuid4()),
|
||||
"type": "access"
|
||||
"type": "access",
|
||||
# No 'sub' claim
|
||||
}
|
||||
|
||||
token = jwt.encode(
|
||||
payload,
|
||||
settings.SECRET_KEY,
|
||||
algorithm=settings.ALGORITHM
|
||||
)
|
||||
token = jwt.encode(payload, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
||||
|
||||
with pytest.raises(TokenMissingClaimError):
|
||||
decode_token(token)
|
||||
@@ -211,20 +196,16 @@ class TestTokenDecoding:
|
||||
"""Test that a token with invalid payload structure raises TokenInvalidError"""
|
||||
# Create a token with an invalid payload structure - missing 'sub' which is required
|
||||
# but including 'exp' to avoid the expiration check
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
payload = {
|
||||
# Missing "sub" field which is required
|
||||
"exp": int((now + timedelta(minutes=30)).timestamp()),
|
||||
"iat": int(now.timestamp()),
|
||||
"jti": str(uuid.uuid4()),
|
||||
"invalid_field": "test"
|
||||
"invalid_field": "test",
|
||||
}
|
||||
|
||||
token = jwt.encode(
|
||||
payload,
|
||||
settings.SECRET_KEY,
|
||||
algorithm=settings.ALGORITHM
|
||||
)
|
||||
token = jwt.encode(payload, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
||||
|
||||
# Should raise TokenMissingClaimError due to missing 'sub'
|
||||
with pytest.raises(TokenMissingClaimError):
|
||||
@@ -236,11 +217,7 @@ class TestTokenDecoding:
|
||||
"exp": int((now + timedelta(minutes=30)).timestamp()),
|
||||
}
|
||||
|
||||
token = jwt.encode(
|
||||
payload,
|
||||
settings.SECRET_KEY,
|
||||
algorithm=settings.ALGORITHM
|
||||
)
|
||||
token = jwt.encode(payload, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
|
||||
|
||||
# Should raise TokenInvalidError due to ValidationError
|
||||
with pytest.raises(TokenInvalidError):
|
||||
@@ -249,12 +226,9 @@ class TestTokenDecoding:
|
||||
def test_get_token_data(self):
|
||||
"""Test extracting TokenData from a token"""
|
||||
user_id = uuid.uuid4()
|
||||
token = create_access_token(
|
||||
subject=str(user_id),
|
||||
claims={"is_superuser": True}
|
||||
)
|
||||
token = create_access_token(subject=str(user_id), claims={"is_superuser": True})
|
||||
|
||||
token_data = get_token_data(token)
|
||||
|
||||
assert token_data.user_id == user_id
|
||||
assert token_data.is_superuser is True
|
||||
assert token_data.is_superuser is True
|
||||
|
||||
@@ -8,11 +8,11 @@ Critical security tests covering:
|
||||
|
||||
These tests cover critical security vulnerabilities that could be exploited.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from jose import jwt
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from app.core.auth import decode_token, create_access_token, TokenInvalidError
|
||||
from app.core.auth import TokenInvalidError, create_access_token, decode_token
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
@@ -46,13 +46,14 @@ class TestJWTAlgorithmSecurityAttacks:
|
||||
"""
|
||||
# Create a payload that would normally be valid (using timestamps)
|
||||
import time
|
||||
|
||||
now = int(time.time())
|
||||
|
||||
payload = {
|
||||
"sub": "user123",
|
||||
"exp": now + 3600, # 1 hour from now
|
||||
"iat": now,
|
||||
"type": "access"
|
||||
"type": "access",
|
||||
}
|
||||
|
||||
# Craft a malicious token with "alg: none"
|
||||
@@ -61,13 +62,13 @@ class TestJWTAlgorithmSecurityAttacks:
|
||||
import json
|
||||
|
||||
header = {"alg": "none", "typ": "JWT"}
|
||||
header_encoded = base64.urlsafe_b64encode(
|
||||
json.dumps(header).encode()
|
||||
).decode().rstrip("=")
|
||||
header_encoded = (
|
||||
base64.urlsafe_b64encode(json.dumps(header).encode()).decode().rstrip("=")
|
||||
)
|
||||
|
||||
payload_encoded = base64.urlsafe_b64encode(
|
||||
json.dumps(payload).encode()
|
||||
).decode().rstrip("=")
|
||||
payload_encoded = (
|
||||
base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
|
||||
)
|
||||
|
||||
# Token with no signature (algorithm "none")
|
||||
malicious_token = f"{header_encoded}.{payload_encoded}."
|
||||
@@ -85,22 +86,17 @@ class TestJWTAlgorithmSecurityAttacks:
|
||||
import time
|
||||
|
||||
now = int(time.time())
|
||||
payload = {
|
||||
"sub": "user123",
|
||||
"exp": now + 3600,
|
||||
"iat": now,
|
||||
"type": "access"
|
||||
}
|
||||
payload = {"sub": "user123", "exp": now + 3600, "iat": now, "type": "access"}
|
||||
|
||||
# Try uppercase "NONE"
|
||||
header = {"alg": "NONE", "typ": "JWT"}
|
||||
header_encoded = base64.urlsafe_b64encode(
|
||||
json.dumps(header).encode()
|
||||
).decode().rstrip("=")
|
||||
header_encoded = (
|
||||
base64.urlsafe_b64encode(json.dumps(header).encode()).decode().rstrip("=")
|
||||
)
|
||||
|
||||
payload_encoded = base64.urlsafe_b64encode(
|
||||
json.dumps(payload).encode()
|
||||
).decode().rstrip("=")
|
||||
payload_encoded = (
|
||||
base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
|
||||
)
|
||||
|
||||
malicious_token = f"{header_encoded}.{payload_encoded}."
|
||||
|
||||
@@ -121,15 +117,11 @@ class TestJWTAlgorithmSecurityAttacks:
|
||||
before our defensive checks at line 212. This is good for security!
|
||||
"""
|
||||
import time
|
||||
|
||||
now = int(time.time())
|
||||
|
||||
# Create a valid payload
|
||||
payload = {
|
||||
"sub": "user123",
|
||||
"exp": now + 3600,
|
||||
"iat": now,
|
||||
"type": "access"
|
||||
}
|
||||
payload = {"sub": "user123", "exp": now + 3600, "iat": now, "type": "access"}
|
||||
|
||||
# Encode with wrong algorithm (RS256 instead of HS256)
|
||||
# This simulates an attacker trying algorithm substitution
|
||||
@@ -137,9 +129,7 @@ class TestJWTAlgorithmSecurityAttacks:
|
||||
|
||||
try:
|
||||
malicious_token = jwt.encode(
|
||||
payload,
|
||||
settings.SECRET_KEY,
|
||||
algorithm=wrong_algorithm
|
||||
payload, settings.SECRET_KEY, algorithm=wrong_algorithm
|
||||
)
|
||||
|
||||
# Should reject the token (library catches mismatch)
|
||||
@@ -156,21 +146,15 @@ class TestJWTAlgorithmSecurityAttacks:
|
||||
Prevents algorithm downgrade/upgrade attacks.
|
||||
"""
|
||||
import time
|
||||
|
||||
now = int(time.time())
|
||||
|
||||
payload = {
|
||||
"sub": "user123",
|
||||
"exp": now + 3600,
|
||||
"iat": now,
|
||||
"type": "access"
|
||||
}
|
||||
payload = {"sub": "user123", "exp": now + 3600, "iat": now, "type": "access"}
|
||||
|
||||
# Create token with HS384 instead of HS256
|
||||
try:
|
||||
malicious_token = jwt.encode(
|
||||
payload,
|
||||
settings.SECRET_KEY,
|
||||
algorithm="HS384"
|
||||
payload, settings.SECRET_KEY, algorithm="HS384"
|
||||
)
|
||||
|
||||
with pytest.raises(TokenInvalidError):
|
||||
@@ -223,20 +207,15 @@ class TestJWTSecurityEdgeCases:
|
||||
|
||||
# Create token without "alg" in header
|
||||
header = {"typ": "JWT"} # Missing "alg"
|
||||
payload = {
|
||||
"sub": "user123",
|
||||
"exp": now + 3600,
|
||||
"iat": now,
|
||||
"type": "access"
|
||||
}
|
||||
payload = {"sub": "user123", "exp": now + 3600, "iat": now, "type": "access"}
|
||||
|
||||
header_encoded = base64.urlsafe_b64encode(
|
||||
json.dumps(header).encode()
|
||||
).decode().rstrip("=")
|
||||
header_encoded = (
|
||||
base64.urlsafe_b64encode(json.dumps(header).encode()).decode().rstrip("=")
|
||||
)
|
||||
|
||||
payload_encoded = base64.urlsafe_b64encode(
|
||||
json.dumps(payload).encode()
|
||||
).decode().rstrip("=")
|
||||
payload_encoded = (
|
||||
base64.urlsafe_b64encode(json.dumps(payload).encode()).decode().rstrip("=")
|
||||
)
|
||||
|
||||
malicious_token = f"{header_encoded}.{payload_encoded}.fake_signature"
|
||||
|
||||
@@ -253,15 +232,20 @@ class TestJWTSecurityEdgeCases:
|
||||
"""Test token with malformed JSON in payload."""
|
||||
import base64
|
||||
|
||||
header = {"alg": "HS256", "typ": "JWT"}
|
||||
header_encoded = base64.urlsafe_b64encode(
|
||||
b'{"alg":"HS256","typ":"JWT"}'
|
||||
).decode().rstrip("=")
|
||||
header_encoded = (
|
||||
base64.urlsafe_b64encode(b'{"alg":"HS256","typ":"JWT"}')
|
||||
.decode()
|
||||
.rstrip("=")
|
||||
)
|
||||
|
||||
# Invalid JSON (missing closing brace)
|
||||
invalid_payload_encoded = base64.urlsafe_b64encode(
|
||||
b'{"sub":"user123"' # Invalid JSON
|
||||
).decode().rstrip("=")
|
||||
invalid_payload_encoded = (
|
||||
base64.urlsafe_b64encode(
|
||||
b'{"sub":"user123"' # Invalid JSON
|
||||
)
|
||||
.decode()
|
||||
.rstrip("=")
|
||||
)
|
||||
|
||||
malicious_token = f"{header_encoded}.{invalid_payload_encoded}.fake_sig"
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# tests/core/test_config.py
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from app.core.config import Settings
|
||||
|
||||
|
||||
@@ -22,11 +23,15 @@ class TestSecretKeyValidation:
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
Settings(SECRET_KEY=default_key, ENVIRONMENT="production")
|
||||
|
||||
assert "must be set to a secure random value in production" in str(exc_info.value)
|
||||
assert "must be set to a secure random value in production" in str(
|
||||
exc_info.value
|
||||
)
|
||||
|
||||
def test_default_secret_key_in_development_allows_with_warning(self, caplog):
|
||||
"""Test that default SECRET_KEY in development is allowed but warns"""
|
||||
settings = Settings(SECRET_KEY="your_secret_key_here" + "x" * 14, ENVIRONMENT="development")
|
||||
settings = Settings(
|
||||
SECRET_KEY="your_secret_key_here" + "x" * 14, ENVIRONMENT="development"
|
||||
)
|
||||
|
||||
assert settings.SECRET_KEY == "your_secret_key_here" + "x" * 14
|
||||
# Note: The warning happens during validation, which we've seen works
|
||||
@@ -44,19 +49,13 @@ class TestSuperuserPasswordValidation:
|
||||
|
||||
def test_none_password_accepted(self):
|
||||
"""Test that None password is accepted (optional field)"""
|
||||
settings = Settings(
|
||||
SECRET_KEY="a" * 32,
|
||||
FIRST_SUPERUSER_PASSWORD=None
|
||||
)
|
||||
settings = Settings(SECRET_KEY="a" * 32, FIRST_SUPERUSER_PASSWORD=None)
|
||||
assert settings.FIRST_SUPERUSER_PASSWORD is None
|
||||
|
||||
def test_password_too_short_raises_error(self):
|
||||
"""Test that password shorter than 12 characters raises error"""
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
Settings(
|
||||
SECRET_KEY="a" * 32,
|
||||
FIRST_SUPERUSER_PASSWORD="Short1"
|
||||
)
|
||||
Settings(SECRET_KEY="a" * 32, FIRST_SUPERUSER_PASSWORD="Short1")
|
||||
|
||||
assert "must be at least 12 characters" in str(exc_info.value)
|
||||
|
||||
@@ -64,14 +63,11 @@ class TestSuperuserPasswordValidation:
|
||||
"""Test that common weak passwords are rejected"""
|
||||
# Test with the exact weak passwords from the validator
|
||||
# These are in the weak_passwords set and should be rejected
|
||||
weak_passwords = ['123456789012'] # Exactly 12 chars, in the weak set
|
||||
weak_passwords = ["123456789012"] # Exactly 12 chars, in the weak set
|
||||
|
||||
for weak_pwd in weak_passwords:
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
Settings(
|
||||
SECRET_KEY="a" * 32,
|
||||
FIRST_SUPERUSER_PASSWORD=weak_pwd
|
||||
)
|
||||
Settings(SECRET_KEY="a" * 32, FIRST_SUPERUSER_PASSWORD=weak_pwd)
|
||||
# Should get "too weak" message
|
||||
error_str = str(exc_info.value)
|
||||
assert "too weak" in error_str
|
||||
@@ -79,30 +75,21 @@ class TestSuperuserPasswordValidation:
|
||||
def test_password_without_lowercase_rejected(self):
|
||||
"""Test that password without lowercase is rejected"""
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
Settings(
|
||||
SECRET_KEY="a" * 32,
|
||||
FIRST_SUPERUSER_PASSWORD="ALLUPPERCASE123"
|
||||
)
|
||||
Settings(SECRET_KEY="a" * 32, FIRST_SUPERUSER_PASSWORD="ALLUPPERCASE123")
|
||||
|
||||
assert "must contain lowercase, uppercase, and digits" in str(exc_info.value)
|
||||
|
||||
def test_password_without_uppercase_rejected(self):
|
||||
"""Test that password without uppercase is rejected"""
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
Settings(
|
||||
SECRET_KEY="a" * 32,
|
||||
FIRST_SUPERUSER_PASSWORD="alllowercase123"
|
||||
)
|
||||
Settings(SECRET_KEY="a" * 32, FIRST_SUPERUSER_PASSWORD="alllowercase123")
|
||||
|
||||
assert "must contain lowercase, uppercase, and digits" in str(exc_info.value)
|
||||
|
||||
def test_password_without_digit_rejected(self):
|
||||
"""Test that password without digit is rejected"""
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
Settings(
|
||||
SECRET_KEY="a" * 32,
|
||||
FIRST_SUPERUSER_PASSWORD="NoDigitsHere"
|
||||
)
|
||||
Settings(SECRET_KEY="a" * 32, FIRST_SUPERUSER_PASSWORD="NoDigitsHere")
|
||||
|
||||
assert "must contain lowercase, uppercase, and digits" in str(exc_info.value)
|
||||
|
||||
@@ -110,8 +97,7 @@ class TestSuperuserPasswordValidation:
|
||||
"""Test that strong password is accepted"""
|
||||
strong_password = "StrongPassword123!"
|
||||
settings = Settings(
|
||||
SECRET_KEY="a" * 32,
|
||||
FIRST_SUPERUSER_PASSWORD=strong_password
|
||||
SECRET_KEY="a" * 32, FIRST_SUPERUSER_PASSWORD=strong_password
|
||||
)
|
||||
|
||||
assert settings.FIRST_SUPERUSER_PASSWORD == strong_password
|
||||
@@ -150,7 +136,7 @@ class TestDatabaseConfiguration:
|
||||
POSTGRES_HOST="testhost",
|
||||
POSTGRES_PORT="5432",
|
||||
POSTGRES_DB="testdb",
|
||||
DATABASE_URL=None # Don't use explicit URL
|
||||
DATABASE_URL=None, # Don't use explicit URL
|
||||
)
|
||||
|
||||
expected_url = "postgresql://testuser:testpass@testhost:5432/testdb"
|
||||
@@ -159,10 +145,7 @@ class TestDatabaseConfiguration:
|
||||
def test_explicit_database_url_used_when_set(self):
|
||||
"""Test that explicit DATABASE_URL is used when provided"""
|
||||
explicit_url = "postgresql://explicit:pass@host:5432/db"
|
||||
settings = Settings(
|
||||
SECRET_KEY="a" * 32,
|
||||
DATABASE_URL=explicit_url
|
||||
)
|
||||
settings = Settings(SECRET_KEY="a" * 32, DATABASE_URL=explicit_url)
|
||||
|
||||
assert settings.database_url == explicit_url
|
||||
|
||||
|
||||
@@ -6,8 +6,10 @@ Critical security tests covering:
|
||||
|
||||
These tests prevent security misconfigurations.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
|
||||
@@ -43,6 +45,7 @@ class TestSecretKeySecurityValidation:
|
||||
# Import Settings class fresh (to pick up new env var)
|
||||
# The ValidationError should be raised during reload when Settings() is instantiated
|
||||
import importlib
|
||||
|
||||
from app.core import config
|
||||
|
||||
# Reload will raise ValidationError because Settings() is instantiated at module level
|
||||
@@ -58,7 +61,9 @@ class TestSecretKeySecurityValidation:
|
||||
|
||||
# Reload config to restore original settings
|
||||
import importlib
|
||||
|
||||
from app.core import config
|
||||
|
||||
importlib.reload(config)
|
||||
|
||||
def test_secret_key_exactly_32_characters_accepted(self):
|
||||
@@ -75,7 +80,9 @@ class TestSecretKeySecurityValidation:
|
||||
os.environ["SECRET_KEY"] = key_32
|
||||
|
||||
import importlib
|
||||
|
||||
from app.core import config
|
||||
|
||||
importlib.reload(config)
|
||||
|
||||
# Should work
|
||||
@@ -89,7 +96,9 @@ class TestSecretKeySecurityValidation:
|
||||
os.environ.pop("SECRET_KEY", None)
|
||||
|
||||
import importlib
|
||||
|
||||
from app.core import config
|
||||
|
||||
importlib.reload(config)
|
||||
|
||||
def test_secret_key_long_enough_accepted(self):
|
||||
@@ -106,7 +115,9 @@ class TestSecretKeySecurityValidation:
|
||||
os.environ["SECRET_KEY"] = key_64
|
||||
|
||||
import importlib
|
||||
|
||||
from app.core import config
|
||||
|
||||
importlib.reload(config)
|
||||
|
||||
# Should work
|
||||
@@ -120,7 +131,9 @@ class TestSecretKeySecurityValidation:
|
||||
os.environ.pop("SECRET_KEY", None)
|
||||
|
||||
import importlib
|
||||
|
||||
from app.core import config
|
||||
|
||||
importlib.reload(config)
|
||||
|
||||
def test_default_secret_key_meets_requirements(self):
|
||||
@@ -132,4 +145,6 @@ class TestSecretKeySecurityValidation:
|
||||
from app.core.config import settings
|
||||
|
||||
# Current settings should have valid SECRET_KEY
|
||||
assert len(settings.SECRET_KEY) >= 32, "Default SECRET_KEY must be at least 32 chars"
|
||||
assert len(settings.SECRET_KEY) >= 32, (
|
||||
"Default SECRET_KEY must be at least 32 chars"
|
||||
)
|
||||
|
||||
@@ -9,18 +9,19 @@ Covers:
|
||||
- init_async_db
|
||||
- close_async_db
|
||||
"""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from unittest.mock import patch, MagicMock, AsyncMock
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import (
|
||||
get_async_database_url,
|
||||
get_db,
|
||||
async_transaction_scope,
|
||||
check_async_database_health,
|
||||
init_async_db,
|
||||
close_async_db,
|
||||
get_async_database_url,
|
||||
get_db,
|
||||
init_async_db,
|
||||
)
|
||||
|
||||
|
||||
@@ -88,12 +89,13 @@ class TestAsyncTransactionScope:
|
||||
async def test_transaction_scope_commits_on_success(self, async_test_db):
|
||||
"""Test that successful operations are committed (covers line 138)."""
|
||||
# Mock the transaction scope to use test database
|
||||
test_engine, SessionLocal = async_test_db
|
||||
_test_engine, SessionLocal = async_test_db
|
||||
|
||||
with patch('app.core.database.SessionLocal', SessionLocal):
|
||||
with patch("app.core.database.SessionLocal", SessionLocal):
|
||||
async with async_transaction_scope() as db:
|
||||
# Execute a simple query to verify transaction works
|
||||
from sqlalchemy import text
|
||||
|
||||
result = await db.execute(text("SELECT 1"))
|
||||
assert result is not None
|
||||
# Transaction should be committed (covers line 138 debug log)
|
||||
@@ -101,12 +103,13 @@ class TestAsyncTransactionScope:
|
||||
@pytest.mark.asyncio
|
||||
async def test_transaction_scope_rollback_on_error(self, async_test_db):
|
||||
"""Test that transaction rolls back on exception."""
|
||||
test_engine, SessionLocal = async_test_db
|
||||
_test_engine, SessionLocal = async_test_db
|
||||
|
||||
with patch('app.core.database.SessionLocal', SessionLocal):
|
||||
with patch("app.core.database.SessionLocal", SessionLocal):
|
||||
with pytest.raises(RuntimeError, match="Test error"):
|
||||
async with async_transaction_scope() as db:
|
||||
from sqlalchemy import text
|
||||
|
||||
await db.execute(text("SELECT 1"))
|
||||
raise RuntimeError("Test error")
|
||||
|
||||
@@ -117,9 +120,9 @@ class TestCheckAsyncDatabaseHealth:
|
||||
@pytest.mark.asyncio
|
||||
async def test_database_health_check_success(self, async_test_db):
|
||||
"""Test health check returns True on success (covers line 156)."""
|
||||
test_engine, SessionLocal = async_test_db
|
||||
_test_engine, SessionLocal = async_test_db
|
||||
|
||||
with patch('app.core.database.SessionLocal', SessionLocal):
|
||||
with patch("app.core.database.SessionLocal", SessionLocal):
|
||||
result = await check_async_database_health()
|
||||
assert result is True
|
||||
|
||||
@@ -127,7 +130,7 @@ class TestCheckAsyncDatabaseHealth:
|
||||
async def test_database_health_check_failure(self):
|
||||
"""Test health check returns False on database error."""
|
||||
# Mock async_transaction_scope to raise an error
|
||||
with patch('app.core.database.async_transaction_scope') as mock_scope:
|
||||
with patch("app.core.database.async_transaction_scope") as mock_scope:
|
||||
mock_scope.side_effect = Exception("Database connection failed")
|
||||
|
||||
result = await check_async_database_health()
|
||||
@@ -140,10 +143,10 @@ class TestInitAsyncDb:
|
||||
@pytest.mark.asyncio
|
||||
async def test_init_async_db_creates_tables(self, async_test_db):
|
||||
"""Test init_async_db creates tables (covers lines 174-176)."""
|
||||
test_engine, SessionLocal = async_test_db
|
||||
test_engine, _SessionLocal = async_test_db
|
||||
|
||||
# Mock the engine to use test engine
|
||||
with patch('app.core.database.engine', test_engine):
|
||||
with patch("app.core.database.engine", test_engine):
|
||||
await init_async_db()
|
||||
# If no exception, tables were created successfully
|
||||
|
||||
@@ -155,7 +158,6 @@ class TestCloseAsyncDb:
|
||||
async def test_close_async_db_disposes_engine(self):
|
||||
"""Test close_async_db disposes engine (covers lines 185-186)."""
|
||||
# Create a fresh engine to test closing
|
||||
from app.core.database import engine
|
||||
|
||||
# Close connections
|
||||
await close_async_db()
|
||||
|
||||
Reference in New Issue
Block a user