Skip to content

Commit fe4fa20

Browse files
committed
test: add unit tests for handling wildcard namespaces in ScopeMeta
1 parent f788785 commit fe4fa20

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

openedx_authz/tests/api/test_data.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,58 @@ def test_get_subclass_by_namespaced_key_unknown_scope_raises(self, namespaced_ke
401401
with self.assertRaisesRegex(ValueError, "Unknown scope"):
402402
ScopeMeta.get_subclass_by_namespaced_key(namespaced_key)
403403

404+
@data(
405+
"lib^*",
406+
"course-v1^*",
407+
"ccx-v1^*",
408+
)
409+
def test_get_subclass_by_namespaced_key_registered_namespace_wildcard_returns_scope_data(
410+
self,
411+
namespaced_key: str,
412+
):
413+
"""Test that a bare namespace wildcard for a registered namespace returns ScopeData.
414+
415+
A key shaped ``namespace^*`` carries a meaningful namespace prefix but no
416+
concrete object. When the namespace is registered in ``scope_registry``
417+
(e.g. 'lib', 'course-v1', 'ccx-v1') the method returns the base ScopeData
418+
class instead of raising.
419+
420+
Expected Result:
421+
- ScopeData is returned
422+
"""
423+
self.assertIs(ScopeMeta.get_subclass_by_namespaced_key(namespaced_key), ScopeData)
424+
425+
def test_get_subclass_by_namespaced_key_unregistered_namespace_wildcard_raises(self):
426+
"""Test that a bare namespace wildcard for an unregistered namespace raises ValueError.
427+
428+
``xyz^*`` has external_key '*' (the bare wildcard) but the 'xyz' namespace is
429+
not registered in ``scope_registry``, so it cannot be resolved to ScopeData.
430+
431+
Expected Result:
432+
- ValueError with "Unknown scope" is raised
433+
"""
434+
with self.assertRaisesRegex(ValueError, "Unknown scope"):
435+
ScopeMeta.get_subclass_by_namespaced_key("xyz^*")
436+
437+
@data(
438+
# Wildcard in the middle, not a trailing org/platform glob
439+
"lib^lib:*:CSPROB",
440+
# Wildcard before the trailing run segment for a course key
441+
"course-v1^course-v1:OpenedX+CS*+2025",
442+
)
443+
def test_get_subclass_by_namespaced_key_invalid_glob_pattern_raises(self, namespaced_key: str):
444+
"""Test that a wildcard key that is neither a platform nor org glob raises ValueError.
445+
446+
These keys contain a wildcard (so they are treated as globs) but do not match
447+
the platform glob shape (``namespace:*``), the org glob shape
448+
(``namespace:ORG<sep>*``), nor the bare namespace wildcard (``namespace^*``).
449+
450+
Expected Result:
451+
- ValueError with "Unknown glob pattern" is raised
452+
"""
453+
with self.assertRaisesRegex(ValueError, "Unknown glob pattern"):
454+
ScopeMeta.get_subclass_by_namespaced_key(namespaced_key)
455+
404456
@data(
405457
("ccx-v1:OpenedX+DemoX+DemoCourse+ccx@1", CCXCourseOverviewData),
406458
("course-v1:WGU+CS002+2025_T1", CourseOverviewData),

0 commit comments

Comments
 (0)