From 9141d1cadb78c9c165627634ba0e7f9aa6187792 Mon Sep 17 00:00:00 2001 From: Vincent Vatelot Date: Fri, 8 Dec 2023 23:46:16 +0100 Subject: [PATCH] fix(api): validate that url must be a valid url with http or https (#19) --- components/ecoindex/models/compute.py | 1 + test/components/ecoindex/compute/test_models.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/components/ecoindex/models/compute.py b/components/ecoindex/models/compute.py index 1351e42..6c5c987 100644 --- a/components/ecoindex/models/compute.py +++ b/components/ecoindex/models/compute.py @@ -103,6 +103,7 @@ class WebPage(BaseModel): @classmethod def url_as_http_url(cls, v: str) -> str: url_object = AnyHttpUrl(url=v) + assert url_object.scheme in {"http", "https"}, "scheme must be http or https" return url_object.unicode_string() diff --git a/test/components/ecoindex/compute/test_models.py b/test/components/ecoindex/compute/test_models.py index b5bfa43..9705f3e 100644 --- a/test/components/ecoindex/compute/test_models.py +++ b/test/components/ecoindex/compute/test_models.py @@ -28,6 +28,9 @@ def test_model_webpage_invalid_url() -> None: "[type=url_parsing, input_value='toto', input_type=str]\n" ) in str(error.value) + with raises(ValidationError): + WebPage(url="about:config") + def test_model_webpage_wrong_size() -> None: with raises(ValidationError) as error: