simple-module-test 0.0.45__tar.gz → 0.0.46__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/PKG-INFO +8 -5
  2. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/README.md +1 -0
  3. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/pyproject.toml +8 -5
  4. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/__init__.py +5 -1
  5. simple_module_test-0.0.46/simple_module_test/oidc_issuer.py +84 -0
  6. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/plugin.py +1 -0
  7. simple_module_test-0.0.46/tests/test_testing_oidc_issuer.py +28 -0
  8. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/.gitignore +0 -0
  9. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/LICENSE +0 -0
  10. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/_compile_cache.py +0 -0
  11. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/_schema.py +0 -0
  12. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/_sqlite_template.py +0 -0
  13. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/app_factory.py +0 -0
  14. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/database.py +0 -0
  15. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/fake_events.py +0 -0
  16. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/fixtures.py +0 -0
  17. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/inertia.py +0 -0
  18. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/inertia_page.py +0 -0
  19. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/mail.py +0 -0
  20. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/process_state.py +0 -0
  21. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/redis_server.py +0 -0
  22. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/routes.py +0 -0
  23. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/session_cookie.py +0 -0
  24. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/setup_wizard.py +0 -0
  25. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/tenant_client.py +0 -0
  26. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/simple_module_test/waiting.py +0 -0
  27. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_app_factory.py +0 -0
  28. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_fake_events.py +0 -0
  29. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_plugin_fixtures.py +0 -0
  30. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_schema_reuse_pg.py +0 -0
  31. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_shared_compile_cache.py +0 -0
  32. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_sqlite_schema_template.py +0 -0
  33. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_testing_inertia_headers.py +0 -0
  34. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_testing_inertia_page.py +0 -0
  35. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_testing_process_state.py +0 -0
  36. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_waiting_helpers.py +0 -0
  37. {simple_module_test-0.0.45 → simple_module_test-0.0.46}/tests/test_worker_database.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: simple_module_test
3
- Version: 0.0.45
3
+ Version: 0.0.46
4
4
  Summary: Shared pytest fixtures (app, client, db_session, authenticated_client) for writing simple_module module tests
5
5
  Project-URL: Homepage, https://github.com/antosubash/simple_module_python
6
6
  Project-URL: Repository, https://github.com/antosubash/simple_module_python
@@ -21,14 +21,16 @@ Classifier: Programming Language :: Python :: 3.12
21
21
  Classifier: Topic :: Software Development :: Testing
22
22
  Classifier: Typing :: Typed
23
23
  Requires-Python: >=3.12
24
+ Requires-Dist: cryptography>=42
24
25
  Requires-Dist: fastapi<0.163,>=0.140
25
26
  Requires-Dist: httpx<0.29,>=0.27
27
+ Requires-Dist: pyjwt[crypto]<3,>=2.8
26
28
  Requires-Dist: pytest-asyncio<2,>=0.24
27
29
  Requires-Dist: pytest<10,>=8.0
28
- Requires-Dist: simple-module-core==0.0.45
29
- Requires-Dist: simple-module-db==0.0.45
30
- Requires-Dist: simple-module-hosting==0.0.45
31
- Requires-Dist: simple-module-inertia==0.0.45
30
+ Requires-Dist: simple-module-core==0.0.46
31
+ Requires-Dist: simple-module-db==0.0.46
32
+ Requires-Dist: simple-module-hosting==0.0.46
33
+ Requires-Dist: simple-module-inertia==0.0.46
32
34
  Requires-Dist: sqlalchemy<3,>=2.0
33
35
  Description-Content-Type: text/markdown
34
36
 
@@ -58,6 +60,7 @@ All fixtures below are auto-registered via the `pytest11` entry point — instal
58
60
  - `client` — an `httpx.AsyncClient` bound to the test app.
59
61
  - `authenticated_client` — same but with an admin user seeded and a forged session cookie attached. **Requires the `users` module** to be installed (it seeds the admin via `users.core.bootstrap`); apps scaffolded by `smpy` include it.
60
62
  - `tenant_client` — factory: `async with tenant_client("member") as (client, tenant_id, user_id)` signs in a fresh user who is an `owner` / `admin` / `member` (default `owner`) of a new tenant — or of `tenant_id=` when given — with that tenant active. **Requires the `users` and `tenants` modules.**
63
+ - `oidc_test_issuer` — an in-process OpenID Connect issuer (`OidcTestIssuer`): an RSA signing key with `jwks()`, `discovery()` and `sign(claims)`, for testing an OIDC sign-in through `httpx_mock` without an IdP.
61
64
 
62
65
  ## Usage
63
66
 
@@ -24,6 +24,7 @@ All fixtures below are auto-registered via the `pytest11` entry point — instal
24
24
  - `client` — an `httpx.AsyncClient` bound to the test app.
