|
11 | 11 | from datetime import timezone # noqa: E402 |
12 | 12 |
|
13 | 13 | from doc.guides._examples.integrations import sort_resources # noqa: E402 |
| 14 | +from doc.guides._examples.integrations import sort_value # noqa: E402 |
14 | 15 | from doc.guides._examples.sqlalchemy_example import EmailRecord # noqa: E402 |
15 | 16 | from doc.guides._examples.sqlalchemy_example import GroupRecord # noqa: E402 |
16 | 17 | from doc.guides._examples.sqlalchemy_example import UserRecord # noqa: E402 |
17 | 18 | from doc.guides._examples.sqlalchemy_example import create_session_factory # noqa: E402 |
18 | 19 | from doc.guides._examples.sqlalchemy_example import query_users # noqa: E402 |
19 | 20 | from doc.guides._examples.sqlalchemy_example import to_scim_user # noqa: E402 |
| 21 | +from scim2_models import EnterpriseUser # noqa: E402 |
20 | 22 | from scim2_models import InvalidFilterException # noqa: E402 |
21 | 23 | from scim2_models import InvalidPathException # noqa: E402 |
22 | 24 | from scim2_models import ScimFilter # noqa: E402 |
@@ -218,6 +220,116 @@ def sorted_names(query): |
218 | 220 | ] |
219 | 221 |
|
220 | 222 |
|
| 223 | +def sorting_users(emails_by_id): |
| 224 | + """Build users carrying the emails each id maps to.""" |
| 225 | + return [ |
| 226 | + User[EnterpriseUser](id=user_id, user_name=user_id, emails=emails) |
| 227 | + for user_id, emails in emails_by_id.items() |
| 228 | + ] |
| 229 | + |
| 230 | + |
| 231 | +def sorting_order(resources, attribute, sort_order=None): |
| 232 | + """Return the ids a ``sortBy`` puts the resources in.""" |
| 233 | + request = SearchRequest[User[EnterpriseUser]]( |
| 234 | + sort_by=attribute, sort_order=sort_order |
| 235 | + ) |
| 236 | + return [ |
| 237 | + resource.id |
| 238 | + for resource in sort_resources(resources, request.sort_by, sort_order) |
| 239 | + ] |
| 240 | + |
| 241 | + |
| 242 | +def sorting_key(resource, attribute): |
| 243 | + """Return the single value a ``sortBy`` orders a resource by.""" |
| 244 | + request = SearchRequest[User[EnterpriseUser]](sort_by=attribute) |
| 245 | + return sort_value(resource, request.sort_by.resolve()) |
| 246 | + |
| 247 | + |
| 248 | +@pytest.mark.parametrize("attribute", ["emails", "emails.value"]) |
| 249 | +def test_sorting_reads_the_primary_entry_of_a_multivalued_attribute(attribute): |
| 250 | + """The entry marked ``primary`` decides the order, not the first one. |
| 251 | +
|
| 252 | + Ordering on the first entry instead would put ``1`` ahead of ``2``, since |
| 253 | + ``a@example.com`` precedes ``m@example.com``. |
| 254 | + """ |
| 255 | + resources = sorting_users( |
| 256 | + { |
| 257 | + "1": [ |
| 258 | + User.Emails(value="a@example.com"), |
| 259 | + User.Emails(value="z@example.com", primary=True), |
| 260 | + ], |
| 261 | + "2": [User.Emails(value="m@example.com")], |
| 262 | + } |
| 263 | + ) |
| 264 | + assert sorting_order(resources, attribute) == ["2", "1"] |
| 265 | + |
| 266 | + |
| 267 | +def test_sorting_reads_a_sub_attribute_from_the_primary_entry(): |
| 268 | + """A path naming a sub-attribute reads it from the entry the order picked. |
| 269 | +
|
| 270 | + Reading the first entry instead would put ``2`` ahead of ``1``, ``other`` |
| 271 | + preceding ``work``. |
| 272 | + """ |
| 273 | + resources = sorting_users( |
| 274 | + { |
| 275 | + "1": [ |
| 276 | + User.Emails(value="a@example.com", type="work"), |
| 277 | + User.Emails(value="z@example.com", type="home", primary=True), |
| 278 | + ], |
| 279 | + "2": [User.Emails(value="m@example.com", type="other")], |
| 280 | + } |
| 281 | + ) |
| 282 | + assert sorting_order(resources, "emails.type") == ["1", "2"] |
| 283 | + |
| 284 | + |
| 285 | +def test_sorting_falls_back_to_the_first_entry_without_a_primary(): |
| 286 | + """An attribute marking no entry primary is ordered by its first one.""" |
| 287 | + resources = sorting_users( |
| 288 | + { |
| 289 | + "1": [ |
| 290 | + User.Emails(value="z@example.com"), |
| 291 | + User.Emails(value="a@example.com"), |
| 292 | + ], |
| 293 | + "2": [User.Emails(value="m@example.com")], |
| 294 | + } |
| 295 | + ) |
| 296 | + assert sorting_order(resources, "emails.value") == ["2", "1"] |
| 297 | + |
| 298 | + |
| 299 | +def test_sorting_an_unassigned_multivalued_attribute(): |
| 300 | + """A resource carrying no entry comes last ascending and first descending.""" |
| 301 | + resources = sorting_users({"1": None, "2": [User.Emails(value="m@example.com")]}) |
| 302 | + assert sorting_order(resources, "emails.value") == ["2", "1"] |
| 303 | + assert sorting_order(resources, "emails.value", "descending") == ["1", "2"] |
| 304 | + |
| 305 | + |
| 306 | +def test_sorting_a_scalar_multivalued_attribute_reads_the_entry_itself(): |
| 307 | + """A scalar entry is the value, where a complex one holds it in a sub-attribute.""" |
| 308 | + resource = sorting_users({"1": [User.Emails(value="m@example.com")]})[0] |
| 309 | + assert sorting_key(resource, "schemas") == User.__schema__ |
| 310 | + |
| 311 | + |
| 312 | +def test_sorting_an_attribute_of_an_extension_left_unset(): |
| 313 | + """An extension that is not set holds no value to order by.""" |
| 314 | + resource = sorting_users({"1": None})[0] |
| 315 | + urn = "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:department" |
| 316 | + assert sorting_key(resource, urn) is None |
| 317 | + |
| 318 | + resource[EnterpriseUser] = EnterpriseUser(department="Tour Operations") |
| 319 | + assert sorting_key(resource, urn) == "Tour Operations" |
| 320 | + |
| 321 | + |
| 322 | +def test_sorting_a_request_that_named_no_resource_type(): |
| 323 | + """The helper orders by a resolved attribute, which an unparameterised request has none of. |
| 324 | +
|
| 325 | + A request naming the type it serves cannot reach here: an attribute the |
| 326 | + model does not declare is refused when the request is built. |
| 327 | + """ |
| 328 | + resources = sorting_users({"1": None}) |
| 329 | + with pytest.raises(InvalidPathException): |
| 330 | + sort_resources(resources, SearchRequest(sort_by="userName").sort_by) |
| 331 | + |
| 332 | + |
221 | 333 | def test_django_example_smoke(): |
222 | 334 | from django.conf import settings |
223 | 335 |
|
|
0 commit comments