#!/usr/bin/env python3 """Verify that a Redis-backed Bearer session expires without logging out.""" import time from datetime import datetime, timezone from client import Client, arguments, check, run, timestamp def main(): args = arguments('Session TTL check; use SESSION_TTL_SECONDS=3 in .env for this test') c = Client(args.base_url, args.ca_file) actor = c.user('ttl') # Confirm the session works before waiting for its server-declared expiry. c.call('GET', '/api/v1/users/me', 200, actor=actor, schema='User') # Client.user stores the registration payload, not the session response. Log in once # more to obtain the exact expires_at returned by the API and test that token. import base64 credentials = actor['registration'] basic = base64.b64encode((credentials['username'] + ':' + credentials['password']).encode()).decode() session = c.call('POST', '/api/v1/auth/sessions', 201, headers={'Authorization': 'Basic ' + basic}, schema='Session')['json'] ttl_seconds = (timestamp(session['expires_at']) - datetime.now(timezone.utc)).total_seconds() check(0 < ttl_seconds <= 30, 'Set SESSION_TTL_SECONDS=3 (or another value <=30) in .env and recreate the API container') token_actor = {'id': actor['id'], 'token': session['token']} c.call('GET', '/api/v1/users/me', 200, actor=token_actor, schema='User') time.sleep(max(0, ttl_seconds) + 1.0) c.call('GET', '/api/v1/users/me', 401, actor=token_actor) print('PASS: expired Redis session is rejected with HTTP 401') if __name__ == '__main__': run(main)