Skip to content

Commit ca52835

Browse files
committed
Omit None when submitting registration data to Auth0 + tests
1 parent 1b7ce8a commit ca52835

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

auth0/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def get_user(self, user_id: str) -> Auth0UserData:
9696

9797
def create_user(self, user: BiocommonsRegisterData) -> Auth0UserData:
9898
url = f"https://{self.domain}/api/v2/users"
99-
resp = self._client.post(url, json=user.model_dump(mode="json"))
99+
# Exclude None values to avoid validation errors.
100+
resp = self._client.post(url, json=user.model_dump(mode="json", exclude_none=True))
100101
resp.raise_for_status()
101102
return Auth0UserData(**resp.json())
102103

tests/datagen.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from polyfactory.decorators import post_generated
55
from polyfactory.factories.pydantic_factory import ModelFactory
66

7-
from schemas.biocommons import Auth0UserData, BiocommonsAppMetadata
7+
from schemas.biocommons import (
8+
Auth0UserData,
9+
BiocommonsAppMetadata,
10+
BiocommonsRegisterData,
11+
)
812
from schemas.biocommons_register import BiocommonsRegistrationRequest
913
from schemas.bpa import BPARegistrationRequest
1014
from schemas.galaxy import GalaxyRegistrationData
@@ -28,6 +32,12 @@ def sub(cls) -> str:
2832
return random_auth0_id()
2933

3034

35+
class BiocommonsRegisterDataFactory(ModelFactory[BiocommonsRegisterData]):
36+
37+
@classmethod
38+
def connection(cls) -> str:
39+
return "Username-Password-Authentication"
40+
3141
class SessionUserFactory(ModelFactory[SessionUser]): ...
3242

3343

tests/test_auth0_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import json
2+
13
import pytest
24
import respx
35
from httpx import Response
46

57
from auth0.client import UsersWithTotals
68
from tests.datagen import (
79
Auth0UserDataFactory,
10+
BiocommonsRegisterDataFactory,
811
random_auth0_id,
912
random_auth0_role_id,
1013
)
@@ -131,3 +134,34 @@ def test_add_roles_to_user(test_auth0_client):
131134
call_data = route.calls[0].request.content
132135
# Check role_id is passed as a list
133136
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

Comments
 (0)