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.5.0.tar.gz (22.7 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.5.0-py3-none-any.whl (29.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_clerk_mock-0.5.0.tar.gz.

File metadata

  • Download URL: pytest_clerk_mock-0.5.0.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pytest_clerk_mock-0.5.0.tar.gz
Algorithm Hash digest
SHA256 dbd63f941b213e29150a96c9b50d87f384bfa2099151cd6fc73a29d590c57b4e
MD5 dd9a82abcb180ea401d873531fc0bf5a
BLAKE2b-256 feaab4e2ed774106cbe3fdd39a419af65d054ad13056934b70169cca7ebf754b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_clerk_mock-0.5.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.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_clerk_mock-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7b67e27bdca6a7864887b00a2a2fc6718baef4ef09f75b83756e525ac239bab
MD5 00d5710592511cfc5feb8def7f2d52a6
BLAKE2b-256 1a9c1aec4171b2a1feacd36e8be6359898a59f6f51dc429d39a638987c6691f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_clerk_mock-0.5.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