From 62698a772412596da55091b536ecc38ba70fea06 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 15 Jun 2018 23:53:25 +0200 Subject: [PATCH] More tests and cleanup --- dev-requirements.txt | 1 + little_boxes/activitypub.py | 2 +- little_boxes/backend.py | 4 ++++ little_boxes/{utils.py => collection.py} | 0 little_boxes/urlutils.py | 15 +++++++++++---- tests/test_backend.py | 4 +++- 6 files changed, 20 insertions(+), 6 deletions(-) rename little_boxes/{utils.py => collection.py} (100%) diff --git a/dev-requirements.txt b/dev-requirements.txt index 899edb9..b902140 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,5 +1,6 @@ pytest pytest-cov +httpretty black codecov flake8 diff --git a/little_boxes/activitypub.py b/little_boxes/activitypub.py index 59c9f3b..196a1b7 100644 --- a/little_boxes/activitypub.py +++ b/little_boxes/activitypub.py @@ -12,7 +12,7 @@ from .errors import NotFromOutboxError # from .errors import ActivityNotFoundError # from .urlutils import check_url -from .utils import parse_collection +from .collection import parse_collection from .backend import Backend from typing import List diff --git a/little_boxes/backend.py b/little_boxes/backend.py index 84e7879..30bbb42 100644 --- a/little_boxes/backend.py +++ b/little_boxes/backend.py @@ -62,6 +62,10 @@ class Backend(abc.ABC): def inbox_update(self, as_actor: "ap.Person", activity: "ap.Update") -> None: pass + @abc.abstractmethod + def outbox_update(self, as_actor: "ap.Person", activity: "ap.Update") -> None: + pass + @abc.abstractmethod def inbox_like(self, as_actor: "ap.Person", activity: "ap.Like") -> None: pass diff --git a/little_boxes/utils.py b/little_boxes/collection.py similarity index 100% rename from little_boxes/utils.py rename to little_boxes/collection.py diff --git a/little_boxes/urlutils.py b/little_boxes/urlutils.py index 0a72fc5..7dbcadf 100644 --- a/little_boxes/urlutils.py +++ b/little_boxes/urlutils.py @@ -28,10 +28,16 @@ def is_url_valid(url: str) -> bool: return False try: - ip_address = socket.getaddrinfo(parsed.hostname, parsed.port or 80)[0][4][0] - except socket.gaierror: - logger.exception(f"failed to lookup url {url}") - return False + ip_address = ipaddress.ip_address(parsed.hostname) + except ValueError: + try: + ip_address = socket.getaddrinfo(parsed.hostname, parsed.port or 80)[0][4][0] + logger.debug(f"dns lookup: {parsed.hostname} -> {ip_address}") + except socket.gaierror: + logger.exception(f"failed to lookup url {url}") + return False + + logger.debug(f"{ip_address}") if ipaddress.ip_address(ip_address).is_private: logger.info(f"rejecting private URL {url}") @@ -41,6 +47,7 @@ def is_url_valid(url: str) -> bool: def check_url(url: str) -> None: + logger.debug(f"check_url {url}") if not is_url_valid(url): raise InvalidURLError(f'"{url}" is invalid') diff --git a/tests/test_backend.py b/tests/test_backend.py index 218f5bb..461936d 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -235,10 +235,12 @@ class InMemBackend(Backend): def outbox_delete(self, as_actor: ap.Person, activity: ap.Delete) -> None: pass + @track_call def inbox_update(self, as_actor: ap.Person, activity: ap.Update) -> None: pass - def outbox_update(self, activity: ap.Update) -> None: + @track_call + def outbox_update(self, as_actor: ap.Person, activity: ap.Update) -> None: pass @track_call