25
25
  - `authenticated_client` — same but with an admin user seeded and a forged session cookie attached. **Requires the `users` module** to be installed (it seeds the admin via `users.core.bootstrap`); apps scaffolded by `smpy` include it.
26
26
  - `tenant_client` — factory: `async with tenant_client("member") as (client, tenant_id, user_id)` signs in a fresh user who is an `owner` / `admin` / `member` (default `owner`) of a new tenant — or of `tenant_id=` when given — with that tenant active. **Requires the `users` and `tenants` modules.**
27
+ - `oidc_test_issuer` — an in-process OpenID Connect issuer (`OidcTestIssuer`): an RSA signing key with `jwks()`, `discovery()` and `sign(claims)`, for testing an OIDC sign-in through `httpx_mock` without an IdP.
27
28
 
28
29
  ## Usage
29
30
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "simple_module_test"
3
- version = "0.0.45"
3
+ version = "0.0.46"
4
4
  description = "Shared pytest fixtures (app, client, db_session, authenticated_client) for writing simple_module module tests"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -23,12 +23,15 @@ classifiers = [
23
23
  dependencies = [
24
24
  "fastapi>=0.140,<0.163",
25
25
  "httpx>=0.27,<0.29",
26
+ # OidcTestIssuer: sign ID tokens and publish a JWKS (#879).
27
+ "cryptography>=42",
28
+ "PyJWT[crypto]>=2.8,<3",
26
29
  "pytest>=8.0,<10",
27
30
  "pytest-asyncio>=0.24,<2",
28
- "simple_module_core==0.0.45",
29
- "simple_module_db==0.0.45",
30
- "simple_module_hosting==0.0.45",
31
- "simple_module_inertia==0.0.45",
31
+ "simple_module_core==0.0.46",
32
+ "simple_module_db==0.0.46",
33
+ "simple_module_hosting==0.0.46",
34
+ "simple_module_inertia==0.0.46",
32
35
  "sqlalchemy>=2.0,<3",
33
36
  ]
34
37
 
@@ -25,13 +25,16 @@ their definitions. ``authenticated_client`` additionally requires the ``users``
25
25
  module to be installed — it seeds an admin via ``users.core.bootstrap``.
26
26
  ``tenant_client`` (``users`` + ``tenants``) is a factory yielding a client signed
27
27
  in as an ``owner``/``admin``/``member`` of a real tenant — see
28
- ``simple_module_test.tenant_client``.
28
+ ``simple_module_test.tenant_client``. ``oidc_test_issuer`` is an in-process
29
+ OpenID Connect issuer (:class:`OidcTestIssuer`: signing key, JWKS, discovery
30
+ document and ``sign(claims)``) for testing an OIDC sign-in without an IdP.
29
31
  """
30
32
 
31
33
  from simple_module_test.app_factory import build_test_app
32
34
  from simple_module_test.fake_events import FakeEventBus, RecordedEvent
33
35
  from simple_module_test.inertia import inertia_headers
34
36
  from simple_module_test.inertia_page import InertiaPage, inertia_page
37
+ from simple_module_test.oidc_issuer import OidcTestIssuer
35
38
  from simple_module_test.routes import effective_route_paths
36
39
  from simple_module_test.session_cookie import forge_session_cookie
37
40
  from simple_module_test.tenant_client import TenantClient
@@ -40,6 +43,7 @@ from simple_module_test.waiting import wait_for, wait_for_sync
40
43
  __all__ = [
41
44
  "FakeEventBus",
42
45
  "InertiaPage",
46
+ "OidcTestIssuer",
43
47
  "RecordedEvent",
44
48
  "TenantClient",
45
49
  "build_test_app",
@@ -0,0 +1,84 @@
1
+ """An in-process OpenID Connect issuer for tests: a signing key, JWKS and discovery.
2
+
3
+ Lets a module test an OIDC sign-in end to end without a real IdP (#879)::
4
+
5
+ issuer = OidcTestIssuer("https://idp.test/tenant/v2.0")
6
+ httpx_mock.add_response(url=issuer.discovery_url, json=issuer.discovery())
7
+ httpx_mock.add_response(url=issuer.jwks_url, json=issuer.jwks())
8
+ id_token = issuer.sign({"aud": "client", "sub": "user-1", "nonce": nonce})
9
+
10
+ ``sign`` fills ``iss``, ``iat`` and ``exp``; pass ``None`` for a claim to drop it.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import functools
16
+ import json
17
+ import time
18
+ from typing import Any
19
+
20
+ import jwt
21
+ import pytest
22
+ from cryptography.hazmat.primitives.asymmetric import rsa
23
+ from jwt.algorithms import RSAAlgorithm
24
+
25
+
26
+ @functools.cache
27
+ def _signing_key() -> rsa.RSAPrivateKey:
28
+ """One RSA key per process: generating 2048-bit keys per test is slow."""
29
+ return rsa.generate_private_key(public_exponent=65537, key_size=2048)
30
+
31
+
32
+ class OidcTestIssuer:
33
+ """A fake IdP whose endpoints live under ``issuer`` and whose tokens verify."""
34
+
35
+ def __init__(self, issuer: str = "https://idp.test/realms/test", kid: str = "test-key") -> None:
36
+ self.issuer = issuer.rstrip("/")
37
+ self.kid = kid
38
+ self._key = _signing_key()
39
+
40
+ @property
41
+ def discovery_url(self) -> str:
42
+ return f"{self.issuer}/.well-known/openid-configuration"
43
+
44
+ @property
45
+ def jwks_url(self) -> str:
46
+ return f"{self.issuer}/keys"
47
+
48
+ @property
49
+ def authorization_endpoint(self) -> str:
50
+ return f"{self.issuer}/authorize"
51
+
52
+ @property
53
+ def token_endpoint(self) -> str:
54
+ return f"{self.issuer}/token"
55
+
56
+ def discovery(self) -> dict[str, Any]:
57
+ """The discovery document for this issuer."""
58
+ return {
59
+ "issuer": self.issuer,
60
+ "authorization_endpoint": self.authorization_endpoint,
61
+ "token_endpoint": self.token_endpoint,
62
+ "jwks_uri": self.jwks_url,
63
+ "response_types_supported": ["code"],
64
+ "code_challenge_methods_supported": ["S256"],
65
+ "id_token_signing_alg_values_supported": ["RS256"],
66
+ }
67
+
68
+ def jwks(self) -> dict[str, Any]:
69
+ """The public signing key as a JWKS document."""
70
+ jwk = json.loads(RSAAlgorithm.to_jwk(self._key.public_key()))
71
+ return {"keys": [{**jwk, "kid": self.kid, "use": "sig", "alg": "RS256"}]}
72
+
73
+ def sign(self, claims: dict[str, Any], *, lifetime: int = 300) -> str:
74
+ """An RS256 ID token from this issuer; ``None`` values drop a claim."""
75
+ now = int(time.time())
76
+ payload = {"iss": self.issuer, "iat": now, "exp": now + lifetime, **claims}
77
+ payload = {k: v for k, v in payload.items() if v is not None}
78
+ return jwt.encode(payload, self._key, algorithm="RS256", headers={"kid": self.kid})
79
+
80
+
81
+ @pytest.fixture
82
+ def oidc_test_issuer() -> OidcTestIssuer:
83
+ """A fresh :class:`OidcTestIssuer` per test."""
84
+ return OidcTestIssuer()
@@ -35,6 +35,7 @@ from simple_module_test.fixtures import ( # noqa: F401
35
35
  setup_pending_app,
36
36
  )
37
37
  from simple_module_test.mail import captured_mail # noqa: F401
38
+ from simple_module_test.oidc_issuer import oidc_test_issuer # noqa: F401
38
39
  from simple_module_test.process_state import _reset_process_state # noqa: F401
39
40
  from simple_module_test.redis_server import redis_server # noqa: F401
40
41
  from simple_module_test.tenant_client import tenant_client # noqa: F401
@@ -0,0 +1,28 @@
1
+ """``OidcTestIssuer`` issues tokens its own JWKS verifies (#879)."""
2
+
3
+ import jwt
4
+ from jwt.algorithms import RSAAlgorithm
5
+ from simple_module_test import OidcTestIssuer
6
+
7
+
8
+ def test_signed_tokens_verify_against_the_published_jwks(oidc_test_issuer):
9
+ token = oidc_test_issuer.sign({"aud": "client", "sub": "user-1", "nonce": "n"})
10
+ key = RSAAlgorithm.from_jwk(oidc_test_issuer.jwks()["keys"][0])
11
+ claims = jwt.decode(
12
+ token, key, algorithms=["RS256"], audience="client", issuer=oidc_test_issuer.issuer
13
+ )
14
+ assert (claims["sub"], claims["nonce"]) == ("user-1", "n")
15
+ assert jwt.get_unverified_header(token)["kid"] == oidc_test_issuer.kid
16
+
17
+
18
+ def test_discovery_points_at_the_issuer_endpoints():
19
+ issuer = OidcTestIssuer("https://idp.test/tenant/v2.0/")
20
+ doc = issuer.discovery()
21
+ assert doc["issuer"] == "https://idp.test/tenant/v2.0"
22
+ assert doc["jwks_uri"] == issuer.jwks_url
23
+ assert issuer.discovery_url.endswith("/.well-known/openid-configuration")
24
+
25
+
26
+ def test_none_drops_a_claim(oidc_test_issuer):
27
+ token = oidc_test_issuer.sign({"sub": "x", "exp": None})
28
+ assert "exp" not in jwt.decode(token, options={"verify_signature": False})