Skip to content

Commit d31ef0d

Browse files
committed
feat: correctly handle base64 random generation
1 parent adb321d commit d31ef0d

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

scim2_tester/resource.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
import base64
12
import random
23
import uuid
34
from enum import Enum
45
from inspect import isclass
6+
from typing import Annotated
57
from typing import Any
68
from typing import get_args
79
from typing import get_origin
810

9-
from pydantic import EmailStr
1011
from scim2_models import ComplexAttribute
1112
from scim2_models import Extension
1213
from scim2_models import ExternalReference
1314
from scim2_models import Meta
1415
from scim2_models import Reference
16+
from scim2_models import Required
1517
from scim2_models import Resource
1618
from scim2_models import ResourceType
1719
from scim2_models import URIReference
@@ -37,8 +39,10 @@ def fill_with_random_values(obj: Resource) -> Resource:
3739
if field.default:
3840
continue
3941

40-
field_type = obj.get_field_root_type(field_name)
4142
is_multiple = obj.get_field_multiplicity(field_name)
43+
field_type = obj.get_field_root_type(field_name)
44+
if get_origin(field_type) == Annotated:
45+
field_type = get_args(field_type)[0]
4246

4347
value: Any
4448
if field_type is Meta:
@@ -50,6 +54,9 @@ def fill_with_random_values(obj: Resource) -> Resource:
5054
elif field_type is bool:
5155
value = random.choice([True, False])
5256

57+
elif field_type is bytes:
58+
value = base64.b64encode(str(uuid.uuid4()).encode("utf-8"))
59+
5360
elif get_origin(field_type) is Reference:
5461
if get_args(field_type)[0] not in (
5562
ExternalReference,
@@ -64,10 +71,6 @@ def fill_with_random_values(obj: Resource) -> Resource:
6471

6572
value = f"https://{str(uuid.uuid4())}.test"
6673

67-
elif field_type is EmailStr:
68-
# pydantic won't allow to use the 'test' TLD here
69-
value = f"{uuid.uuid4()}@{uuid.uuid4()}.com"
70-
7174
elif isclass(field_type) and issubclass(field_type, Enum):
7275
value = random.choice(list(field_type))
7376

@@ -78,7 +81,8 @@ def fill_with_random_values(obj: Resource) -> Resource:
7881
value = fill_with_random_values(field_type())
7982

8083
else:
81-
value = str(uuid.uuid4())
84+
# Put emails so this will be accepted by EmailStr too
85+
value = f"{uuid.uuid4()}@{uuid.uuid4()}.com"
8286

8387
if is_multiple:
8488
setattr(obj, field_name, [value])

uv.lock

Lines changed: 48 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)