diff options
| author | Dennis Fink | 2026-11-17 17:09:59 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-17 17:09:59 +0100 |
| commit | 6d6d079a3a99d3d3a922a640b6925eb2adf044de (patch) | |
| tree | 33cc19241f4ce86ea43b2f194087c58e3fdc8a4d /tests/test_moderation_policy.py | |
| parent | 227092b57acc886469e16bcc4c0d3256e86d3d1e (diff) | |
| download | webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.tar.gz webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.zip | |
refactor(core)!: split forms and models by feature
Replace monolithic form and model modules with focused packages for
receiver, moderation, source, user, and shared validation concerns.
Rename source policy concepts to domain moderation, including routes,
models, tables, configuration, templates, and tests.
BREAKING CHANGE: form and model import paths, moderation routes, and
`SOURCE_ALLOWLIST_ENABLED` have changed; use `ALLOWLIST_ENABLED` and the
new feature-specific modules.
Diffstat (limited to 'tests/test_moderation_policy.py')
| -rw-r--r-- | tests/test_moderation_policy.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/test_moderation_policy.py b/tests/test_moderation_policy.py new file mode 100644 index 0000000..48a34ac --- /dev/null +++ b/tests/test_moderation_policy.py @@ -0,0 +1,62 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Domain normalization and receiver admission policy.""" + +from flask import Flask + +from webmentions_ssg import db +from webmentions_ssg.models.moderation import AllowedDomain, BlockedDomain +from webmentions_ssg.moderation_policy import domain_from_url, rejection_reason + + +def test_domain_from_url() -> None: + assert ( + domain_from_url("https://BÜCHER.example.:8443/post") == "xn--bcher-kva.example" + ) + assert domain_from_url("https://[2001:db8::1]/post") == "2001:db8::1" + assert domain_from_url("invalid") is None + + +def test_blocked_domain_matches_itself_and_subdomains_but_not_suffixes( + app: Flask, +) -> None: + with app.app_context(): + db.session.add(BlockedDomain(domain="example.org")) + db.session.commit() + for source in ( + "https://example.org/post", + "https://blog.example.org/post", + "https://A.blog.Example.ORG./post", + ): + assert rejection_reason(source) == "Source domain is blocked" + for source in ( + "https://notexample.org/post", + "https://example.org.evil/post", + "https://other.net/post", + ): + assert rejection_reason(source) is None + + +def test_allowlist_is_fail_closed_and_blocklist_takes_precedence(app: Flask) -> None: + app.config["ALLOWLIST_ENABLED"] = True + with app.app_context(): + assert ( + rejection_reason("https://example.org/post") + == "Source domain is not allowlisted" + ) + db.session.add(AllowedDomain(domain="example.org")) + db.session.commit() + assert rejection_reason("https://blog.example.org/post") is None + assert ( + rejection_reason("https://notexample.org/post") + == "Source domain is not allowlisted" + ) + db.session.add(BlockedDomain(domain="blog.example.org")) + db.session.commit() + assert ( + rejection_reason("https://blog.example.org/post") + == "Source domain is blocked" + ) + assert rejection_reason("https://example.org/post") is None |
