From 7f271dbde712fe2ce76d14fc2f48000e5a3cd140 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 23 Mar 2021 10:41:18 -0700 Subject: [PATCH] Fixes model tests --- .../tests/models/test_activitypub_mixin.py | 33 +++++----- bookwyrm/tests/models/test_base_model.py | 6 +- bookwyrm/tests/models/test_fields.py | 3 +- bookwyrm/tests/models/test_status_model.py | 62 ++++++++++--------- 4 files changed, 54 insertions(+), 50 deletions(-) diff --git a/bookwyrm/tests/models/test_activitypub_mixin.py b/bookwyrm/tests/models/test_activitypub_mixin.py index 930f3a533..0d1acd978 100644 --- a/bookwyrm/tests/models/test_activitypub_mixin.py +++ b/bookwyrm/tests/models/test_activitypub_mixin.py @@ -13,6 +13,7 @@ from bookwyrm.models.activitypub_mixin import ActivitypubMixin from bookwyrm.models.activitypub_mixin import ActivityMixin, ObjectMixin +@patch("bookwyrm.activitystreams.ActivityStream.add_status") class ActivitypubMixins(TestCase): """ functionality shared across models """ @@ -44,7 +45,7 @@ class ActivitypubMixins(TestCase): } # ActivitypubMixin - def test_to_activity(self): + def test_to_activity(self, _): """ model to ActivityPub json """ @dataclass(init=False) @@ -65,7 +66,7 @@ class ActivitypubMixins(TestCase): self.assertEqual(activity["id"], "https://www.example.com/test") self.assertEqual(activity["type"], "Test") - def test_find_existing_by_remote_id(self): + def test_find_existing_by_remote_id(self, _): """ attempt to match a remote id to an object in the db """ # uses a different remote id scheme # this isn't really part of this test directly but it's helpful to state @@ -98,7 +99,7 @@ class ActivitypubMixins(TestCase): # test subclass match result = models.Status.find_existing_by_remote_id("https://comment.net") - def test_find_existing(self): + def test_find_existing(self, _): """ match a blob of data to a model """ book = models.Edition.objects.create( title="Test edition", @@ -108,7 +109,7 @@ class ActivitypubMixins(TestCase): result = models.Edition.find_existing({"openlibraryKey": "OL1234"}) self.assertEqual(result, book) - def test_get_recipients_public_object(self): + def test_get_recipients_public_object(self, _): """ determines the recipients for an object's broadcast """ MockSelf = namedtuple("Self", ("privacy")) mock_self = MockSelf("public") @@ -116,7 +117,7 @@ class ActivitypubMixins(TestCase): self.assertEqual(len(recipients), 1) self.assertEqual(recipients[0], self.remote_user.inbox) - def test_get_recipients_public_user_object_no_followers(self): + def test_get_recipients_public_user_object_no_followers(self, _): """ determines the recipients for a user's object broadcast """ MockSelf = namedtuple("Self", ("privacy", "user")) mock_self = MockSelf("public", self.local_user) @@ -124,7 +125,7 @@ class ActivitypubMixins(TestCase): recipients = ActivitypubMixin.get_recipients(mock_self) self.assertEqual(len(recipients), 0) - def test_get_recipients_public_user_object(self): + def test_get_recipients_public_user_object(self, _): """ determines the recipients for a user's object broadcast """ MockSelf = namedtuple("Self", ("privacy", "user")) mock_self = MockSelf("public", self.local_user) @@ -134,7 +135,7 @@ class ActivitypubMixins(TestCase): self.assertEqual(len(recipients), 1) self.assertEqual(recipients[0], self.remote_user.inbox) - def test_get_recipients_public_user_object_with_mention(self): + def test_get_recipients_public_user_object_with_mention(self, _): """ determines the recipients for a user's object broadcast """ MockSelf = namedtuple("Self", ("privacy", "user")) mock_self = MockSelf("public", self.local_user) @@ -157,7 +158,7 @@ class ActivitypubMixins(TestCase): self.assertEqual(recipients[0], another_remote_user.inbox) self.assertEqual(recipients[1], self.remote_user.inbox) - def test_get_recipients_direct(self): + def test_get_recipients_direct(self, _): """ determines the recipients for a user's object broadcast """ MockSelf = namedtuple("Self", ("privacy", "user")) mock_self = MockSelf("public", self.local_user) @@ -179,7 +180,7 @@ class ActivitypubMixins(TestCase): self.assertEqual(len(recipients), 1) self.assertEqual(recipients[0], another_remote_user.inbox) - def test_get_recipients_combine_inboxes(self): + def test_get_recipients_combine_inboxes(self, _): """ should combine users with the same shared_inbox """ self.remote_user.shared_inbox = "http://example.com/inbox" self.remote_user.save(broadcast=False) @@ -203,7 +204,7 @@ class ActivitypubMixins(TestCase): self.assertEqual(len(recipients), 1) self.assertEqual(recipients[0], "http://example.com/inbox") - def test_get_recipients_software(self): + def test_get_recipients_software(self, _): """ should differentiate between bookwyrm and other remote users """ with patch("bookwyrm.models.user.set_remote_server.delay"): another_remote_user = models.User.objects.create_user( @@ -233,7 +234,7 @@ class ActivitypubMixins(TestCase): self.assertEqual(recipients[0], another_remote_user.inbox) # ObjectMixin - def test_object_save_create(self): + def test_object_save_create(self, _): """ should save uneventufully when broadcast is disabled """ class Success(Exception): @@ -264,7 +265,7 @@ class ActivitypubMixins(TestCase): ObjectModel(user=self.local_user).save(broadcast=False) ObjectModel(user=None).save() - def test_object_save_update(self): + def test_object_save_update(self, _): """ should save uneventufully when broadcast is disabled """ class Success(Exception): @@ -290,7 +291,7 @@ class ActivitypubMixins(TestCase): with self.assertRaises(Success): UpdateObjectModel(id=1, last_edited_by=self.local_user).save() - def test_object_save_delete(self): + def test_object_save_delete(self, _): """ should create delete activities when objects are deleted by flag """ class ActivitySuccess(Exception): @@ -312,7 +313,7 @@ class ActivitypubMixins(TestCase): with self.assertRaises(ActivitySuccess): DeletableObjectModel(id=1, user=self.local_user, deleted=True).save() - def test_to_delete_activity(self): + def test_to_delete_activity(self, _): """ wrapper for Delete activity """ MockSelf = namedtuple("Self", ("remote_id", "to_activity")) mock_self = MockSelf( @@ -327,7 +328,7 @@ class ActivitypubMixins(TestCase): activity["cc"], ["https://www.w3.org/ns/activitystreams#Public"] ) - def test_to_update_activity(self): + def test_to_update_activity(self, _): """ ditto above but for Update """ MockSelf = namedtuple("Self", ("remote_id", "to_activity")) mock_self = MockSelf( @@ -345,7 +346,7 @@ class ActivitypubMixins(TestCase): self.assertIsInstance(activity["object"], dict) # Activity mixin - def test_to_undo_activity(self): + def test_to_undo_activity(self, _): """ and again, for Undo """ MockSelf = namedtuple("Self", ("remote_id", "to_activity", "user")) mock_self = MockSelf( diff --git a/bookwyrm/tests/models/test_base_model.py b/bookwyrm/tests/models/test_base_model.py index 4479d1560..25a2e7ee6 100644 --- a/bookwyrm/tests/models/test_base_model.py +++ b/bookwyrm/tests/models/test_base_model.py @@ -27,18 +27,18 @@ class BaseModel(TestCase): expected = instance.get_remote_id() self.assertEqual(expected, "https://%s/user/mouse/bookwyrmmodel/1" % DOMAIN) - def test_execute_after_save(self): + def test_set_remote_id(self): """ this function sets remote ids after creation """ # using Work because it BookWrymModel is abstract and this requires save # Work is a relatively not-fancy model. instance = models.Work.objects.create(title="work title") instance.remote_id = None - base_model.execute_after_save(None, instance, True) + base_model.set_remote_id(None, instance, True) self.assertEqual( instance.remote_id, "https://%s/book/%d" % (DOMAIN, instance.id) ) # shouldn't set remote_id if it's not created instance.remote_id = None - base_model.execute_after_save(None, instance, False) + base_model.set_remote_id(None, instance, False) self.assertIsNone(instance.remote_id) diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index 28faf52c3..18bb028ff 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -185,7 +185,8 @@ class ActivitypubFields(TestCase): self.assertEqual(model_instance.privacy_field, "unlisted") @patch("bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast") - def test_privacy_field_set_activity_from_field(self, _): + @patch("bookwyrm.activitystreams.ActivityStream.add_status") + def test_privacy_field_set_activity_from_field(self, *_): """ translate between to/cc fields and privacy """ user = User.objects.create_user( "rat", "rat@rat.rat", "ratword", local=True, localname="rat" diff --git a/bookwyrm/tests/models/test_status_model.py b/bookwyrm/tests/models/test_status_model.py index 1dcf56339..54fe7fee9 100644 --- a/bookwyrm/tests/models/test_status_model.py +++ b/bookwyrm/tests/models/test_status_model.py @@ -15,6 +15,7 @@ from bookwyrm import activitypub, models, settings # pylint: disable=too-many-public-methods @patch("bookwyrm.models.Status.broadcast") +@patch("bookwyrm.activitystreams.ActivityStream.add_status") class Status(TestCase): """ lotta types of statuses """ @@ -44,14 +45,14 @@ class Status(TestCase): image.save(output, format=image.format) self.book.cover.save("test.jpg", ContentFile(output.getvalue())) - def test_status_generated_fields(self, _): + def test_status_generated_fields(self, *_): """ setting remote id """ status = models.Status.objects.create(content="bleh", user=self.local_user) expected_id = "https://%s/user/mouse/status/%d" % (settings.DOMAIN, status.id) self.assertEqual(status.remote_id, expected_id) self.assertEqual(status.privacy, "public") - def test_replies(self, _): + def test_replies(self, *_): """ get a list of replies """ parent = models.Status.objects.create(content="hi", user=self.local_user) child = models.Status.objects.create( @@ -70,7 +71,7 @@ class Status(TestCase): # should select subclasses self.assertIsInstance(replies.last(), models.Review) - def test_status_type(self, _): + def test_status_type(self, *_): """ class name """ self.assertEqual(models.Status().status_type, "Note") self.assertEqual(models.Review().status_type, "Review") @@ -78,14 +79,14 @@ class Status(TestCase): self.assertEqual(models.Comment().status_type, "Comment") self.assertEqual(models.Boost().status_type, "Announce") - def test_boostable(self, _): + def test_boostable(self, *_): """ can a status be boosted, based on privacy """ self.assertTrue(models.Status(privacy="public").boostable) self.assertTrue(models.Status(privacy="unlisted").boostable) self.assertFalse(models.Status(privacy="followers").boostable) self.assertFalse(models.Status(privacy="direct").boostable) - def test_to_replies(self, _): + def test_to_replies(self, *_): """ activitypub replies collection """ parent = models.Status.objects.create(content="hi", user=self.local_user) child = models.Status.objects.create( @@ -102,7 +103,7 @@ class Status(TestCase): self.assertEqual(replies["id"], "%s/replies" % parent.remote_id) self.assertEqual(replies["totalItems"], 2) - def test_status_to_activity(self, _): + def test_status_to_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.Status.objects.create( content="test content", user=self.local_user @@ -113,20 +114,21 @@ class Status(TestCase): self.assertEqual(activity["content"], "test content") self.assertEqual(activity["sensitive"], False) - def test_status_to_activity_tombstone(self, _): + def test_status_to_activity_tombstone(self, *_): """ subclass of the base model version with a "pure" serializer """ - status = models.Status.objects.create( - content="test content", - user=self.local_user, - deleted=True, - deleted_date=timezone.now(), - ) + with patch("bookwyrm.activitystreams.ActivityStream.remove_status"): + status = models.Status.objects.create( + content="test content", + user=self.local_user, + deleted=True, + deleted_date=timezone.now(), + ) activity = status.to_activity() self.assertEqual(activity["id"], status.remote_id) self.assertEqual(activity["type"], "Tombstone") self.assertFalse(hasattr(activity, "content")) - def test_status_to_pure_activity(self, _): + def test_status_to_pure_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.Status.objects.create( content="test content", user=self.local_user @@ -138,7 +140,7 @@ class Status(TestCase): self.assertEqual(activity["sensitive"], False) self.assertEqual(activity["attachment"], []) - def test_generated_note_to_activity(self, _): + def test_generated_note_to_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.GeneratedNote.objects.create( content="test content", user=self.local_user @@ -152,7 +154,7 @@ class Status(TestCase): self.assertEqual(activity["sensitive"], False) self.assertEqual(len(activity["tag"]), 2) - def test_generated_note_to_pure_activity(self, _): + def test_generated_note_to_pure_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.GeneratedNote.objects.create( content="test content", user=self.local_user @@ -176,7 +178,7 @@ class Status(TestCase): ) self.assertEqual(activity["attachment"][0].name, "Test Edition") - def test_comment_to_activity(self, _): + def test_comment_to_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.Comment.objects.create( content="test content", user=self.local_user, book=self.book @@ -187,7 +189,7 @@ class Status(TestCase): self.assertEqual(activity["content"], "test content") self.assertEqual(activity["inReplyToBook"], self.book.remote_id) - def test_comment_to_pure_activity(self, _): + def test_comment_to_pure_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.Comment.objects.create( content="test content", user=self.local_user, book=self.book @@ -207,7 +209,7 @@ class Status(TestCase): ) self.assertEqual(activity["attachment"][0].name, "Test Edition") - def test_quotation_to_activity(self, _): + def test_quotation_to_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.Quotation.objects.create( quote="a sickening sense", @@ -222,7 +224,7 @@ class Status(TestCase): self.assertEqual(activity["content"], "test content") self.assertEqual(activity["inReplyToBook"], self.book.remote_id) - def test_quotation_to_pure_activity(self, _): + def test_quotation_to_pure_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.Quotation.objects.create( quote="a sickening sense", @@ -245,7 +247,7 @@ class Status(TestCase): ) self.assertEqual(activity["attachment"][0].name, "Test Edition") - def test_review_to_activity(self, _): + def test_review_to_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.Review.objects.create( name="Review name", @@ -262,7 +264,7 @@ class Status(TestCase): self.assertEqual(activity["content"], "test content") self.assertEqual(activity["inReplyToBook"], self.book.remote_id) - def test_review_to_pure_activity(self, _): + def test_review_to_pure_activity(self, *_): """ subclass of the base model version with a "pure" serializer """ status = models.Review.objects.create( name="Review name", @@ -285,7 +287,7 @@ class Status(TestCase): ) self.assertEqual(activity["attachment"][0].name, "Test Edition") - def test_favorite(self, _): + def test_favorite(self, *_): """ fav a status """ real_broadcast = models.Favorite.broadcast @@ -311,7 +313,7 @@ class Status(TestCase): self.assertEqual(activity["object"], status.remote_id) models.Favorite.broadcast = real_broadcast - def test_boost(self, _): + def test_boost(self, *_): """ boosting, this one's a bit fussy """ status = models.Status.objects.create( content="test content", user=self.local_user @@ -323,7 +325,7 @@ class Status(TestCase): self.assertEqual(activity["type"], "Announce") self.assertEqual(activity, boost.to_activity(pure=True)) - def test_notification(self, _): + def test_notification(self, *_): """ a simple model """ notification = models.Notification.objects.create( user=self.local_user, notification_type="FAVORITE" @@ -335,7 +337,7 @@ class Status(TestCase): user=self.local_user, notification_type="GLORB" ) - def test_create_broadcast(self, broadcast_mock): + def test_create_broadcast(self, _, broadcast_mock): """ should send out two verions of a status on create """ models.Comment.objects.create( content="hi", user=self.local_user, book=self.book @@ -355,7 +357,7 @@ class Status(TestCase): self.assertEqual(args["type"], "Create") self.assertEqual(args["object"]["type"], "Comment") - def test_recipients_with_mentions(self, _): + def test_recipients_with_mentions(self, *_): """ get recipients to broadcast a status """ status = models.GeneratedNote.objects.create( content="test content", user=self.local_user @@ -364,7 +366,7 @@ class Status(TestCase): self.assertEqual(status.recipients, [self.remote_user]) - def test_recipients_with_reply_parent(self, _): + def test_recipients_with_reply_parent(self, *_): """ get recipients to broadcast a status """ parent_status = models.GeneratedNote.objects.create( content="test content", user=self.remote_user @@ -375,7 +377,7 @@ class Status(TestCase): self.assertEqual(status.recipients, [self.remote_user]) - def test_recipients_with_reply_parent_and_mentions(self, _): + def test_recipients_with_reply_parent_and_mentions(self, *_): """ get recipients to broadcast a status """ parent_status = models.GeneratedNote.objects.create( content="test content", user=self.remote_user @@ -388,7 +390,7 @@ class Status(TestCase): self.assertEqual(status.recipients, [self.remote_user]) @responses.activate - def test_ignore_activity_boost(self, _): + def test_ignore_activity_boost(self, *_): """ don't bother with most remote statuses """ activity = activitypub.Announce( id="http://www.faraway.com/boost/12",