From a7ee8fea247935556a1f21705c1bc485d9cf4682 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 28 Nov 2021 08:49:54 -0800 Subject: [PATCH] Adds test for setting model image field --- bookwyrm/tests/models/test_fields.py | 29 +++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index 6499e96c..278272d8 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -19,7 +19,7 @@ from django.utils import timezone from bookwyrm import activitypub from bookwyrm.activitypub.base_activity import ActivityObject -from bookwyrm.models import fields, User, Status +from bookwyrm.models import fields, User, Status, Edition from bookwyrm.models.base_model import BookWyrmModel from bookwyrm.models.activitypub_mixin import ActivitypubMixin from bookwyrm.settings import DOMAIN @@ -458,6 +458,33 @@ class ModelFields(TestCase): self.assertIsInstance(loaded_image, list) self.assertIsInstance(loaded_image[1], ContentFile) + @responses.activate + def test_image_field_set_field_from_activity(self, *_): + """update a model instance from an activitypub object""" + image_file = pathlib.Path(__file__).parent.joinpath( + "../../static/images/default_avi.jpg" + ) + image = Image.open(image_file) + output = BytesIO() + image.save(output, format=image.format) + + instance = fields.ImageField(activitypub_field="cover", name="cover") + + responses.add( + responses.GET, + "http://www.example.com/image.jpg", + body=image.tobytes(), + status=200, + ) + book = Edition.objects.create(title="hello") + + MockActivity = namedtuple("MockActivity", ("cover")) + mock_activity = MockActivity("http://www.example.com/image.jpg") + + instance.set_field_from_activity(book, mock_activity) + self.assertIsNotNone(book.cover.name) + self.assertEqual(book.cover.size, 43200) + def test_datetime_field(self, *_): """this one is pretty simple, it just has to use isoformat""" instance = fields.DateTimeField()