"""Tests for EndpointRulesValidator.""" import pytest from owsrequest import rules TEST_RULES = 'tests/fixtures/rules.yaml' UNSUPPORTED_RULES = 'tests/fixtures/unsupported_rules.yaml' @pytest.mark.parametrize( 'route, result_regex', [ ('/holds/', '^/holds/\\d+$'), ('/holds/active', '^/holds/active$'), ('/holds/<*>', '^/holds/<*>$'), ('<*>', '^<*>$'), ('///average', '^/[\\w_\\-]+/\\d+/average$') ] ) def test_route_to_regex(route, result_regex): """Test converting an endpoint route to regex.""" assert rules.route_to_regex(route) == result_regex @pytest.mark.parametrize( 'path, method, group, role, has_access', [ ('/holds/234', 'PUT', 'LabelProfile', ['catalog'], True), ('/holds/234', 'PUT', 'OrchAdminProfile', ['catalog', 'administrator'], True), ('/holds/234', 'PUT', 'LabelProfile', ['marketing'], False), ('/holds/234', 'GET', 'LabelProfile', ['marketing', 'administrator'], True), ('/holds/active', 'GET', 'LabelProfile', ['marketing'], True), ('/holds/active', 'GET', 'OrchAdminProfile', ['marketing'], False), ('/holds/active', 'GET', 'OrchAdminProfile', ['catalog'], True), ('/holds/active', 'POST', 'LabelProfile', ['marketing'], False), ('/holds/vendor/123', 'GET', 'LabelProfile', ['analytics'], True), ('/holds/vendor/123', 'POST', 'LabelProfile', ['analytics'], False), ('/holds/vendor/active', 'PATCH', 'LabelProfile', ['catalog'], True), ('/holds/vendor/active', 'PATCH', 'LabelProfile', ['analytics'], False), ('/holds/blah', 'GET', 'LabelProfile', ['catalog'], False), ('/holds/blah', 'GET', 'LabelProfile', ['analytics'], False), ('/test/3/average', 'GET', 'LabelProfile', ['catalog'], True), ('/test/what/average', 'GET', 'LabelProfile', ['catalog'], False), ('/holdsregex/vendor/123', 'GET', 'LabelProfile', ['catalog'], True), ('/holdsregex/7777/123', 'GET', 'LabelProfile', ['catalog'], False), ('/report', 'GET', 'LabelProfile', ['administrator', 'accounting', 'masterrights'], True), ('/report', 'GET', 'LabelProfile', ['masterrights'], False), ('/report', 'GET', 'OrchAdminProfile', ['catalog'], False), ('/upload-token?rand=a234fdsdf', 'GET', 'LabelProfile', ['catalog'], False), ('/test_slash/', 'GET', 'LabelProfile', ['catalog'], True), ('/test_slash', 'GET', 'LabelProfile', ['catalog'], True), ('/test_slash/23', 'GET', 'LabelProfile', ['catalog'], True), ('/test_slash/23/', 'GET', 'LabelProfile', ['catalog'], True), ('/reporting/report/product-view-UK', 'GET', 'LabelProfile', ['analytics'], True), ('/reporting/report/product_view_UK', 'GET', 'LabelProfile', ['analytics'], True), ('/test_admin', 'POST', 'OrchAdminProfile', [], True), ('/test_admin', 'POST', 'OrchAdminProfile', ['admin'], True), ] ) def test_rules(path, method, group, role, has_access): """Test rules validator.""" validator = rules.EndpointRulesValidator(TEST_RULES) assert validator.has_access(path, method, group, role) == has_access @pytest.mark.parametrize( 'path, method, group, role', [ ('/holds/blah', 'GET', 'LabelProfile', ['catalog']), ('/holds/blah/nested', 'PUT', 'LabelProfile', ['catalog']), ('/upload-token?rand=a234fdsdf', 'GET', 'LabelProfile', ['catalog']), ('/anything', 'GET', 'LabelProfile', ['catalog']), ('/anything', 'GET', 'OrchAdminProfile', ['catalog']), ] ) def test_unsupported_rules_deny_access(path, method, group, role): """Wildcard <*> rules must not grant access to any path.""" validator = rules.EndpointRulesValidator(UNSUPPORTED_RULES) assert validator.has_access(path, method, group, role) is False