Skip to content

Commit cf1e308

Browse files
committed
fix tests
1 parent 9456ccb commit cf1e308

3 files changed

Lines changed: 23 additions & 15 deletions

File tree

.secrets.baseline

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
{
9191
"path": "detect_secrets.filters.allowlist.is_line_allowlisted"
9292
},
93+
{
94+
"path": "detect_secrets.filters.common.is_baseline_file",
95+
"filename": ".secrets.baseline"
96+
},
9397
{
9498
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
9599
"min_level": 2
@@ -145,7 +149,7 @@
145149
"filename": "auth_platform/auth_platform/auth_service/auth.py",
146150
"hashed_secret": "c3d3a05a92ff2b63e27aa3e0cd0bc32ff36be9c6",
147151
"is_verified": false,
148-
"line_number": 6
152+
"line_number": 7
149153
}
150154
],
151155
"auth_platform/auth_platform_tests/test_2fa.py": [
@@ -199,5 +203,5 @@
199203
}
200204
]
201205
},
202-
"generated_at": "2025-11-17T00:44:04Z"
206+
"generated_at": "2025-11-17T00:58:14Z"
203207
}

auth_platform/auth_platform/auth_service/auth.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime, timedelta
33
import jwt
44
from sqlalchemy.orm import Session
5+
from .models import TOTPAttempt
56

67
SECRET_KEY = "change-this-secret-in-prod"
78
ALGORITHM = "HS256"
@@ -34,16 +35,17 @@ def check_rate_limit(user_id: int, db: Session) -> tuple[bool, int]:
3435
- is_rate_limited: True if user has exceeded 5 failed attempts in 15 minutes
3536
- minutes_until_reset: Minutes until rate limit resets (0 if not limited)
3637
"""
37-
from .models import TOTPAttempt
38-
3938
# Calculate time window (15 minutes ago)
4039
time_window = datetime.utcnow() - timedelta(minutes=15)
4140

41+
# Refresh session to get latest data
42+
db.expire_all()
43+
4244
# Query failed attempts within the time window
4345
failed_attempts = db.query(TOTPAttempt).filter(
4446
TOTPAttempt.user_id == user_id,
4547
TOTPAttempt.attempted_at > time_window,
46-
TOTPAttempt.success is False
48+
TOTPAttempt.success == False # pylint: disable=singleton-comparison
4749
).order_by(TOTPAttempt.attempted_at.asc()).all()
4850

4951
# Check if rate limit exceeded
@@ -69,8 +71,6 @@ def record_totp_attempt(user_id: int, success: bool, db: Session) -> None:
6971
success: Whether the verification was successful
7072
db: Database session
7173
"""
72-
from .models import TOTPAttempt
73-
7474
attempt = TOTPAttempt(
7575
user_id=user_id,
7676
success=success,

auth_platform/auth_platform/auth_service/db.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from sqlalchemy import create_engine, Index
1+
from sqlalchemy import create_engine, Index, inspect
22
from sqlalchemy.ext.declarative import declarative_base
33
from sqlalchemy.orm import sessionmaker
44
from sqlalchemy.exc import SQLAlchemyError
@@ -13,13 +13,17 @@ def init_db():
1313
Base.metadata.create_all(bind=engine)
1414

1515
# Create index for TOTP attempts if not exists
16-
from .models import TOTPAttempt
17-
idx = Index('idx_totp_attempts_user_time', TOTPAttempt.user_id, TOTPAttempt.attempted_at)
18-
try:
19-
idx.create(bind=engine, checkfirst=True)
20-
except SQLAlchemyError:
21-
# Index may already exist
22-
pass
16+
from .models import TOTPAttempt # Import here to avoid circular dependency
17+
inspector = inspect(engine)
18+
existing_indexes = [idx['name'] for idx in inspector.get_indexes('totp_attempts')]
19+
20+
if 'idx_totp_attempts_user_time' not in existing_indexes:
21+
idx = Index('idx_totp_attempts_user_time', TOTPAttempt.user_id, TOTPAttempt.attempted_at)
22+
try:
23+
idx.create(bind=engine)
24+
except SQLAlchemyError:
25+
# Index may already exist (race condition)
26+
pass
2327

2428
def get_db():
2529
db = SessionLocal()

0 commit comments

Comments
 (0)