Add test for permission path normalization

This commit is contained in:
Josué Tille 2020-04-07 10:55:04 +02:00
parent 130ffab6ff
commit 7d69079e46
No known key found for this signature in database
GPG key ID: 716A6C99B04194EF
2 changed files with 43 additions and 2 deletions

View file

@ -437,7 +437,7 @@ def _check_and_normalize_permission_path(url):
if '/' not in url:
raise YunohostError('regex_with_only_domain')
domain = url[3:].split('/')[0]
path = url[3:].split('/', 1)[1]
path = '/' + url[3:].split('/', 1)[1]
if domain not in domains:
raise YunohostError('domain_named_unknown', domain=domain)

View file

@ -2,7 +2,7 @@ import pytest
from yunohost.utils.error import YunohostError
from yunohost.app import app_install, app_remove
from yunohost.domain import _get_maindomain, domain_url_available, _normalize_domain_path
from yunohost.domain import _get_maindomain, domain_url_available, _normalize_domain_path, _check_and_normalize_permission_path
# Get main domain
maindomain = _get_maindomain()
@ -59,3 +59,44 @@ def test_registerurl_baddomain():
with pytest.raises(YunohostError):
app_install("./tests/apps/register_url_app_ynh",
args="domain=%s&path=%s" % ("yolo.swag", "/urlregisterapp"), force=True)
def test_normalize_permission_path():
# Relative path
assert _check_and_normalize_permission_path("/wiki/") == "/wiki"
assert _check_and_normalize_permission_path("/") == "/"
assert _check_and_normalize_permission_path("//salut/") == "/salut"
# Full path
assert _check_and_normalize_permission_path(maindomain + "/hey/") == maindomain + "/hey"
assert _check_and_normalize_permission_path(maindomain + "//") == maindomain + "/"
assert _check_and_normalize_permission_path(maindomain + "/") == maindomain + "/"
# Relative Regex
assert _check_and_normalize_permission_path("re:/yolo.*/") == "re:/yolo.*/"
assert _check_and_normalize_permission_path("re:/y.*o(o+)[a-z]*/bo\1y") == "re:/y.*o(o+)[a-z]*/bo\1y"
# Full Regex
assert _check_and_normalize_permission_path("re:" + maindomain + "/yolo.*/") == "re:" + maindomain + "/yolo.*/"
assert _check_and_normalize_permission_path("re:" + maindomain + "/y.*o(o+)[a-z]*/bo\1y") == "re:" + maindomain + "/y.*o(o+)[a-z]*/bo\1y"
def test_normalize_permission_path_with_bad_regex():
# Relative Regex
with pytest.raises(YunohostError):
_check_and_normalize_permission_path("re:/yolo.*[1-7]^?/")
with pytest.raises(YunohostError):
_check_and_normalize_permission_path("re:/yolo.*[1-7](]/")
# Full Regex
with pytest.raises(YunohostError):
_check_and_normalize_permission_path("re:" + maindomain + "/yolo?+/")
with pytest.raises(YunohostError):
_check_and_normalize_permission_path("re:" + maindomain + "/yolo[1-9]**/")
def test_normalize_permission_path_with_unknown_domain():
with pytest.raises(YunohostError):
_check_and_normalize_permission_path("shouldntexist.tld/hey")
with pytest.raises(YunohostError):
_check_and_normalize_permission_path("re:shouldntexist.tld/hey.*")