pytest-clerk-mock 0.0.2__py3-none-any.whl
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.
- pytest_clerk_mock/__init__.py +39 -0
- pytest_clerk_mock/client.py +189 -0
- pytest_clerk_mock/helpers.py +148 -0
- pytest_clerk_mock/models/__init__.py +18 -0
- pytest_clerk_mock/models/auth.py +50 -0
- pytest_clerk_mock/models/organization.py +30 -0
- pytest_clerk_mock/models/user.py +92 -0
- pytest_clerk_mock/plugin.py +215 -0
- pytest_clerk_mock/services/__init__.py +14 -0
- pytest_clerk_mock/services/auth.py +70 -0
- pytest_clerk_mock/services/users.py +495 -0
- pytest_clerk_mock-0.0.2.dist-info/METADATA +251 -0
- pytest_clerk_mock-0.0.2.dist-info/RECORD +16 -0
- pytest_clerk_mock-0.0.2.dist-info/WHEEL +4 -0
- pytest_clerk_mock-0.0.2.dist-info/entry_points.txt +3 -0
- pytest_clerk_mock-0.0.2.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-clerk-mock
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: A pytest plugin for mocking Clerk authentication
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Author: Julien Kmec
|
|
7
|
+
Author-email: me@julien.dev
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Requires-Dist: clerk-backend-api (>=1.0.0)
|
|
15
|
+
Requires-Dist: pydantic (>=2.0.0)
|
|
16
|
+
Project-URL: Homepage, https://github.com/julien777z/pytest-mock-clerk
|
|
17
|
+
Project-URL: Repository, https://github.com/julien777z/pytest-mock-clerk
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# pytest-clerk-mock
|
|
21
|
+
|
|
22
|
+
A pytest plugin for mocking [Clerk](https://clerk.com/) authentication in your tests.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install pytest-clerk-mock
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or with Poetry:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
poetry add --group dev pytest-clerk-mock
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
The plugin provides a `mock_clerk` fixture that you can use in your tests:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
def test_create_user(mock_clerk):
|
|
42
|
+
user = mock_clerk.users.create(
|
|
43
|
+
email_address=["test@example.com"],
|
|
44
|
+
first_name="John",
|
|
45
|
+
last_name="Doe",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
assert user.id is not None
|
|
49
|
+
assert user.first_name == "John"
|
|
50
|
+
|
|
51
|
+
fetched = mock_clerk.users.get(user.id)
|
|
52
|
+
assert fetched.email_addresses[0].email_address == "test@example.com"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Async API
|
|
56
|
+
|
|
57
|
+
All methods have async variants with an `_async` suffix, matching the clerk-backend-api SDK:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
async def test_create_user_async(mock_clerk):
|
|
61
|
+
user = await mock_clerk.users.create_async(
|
|
62
|
+
email_address=["test@example.com"],
|
|
63
|
+
first_name="John",
|
|
64
|
+
last_name="Doe",
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
assert user.id is not None
|
|
68
|
+
|
|
69
|
+
fetched = await mock_clerk.users.get_async(user.id)
|
|
70
|
+
assert fetched.first_name == "John"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Authentication
|
|
74
|
+
|
|
75
|
+
The mock client provides full authentication state management:
|
|
76
|
+
|
|
77
|
+
### Configure Auth State
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
def test_with_auth(mock_clerk):
|
|
81
|
+
mock_clerk.configure_auth("user_123", org_id="org_456", org_role="org:admin")
|
|
82
|
+
|
|
83
|
+
result = mock_clerk.authenticate_request(request, options)
|
|
84
|
+
assert result.is_signed_in
|
|
85
|
+
assert result.payload["sub"] == "user_123"
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Temporary User Context
|
|
89
|
+
|
|
90
|
+
Use the `as_user` context manager to temporarily switch users:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
def test_as_different_user(mock_clerk):
|
|
94
|
+
with mock_clerk.as_user("user_456", org_id="org_789"):
|
|
95
|
+
result = mock_clerk.authenticate_request(request, options)
|
|
96
|
+
assert result.payload["sub"] == "user_456"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Predefined User Types
|
|
100
|
+
|
|
101
|
+
Use `MockClerkUser` for common test scenarios:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from pytest_clerk_mock import MockClerkUser
|
|
105
|
+
|
|
106
|
+
def test_with_predefined_user(mock_clerk):
|
|
107
|
+
with mock_clerk.as_clerk_user(MockClerkUser.TEAM_OWNER, org_id="org_123"):
|
|
108
|
+
# Authenticated as team owner
|
|
109
|
+
pass
|
|
110
|
+
|
|
111
|
+
with mock_clerk.as_clerk_user(MockClerkUser.GUEST):
|
|
112
|
+
# Authenticated as guest
|
|
113
|
+
pass
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Available predefined users:
|
|
117
|
+
- `MockClerkUser.TEAM_OWNER`
|
|
118
|
+
- `MockClerkUser.TEAM_MEMBER`
|
|
119
|
+
- `MockClerkUser.GUEST`
|
|
120
|
+
- `MockClerkUser.UNAUTHENTICATED`
|
|
121
|
+
|
|
122
|
+
## Organization Memberships
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
def test_organization_memberships(mock_clerk):
|
|
126
|
+
user = mock_clerk.users.create(email_address=["test@example.com"])
|
|
127
|
+
|
|
128
|
+
mock_clerk.add_organization_membership(
|
|
129
|
+
user_id=user.id,
|
|
130
|
+
org_id="org_123",
|
|
131
|
+
role="org:admin",
|
|
132
|
+
org_name="My Organization",
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
memberships = mock_clerk.users.get_organization_memberships(user.id)
|
|
136
|
+
assert memberships.total_count == 1
|
|
137
|
+
assert memberships.data[0].organization.id == "org_123"
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Custom Fixture Configuration
|
|
141
|
+
|
|
142
|
+
Create a custom fixture with different defaults:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
# conftest.py
|
|
146
|
+
from pytest_clerk_mock import create_mock_clerk_fixture
|
|
147
|
+
|
|
148
|
+
mock_clerk = create_mock_clerk_fixture(
|
|
149
|
+
default_user_id="user_custom",
|
|
150
|
+
default_org_id="org_custom",
|
|
151
|
+
default_org_role="org:member",
|
|
152
|
+
autouse=True,
|
|
153
|
+
)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Context Manager API
|
|
157
|
+
|
|
158
|
+
For use outside of pytest fixtures:
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
from pytest_clerk_mock import mock_clerk_backend
|
|
162
|
+
|
|
163
|
+
def test_with_context_manager():
|
|
164
|
+
with mock_clerk_backend(default_user_id="user_123") as mock:
|
|
165
|
+
mock.configure_auth("user_456")
|
|
166
|
+
# Your test code here
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Supported Operations
|
|
170
|
+
|
|
171
|
+
### Users
|
|
172
|
+
|
|
173
|
+
| Sync | Async |
|
|
174
|
+
|------|-------|
|
|
175
|
+
| `create()` | `create_async()` |
|
|
176
|
+
| `get()` | `get_async()` |
|
|
177
|
+
| `list()` | `list_async()` |
|
|
178
|
+
| `update()` | `update_async()` |
|
|
179
|
+
| `delete()` | `delete_async()` |
|
|
180
|
+
| `count()` | `count_async()` |
|
|
181
|
+
| `get_organization_memberships()` | `get_organization_memberships_async()` |
|
|
182
|
+
|
|
183
|
+
### Authentication
|
|
184
|
+
|
|
185
|
+
| Method | Description |
|
|
186
|
+
|--------|-------------|
|
|
187
|
+
| `authenticate_request()` | Mock Clerk's authenticate_request |
|
|
188
|
+
| `configure_auth()` | Set current auth state |
|
|
189
|
+
| `configure_auth_from_user()` | Set auth using MockClerkUser |
|
|
190
|
+
| `as_user()` | Context manager for temporary user |
|
|
191
|
+
| `as_clerk_user()` | Context manager with MockClerkUser |
|
|
192
|
+
| `add_organization_membership()` | Add org membership for a user |
|
|
193
|
+
| `reset()` | Reset all mock state |
|
|
194
|
+
|
|
195
|
+
## Helper Functions
|
|
196
|
+
|
|
197
|
+
Low-level helpers for specific mocking scenarios:
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
from pytest_clerk_mock import (
|
|
201
|
+
mock_clerk_user_creation,
|
|
202
|
+
mock_clerk_user_creation_failure,
|
|
203
|
+
mock_clerk_user_exists,
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
def test_user_creation():
|
|
207
|
+
with mock_clerk_user_creation("myapp.clerk.users.create_async", "user_123") as mock:
|
|
208
|
+
# Your code that creates a user
|
|
209
|
+
mock.assert_called_once()
|
|
210
|
+
|
|
211
|
+
def test_creation_failure():
|
|
212
|
+
with mock_clerk_user_creation_failure("myapp.clerk.users.create_async"):
|
|
213
|
+
# Your code that handles creation failure
|
|
214
|
+
pass
|
|
215
|
+
|
|
216
|
+
def test_duplicate_email():
|
|
217
|
+
with mock_clerk_user_exists(
|
|
218
|
+
"myapp.clerk.users.create_async",
|
|
219
|
+
"myapp.clerk.users.list_async",
|
|
220
|
+
"user_existing_123",
|
|
221
|
+
) as (mock_create, mock_list):
|
|
222
|
+
# Your code that handles duplicate email scenario
|
|
223
|
+
pass
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Exceptions
|
|
227
|
+
|
|
228
|
+
The mock raises appropriate exceptions matching Clerk's behavior:
|
|
229
|
+
|
|
230
|
+
- `UserNotFoundError` - When getting/updating/deleting a non-existent user
|
|
231
|
+
- `ClerkErrors` - When creating a user with a duplicate email (matches real Clerk API)
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
from pytest_clerk_mock import UserNotFoundError
|
|
235
|
+
from clerk_backend_api.models import ClerkErrors
|
|
236
|
+
|
|
237
|
+
def test_user_not_found(mock_clerk):
|
|
238
|
+
with pytest.raises(UserNotFoundError):
|
|
239
|
+
mock_clerk.users.get("nonexistent_user")
|
|
240
|
+
|
|
241
|
+
def test_duplicate_email(mock_clerk):
|
|
242
|
+
mock_clerk.users.create(email_address=["test@example.com"])
|
|
243
|
+
|
|
244
|
+
with pytest.raises(ClerkErrors):
|
|
245
|
+
mock_clerk.users.create(email_address=["test@example.com"])
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
MIT
|
|
251
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
pytest_clerk_mock/__init__.py,sha256=p5sY3Scfv9AfZYLj8FjZVCXLwgLCgAq4mzeNplLuiTQ,1164
|
|
2
|
+
pytest_clerk_mock/client.py,sha256=6f2MAc67uuHuafFjRbzmgAq5XRx1HJDepSrG2roWk48,5621
|
|
3
|
+
pytest_clerk_mock/helpers.py,sha256=ofdoiYMaZ0fN-V2sSt6Db45aeg3KvW3L1urMBVmAZ2Q,4558
|
|
4
|
+
pytest_clerk_mock/models/__init__.py,sha256=-2bUPYrm5cOAgBHBEW-f-0qkWFC0k0vHg9Cicc7g8Qs,529
|
|
5
|
+
pytest_clerk_mock/models/auth.py,sha256=L2IgReu1ldMIqsGLp6Rhh8LwQKSntSB7DZm_z_okTfY,1191
|
|
6
|
+
pytest_clerk_mock/models/organization.py,sha256=braNZGVafW2VDVokCt-7CZZEMNKmkHauuYsTTSM074U,721
|
|
7
|
+
pytest_clerk_mock/models/user.py,sha256=VgmM9IWoYMDQ6t07GD5amv_kvRRBZVIRnI0n-2L5OP0,3044
|
|
8
|
+
pytest_clerk_mock/plugin.py,sha256=Y8vJJtrzL6wfwKOtKibyHxQYjpyNy376oOkg5wrDamg,6207
|
|
9
|
+
pytest_clerk_mock/services/__init__.py,sha256=R7_O0dZGltOKZnuMgT4_3LUtYZ3yaVeyg4_jtJYkd6k,315
|
|
10
|
+
pytest_clerk_mock/services/auth.py,sha256=jsOMl7HtuUOlEdjHyYUD7ReE3_m-MTFcoKpKKRKvrnA,1790
|
|
11
|
+
pytest_clerk_mock/services/users.py,sha256=Pfj4GjFXwHkZNWDLmfTEVVHBj3j0Wz5xw9WQC6V17zM,16503
|
|
12
|
+
pytest_clerk_mock-0.0.2.dist-info/METADATA,sha256=UItf6kTFlw6YXwhh7RQfy7LTA2twsIw-SezriLhPGko,6638
|
|
13
|
+
pytest_clerk_mock-0.0.2.dist-info/WHEEL,sha256=M5asmiAlL6HEcOq52Yi5mmk9KmTVjY2RDPtO4p9DMrc,88
|
|
14
|
+
pytest_clerk_mock-0.0.2.dist-info/entry_points.txt,sha256=u1-5SRz1tzuKx2ZWe4rtaRrrf82P3PbofwsVTu2oHZ4,48
|
|
15
|
+
pytest_clerk_mock-0.0.2.dist-info/licenses/LICENSE,sha256=nz5adr2tudZA3ZTRDj2lolnY1DKGr7oVUVUra5KMnbU,1068
|
|
16
|
+
pytest_clerk_mock-0.0.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Julien Kmec
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|