|
| 1 | +import json |
| 2 | + |
1 | 3 | import pytest |
2 | 4 | import respx |
3 | 5 | from httpx import Response |
4 | 6 |
|
5 | 7 | from auth0.client import UsersWithTotals |
6 | 8 | from tests.datagen import ( |
7 | 9 | Auth0UserDataFactory, |
| 10 | + BiocommonsRegisterDataFactory, |
8 | 11 | random_auth0_id, |
9 | 12 | random_auth0_role_id, |
10 | 13 | ) |
@@ -131,3 +134,34 @@ def test_add_roles_to_user(test_auth0_client): |
131 | 134 | call_data = route.calls[0].request.content |
132 | 135 | # Check role_id is passed as a list |
133 | 136 | assert call_data == b'{"roles":["' +role_id.encode() + b'"]}' |
| 137 | + |
| 138 | + |
| 139 | +@respx.mock |
| 140 | +def test_create_user(test_auth0_client): |
| 141 | + """ |
| 142 | + Test that we call the Auth0 API to create a user with the data we expect |
| 143 | + """ |
| 144 | + register_data = BiocommonsRegisterDataFactory.build() |
| 145 | + # Mock the response from the Auth0 API, non-matching data |
| 146 | + auth0_data = Auth0UserDataFactory.build() |
| 147 | + route = respx.post("https://auth0.example.com/api/v2/users").respond(201, json=auth0_data.model_dump(mode="json")) |
| 148 | + test_auth0_client.create_user(register_data) |
| 149 | + assert route.called |
| 150 | + assert json.loads(route.calls.last.request.content) == register_data.model_dump(mode="json", exclude_none=True) |
| 151 | + |
| 152 | + |
| 153 | +@respx.mock |
| 154 | +def test_create_user_omits_none(test_auth0_client): |
| 155 | + """ |
| 156 | + Test that None/null fields are omitted from the request to Auth0 API |
| 157 | + """ |
| 158 | + register_data = BiocommonsRegisterDataFactory.build(name=None, user_metadata=None) |
| 159 | + # Mock the response from the Auth0 API, non-matching data |
| 160 | + auth0_data = Auth0UserDataFactory.build() |
| 161 | + route = respx.post("https://auth0.example.com/api/v2/users").respond(201, json=auth0_data.model_dump(mode="json")) |
| 162 | + test_auth0_client.create_user(register_data) |
| 163 | + assert route.called |
| 164 | + call_data = json.loads(route.calls.last.request.content) |
| 165 | + assert call_data == register_data.model_dump(mode="json", exclude_none=True) |
| 166 | + assert "name" not in call_data |
| 167 | + assert "user_metadata" not in call_data |
0 commit comments