From c2524dccb23a19de7520bfe6b1fce84325382b99 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 24 Jun 2018 11:33:14 +0200 Subject: [PATCH] Improve the debug mode, fixes #4 --- little_boxes/backend.py | 13 ++++++++++--- little_boxes/urlutils.py | 13 +++++-------- tests/test_backend.py | 3 +++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/little_boxes/backend.py b/little_boxes/backend.py index cadfbb4..ef94b7a 100644 --- a/little_boxes/backend.py +++ b/little_boxes/backend.py @@ -8,13 +8,20 @@ import requests from .__version__ import __version__ from .errors import ActivityNotFoundError from .errors import RemoteActivityGoneError -from .urlutils import check_url +from .urlutils import check_url as check_url if typing.TYPE_CHECKING: from little_boxes import activitypub as ap # noqa: type checking class Backend(abc.ABC): + def debug_mode(self) -> bool: + """Should be overidded to return `True` in order to enable the debug mode.""" + return False + + def check_url(self, url: str) -> None: + check_url(url, debug=self.debug_mode()) + def user_agent(self) -> str: return ( f"{requests.utils.default_user_agent()} (Little Boxes/{__version__};" @@ -26,7 +33,7 @@ class Backend(abc.ABC): return binascii.hexlify(os.urandom(8)).decode("utf-8") def fetch_json(self, url: str, **kwargs): - check_url(url) + self.check_url(url) resp = requests.get( url, headers={"User-Agent": self.user_agent(), "Accept": "application/json"}, @@ -53,7 +60,7 @@ class Backend(abc.ABC): pass # pragma: no cover def fetch_iri(self, iri: str, **kwargs) -> "ap.ObjectType": # pragma: no cover - check_url(iri) + self.check_url(iri) resp = requests.get( iri, headers={ diff --git a/little_boxes/urlutils.py b/little_boxes/urlutils.py index e962ed7..788190c 100644 --- a/little_boxes/urlutils.py +++ b/little_boxes/urlutils.py @@ -1,11 +1,9 @@ import ipaddress import logging -import os import socket from typing import Dict from urllib.parse import urlparse -from . import strtobool from .errors import Error logger = logging.getLogger(__name__) @@ -18,14 +16,13 @@ class InvalidURLError(Error): pass -def is_url_valid(url: str) -> bool: +def is_url_valid(url: str, debug: bool = False) -> bool: parsed = urlparse(url) if parsed.scheme not in ["http", "https"]: return False # XXX in debug mode, we want to allow requests to localhost to test the federation with local instances - debug_mode = strtobool(os.getenv("MICROBLOGPUB_DEBUG", "false")) - if debug_mode: # pragma: no cover + if debug: # pragma: no cover return True if parsed.hostname in ["localhost"]: @@ -56,9 +53,9 @@ def is_url_valid(url: str) -> bool: return True -def check_url(url: str) -> None: - logger.debug(f"check_url {url}") - if not is_url_valid(url): +def check_url(url: str, debug: bool = False) -> None: + logger.debug(f"check_url {url} debug={debug}") + if not is_url_valid(url, debug=debug): raise InvalidURLError(f'"{url}" is invalid') return None diff --git a/tests/test_backend.py b/tests/test_backend.py index bbfbfb0..5189cb1 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -65,6 +65,9 @@ class InMemBackend(Backend): return calls + def debug_mode(self) -> bool: + return True + def setup_actor(self, name, pusername): """Create a new actor in this backend.""" p = ap.Person(