1- import pytest
1+ import time
22from collections .abc import AsyncIterator , Iterator
33
4+ import jwt
5+ import pytest
6+ from alembic import command
7+ from alembic .config import Config as AlembicConfig
8+ from cryptography .hazmat .primitives import serialization
9+ from cryptography .hazmat .primitives .asymmetric import rsa
10+ from sqlalchemy .ext .asyncio import (
11+ AsyncEngine ,
12+ AsyncSession ,
13+ async_sessionmaker ,
14+ create_async_engine ,
15+ )
16+ from testcontainers .postgres import PostgresContainer
17+
418
519def pytest_collection_modifyitems (items ):
620 """Ensure test_schema runs before test_progress to avoid committed-row cross-pollution."""
21+
722 def _key (item ):
823 path = item .nodeid
924 if "test_schema" in path :
1025 return 0
1126 if "test_progress" in path :
1227 return 1
1328 return 2
14- items .sort (key = _key )
1529
16- from alembic import command
17- from alembic .config import Config as AlembicConfig
18- from sqlalchemy .ext .asyncio import AsyncEngine , AsyncSession , async_sessionmaker , create_async_engine
19- from testcontainers .postgres import PostgresContainer
30+ items .sort (key = _key )
2031
2132
2233@pytest .fixture (scope = "session" )
@@ -49,13 +60,6 @@ async def session(engine: AsyncEngine) -> AsyncIterator[AsyncSession]:
4960 await s .rollback ()
5061
5162
52- import time
53-
54- import jwt
55- from cryptography .hazmat .primitives import serialization
56- from cryptography .hazmat .primitives .asymmetric import rsa
57-
58-
5963@pytest .fixture (scope = "session" )
6064def signing_key ():
6165 key = rsa .generate_private_key (public_exponent = 65537 , key_size = 2048 )
@@ -81,10 +85,20 @@ def audience() -> str:
8185@pytest .fixture
8286def make_token (signing_key , issuer , audience ):
8387 priv , _ = signing_key
88+
8489 def _make (sub : str , ttl : int = 60 , ** extra ) -> str :
8590 now = int (time .time ())
86- claims = {"sub" : sub , "iss" : issuer , "aud" : audience , "iat" : now , "nbf" : now , "exp" : now + ttl , ** extra }
91+ claims = {
92+ "sub" : sub ,
93+ "iss" : issuer ,
94+ "aud" : audience ,
95+ "iat" : now ,
96+ "nbf" : now ,
97+ "exp" : now + ttl ,
98+ ** extra ,
99+ }
87100 return jwt .encode (claims , priv , algorithm = "RS256" , headers = {"kid" : "test-key" })
101+
88102 return _make
89103
90104
@@ -97,6 +111,7 @@ def app_under_test(postgres_url, alembic_upgrade, monkeypatch, signing_key, issu
97111
98112 # Bust the lru_cache so the env vars take effect.
99113 from opds_sync .config import get_settings
114+
100115 get_settings .cache_clear ()
101116
102117 from opds_sync .core .auth import JwksFetcher , JwtValidator
@@ -106,7 +121,9 @@ def app_under_test(postgres_url, alembic_upgrade, monkeypatch, signing_key, issu
106121
107122 class StaticJwks (JwksFetcher ):
108123 async def get_signing_key (self , kid ): # type: ignore[override]
109- class _K : key = pub # noqa: E701
124+ class _K :
125+ key = pub # noqa: E701
126+
110127 return _K ()
111128
112129 app = create_app ()
0 commit comments