Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate "path matches" conditions as regular expressions #78

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/releasenotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Release Notes

## Unreleased

### What's new?

- Fixed the "path matches" condition validator to allow any valid regular expression.


## 5.0.3

### What's new?
Expand Down
2 changes: 1 addition & 1 deletion flags/conditions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
validate_boolean,
validate_date,
validate_parameter,
validate_path,
validate_path_re,
validate_user,
)
4 changes: 2 additions & 2 deletions flags/conditions/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
validate_boolean,
validate_date,
validate_parameter,
validate_path,
validate_path_re,
validate_user,
)

Expand Down Expand Up @@ -67,7 +67,7 @@ def parameter_condition(param_name, request=None, **kwargs):
return request.GET.get(param_name) == param_value


@register("path matches", validator=validate_path)
@register("path matches", validator=validate_path_re)
def path_condition(pattern, request=None, **kwargs):
""" Does the request's path match the given regular expression? """
if request is None:
Expand Down
19 changes: 10 additions & 9 deletions flags/conditions/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@
from django.utils import dateparse


validate_path = RegexValidator(
re.compile(r"^[^\s:?#]+$", re.UNICODE),
message=(
"Enter a valid path without a URL scheme, query string, or fragment."
),
code="invalid",
)


validate_parameter = RegexValidator(
re.compile(r"^[-_\w=]+$", re.UNICODE),
message="Enter a valid HTTP parameter name.",
code="invalid",
)


def validate_path_re(value):
try:
re.compile(value)
except re.error as e:
raise ValidationError(
"Enter either a valid path or a regular expression to match a "
"path, without a URL scheme, query string, or fragment."
) from e


def validate_boolean(value):
message = "Enter one of 'on', 'off', 'true', 'false', etc."
try:
Expand Down
19 changes: 8 additions & 11 deletions flags/tests/test_conditions_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
validate_boolean,
validate_date,
validate_parameter,
validate_path,
validate_path_re,
validate_user,
)

Expand All @@ -26,18 +26,15 @@ def test_valid_parameter_strings(self):


class ValidatePathTestCase(TestCase):
def test_invalid_path_strings(self):
def test_invalid_path_regexs(self):
with self.assertRaises(ValidationError):
validate_path("/my/path#foo")
with self.assertRaises(ValidationError):
validate_path("/my/path?foo=bar")
with self.assertRaises(ValidationError):
validate_path("https://foo/my/path")
validate_path_re("*foo/my/path")

def test_valid_path_strings(self):
validate_path("/my/path")
validate_path("/my/path/")
validate_path("my/path/")
def test_valid_path_regexs(self):
validate_path_re("/my/path")
validate_path_re("/my/path/")
validate_path_re("my/path/")
validate_path_re(r"^/my/(path)?$")


class ValidateBooleanTestCase(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ deps=
dj31: Django>=3.1,<3.2

[testenv:lint]
basepython=python3.6
basepython=python3.8
deps=
black
flake8
Expand Down