This commit is contained in:
Оля Мороз
2026-10-10 12:33:19 +03:00
parent 692cfec080
commit 66dffa5346
7 changed files with 203 additions and 88 deletions
+30 -5
View File
@@ -62,18 +62,22 @@ def test_redis_down_api_still_works(c):
def test_postgres_down_readiness_503(c):
"""Остановить PostgreSQL → /health/ready должен вернуть 503."""
c.call('GET', '/health/ready', 200, schema='Health')
"""Активный токен при недоступном PostgreSQL → 503, не 401."""
alice, _, _, _ = c.bootstrap()
# очищаем кэш, чтобы гарантированно идти в БД
compose('exec', '-T', 'redis', 'redis-cli', 'FLUSHALL')
compose('stop', '-t', '1', 'postgres')
try:
c.call('GET', '/health/ready', 503)
c.call('GET', '/api/v1/users/me', 503, actor=alice)
# анонимный доступ всё ещё 401
c.call('GET', '/api/v1/users/me', 401)
finally:
compose('start', 'postgres')
for _ in range(60):
if compose('ps', '--format', 'json', 'postgres', check_rc=False).stdout.find('"Health":"healthy"') >= 0:
if '"Health":"healthy"' in compose('ps', '--format', 'json', 'postgres', check_rc=False).stdout:
break
time.sleep(0.5)
print('PASS: /health/ready returns 503 when PostgreSQL is down')
print('PASS: PostgreSQL down returns 503 for valid token, not 401')
def test_username_race(c):
@@ -118,6 +122,26 @@ def test_logout_removes_from_source_of_truth(c):
c.call('GET', '/api/v1/users/me', 401, actor=alice)
print('PASS: logout revokes session in source of truth')
def test_revoked_token_not_resurrected_by_redis(c):
"""Отозванный токен не оживает после рестарта Redis."""
alice, _, _, _ = c.bootstrap()
compose('stop', '-t', '1', 'redis')
try:
# logout при лежащем Redis
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=alice)
# токен уже не работает (Redis лежит, идём в БД)
c.call('GET', '/api/v1/users/me', 401, actor=alice)
finally:
compose('start', 'redis')
# ждём healthy
for _ in range(30):
out = compose('ps', '--format', 'json', 'redis', check_rc=False).stdout
if '"Health":"healthy"' in out:
break
time.sleep(0.5)
# ключевая проверка: после рестарта Redis тот же токен по-прежнему 401
c.call('GET', '/api/v1/users/me', 401, actor=alice)
print('PASS: revoked token is not resurrected by Redis restart')
def main():
@@ -139,6 +163,7 @@ def main():
else:
test_redis_down_api_still_works(c)
test_postgres_down_readiness_503(c)
test_revoked_token_not_resurrected_by_redis(c)
print('PASS: lab2 level-4 criteria (own tests, no short TTL)')