kopia lustrzana https://gitlab.com/marnanel/chapeau
Sombrero's webfinger support doesn't break if the remote host is unreachable
or times out. Tests updated. Sombrero webfinger tests gain a context manager suppress_thread_exceptions(), to stop spurious exceptions being reported if they occur in threads other than the main thread. See https://stackoverflow.com/questions/63206653/ .status-serialisers
rodzic
a6f3673fa7
commit
001698cda6
|
@ -0,0 +1,15 @@
|
|||
import threading
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def suppress_thread_exceptions():
|
||||
"""
|
||||
Context manager which causes exceptions in threads not to
|
||||
be printed. See https://stackoverflow.com/questions/63206653/ .
|
||||
"""
|
||||
orig = threading.excepthook
|
||||
threading.excepthook = lambda a: None
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
threading.excepthook = orig
|
|
@ -7,6 +7,7 @@
|
|||
from unittest import skip
|
||||
from django.test import TestCase
|
||||
from kepi.sombrero_sendpub.webfinger import get_webfinger
|
||||
from . import suppress_thread_exceptions
|
||||
import httpretty
|
||||
import logging
|
||||
import requests
|
||||
|
@ -166,10 +167,11 @@ class TestWebfinger(TestCase):
|
|||
body = timeout,
|
||||
)
|
||||
|
||||
webfinger = get_webfinger(
|
||||
EXAMPLE_USERNAME,
|
||||
EXAMPLE_HOSTNAME,
|
||||
)
|
||||
with suppress_thread_exceptions():
|
||||
webfinger = get_webfinger(
|
||||
EXAMPLE_USERNAME,
|
||||
EXAMPLE_HOSTNAME,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
webfinger.url,
|
||||
|
@ -192,10 +194,11 @@ class TestWebfinger(TestCase):
|
|||
body = no_such_host,
|
||||
)
|
||||
|
||||
webfinger = get_webfinger(
|
||||
EXAMPLE_USERNAME,
|
||||
EXAMPLE_HOSTNAME,
|
||||
)
|
||||
with suppress_thread_exceptions():
|
||||
webfinger = get_webfinger(
|
||||
EXAMPLE_USERNAME,
|
||||
EXAMPLE_HOSTNAME,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
webfinger.url,
|
||||
|
|
|
@ -20,18 +20,27 @@ def get_webfinger(username, hostname):
|
|||
url = f'https://{hostname}/.well-known/'+\
|
||||
f'webfinger?acct={username}'
|
||||
|
||||
response = requests.get(
|
||||
url,
|
||||
headers = {
|
||||
'Accept': 'application/activity+json',
|
||||
},
|
||||
)
|
||||
try:
|
||||
response = requests.get(
|
||||
url,
|
||||
headers = {
|
||||
'Accept': 'application/activity+json',
|
||||
},
|
||||
)
|
||||
except requests.ConnectionError:
|
||||
logger.info("webfinger: Connection to %s failed",
|
||||
hostname)
|
||||
result.status = 0
|
||||
result.save()
|
||||
return result
|
||||
|
||||
result.status = response.status_code
|
||||
|
||||
if response.status_code!=200:
|
||||
result.save()
|
||||
logger.info("Unexpected status code from webfinger lookup")
|
||||
logger.info("webfinger: Unexpected status code %d from lookup of %s@%s",
|
||||
response.status_code,
|
||||
username, hostname)
|
||||
return result
|
||||
|
||||
self_link = [x for x in response.json()['links']
|
||||
|
@ -39,7 +48,8 @@ def get_webfinger(username, hostname):
|
|||
|
||||
if not self_link:
|
||||
result.save()
|
||||
logger.info("Webfinger has no activity information")
|
||||
logger.info("webfinger: retrieved %s@%s, which has no activity information",
|
||||
username, hostname)
|
||||
return result
|
||||
|
||||
result.url = self_link[0]['href']
|
||||
|
|
Ładowanie…
Reference in New Issue