Skip to content

Commit 08b9f51

Browse files
committed
chore: test user role permission
1 parent a25a774 commit 08b9f51

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

bc_obps/common/tests/endpoints/auth/test_endpoint_permissions.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,45 @@ def test_all_api_endpoints_are_permission_tested(cls):
616616
f"The following endpoints (COUNT:{len(untested_endpoints)}) are not covered by permission tests:\n"
617617
+ "\n".join(f"- {endpoint}" for endpoint in untested_endpoints)
618618
)
619+
620+
@classmethod
621+
@patch("common.permissions.check_permission_for_role")
622+
def test_role_access_restrictions(cls, mock_check_permission_for_role: MagicMock):
623+
"""This test verifies that roles have no access to endpoints outside of their list"""
624+
# Create a mapping of endpoints to their required roles
625+
endpoint_role_mapping = {}
626+
for role, configs in cls.endpoints_to_test.items():
627+
for config in configs:
628+
key = (config["endpoint_name"], config["method"], tuple(sorted(config.get("kwargs", {}).items())))
629+
endpoint_role_mapping[key] = role
630+
631+
# Test each role's access restrictions
632+
for current_role, configs in cls.endpoints_to_test.items():
633+
# Set of allowed endpoints for this role
634+
allowed_endpoints = {
635+
(config["endpoint_name"], config["method"], tuple(sorted(config.get("kwargs", {}).items())))
636+
for config in configs
637+
}
638+
639+
# Endpoints this role should NOT have access to
640+
all_endpoints = set(endpoint_role_mapping.keys())
641+
forbidden_endpoints = all_endpoints - allowed_endpoints
642+
643+
for endpoint_key in forbidden_endpoints:
644+
endpoint_name, method, kwargs_items = endpoint_key
645+
kwargs = dict(kwargs_items)
646+
endpoint = custom_reverse_lazy(endpoint_name, kwargs=kwargs)
647+
required_role = endpoint_role_mapping[endpoint_key]
648+
649+
# Deny forbidden endpoint
650+
def check_permission_side_effect(request, role):
651+
return False
652+
653+
mock_check_permission_for_role.side_effect = check_permission_side_effect
654+
655+
cls._call_endpoint(method, endpoint)
656+
657+
# Assert that permission check was called with the correct role
658+
mock_check_permission_for_role.assert_called_once_with(ANY, required_role)
659+
# Reset mock
660+
mock_check_permission_for_role.reset_mock()

0 commit comments

Comments
 (0)