-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_list_response.py
More file actions
242 lines (201 loc) · 7.82 KB
/
test_list_response.py
File metadata and controls
242 lines (201 loc) · 7.82 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from typing import Annotated
from typing import Union
import pytest
from pydantic import ValidationError
from scim2_models import Context
from scim2_models import EnterpriseUser
from scim2_models import Group
from scim2_models import ListResponse
from scim2_models import Required
from scim2_models import Resource
from scim2_models import ResourceType
from scim2_models import ServiceProviderConfig
from scim2_models import User
def test_user(load_sample):
resource_payload = load_sample("rfc7643-8.1-user-minimal.json")
payload = {
"totalResults": 1,
"itemsPerPage": 10,
"startIndex": 1,
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"Resources": [resource_payload],
}
response = ListResponse[User].model_validate(payload)
obj = response.resources[0]
assert isinstance(obj, User)
def test_enterprise_user(load_sample):
resource_payload = load_sample("rfc7643-8.3-enterprise_user.json")
payload = {
"totalResults": 1,
"itemsPerPage": 10,
"startIndex": 1,
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"Resources": [resource_payload],
}
response = ListResponse[User[EnterpriseUser]].model_validate(payload)
obj = response.resources[0]
assert isinstance(obj, User[EnterpriseUser])
def test_group(load_sample):
resource_payload = load_sample("rfc7643-8.4-group.json")
payload = {
"totalResults": 1,
"itemsPerPage": 10,
"startIndex": 1,
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"Resources": [resource_payload],
}
response = ListResponse[Group].model_validate(payload)
obj = response.resources[0]
assert isinstance(obj, Group)
def test_service_provider_configuration(load_sample):
resource_payload = load_sample("rfc7643-8.5-service_provider_configuration.json")
payload = {
"totalResults": 1,
"itemsPerPage": 10,
"startIndex": 1,
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"Resources": [resource_payload],
}
response = ListResponse[ServiceProviderConfig].model_validate(payload)
obj = response.resources[0]
assert isinstance(obj, ServiceProviderConfig)
def test_resource_type(load_sample):
"""Test returning a list of resource types.
https://datatracker.ietf.org/doc/html/rfc7644#section-4
"""
user_resource_type_payload = load_sample("rfc7643-8.6-resource_type-user.json")
group_resource_type_payload = load_sample("rfc7643-8.6-resource_type-group.json")
payload = {
"totalResults": 2,
"itemsPerPage": 10,
"startIndex": 1,
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"Resources": [user_resource_type_payload, group_resource_type_payload],
}
response = ListResponse[ResourceType].model_validate(payload)
obj = response.resources[0]
assert isinstance(obj, ResourceType)
def test_mixed_types(load_sample):
"""Check that given the good type, a ListResponse can handle several resource types."""
user_payload = load_sample("rfc7643-8.1-user-minimal.json")
group_payload = load_sample("rfc7643-8.4-group.json")
payload = {
"totalResults": 2,
"itemsPerPage": 10,
"startIndex": 1,
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"Resources": [user_payload, group_payload],
}
response = ListResponse[Union[User, Group]].model_validate(payload)
user, group = response.resources
assert isinstance(user, User)
assert isinstance(group, Group)
assert response.model_dump() == payload
class Foobar(Resource):
schemas: Annotated[list[str], Required.true] = ["foobarschema"]
def test_mixed_types_type_missing(load_sample):
"""Check that ValidationError are raised when unknown schemas are met."""
user_payload = load_sample("rfc7643-8.1-user-minimal.json")
group_payload = load_sample("rfc7643-8.4-group.json")
payload = {
"totalResults": 2,
"itemsPerPage": 10,
"startIndex": 1,
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"Resources": [user_payload, group_payload],
}
ListResponse[Union[User, Group]].model_validate(payload)
with pytest.raises(ValidationError):
ListResponse[Union[User, Foobar]].model_validate(payload)
with pytest.raises(ValidationError):
ListResponse[User].model_validate(payload)
def test_missing_resource_payload(load_sample):
"""Check that validation fails if resources schemas are missing."""
payload = {
"totalResults": 2,
"itemsPerPage": 10,
"startIndex": 1,
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"Resources": [{}],
}
with pytest.raises(ValidationError):
ListResponse[Union[User, Group]].model_validate(payload, strict=True)
# TODO: This should raise a ValidationError
ListResponse[User].model_validate(payload, strict=True)
def test_missing_resource_schema(load_sample):
"""Check that validation fails if resources schemas are missing."""
payload = {
"totalResults": 2,
"itemsPerPage": 10,
"startIndex": 1,
"schemas": ["urn:ietf:params:scim:api:messages:2.0:ListResponse"],
"Resources": [{"id": "foobar"}],
}
with pytest.raises(ValidationError):
ListResponse[Union[User, Group]].model_validate(payload, strict=True)
# TODO: This should raise a ValidationError
ListResponse[User].model_validate(payload, strict=True)
def test_zero_results():
""":rfc:`RFC7644 §3.4.2 <7644#section-3.4.2>` indicates that ListResponse.Resources is required when ListResponse.totalResults is non- zero.
This MAY be a subset of the full set of resources if pagination
(Section 3.4.2.4) is requested. REQUIRED if "totalResults" is non-
zero.
"""
payload = {
"totalResults": 1,
"Resources": [
{
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
"userName": "foobar",
"id": "foobar",
}
],
}
ListResponse[User].model_validate(payload, scim_ctx=Context.RESOURCE_QUERY_RESPONSE)
payload = {"totalResults": 1, "Resources": []}
with pytest.raises(ValidationError):
ListResponse[User].model_validate(
payload, scim_ctx=Context.RESOURCE_QUERY_RESPONSE
)
payload = {"totalResults": 1}
with pytest.raises(ValidationError):
ListResponse[User].model_validate(
payload, scim_ctx=Context.RESOURCE_QUERY_RESPONSE
)
def test_list_response_schema_ordering():
"""Test that the "schemas" attribute order does not impact behavior.
https://datatracker.ietf.org/doc/html/rfc7643#section-3
"""
payload = {
"totalResults": 1,
"Resources": [
{
"schemas": [
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User",
"urn:ietf:params:scim:schemas:core:2.0:User",
],
"userName": "bjensen@example.com",
}
],
}
ListResponse[Union[User[EnterpriseUser], Group]].model_validate(payload)
def test_total_results_required():
"""ListResponse.total_results is required."""
payload = {
"Resources": [
{
"schemas": [
"urn:ietf:params:scim:schemas:core:2.0:User",
],
"userName": "bjensen@example.com",
"id": "foobar",
}
],
}
with pytest.raises(
ValidationError,
match="Field 'total_results' is required but value is missing or null",
):
ListResponse[User].model_validate(
payload, scim_ctx=Context.RESOURCE_QUERY_RESPONSE
)