A pytest plugin for mocking Clerk authentication
Project description
pytest-clerk-mock
A pytest plugin for mocking Clerk auth plus the supported Clerk backend SDK clients.
Supported SDK surfaces:
usersorganizationsorganization_memberships
The plugin patches Clerk at the SDK level, so it works even if your app instantiates its Clerk client before the test runs.
Installation
pip install pytest-clerk-mock
Or with Poetry:
poetry add --group dev pytest-clerk-mock
Quick Start
def test_create_user(mock_clerk):
user = mock_clerk.users.create(
email_address=["test@example.com"],
first_name="John",
last_name="Doe",
)
fetched = mock_clerk.users.get(user_id=user.id)
assert fetched.email_addresses[0].email_address == "test@example.com"
Async methods use Clerk-style signatures too:
async def test_create_user_async(mock_clerk):
user = await mock_clerk.users.create_async(email_address=["test@example.com"])
fetched = await mock_clerk.users.get_async(user_id=user.id)
assert fetched.id == user.id
Auth Helpers
def test_auth(mock_clerk, request, options):
mock_clerk.configure_auth("user_123", org_id="org_456", org_role="org:admin")
result = mock_clerk.authenticate_request(request, options)
assert result.is_signed_in
Temporary auth context:
with mock_clerk.as_user("user_456", org_id="org_789"):
...
Predefined users:
MockClerkUser.TEAM_OWNERMockClerkUser.TEAM_MEMBERMockClerkUser.GUESTMockClerkUser.UNAUTHENTICATED
Common Examples
Organizations:
created = mock_clerk.organizations.create(
request={
"name": "My Organization",
"created_by": "user_123",
"slug": "my-org",
}
)
org = mock_clerk.organizations.get(organization_id=created.id)
Organization memberships:
membership = mock_clerk.organization_memberships.create(
organization_id="org_123",
user_id="user_123",
role="org:admin",
)
If your app reads memberships through users.get_organization_memberships(...), seed them explicitly:
from pytest_clerk_mock import MockOrganizationMembershipsResponse
mock_clerk.users.set_organization_memberships(
"user_123",
MockOrganizationMembershipsResponse(data=[], total_count=0),
)
Scope
This package aims for strong parity for the supported Clerk SDK surfaces above. It does not try to mock every Clerk API domain.
The contract tests and CI job are the source of truth for supported method and model parity.
Test Helpers
Useful non-SDK helpers:
mock_clerk.users.set_organization_memberships(...)mock_clerk.organizations.add(...)mock_clerk.organization_memberships.get(...)
Custom fixture:
from pytest_clerk_mock import create_mock_clerk_fixture
mock_clerk = create_mock_clerk_fixture(
default_user_id="user_custom",
default_org_id="org_custom",
default_org_role="org:member",
autouse=True,
)
Context manager:
from pytest_clerk_mock import mock_clerk_backend
with mock_clerk_backend(default_user_id="user_123") as mock:
mock.configure_auth("user_456")
Low-level patch helpers:
from pytest_clerk_mock import (
create_clerk_errors,
mock_clerk_user_creation,
mock_clerk_user_creation_failure,
mock_clerk_user_exists,
)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_clerk_mock-0.3.0.tar.gz.
File metadata
- Download URL: pytest_clerk_mock-0.3.0.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9cb24e0ca02d8864374ac6e86b4ab244d947c1d360341cb5f7af6a355fb2f31
|
|
| MD5 |
381e0690df7da6c5b4aac4a9c138b7b1
|
|
| BLAKE2b-256 |
9a6ae40015baaf79fc9d527e33cb31ed1327334f2ae0d58265b50d0865e9ddf6
|
Provenance
The following attestation bundles were made for pytest_clerk_mock-0.3.0.tar.gz:
Publisher:
publish-to-pypi.yml on julien777z/pytest-clerk-mock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_clerk_mock-0.3.0.tar.gz -
Subject digest:
a9cb24e0ca02d8864374ac6e86b4ab244d947c1d360341cb5f7af6a355fb2f31 - Sigstore transparency entry: 1099052703
- Sigstore integration time:
-
Permalink:
julien777z/pytest-clerk-mock@255c12f5a00d500bd67c032099fecd5495a9e599 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/julien777z
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@255c12f5a00d500bd67c032099fecd5495a9e599 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pytest_clerk_mock-0.3.0-py3-none-any.whl.
File metadata
- Download URL: pytest_clerk_mock-0.3.0-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cac82565866b68c976c379a040cedbaa8ce0e377c477ec267b41e7b018130614
|
|
| MD5 |
329dc0b81cb12ed197bb79b0aaecbecf
|
|
| BLAKE2b-256 |
c49f411b1dc9ea78e64375b03e5a45c50542848fe90a40b856545a007736406a
|
Provenance
The following attestation bundles were made for pytest_clerk_mock-0.3.0-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on julien777z/pytest-clerk-mock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_clerk_mock-0.3.0-py3-none-any.whl -
Subject digest:
cac82565866b68c976c379a040cedbaa8ce0e377c477ec267b41e7b018130614 - Sigstore transparency entry: 1099052756
- Sigstore integration time:
-
Permalink:
julien777z/pytest-clerk-mock@255c12f5a00d500bd67c032099fecd5495a9e599 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/julien777z
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@255c12f5a00d500bd67c032099fecd5495a9e599 -
Trigger Event:
workflow_dispatch
-
Statement type: