Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion auth0/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def get_user(self, user_id: str) -> Auth0UserData:

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

Expand Down
12 changes: 11 additions & 1 deletion tests/datagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
from polyfactory.decorators import post_generated
from polyfactory.factories.pydantic_factory import ModelFactory

from schemas.biocommons import Auth0UserData, BiocommonsAppMetadata
from schemas.biocommons import (
Auth0UserData,
BiocommonsAppMetadata,
BiocommonsRegisterData,
)
from schemas.biocommons_register import BiocommonsRegistrationRequest
from schemas.bpa import BPARegistrationRequest
from schemas.galaxy import GalaxyRegistrationData
Expand All @@ -28,6 +32,12 @@ def sub(cls) -> str:
return random_auth0_id()


class BiocommonsRegisterDataFactory(ModelFactory[BiocommonsRegisterData]):

@classmethod
def connection(cls) -> str:
return "Username-Password-Authentication"

class SessionUserFactory(ModelFactory[SessionUser]): ...


Expand Down
34 changes: 34 additions & 0 deletions tests/test_auth0_client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import json

import pytest
import respx
from httpx import Response

from auth0.client import UsersWithTotals
from tests.datagen import (
Auth0UserDataFactory,
BiocommonsRegisterDataFactory,
random_auth0_id,
random_auth0_role_id,
)
Expand Down Expand Up @@ -131,3 +134,34 @@ def test_add_roles_to_user(test_auth0_client):
call_data = route.calls[0].request.content
# Check role_id is passed as a list
assert call_data == b'{"roles":["' +role_id.encode() + b'"]}'


@respx.mock
def test_create_user(test_auth0_client):
"""
Test that we call the Auth0 API to create a user with the data we expect
"""
register_data = BiocommonsRegisterDataFactory.build()
# Mock the response from the Auth0 API, non-matching data
auth0_data = Auth0UserDataFactory.build()
route = respx.post("https://auth0.example.com/api/v2/users").respond(201, json=auth0_data.model_dump(mode="json"))
test_auth0_client.create_user(register_data)
assert route.called
assert json.loads(route.calls.last.request.content) == register_data.model_dump(mode="json", exclude_none=True)


@respx.mock
def test_create_user_omits_none(test_auth0_client):
"""
Test that None/null fields are omitted from the request to Auth0 API
"""
register_data = BiocommonsRegisterDataFactory.build(name=None, user_metadata=None)
# Mock the response from the Auth0 API, non-matching data
auth0_data = Auth0UserDataFactory.build()
route = respx.post("https://auth0.example.com/api/v2/users").respond(201, json=auth0_data.model_dump(mode="json"))
test_auth0_client.create_user(register_data)
assert route.called
call_data = json.loads(route.calls.last.request.content)
assert call_data == register_data.model_dump(mode="json", exclude_none=True)
assert "name" not in call_data
assert "user_metadata" not in call_data