|
6 | 6 | django = pytest.importorskip("django") |
7 | 7 | fastapi = pytest.importorskip("fastapi") |
8 | 8 |
|
| 9 | +from doc.guides._examples.integrations import sort_resources # noqa: E402 |
| 10 | +from doc.guides._examples.integrations import sort_value # noqa: E402 |
| 11 | +from scim2_models import EnterpriseUser # noqa: E402 |
| 12 | +from scim2_models import InvalidPathException # noqa: E402 |
| 13 | +from scim2_models import SearchRequest # noqa: E402 |
| 14 | +from scim2_models import User # noqa: E402 |
| 15 | + |
9 | 16 |
|
10 | 17 | def create_flask_app(): |
11 | 18 | from flask import Flask |
@@ -176,6 +183,120 @@ def sorted_names(query): |
176 | 183 | "Zoe@example.com", |
177 | 184 | ] |
178 | 185 |
|
| 186 | + # A sub-attribute of a multi-valued attribute is read from the entry the |
| 187 | + # order picks, and only one user carries an email. |
| 188 | + assert sorted_names("sortBy=emails.value")[0] == "bjensen@example.com" |
| 189 | + |
| 190 | + |
| 191 | +def sorting_users(emails_by_id): |
| 192 | + """Build users carrying the emails each id maps to.""" |
| 193 | + return [ |
| 194 | + User[EnterpriseUser](id=user_id, user_name=user_id, emails=emails) |
| 195 | + for user_id, emails in emails_by_id.items() |
| 196 | + ] |
| 197 | + |
| 198 | + |
| 199 | +def sorting_order(resources, attribute, sort_order=None): |
| 200 | + """Return the ids a ``sortBy`` puts the resources in.""" |
| 201 | + request = SearchRequest[User[EnterpriseUser]]( |
| 202 | + sort_by=attribute, sort_order=sort_order |
| 203 | + ) |
| 204 | + return [ |
| 205 | + resource.id |
| 206 | + for resource in sort_resources(resources, request.sort_by, sort_order) |
| 207 | + ] |
| 208 | + |
| 209 | + |
| 210 | +def sorting_key(resource, attribute): |
| 211 | + """Return the single value a ``sortBy`` orders a resource by.""" |
| 212 | + request = SearchRequest[User[EnterpriseUser]](sort_by=attribute) |
| 213 | + return sort_value(resource, request.sort_by) |
| 214 | + |
| 215 | + |
| 216 | +@pytest.mark.parametrize("attribute", ["emails", "emails.value"]) |
| 217 | +def test_sorting_reads_the_primary_entry_of_a_multivalued_attribute(attribute): |
| 218 | + """The entry marked ``primary`` decides the order, not the first one. |
| 219 | +
|
| 220 | + Ordering on the first entry instead would put ``1`` ahead of ``2``, since |
| 221 | + ``a@example.com`` precedes ``m@example.com``. |
| 222 | + """ |
| 223 | + resources = sorting_users( |
| 224 | + { |
| 225 | + "1": [ |
| 226 | + User.Emails(value="a@example.com"), |
| 227 | + User.Emails(value="z@example.com", primary=True), |
| 228 | + ], |
| 229 | + "2": [User.Emails(value="m@example.com")], |
| 230 | + } |
| 231 | + ) |
| 232 | + assert sorting_order(resources, attribute) == ["2", "1"] |
| 233 | + |
| 234 | + |
| 235 | +def test_sorting_reads_a_sub_attribute_from_the_primary_entry(): |
| 236 | + """A path naming a sub-attribute reads it from the entry the order picked. |
| 237 | +
|
| 238 | + Reading the first entry instead would put ``2`` ahead of ``1``, ``other`` |
| 239 | + preceding ``work``. |
| 240 | + """ |
| 241 | + resources = sorting_users( |
| 242 | + { |
| 243 | + "1": [ |
| 244 | + User.Emails(value="a@example.com", type="work"), |
| 245 | + User.Emails(value="z@example.com", type="home", primary=True), |
| 246 | + ], |
| 247 | + "2": [User.Emails(value="m@example.com", type="other")], |
| 248 | + } |
| 249 | + ) |
| 250 | + assert sorting_order(resources, "emails.type") == ["1", "2"] |
| 251 | + |
| 252 | + |
| 253 | +def test_sorting_falls_back_to_the_first_entry_without_a_primary(): |
| 254 | + """An attribute marking no entry primary is ordered by its first one.""" |
| 255 | + resources = sorting_users( |
| 256 | + { |
| 257 | + "1": [ |
| 258 | + User.Emails(value="z@example.com"), |
| 259 | + User.Emails(value="a@example.com"), |
| 260 | + ], |
| 261 | + "2": [User.Emails(value="m@example.com")], |
| 262 | + } |
| 263 | + ) |
| 264 | + assert sorting_order(resources, "emails.value") == ["2", "1"] |
| 265 | + |
| 266 | + |
| 267 | +def test_sorting_an_unassigned_multivalued_attribute(): |
| 268 | + """A resource carrying no entry comes last ascending and first descending.""" |
| 269 | + resources = sorting_users({"1": None, "2": [User.Emails(value="m@example.com")]}) |
| 270 | + assert sorting_order(resources, "emails.value") == ["2", "1"] |
| 271 | + assert sorting_order(resources, "emails.value", "descending") == ["1", "2"] |
| 272 | + |
| 273 | + |
| 274 | +def test_sorting_a_scalar_multivalued_attribute_reads_the_entry_itself(): |
| 275 | + """A scalar entry is the value, where a complex one holds it in a sub-attribute.""" |
| 276 | + resource = sorting_users({"1": [User.Emails(value="m@example.com")]})[0] |
| 277 | + assert sorting_key(resource, "schemas") == User.__schema__ |
| 278 | + |
| 279 | + |
| 280 | +def test_sorting_an_attribute_of_an_extension_left_unset(): |
| 281 | + """An extension that is not set holds no value to order by.""" |
| 282 | + resource = sorting_users({"1": None})[0] |
| 283 | + urn = "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:department" |
| 284 | + assert sorting_key(resource, urn) is None |
| 285 | + |
| 286 | + resource[EnterpriseUser] = EnterpriseUser(department="Tour Operations") |
| 287 | + assert sorting_key(resource, urn) == "Tour Operations" |
| 288 | + |
| 289 | + |
| 290 | +def test_sorting_a_request_that_named_no_resource_type(): |
| 291 | + """The helper orders by a resolved attribute, which an unparameterised request has none of. |
| 292 | +
|
| 293 | + A request naming the type it serves cannot reach here: an attribute the |
| 294 | + model does not declare is refused when the request is built. |
| 295 | + """ |
| 296 | + resources = sorting_users({"1": None}) |
| 297 | + with pytest.raises(InvalidPathException): |
| 298 | + sort_resources(resources, SearchRequest(sort_by="userName").sort_by) |
| 299 | + |
179 | 300 |
|
180 | 301 | def test_django_example_smoke(): |
181 | 302 | from django.conf import settings |
|
0 commit comments