from datetime import timedelta import pytest from fastapi.testclient import TestClient from main import app from config import ( OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS, OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS, ) from handler.auth import oauth_handler from handler.redis_handler import sync_cache @pytest.fixture def client(): with TestClient(app) as client: yield client @pytest.fixture(autouse=True) def clear_cache(): sync_cache.flushall() yield sync_cache.flushall() @pytest.fixture() def access_token(admin_user): # noqa data = { "sub": admin_user.username, "iss": "romm:oauth", "scopes": " ".join(admin_user.oauth_scopes), } return oauth_handler.create_access_token( data=data, expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS) ) @pytest.fixture() def refresh_token(admin_user): # noqa data = { "sub": admin_user.username, "iss": "romm:oauth", "scopes": " ".join(admin_user.oauth_scopes), } return oauth_handler.create_refresh_token( data=data, expires_delta=timedelta(seconds=OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS) ) @pytest.fixture def editor_access_token(editor_user): # noqa return oauth_handler.create_access_token( data={ "sub": editor_user.username, "iss": "romm:oauth", "scopes": " ".join(editor_user.oauth_scopes), }, expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS), )