import pytest from http import HTTPStatus as http_status from server.cache.base import get_cache @pytest.mark.parametrize( "t,user_id,last_updated,details,status,expected_result", ( (None, None, None, None, http_status.BAD_REQUEST, {}), (None, "", None, None, http_status.BAD_REQUEST, {}), (None, "123", None, None, http_status.OK, {"need_authorize": False, "updated_at": 946684800}), (None, "123", None, "td1", http_status.OK, {"need_authorize": False, "updated_at": 946684800}), (946684801, "123", None, None, http_status.OK, {"need_authorize": False, "updated_at": 946684800}), (946684801, "123", None, "td1", http_status.OK, {"need_authorize": False, "updated_at": 946684800}), (946684801, "1", 946684805, None, http_status.OK, {"need_authorize": True, "updated_at": 946684805}), (946684900, "1", 946684850, None, http_status.OK, {"need_authorize": False, "updated_at": 946684850}), (None, "123", 946684805, None, http_status.OK, {"need_authorize": True, "updated_at": 946684805}), ( 946684801, "1234", 946684900, "td1", http_status.OK, {"need_authorize": True, "updated_at": 946684900, "details": "td1"}, ), ( 946684900, "4321", 946684950, b"td1", http_status.OK, {"need_authorize": True, "updated_at": 946684950, "details": "td1"}, ), ), ) async def test_check_session(t, user_id, last_updated, details, status, expected_result, client, auth): redis_client = get_cache() if last_updated: await redis_client.cache_client.set(f"auth0:user_{user_id}:updated_at", last_updated) if details: await redis_client.cache_client.set(f"auth0:user_{user_id}:details", details) response = await client.get( "/api/auth/check-session/", params={"t": t} if t else None, headers={**auth, **({"X-User-Id": user_id} if user_id else {})}, ) assert response.status == status if status == http_status.OK: assert await response.json() == expected_result