From 8ef1d0d3ac4dde5e11bb80332722bbcaa7a3e6ec Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 18:55:08 -0700 Subject: [PATCH 1/8] Fixes image federation --- bookwyrm/models/fields.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index b59b61072..ca837e034 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -11,8 +11,10 @@ from django.core.exceptions import ValidationError from django.core.files.base import ContentFile from django.db import models from django.forms import ClearableFileInput, ImageField as DjangoImageField +from django.templatetags.static import static from django.utils import timezone from django.utils.translation import gettext_lazy as _ + from bookwyrm import activitypub from bookwyrm.connectors import get_image from bookwyrm.sanitize_html import InputHtmlParser @@ -354,7 +356,7 @@ def image_serializer(value, alt): url = value.url else: return None - url = "https://%s%s" % (DOMAIN, url) + url = static(url) return activitypub.Document(url=url, name=alt) From 9e6f78c0f5dcea6d91733aeede9df68ab0b59f3c Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 19:01:16 -0700 Subject: [PATCH 2/8] Fixes linting issue --- bookwyrm/models/fields.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index ca837e034..2849f00cb 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -18,7 +18,6 @@ from django.utils.translation import gettext_lazy as _ from bookwyrm import activitypub from bookwyrm.connectors import get_image from bookwyrm.sanitize_html import InputHtmlParser -from bookwyrm.settings import DOMAIN def validate_remote_id(value): From b7563ca221f577f90f24918a13c2373136ab21d9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 19:01:23 -0700 Subject: [PATCH 3/8] Adds test --- bookwyrm/tests/models/test_fields.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index a8a6096be..bf907bf40 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -426,6 +426,17 @@ class ActivitypubFields(TestCase): self.assertIsInstance(loaded_image, list) self.assertIsInstance(loaded_image[1], ContentFile) + def test_image_serialize(self): + """make sure we're creating sensible image paths""" + ValueMock = namedtuple( + "ValueMock", ("url") + ) + value_mock = ValueMock("images/fish.jpg") + result = fields.image_serializer(value_mock, "hello") + self.assertEqual(result.type, "Document") + self.assertEqual(result.url, "https://test.com/images/fish.jpg") + self.assertEqual(result.name, "hello") + def test_datetime_field(self): """this one is pretty simple, it just has to use isoformat""" instance = fields.DateTimeField() From 68f9b67fd97314e8ac4278dbad76cd06b831c8b9 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 19:30:42 -0700 Subject: [PATCH 4/8] Updates test --- bookwyrm/models/fields.py | 3 +++ bookwyrm/tests/models/test_fields.py | 8 +++----- pytest.ini | 16 ++++++++++++++++ requirements.txt | 1 + 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 2849f00cb..11854dff7 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -18,6 +18,7 @@ from django.utils.translation import gettext_lazy as _ from bookwyrm import activitypub from bookwyrm.connectors import get_image from bookwyrm.sanitize_html import InputHtmlParser +from bookwyrm.settings import DOMAIN def validate_remote_id(value): @@ -356,6 +357,8 @@ def image_serializer(value, alt): else: return None url = static(url) + if not url[:4] == "http": + url = "https://{:s}{:s}".format(DOMAIN, url) return activitypub.Document(url=url, name=alt) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index bf907bf40..65f9a1cc2 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -17,7 +17,7 @@ from django.db import models from django.test import TestCase from django.utils import timezone -from bookwyrm import activitypub +from bookwyrm import activitypub, settings from bookwyrm.activitypub.base_activity import ActivityObject from bookwyrm.models import fields, User, Status from bookwyrm.models.base_model import BookWyrmModel @@ -428,13 +428,11 @@ class ActivitypubFields(TestCase): def test_image_serialize(self): """make sure we're creating sensible image paths""" - ValueMock = namedtuple( - "ValueMock", ("url") - ) + ValueMock = namedtuple("ValueMock", ("url")) value_mock = ValueMock("images/fish.jpg") result = fields.image_serializer(value_mock, "hello") self.assertEqual(result.type, "Document") - self.assertEqual(result.url, "https://test.com/images/fish.jpg") + self.assertEqual(result.url, "https://your.domain.here/static/images/fish.jpg") self.assertEqual(result.name, "hello") def test_datetime_field(self): diff --git a/pytest.ini b/pytest.ini index fa9dbc59f..8539d1167 100644 --- a/pytest.ini +++ b/pytest.ini @@ -4,3 +4,19 @@ python_files = tests.py test_*.py *_tests.py addopts = --cov=bookwyrm --cov-config=.coveragerc markers = integration: marks tests as requiring external resources (deselect with '-m "not integration"') + +env = + DEBUG = false + DOMAIN = your.domain.here + BOOKWYRM_DATABASE_BACKEND = postgres + MEDIA_ROOT = images/ + CELERY_BROKER = "" + REDIS_BROKER_PORT = 6379 + FLOWER_PORT = 8888 + EMAIL_HOST = "smtp.mailgun.org" + EMAIL_PORT = 587 + EMAIL_HOST_USER = "" + EMAIL_HOST_PASSWORD = "" + EMAIL_USE_TLS = true + ENABLE_PREVIEW_IMAGES = false + USE_S3 = false diff --git a/requirements.txt b/requirements.txt index 4a8c15aa1..ec0eb1a81 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,4 +23,5 @@ coverage==5.1 pytest-django==4.1.0 pytest==6.1.2 pytest-cov==2.10.1 +pytest-env==0.6.2 pytest-xdist==2.3.0 From 815dbc81e3a93c7950c270a25fee68f3e318d8eb Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 19:50:33 -0700 Subject: [PATCH 5/8] Remove static tempaletag from image_serializer --- bookwyrm/models/fields.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 11854dff7..3b369e84e 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -11,7 +11,6 @@ from django.core.exceptions import ValidationError from django.core.files.base import ContentFile from django.db import models from django.forms import ClearableFileInput, ImageField as DjangoImageField -from django.templatetags.static import static from django.utils import timezone from django.utils.translation import gettext_lazy as _ @@ -356,7 +355,6 @@ def image_serializer(value, alt): url = value.url else: return None - url = static(url) if not url[:4] == "http": url = "https://{:s}{:s}".format(DOMAIN, url) return activitypub.Document(url=url, name=alt) From a334970692f64b5bad2fb151073d9b9a328c1752 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 20:00:02 -0700 Subject: [PATCH 6/8] Fixes path for image field in test --- bookwyrm/tests/models/test_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index 65f9a1cc2..caf12b847 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -432,7 +432,7 @@ class ActivitypubFields(TestCase): value_mock = ValueMock("images/fish.jpg") result = fields.image_serializer(value_mock, "hello") self.assertEqual(result.type, "Document") - self.assertEqual(result.url, "https://your.domain.here/static/images/fish.jpg") + self.assertEqual(result.url, "https://your.domain.here/images/fish.jpg") self.assertEqual(result.name, "hello") def test_datetime_field(self): From 39b2f3794745a0a4b6d1576c808874489523f4b1 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 20:07:46 -0700 Subject: [PATCH 7/8] One (hopefully) last fix --- bookwyrm/models/fields.py | 2 +- bookwyrm/tests/models/test_fields.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 3b369e84e..e1c947f49 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -356,7 +356,7 @@ def image_serializer(value, alt): else: return None if not url[:4] == "http": - url = "https://{:s}{:s}".format(DOMAIN, url) + url = "https://{:s}/{:s}".format(DOMAIN, url) return activitypub.Document(url=url, name=alt) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index caf12b847..0d93d99c6 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -17,7 +17,7 @@ from django.db import models from django.test import TestCase from django.utils import timezone -from bookwyrm import activitypub, settings +from bookwyrm import activitypub from bookwyrm.activitypub.base_activity import ActivityObject from bookwyrm.models import fields, User, Status from bookwyrm.models.base_model import BookWyrmModel From 909317066036901e28960385a7933382e7d42037 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 2 Aug 2021 20:16:17 -0700 Subject: [PATCH 8/8] Correct test structure --- bookwyrm/models/fields.py | 2 +- bookwyrm/tests/models/test_fields.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index e1c947f49..3b369e84e 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -356,7 +356,7 @@ def image_serializer(value, alt): else: return None if not url[:4] == "http": - url = "https://{:s}/{:s}".format(DOMAIN, url) + url = "https://{:s}{:s}".format(DOMAIN, url) return activitypub.Document(url=url, name=alt) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index 0d93d99c6..c234ffd08 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -429,7 +429,7 @@ class ActivitypubFields(TestCase): def test_image_serialize(self): """make sure we're creating sensible image paths""" ValueMock = namedtuple("ValueMock", ("url")) - value_mock = ValueMock("images/fish.jpg") + value_mock = ValueMock("/images/fish.jpg") result = fields.image_serializer(value_mock, "hello") self.assertEqual(result.type, "Document") self.assertEqual(result.url, "https://your.domain.here/images/fish.jpg")