Skip to main content

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:

  • users
  • organizations
  • organization_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_OWNER
  • MockClerkUser.TEAM_MEMBER
  • MockClerkUser.GUEST
  • MockClerkUser.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_clerk_mock-0.3.0.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_clerk_mock-0.3.0-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

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

Hashes for pytest_clerk_mock-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a9cb24e0ca02d8864374ac6e86b4ab244d947c1d360341cb5f7af6a355fb2f31
MD5 381e0690df7da6c5b4aac4a9c138b7b1
BLAKE2b-256 9a6ae40015baaf79fc9d527e33cb31ed1327334f2ae0d58265b50d0865e9ddf6

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_clerk_mock-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_clerk_mock-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cac82565866b68c976c379a040cedbaa8ce0e377c477ec267b41e7b018130614
MD5 329dc0b81cb12ed197bb79b0aaecbecf
BLAKE2b-256 c49f411b1dc9ea78e64375b03e5a45c50542848fe90a40b856545a007736406a

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page