feat: completed tasks for grade 4
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-11-22 14:40:35 +03:00
parent 0b97f77d76
commit e0263ba902
42 changed files with 5257 additions and 523 deletions
+65
View File
@@ -296,3 +296,68 @@ async def test_whitespace_only_password_at_login_returns_401(client: AsyncClient
assert response.status_code == 401
assert response.headers["WWW-Authenticate"].startswith("Basic")
async def test_login_rehashes_only_after_success(client, db_engine):
from argon2 import PasswordHasher
from sqlalchemy import text
user = await register(client)
previous = await asyncio.to_thread(
PasswordHasher(memory_cost=8192, time_cost=2, parallelism=1).hash, user["password"]
)
async with db_engine.begin() as connection:
await connection.execute(
text("UPDATE users SET password_hash=:hash WHERE username=:name"),
{"hash": previous, "name": user["username"]},
)
async def read_hash():
async with db_engine.connect() as connection:
return await connection.scalar(
text("SELECT password_hash FROM users WHERE username=:name"),
{"name": user["username"]},
)
bad = await client.post("/api/v1/auth/sessions",
headers=basic_header(user["username"], "wrong-password"))
assert bad.status_code == 401
assert await read_hash() == previous
await login(client, user)
updated = await read_hash()
assert updated != previous
assert updated.startswith("$argon2id$v=19$m=19456,t=2,p=1$")
await login(client, user)
assert await read_hash() == updated
async def test_login_capacity_rejects_before_password_verification(client, monkeypatch):
from app.domain.auth import security
limit = 10
all_inside = asyncio.Event()
release = asyncio.Event()
entered = 0
async def wait_for_release(_password):
nonlocal entered
entered += 1
if entered == limit:
all_inside.set()
await release.wait()
monkeypatch.setattr(security, "dummy_verify_password", wait_for_release)
headers = basic_header("missing_user", "synthetic-password")
pending = [
asyncio.create_task(client.post("/api/v1/auth/sessions", headers=headers))
for _ in range(limit)
]
try:
await asyncio.wait_for(all_inside.wait(), timeout=5)
response = await client.post("/api/v1/auth/sessions", headers=headers)
assert response.status_code == 429
assert response.headers["Retry-After"] == "1"
assert entered == limit
finally:
release.set()
assert all(response.status_code == 401 for response in await asyncio.gather(*pending))