diff --git a/config/settings/base.py b/config/settings/base.py index 3c6f357b372f880da92b968c43ba3acbafaa672e..fa5d81490d5ce0224dfdc56d18a51604730b456a 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -283,3 +283,4 @@ ADMIN_URL = env("DJANGO_ADMIN_URL", default="admin/") # ------------------------------------------------------------------------------ # http://whitenoise.evans.io/en/latest/django.html#enable-whitenoise MIDDLEWARE.insert(1, "whitenoise.middleware.WhiteNoiseMiddleware") # noqa F405 +BASE_URL = env("BASE_URL") diff --git a/retribute_api/search/consumers.py b/retribute_api/search/consumers.py index 01a53efd19cebc452fc43494f90ed0bb0ea74540..c3a8a7988251489363630b4f7e2cd4aa837fb37d 100644 --- a/retribute_api/search/consumers.py +++ b/retribute_api/search/consumers.py @@ -11,11 +11,22 @@ async def json_response(self, status, content): await self.send_response( status, json.dumps(content, indent=2, sort_keys=True).encode("utf-8"), - headers=[{"Content-Type": "application/json"}], + headers=[(b"Content-Type", b"application/json")], ) +def wrapper_500(callback): + async def inner(self, body): + try: + await callback(self, body) + except Exception as e: + await json_response(self, 400, {"detail": str(e)}) + + return callback + + class SearchSingleConsumer(AsyncHttpConsumer): + @wrapper_500 async def handle(self, body): lookup_type = self.scope["url_route"]["kwargs"]["lookup_type"] lookup = self.scope["url_route"]["kwargs"]["lookup"] @@ -24,17 +35,10 @@ class SearchSingleConsumer(AsyncHttpConsumer): except KeyError: await json_response(self, 400, {"detail": "Invalid lookup"}) try: - import ipdb - - ipdb.set_trace() async with aiohttp.client.ClientSession() as session: data = await source.get(lookup, session) + profile = sources.result_to_retribute_profile(lookup_type, lookup, data) except exceptions.SearchError as e: await json_response(self, 400, {"detail": e.message}) - raise - try: - profile = sources.result_to_retribute_profile(lookup_type, lookup, data) - except Exception: - raise await json_response(self, 200, profile) diff --git a/retribute_api/search/sources.py b/retribute_api/search/sources.py index e5a4b2328f5a2dab60a65c3efb48fead3a6519d8..ea74c24fca135c54c8892216e23572c495f98570 100644 --- a/retribute_api/search/sources.py +++ b/retribute_api/search/sources.py @@ -32,6 +32,10 @@ class Source(object): @registry.register class Activitypub(Source): id = "activitypub" + excluded_tags = [ + "#noretribute", + # '#nobot', + ] async def get(self, lookup, session): async with session.get( @@ -42,7 +46,7 @@ class Activitypub(Source): serializer = activitypub.ActorSerializer(data=actor_data) serializer.is_valid(raise_exception=True) for tag in serializer.validated_data["tag"]: - if tag["name"] in ["#nobot"]: + if tag["name"].lower() in self.excluded_tags: raise exceptions.SkippedProfile() data = { diff --git a/tests/search/test_activitypub.py b/tests/search/test_activitypub.py index 86735c3ef1429bc656b670ee75bcedb4da0d096a..6c27c6c2a5fe86918021036f5b5c7d0d6237f513 100644 --- a/tests/search/test_activitypub.py +++ b/tests/search/test_activitypub.py @@ -5,7 +5,7 @@ def test_profile_serializer(): payload = { "id": "https://test.domain", "url": "https://test.domain", - "tag": [{"type": "Hashtag", "name": "#nobot"}], + "tag": [{"type": "Hashtag", "name": "#NoRetribute"}], "attachment": [ { "type": "PropertyValue", diff --git a/tests/search/test_consumers.py b/tests/search/test_consumers.py index dfecbca2ce1627da2769021ec2715460d670b29c..de59c0446aa508a6ed24f08e7112a0b08a9ea347 100644 --- a/tests/search/test_consumers.py +++ b/tests/search/test_consumers.py @@ -20,5 +20,5 @@ async def test_search_consumer_success(loop, application, mocker, coroutine_mock "webfinger", "test@user.domain", get.return_value ) assert response["status"] == 200 - assert response["headers"] == [{"Content-Type": "application/json"}] + assert response["headers"] == [(b"Content-Type", b"application/json")] assert response["body"] == json.dumps(expected, indent=2, sort_keys=True).encode()