-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_dynamic_schemas.py
More file actions
193 lines (150 loc) · 5.95 KB
/
test_dynamic_schemas.py
File metadata and controls
193 lines (150 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import copyreg
import operator
import pytest
from pydantic.fields import FieldInfo
from scim2_models import URN
from scim2_models.context import Context
from scim2_models.resources.enterprise_user import EnterpriseUser
from scim2_models.resources.group import Group
from scim2_models.resources.resource import Resource
from scim2_models.resources.resource_type import ResourceType
from scim2_models.resources.schema import Attribute
from scim2_models.resources.schema import Schema
from scim2_models.resources.schema import _make_python_model
from scim2_models.resources.service_provider_config import ServiceProviderConfig
from scim2_models.resources.user import User
def canonic_schema(schema):
"""Remove descriptions and sort attributes so schemas are easily comparable."""
schema["meta"] = None
schema["name"] = None
schema["schemas"] = None
schema["description"] = None
schema["attributes"].sort(key=operator.itemgetter("name"))
for attr in schema["attributes"]:
attr["description"] = None
if attr.get("subAttributes"):
attr["subAttributes"].sort(key=operator.itemgetter("name"))
for subattr in attr["subAttributes"]:
subattr["description"] = None
if subattr.get("subAttributes"):
subattr["subAttributes"].sort(key=operator.itemgetter("name"))
for subsubattr in subattr["subAttributes"]:
subsubattr["description"] = None
def test_dynamic_group_schema(load_sample):
sample = Schema.model_validate(
load_sample("rfc7643-8.7.1-schema-group.json")
).model_dump()
schema = Group.to_schema().model_dump()
canonic_schema(schema)
canonic_schema(sample)
assert sample == schema
def test_dynamic_user_schema(load_sample):
sample = Schema.model_validate(
load_sample("rfc7643-8.7.1-schema-user.json")
).model_dump()
schema = User.to_schema().model_dump()
canonic_schema(schema)
canonic_schema(sample)
assert sample == schema
def test_dynamic_enterprise_user_schema(load_sample):
sample = Schema.model_validate(
load_sample("rfc7643-8.7.1-schema-enterprise_user.json")
).model_dump()
schema = EnterpriseUser.to_schema().model_dump()
canonic_schema(schema)
canonic_schema(sample)
assert sample == schema
def test_dynamic_resource_type_schema(load_sample):
sample = Schema.model_validate(
load_sample("rfc7643-8.7.2-schema-resource_type.json")
).model_dump()
schema = ResourceType.to_schema().model_dump()
canonic_schema(schema)
canonic_schema(sample)
assert sample == schema
def test_dynamic_service_provider_config_schema(load_sample):
sample = Schema.model_validate(
load_sample("rfc7643-8.7.2-schema-service_provider_configuration.json")
).model_dump()
schema = ServiceProviderConfig.to_schema().model_dump()
canonic_schema(schema)
canonic_schema(sample)
schema["attributes"] = [
attr for attr in schema["attributes"] if attr["name"] != "id"
]
assert sample == schema
def test_dynamic_schema_schema(load_sample):
sample = Schema.model_validate(
load_sample("rfc7643-8.7.2-schema-schema.json")
).model_dump()
schema = Schema.to_schema().model_dump()
canonic_schema(schema)
canonic_schema(sample)
assert sample == schema
def test_dump_with_context():
models = [User, EnterpriseUser, Group, ResourceType, Schema, ServiceProviderConfig]
for model in models:
model.to_schema().model_dump(scim_ctx=Context.RESOURCE_QUERY_RESPONSE)
def test_inheritance():
"""Check that parent attributes are included in the schema."""
class Foo(Resource):
__schema__ = URN("urn:ietf:params:scim:schemas:core:2.0:Foo")
foo: str | None = None
class Bar(Foo):
bar: str | None = None
schema = Bar.to_schema()
assert schema.model_dump() == {
"attributes": [
{
"caseExact": False,
"multiValued": False,
"mutability": "readWrite",
"name": "foo",
"required": False,
"returned": "default",
"type": "string",
"uniqueness": "none",
},
{
"caseExact": False,
"multiValued": False,
"mutability": "readWrite",
"name": "bar",
"required": False,
"returned": "default",
"type": "string",
"uniqueness": "none",
},
],
"description": "Bar",
"id": "urn:ietf:params:scim:schemas:core:2.0:Foo",
"name": "Bar",
"schemas": [
"urn:ietf:params:scim:schemas:core:2.0:Schema",
],
}
def test_to_schema_without_slotnames_cache():
"""Regression test for ``FieldInfo.__slotnames__`` usage.
``FieldInfo.__slotnames__`` is a cache populated lazily by ``copy.copy`` /
``copyreg._slotnames``. Computing a schema must not rely on that cache
being already warm, otherwise ``to_schema`` raises ``AttributeError`` on a
fresh process.
"""
copyreg._slotnames(FieldInfo)
del FieldInfo.__slotnames__
class Foo(Resource):
__schema__ = URN("urn:ietf:params:scim:schemas:core:2.0:Foo")
foo: str | None = None
Foo.to_schema()
def test_make_python_model_validates_name():
"""Test that make_python_model raises an exception when obj.name is not defined."""
# Test with Schema object without name
schema = Schema(id="urn:example:schema")
schema.name = None
with pytest.raises(ValueError, match="Schema or Attribute 'name' must be defined"):
_make_python_model(schema, Resource)
# Test with Attribute object without name
attribute = Attribute(type=Attribute.Type.string)
attribute.name = None
with pytest.raises(ValueError, match="Schema or Attribute 'name' must be defined"):
_make_python_model(attribute, Resource)