Не оживлять отозванный токен после недоступности Valkey
This commit is contained in:
@@ -48,13 +48,22 @@ def main():
|
||||
started = time.monotonic()
|
||||
other = new_user('nocache')
|
||||
expect('регистрация и вход без кэша', (200, time.monotonic() - started), [200], 1)
|
||||
expect('logout без кэша: отзыв не подтверждён', timed('DELETE', '/api/v1/auth/sessions/current', other['token']), [503], 1.5)
|
||||
expect('токен после неудачного logout работает', timed('GET', '/api/v1/users/me', other['token']), [200], 1)
|
||||
expect('logout без кэша', timed('DELETE', '/api/v1/auth/sessions/current', other['token']), [204], 1.5)
|
||||
expect('отозванный токен', timed('GET', '/api/v1/users/me', other['token']), [401], 1)
|
||||
finally:
|
||||
compose('up', '-d', '--wait', 'valkey')
|
||||
|
||||
print('1б. Valkey на паузе: logout, затем unpause — отозванный токен не оживает из кэша')
|
||||
revoked = new_user('paused')
|
||||
call('GET', '/api/v1/users/me', token=revoked['token'])
|
||||
compose('pause', 'valkey')
|
||||
try:
|
||||
expect('logout при паузе Valkey', timed('DELETE', '/api/v1/auth/sessions/current', revoked['token']), [204], 1.5)
|
||||
finally:
|
||||
compose('unpause', 'valkey')
|
||||
time.sleep(6)
|
||||
expect('logout после восстановления Valkey', timed('DELETE', '/api/v1/auth/sessions/current', other['token']), [204], 1)
|
||||
expect('отозванный токен', timed('GET', '/api/v1/users/me', other['token']), [401], 1)
|
||||
expect('через 6 с после unpause: /users/me', timed('GET', '/api/v1/users/me', revoked['token']), [401], 1)
|
||||
expect('через 6 с после unpause: сообщение', timed('POST', history, revoked['token'], {'text': 'после logout'}), [401], 1)
|
||||
|
||||
print('2. PostgreSQL остановлен: ограниченная ошибка 503, анонимный запрос не проходит')
|
||||
compose('stop', 'postgres')
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
import unittest
|
||||
|
||||
from helpers import call, compose, new_user
|
||||
|
||||
BYPASS_SECONDS = 5
|
||||
|
||||
|
||||
def key(token):
|
||||
return 'session:' + hashlib.sha256(token.encode()).hexdigest()
|
||||
|
||||
|
||||
def valkey(*args):
|
||||
return compose('exec', '-T', 'valkey', 'valkey-cli', *args)
|
||||
|
||||
|
||||
def me(token):
|
||||
return call('GET', '/api/v1/users/me', token=token)[0]
|
||||
|
||||
|
||||
def logout(token):
|
||||
return call('DELETE', '/api/v1/auth/sessions/current', token=token)[0]
|
||||
|
||||
|
||||
class SessionCacheTest(unittest.TestCase):
|
||||
def test_logout_during_cache_outage_never_leaves_token_alive(self):
|
||||
"""Valkey на паузе: logout не отвечает 204, пока отзыв не записан в кэш; после восстановления токен отзывается"""
|
||||
alice = new_user('revoke')
|
||||
self.assertEqual(me(alice['token']), 200)
|
||||
compose('pause', 'valkey')
|
||||
try:
|
||||
status = logout(alice['token'])
|
||||
finally:
|
||||
compose('unpause', 'valkey')
|
||||
time.sleep(BYPASS_SECONDS + 1)
|
||||
if status == 204:
|
||||
self.assertEqual(me(alice['token']), 401, '204 на logout, но токен остался рабочим')
|
||||
else:
|
||||
self.assertEqual(status, 503)
|
||||
self.assertEqual(logout(alice['token']), 204)
|
||||
self.assertEqual(me(alice['token']), 401)
|
||||
time.sleep(BYPASS_SECONDS + 1)
|
||||
self.assertEqual(me(alice['token']), 401)
|
||||
|
||||
def test_tombstone_blocks_stale_cache_fill(self):
|
||||
"""После logout в кэше tombstone; запоздалая запись положительной сессии (SET NX) не может его затереть"""
|
||||
alice = new_user('tombstone')
|
||||
self.assertEqual(me(alice['token']), 200)
|
||||
self.assertEqual(logout(alice['token']), 204)
|
||||
self.assertEqual(valkey('GET', key(alice['token'])), 'revoked')
|
||||
stale = json.dumps({'user': {'id': alice['id']}, 'expires_at': time.time() + 3600})
|
||||
self.assertEqual(valkey('SET', key(alice['token']), stale, 'EX', '60', 'NX'), '')
|
||||
self.assertEqual(me(alice['token']), 401)
|
||||
|
||||
def test_lost_cache_entry_does_not_log_out(self):
|
||||
"""Потеря записи кэша (DEL/FLUSH) — только промах: сессия читается из PostgreSQL"""
|
||||
alice = new_user('miss')
|
||||
self.assertEqual(me(alice['token']), 200)
|
||||
valkey('DEL', key(alice['token']))
|
||||
self.assertEqual(me(alice['token']), 200)
|
||||
self.assertNotEqual(valkey('GET', key(alice['token'])), '')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user