Skip to content

Commit

Permalink
Fixed test to check options are applied correctly.
Browse files Browse the repository at this point in the history
Previously only really tested underlying `accept_domain_incoming`, which
is tested directly by `test_config`.
  • Loading branch information
beatonma committed Feb 9, 2024
1 parent a9d3bdd commit b14e176
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 34 deletions.
11 changes: 11 additions & 0 deletions mentions/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Helper functions that derive from `mentions.options` values."""
import logging
from typing import Optional, Set
from urllib.parse import urljoin

from mentions import options
from mentions.util import compatibility, get_domain

log = logging.getLogger(__name__)


def base_url() -> str:
"""Get the base URL of this server from current `options`."""
Expand Down Expand Up @@ -45,6 +48,10 @@ def accept_domain_incoming(

domain = get_domain(url)

if not domain:
log.warning(f"accept_domain_incoming received invalid URL: {url}")
return False

if domains_allow:
return _domain_in_set(domain, domains_allow)

Expand Down Expand Up @@ -74,6 +81,10 @@ def accept_domain_outgoing(
"""
domain = get_domain(url)

if not domain:
log.warning(f"accept_domain_outgoing received invalid URL: {url}")
return False

if not allow_self_mention and domain == options.domain_name():
return False
if domains_allow and domains_deny:
Expand Down
7 changes: 2 additions & 5 deletions mentions/tasks/incoming/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ def process_incoming_webmention(
) -> Optional[Webmention]:
log.info(f"Processing webmention '{source_url}' -> '{target_url}'")

domains_allow = domains_allow or options.incoming_domains_allow()
domains_deny = domains_deny or options.incoming_domains_deny()

allow_source_domain = config.accept_domain_incoming(
source_url,
domains_allow=domains_allow,
domains_deny=domains_deny,
domains_allow=domains_allow or options.incoming_domains_allow(),
domains_deny=domains_deny or options.incoming_domains_deny(),
)
if not allow_source_domain:
log.warning(
Expand Down
34 changes: 5 additions & 29 deletions tests/tests/test_tasks/test_incoming_webmention.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"""
import logging

from mentions import config
from django.test import override_settings

from mentions import options
from mentions.exceptions import SourceNotAccessible, TargetWrongDomain
from mentions.models import Webmention
from mentions.models.mixins import IncomingMentionType
Expand Down Expand Up @@ -246,64 +248,38 @@ def test_target_object_with_object_not_required(self):


class AllowDenyOptionTests(SimpleTestCase):
def test_is_domain_acceptable(self):
self.assertTrue(config.accept_domain_incoming("https://allow.org", None, None))
self.assertTrue(
config.accept_domain_incoming("https://allow.org", {"allow.org"}, None)
)
self.assertFalse(
config.accept_domain_incoming("https://deny.org", {"allow.org"}, None)
)

self.assertTrue(
config.accept_domain_incoming("https://allow.org", None, {"deny.org"})
)
self.assertFalse(
config.accept_domain_incoming("https://deny.org", None, {"deny.org"})
)

@override_settings(**{options.SETTING_DOMAINS_INCOMING_ALLOW: {"allow.org"}})
@patch_http_get(text=SOURCE_TEXT_DEFAULT)
def test_process_incoming_webmention_with_domains_allow(self):
self.assertIsNotNone(
incoming.process_incoming_webmention(
testfunc.random_url(subdomain="", domain="allow.org", port=""),
TARGET_URL,
sent_by=testfunc.random_url(),
domains_allow={"allow.org"},
)
)
self.assertIsNone(
incoming.process_incoming_webmention(
testfunc.random_url(subdomain="", domain="other.org", port=""),
TARGET_URL,
sent_by=testfunc.random_url(),
domains_allow={"allow.org"},
)
)

@override_settings(**{options.SETTING_DOMAINS_INCOMING_DENY: {"deny.org"}})
@patch_http_get(text=SOURCE_TEXT_DEFAULT)
def test_process_incoming_webmention_with_domains_deny(self):
self.assertIsNotNone(
incoming.process_incoming_webmention(
testfunc.random_url(subdomain="", domain="allow.org", port=""),
TARGET_URL,
sent_by=testfunc.random_url(),
domains_deny={"deny.org"},
)
)
self.assertIsNone(
incoming.process_incoming_webmention(
testfunc.random_url(subdomain="", domain="deny.org", port=""),
TARGET_URL,
sent_by=testfunc.random_url(),
domains_deny={"deny.org"},
)
)
self.assertIsNone(
incoming.process_incoming_webmention(
testfunc.random_url(domain="deny.org", port=""),
TARGET_URL,
sent_by=testfunc.random_url(),
domains_deny={"*.deny.org"},
)
)

0 comments on commit b14e176

Please sign in to comment.