diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/test_forms.py | 56 |
1 files changed, 29 insertions, 27 deletions
diff --git a/tests/test_forms.py b/tests/test_forms.py index e636eae..b5b3296 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -8,12 +8,12 @@ from werkzeug.datastructures import MultiDict from wtforms import Form, StringField, ValidationError from webmentions_ssg.forms import ( - AdminActionForm, - AllowedHostname, - EndpointForm, + AllowedTargetHostname, + ConfirmActionForm, LoginForm, + MaxURLBytes, PublicIPLiteral, - URLMaxBytes, + WebmentionSubmissionForm, ) VALID_SOURCE = "https://source.example/post" @@ -81,7 +81,9 @@ def test_endpoint_form_rejects_invalid_field_syntax( app: Flask, form_data: dict[str, str], invalid_field: str, expected_error: str ) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm(formdata=MultiDict(form_data), meta={"csrf": False}) + form = WebmentionSubmissionForm( + formdata=MultiDict(form_data), meta={"csrf": False} + ) assert not form.validate() assert expected_error in form.errors[invalid_field] @@ -92,7 +94,7 @@ def test_endpoint_form_accepts_http_schemes_case_insensitively( app: Flask, scheme: str ) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict( { "source": f"{scheme}://source.example/post", @@ -106,7 +108,7 @@ def test_endpoint_form_accepts_http_schemes_case_insensitively( def test_endpoint_form_accepts_valid_data(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -143,7 +145,7 @@ def test_login_form_accepts_credentials(app: Flask) -> None: def test_admin_action_form_accepts_submission(app: Flask) -> None: with app.test_request_context(method="POST"): - form = AdminActionForm(meta={"csrf": False}) + form = ConfirmActionForm(meta={"csrf": False}) assert form.validate() @@ -158,7 +160,7 @@ def test_endpoint_does_not_do_dns_in_web_request( ) with app.test_request_context("/endpoint", method="POST"): for source in (VALID_SOURCE, "ftp://source.example/post", "not a URL"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": source, "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -170,7 +172,7 @@ def test_endpoint_does_not_do_dns_in_web_request( ) def test_endpoint_rejects_private_ip_literals(app: Flask, source: str) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": source, "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -180,7 +182,7 @@ def test_endpoint_rejects_private_ip_literals(app: Flask, source: str) -> None: def test_endpoint_rejects_private_ip_literal_with_trailing_dot(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict( {"source": "http://127.0.0.1./", "target": VALID_TARGET} ), @@ -192,7 +194,7 @@ def test_endpoint_rejects_private_ip_literal_with_trailing_dot(app: Flask) -> No def test_endpoint_handles_malformed_ipv6_without_crashing(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_SOURCE, "target": "http://[::1"}), meta={"csrf": False}, ) @@ -205,7 +207,7 @@ def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields( ) -> None: with app.test_request_context("/endpoint", method="POST"): app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_SOURCE.encode("utf-8")) - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_SOURCE + "x", "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -214,7 +216,7 @@ def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields( assert any("bytes" in message for message in form.errors["source"]) app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_TARGET.encode("utf-8")) - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET + "x"}), meta={"csrf": False}, ) @@ -243,14 +245,14 @@ def test_endpoint_rejects_invalid_httpx_urls_and_whitespace( data = {"source": VALID_SOURCE, "target": VALID_TARGET} data[field_name] = invalid_url with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm(formdata=MultiDict(data), meta={"csrf": False}) + form = WebmentionSubmissionForm(formdata=MultiDict(data), meta={"csrf": False}) assert not form.validate() assert "Invalid URL." in form.errors[field_name] def test_endpoint_accepts_encoded_path_space(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict( {"source": "https://source.example/a%20b", "target": VALID_TARGET} ), @@ -261,7 +263,7 @@ def test_endpoint_accepts_encoded_path_space(app: Flask) -> None: def test_endpoint_form_rejects_matching_source_and_target(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): - form = EndpointForm( + form = WebmentionSubmissionForm( formdata=MultiDict({"source": VALID_TARGET, "target": VALID_TARGET}), meta={"csrf": False}, ) @@ -271,27 +273,27 @@ def test_endpoint_form_rejects_matching_source_and_target(app: Flask) -> None: def test_allowed_hostname_accepts_configured_hostname(app: Flask) -> None: - app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"] + app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] form = URLForm(MultiDict({"url": "https://dennisfink.me/blog/example/"})) with app.app_context(): - AllowedHostname()(form, form.url) + AllowedTargetHostname()(form, form.url) def test_allowed_hostname_rejects_unconfigured_hostname(app: Flask) -> None: - app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"] + app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] form = URLForm(MultiDict({"url": "https://example.com/post"})) with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): - AllowedHostname()(form, form.url) + AllowedTargetHostname()(form, form.url) def test_allowed_hostname_uses_custom_message(app: Flask) -> None: - app.config["ALLOWED_HOSTNAMES"] = ["dennisfink.me"] + app.config["ALLOWED_TARGET_HOSTNAMES"] = ["dennisfink.me"] form = URLForm(MultiDict({"url": "https://example.com/post"})) with ( app.app_context(), pytest.raises(ValidationError, match="Hostname is not allowed"), ): - AllowedHostname("Hostname is not allowed")(form, form.url) + AllowedTargetHostname("Hostname is not allowed")(form, form.url) @pytest.mark.parametrize( @@ -312,11 +314,11 @@ def test_public_ip_literal_accepts_public_ipv6_address() -> None: def test_allowed_hostname_handles_invalid_ipv6(app: Flask) -> None: form = URLForm(MultiDict({"url": "http://[::1"})) with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): - AllowedHostname()(form, form.url) + AllowedTargetHostname()(form, form.url) def test_url_max_bytes_respects_utf8_byte_length_and_current_config(app: Flask) -> None: - validator = URLMaxBytes() + validator = MaxURLBytes() form = URLForm(MultiDict({"url": "é" * 3})) with app.app_context(): app.config["MAX_WEBMENTION_URL_BYTES"] = 6 @@ -331,10 +333,10 @@ def test_url_max_bytes_custom_message(app: Flask) -> None: with app.app_context(): app.config["MAX_WEBMENTION_URL_BYTES"] = 2 with pytest.raises(ValidationError, match="URL too long"): - URLMaxBytes(message="URL too long")(form, form.url) + MaxURLBytes(message="URL too long")(form, form.url) def test_url_max_bytes_rejects_malformed_unicode(app: Flask) -> None: form = URLForm(MultiDict({"url": "\ud800"})) with app.app_context(), pytest.raises(ValidationError, match="valid UTF-8"): - URLMaxBytes()(form, form.url) + MaxURLBytes()(form, form.url) |
