From c313fbc04afdd419199bbc17ab9d7d73427f5aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Sun, 28 Jul 2024 18:39:27 -0300 Subject: [PATCH] celery inmem test suite: tests/views/*.py --- bookwyrm/tests/views/test_annual_summary.py | 46 ++---- bookwyrm/tests/views/test_author.py | 24 +-- bookwyrm/tests/views/test_directory.py | 28 +--- bookwyrm/tests/views/test_discover.py | 23 +-- bookwyrm/tests/views/test_feed.py | 87 ++++------ bookwyrm/tests/views/test_follow.py | 123 ++++++-------- bookwyrm/tests/views/test_status.py | 173 +++++++++----------- 7 files changed, 206 insertions(+), 298 deletions(-) diff --git a/bookwyrm/tests/views/test_annual_summary.py b/bookwyrm/tests/views/test_annual_summary.py index 1ee4322f73..15620f7cd2 100644 --- a/bookwyrm/tests/views/test_annual_summary.py +++ b/bookwyrm/tests/views/test_annual_summary.py @@ -1,6 +1,5 @@ """testing the annual summary page""" import datetime -from unittest.mock import patch from django.contrib.auth.models import AnonymousUser from django.http import Http404 @@ -23,20 +22,15 @@ class AnnualSummary(TestCase): @classmethod def setUpTestData(cls): """we need basic test data and mocks""" - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - cls.local_user = models.User.objects.create_user( - "mouse@local.com", - "mouse@mouse.com", - "mouseword", - local=True, - localname="mouse", - remote_id="https://example.com/users/mouse", - summary_keys={"2020": "0123456789"}, - ) + cls.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.com", + "mouseword", + local=True, + localname="mouse", + remote_id="https://example.com/users/mouse", + summary_keys={"2020": "0123456789"}, + ) cls.work = models.Work.objects.create(title="Test Work") cls.book = models.Edition.objects.create( title="Example Edition", @@ -53,7 +47,7 @@ def setUp(self): self.anonymous_user = AnonymousUser self.anonymous_user.is_authenticated = False - def test_annual_summary_not_authenticated(self, *_): + def test_annual_summary_not_authenticated(self): """there are so many views, this just makes sure it DOESN’T LOAD""" view = views.AnnualSummary.as_view() request = self.factory.get("") @@ -62,7 +56,7 @@ def test_annual_summary_not_authenticated(self, *_): with self.assertRaises(Http404): view(request, self.local_user.localname, self.year) - def test_annual_summary_not_authenticated_with_key(self, *_): + def test_annual_summary_not_authenticated_with_key(self): """there are so many views, this just makes sure it DOES LOAD""" key = self.local_user.summary_keys[self.year] view = views.AnnualSummary.as_view() @@ -78,7 +72,7 @@ def test_annual_summary_not_authenticated_with_key(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - def test_annual_summary_wrong_year(self, *_): + def test_annual_summary_wrong_year(self): """there are so many views, this just makes sure it DOESN’T LOAD""" view = views.AnnualSummary.as_view() request = self.factory.get("") @@ -87,7 +81,7 @@ def test_annual_summary_wrong_year(self, *_): with self.assertRaises(Http404): view(request, self.local_user.localname, self.year) - def test_annual_summary_empty_page(self, *_): + def test_annual_summary_empty_page(self): """there are so many views, this just makes sure it LOADS""" view = views.AnnualSummary.as_view() request = self.factory.get("") @@ -99,9 +93,7 @@ def test_annual_summary_empty_page(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") - @patch("bookwyrm.activitystreams.add_book_statuses_task.delay") - def test_annual_summary_page(self, *_): + def test_annual_summary_page(self): """there are so many views, this just makes sure it LOADS""" models.ReadThrough.objects.create( user=self.local_user, book=self.book, finish_date=make_date(2020, 1, 1) @@ -117,9 +109,7 @@ def test_annual_summary_page(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") - @patch("bookwyrm.activitystreams.add_book_statuses_task.delay") - def test_annual_summary_page_with_review(self, *_): + def test_annual_summary_page_with_review(self): """there are so many views, this just makes sure it LOADS""" models.Review.objects.create( @@ -144,7 +134,7 @@ def test_annual_summary_page_with_review(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - def test_personal_annual_summary(self, *_): + def test_personal_annual_summary(self): """redirect to unique user url""" view = views.personal_annual_summary request = self.factory.get("") @@ -155,7 +145,7 @@ def test_personal_annual_summary(self, *_): self.assertEqual(result.status_code, 302) self.assertEqual(result.url, "/user/mouse/2020-in-the-books") - def test_summary_add_key(self, *_): + def test_summary_add_key(self): """add shareable key""" self.assertFalse("2022" in self.local_user.summary_keys.keys()) @@ -167,7 +157,7 @@ def test_summary_add_key(self, *_): self.assertEqual(result.status_code, 302) self.assertIsNotNone(self.local_user.summary_keys["2022"]) - def test_summary_revoke_key(self, *_): + def test_summary_revoke_key(self): """add shareable key""" self.assertTrue("2020" in self.local_user.summary_keys.keys()) diff --git a/bookwyrm/tests/views/test_author.py b/bookwyrm/tests/views/test_author.py index ed65fc30bb..01297a3b85 100644 --- a/bookwyrm/tests/views/test_author.py +++ b/bookwyrm/tests/views/test_author.py @@ -19,19 +19,14 @@ class AuthorViews(TestCase): @classmethod def setUpTestData(cls): """we need basic test data and mocks""" - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - cls.local_user = models.User.objects.create_user( - "mouse@local.com", - "mouse@mouse.com", - "mouseword", - local=True, - localname="mouse", - remote_id="https://example.com/users/mouse", - ) + cls.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.com", + "mouseword", + local=True, + localname="mouse", + remote_id="https://example.com/users/mouse", + ) cls.group = Group.objects.create(name="editor") cls.group.permissions.add( Permission.objects.create( @@ -153,8 +148,7 @@ def test_edit_author(self): request = self.factory.post("", form.data) request.user = self.local_user - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - view(request, author.id) + view(request, author.id) author.refresh_from_db() self.assertEqual(author.name, "New Name") self.assertEqual(author.last_edited_by, self.local_user) diff --git a/bookwyrm/tests/views/test_directory.py b/bookwyrm/tests/views/test_directory.py index 3ea2241542..980b249fae 100644 --- a/bookwyrm/tests/views/test_directory.py +++ b/bookwyrm/tests/views/test_directory.py @@ -1,5 +1,4 @@ """ test for app action functionality """ -from unittest.mock import patch from django.contrib.auth.models import AnonymousUser from django.template.response import TemplateResponse @@ -16,19 +15,14 @@ class DirectoryViews(TestCase): @classmethod def setUpTestData(cls): """we need basic test data and mocks""" - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - cls.local_user = models.User.objects.create_user( - "mouse@local.com", - "mouse@mouse.com", - "mouseword", - local=True, - localname="mouse", - remote_id="https://example.com/users/mouse", - ) + cls.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.com", + "mouseword", + local=True, + localname="mouse", + remote_id="https://example.com/users/mouse", + ) models.SiteSettings.objects.create() def setUp(self): @@ -37,11 +31,7 @@ def setUp(self): self.anonymous_user = AnonymousUser self.anonymous_user.is_authenticated = False - @patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") - @patch("bookwyrm.activitystreams.populate_stream_task.delay") - @patch("bookwyrm.lists_stream.populate_lists_task.delay") - @patch("bookwyrm.suggested_users.rerank_user_task.delay") - def test_directory_page(self, *_): + def test_directory_page(self): """there are so many views, this just makes sure it LOADS""" models.User.objects.create_user( "rat@local.com", diff --git a/bookwyrm/tests/views/test_discover.py b/bookwyrm/tests/views/test_discover.py index 15732b9242..8fdba3f14f 100644 --- a/bookwyrm/tests/views/test_discover.py +++ b/bookwyrm/tests/views/test_discover.py @@ -14,18 +14,13 @@ class DiscoverViews(TestCase): @classmethod def setUpTestData(cls): """we need basic test data and mocks""" - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - cls.local_user = models.User.objects.create_user( - "mouse@local.com", - "mouse@mouse.mouse", - "password", - local=True, - localname="mouse", - ) + cls.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.mouse", + "password", + local=True, + localname="mouse", + ) models.SiteSettings.objects.create() def setUp(self): @@ -47,9 +42,7 @@ def test_discover_page_empty(self): self.assertEqual(result.status_code, 200) validate_html(result.render()) - @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") - @patch("bookwyrm.activitystreams.add_status_task.delay") - def test_discover_page_with_posts(self, *_): + def test_discover_page_with_posts(self): """there are so many views, this just makes sure it LOADS""" view = views.Discover.as_view() request = self.factory.get("") diff --git a/bookwyrm/tests/views/test_feed.py b/bookwyrm/tests/views/test_feed.py index be4956c648..1a4b2c59ae 100644 --- a/bookwyrm/tests/views/test_feed.py +++ b/bookwyrm/tests/views/test_feed.py @@ -12,37 +12,26 @@ from bookwyrm.tests.validate_html import validate_html -@patch("bookwyrm.activitystreams.ActivityStream.get_activity_stream") -@patch("bookwyrm.activitystreams.add_status_task.delay") -@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") -@patch("bookwyrm.activitystreams.populate_stream_task.delay") -@patch("bookwyrm.lists_stream.populate_lists_task.delay") -@patch("bookwyrm.suggested_users.remove_user_task.delay") class FeedViews(TestCase): """activity feed, statuses, dms""" @classmethod def setUpTestData(cls): """we need basic test data and mocks""" - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - cls.local_user = models.User.objects.create_user( - "mouse@local.com", - "mouse@mouse.mouse", - "password", - local=True, - localname="mouse", - ) - cls.another_user = models.User.objects.create_user( - "nutria@local.com", - "nutria@nutria.nutria", - "password", - local=True, - localname="nutria", - ) + cls.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.mouse", + "password", + local=True, + localname="mouse", + ) + cls.another_user = models.User.objects.create_user( + "nutria@local.com", + "nutria@nutria.nutria", + "password", + local=True, + localname="nutria", + ) cls.book = models.Edition.objects.create( parent_work=models.Work.objects.create(title="hi"), title="Example Edition", @@ -55,6 +44,7 @@ def setUp(self): self.factory = RequestFactory() @patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions") + @patch("bookwyrm.activitystreams.ActivityStream.get_activity_stream") def test_feed(self, *_): """there are so many views, this just makes sure it LOADS""" view = views.Feed.as_view() @@ -66,6 +56,7 @@ def test_feed(self, *_): self.assertEqual(result.status_code, 200) @patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions") + @patch("bookwyrm.activitystreams.ActivityStream.get_activity_stream") def test_save_feed_settings(self, *_): """update display preferences""" self.assertEqual( @@ -84,11 +75,10 @@ def test_save_feed_settings(self, *_): self.local_user.refresh_from_db() self.assertEqual(self.local_user.feed_status_types, ["review"]) - def test_status_page(self, *_): + def test_status_page(self): """there are so many views, this just makes sure it LOADS""" view = views.Status.as_view() - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - status = models.Status.objects.create(content="hi", user=self.local_user) + status = models.Status.objects.create(content="hi", user=self.local_user) request = self.factory.get("") request.user = self.local_user with patch("bookwyrm.views.feed.is_api_request") as is_api: @@ -104,7 +94,7 @@ def test_status_page(self, *_): self.assertIsInstance(result, ActivitypubResponse) self.assertEqual(result.status_code, 200) - def test_status_page_not_found(self, *_): + def test_status_page_not_found(self): """there are so many views, this just makes sure it LOADS""" view = views.Status.as_view() @@ -115,7 +105,7 @@ def test_status_page_not_found(self, *_): with self.assertRaises(Http404): view(request, "mouse", 12345) - def test_status_page_not_found_wrong_user(self, *_): + def test_status_page_not_found_wrong_user(self): """there are so many views, this just makes sure it LOADS""" view = views.Status.as_view() another_user = models.User.objects.create_user( @@ -125,8 +115,7 @@ def test_status_page_not_found_wrong_user(self, *_): local=True, localname="rat", ) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - status = models.Status.objects.create(content="hi", user=another_user) + status = models.Status.objects.create(content="hi", user=another_user) request = self.factory.get("") request.user = self.local_user @@ -135,24 +124,21 @@ def test_status_page_not_found_wrong_user(self, *_): with self.assertRaises(Http404): view(request, "mouse", status.id) - def test_status_page_with_image(self, *_): + def test_status_page_with_image(self): """there are so many views, this just makes sure it LOADS""" view = views.Status.as_view() image_path = pathlib.Path(__file__).parent.joinpath( "../../static/images/default_avi.jpg" ) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - status = models.Review.objects.create( - content="hi", - user=self.local_user, - book=self.book, - ) - attachment = models.Image.objects.create( - status=status, caption="alt text here" - ) - with open(image_path, "rb") as image_file: - attachment.image.save("test.jpg", image_file) + status = models.Review.objects.create( + content="hi", + user=self.local_user, + book=self.book, + ) + attachment = models.Image.objects.create(status=status, caption="alt text here") + with open(image_path, "rb") as image_file: + attachment.image.save("test.jpg", image_file) request = self.factory.get("") request.user = self.local_user @@ -169,11 +155,10 @@ def test_status_page_with_image(self, *_): self.assertIsInstance(result, ActivitypubResponse) self.assertEqual(result.status_code, 200) - def test_replies_page(self, *_): + def test_replies_page(self): """there are so many views, this just makes sure it LOADS""" view = views.Replies.as_view() - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - status = models.Status.objects.create(content="hi", user=self.local_user) + status = models.Status.objects.create(content="hi", user=self.local_user) request = self.factory.get("") request.user = self.local_user with patch("bookwyrm.views.feed.is_api_request") as is_api: @@ -189,7 +174,7 @@ def test_replies_page(self, *_): self.assertIsInstance(result, ActivitypubResponse) self.assertEqual(result.status_code, 200) - def test_direct_messages_page(self, *_): + def test_direct_messages_page(self): """there are so many views, this just makes sure it LOADS""" view = views.DirectMessage.as_view() request = self.factory.get("") @@ -199,7 +184,7 @@ def test_direct_messages_page(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - def test_direct_messages_page_user(self, *_): + def test_direct_messages_page_user(self): """there are so many views, this just makes sure it LOADS""" view = views.DirectMessage.as_view() request = self.factory.get("") @@ -210,9 +195,7 @@ def test_direct_messages_page_user(self, *_): self.assertEqual(result.status_code, 200) self.assertEqual(result.context_data["partner"], self.another_user) - @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") - @patch("bookwyrm.activitystreams.add_book_statuses_task.delay") - def test_get_suggested_book(self, *_): + def test_get_suggested_book(self): """gets books the ~*~ algorithm ~*~ thinks you want to post about""" models.ShelfBook.objects.create( book=self.book, diff --git a/bookwyrm/tests/views/test_follow.py b/bookwyrm/tests/views/test_follow.py index c26a9372a9..64c6e2e12e 100644 --- a/bookwyrm/tests/views/test_follow.py +++ b/bookwyrm/tests/views/test_follow.py @@ -12,8 +12,6 @@ from bookwyrm.tests.validate_html import validate_html -@patch("bookwyrm.activitystreams.add_user_statuses_task.delay") -@patch("bookwyrm.lists_stream.add_user_lists_task.delay") class FollowViews(TestCase): """follows""" @@ -21,29 +19,23 @@ class FollowViews(TestCase): def setUpTestData(cls): """we need basic test data and mocks""" models.SiteSettings.objects.create() - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - cls.local_user = models.User.objects.create_user( - "mouse@local.com", - "mouse@mouse.com", - "mouseword", - local=True, - localname="mouse", - remote_id="https://example.com/users/mouse", - ) - with patch("bookwyrm.models.user.set_remote_server"): - cls.remote_user = models.User.objects.create_user( - "rat", - "rat@email.com", - "ratword", - local=False, - remote_id="https://example.com/users/rat", - inbox="https://example.com/users/rat/inbox", - outbox="https://example.com/users/rat/outbox", - ) + cls.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.com", + "mouseword", + local=True, + localname="mouse", + remote_id="https://example.com/users/mouse", + ) + cls.remote_user = models.User.objects.create_user( + "rat", + "rat@email.com", + "ratword", + local=False, + remote_id="https://example.com/users/rat", + inbox="https://example.com/users/rat/inbox", + outbox="https://example.com/users/rat/outbox", + ) cls.group = Group.objects.create(name="editor") cls.group.permissions.add( Permission.objects.create( @@ -63,14 +55,13 @@ def setUp(self): """individual test setup""" self.factory = RequestFactory() - def test_handle_follow_remote(self, *_): + def test_handle_follow_remote(self): """send a follow request""" request = self.factory.post("", {"user": self.remote_user.username}) request.user = self.local_user self.assertEqual(models.UserFollowRequest.objects.count(), 0) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - views.follow(request) + views.follow(request) rel = models.UserFollowRequest.objects.get() @@ -78,65 +69,50 @@ def test_handle_follow_remote(self, *_): self.assertEqual(rel.user_object, self.remote_user) self.assertEqual(rel.status, "follow_request") - def test_handle_follow_local_manually_approves(self, *_): + def test_handle_follow_local_manually_approves(self): """send a follow request""" - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - rat = models.User.objects.create_user( - "rat@local.com", - "rat@rat.com", - "ratword", - local=True, - localname="rat", - remote_id="https://example.com/users/rat", - manually_approves_followers=True, - ) + rat = models.User.objects.create_user( + "rat@local.com", + "rat@rat.com", + "ratword", + local=True, + localname="rat", + remote_id="https://example.com/users/rat", + manually_approves_followers=True, + ) request = self.factory.post("", {"user": rat}) request.user = self.local_user self.assertEqual(models.UserFollowRequest.objects.count(), 0) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - views.follow(request) + views.follow(request) rel = models.UserFollowRequest.objects.get() self.assertEqual(rel.user_subject, self.local_user) self.assertEqual(rel.user_object, rat) self.assertEqual(rel.status, "follow_request") - def test_handle_follow_local(self, *_): + def test_handle_follow_local(self): """send a follow request""" - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - rat = models.User.objects.create_user( - "rat@local.com", - "rat@rat.com", - "ratword", - local=True, - localname="rat", - remote_id="https://example.com/users/rat", - ) + rat = models.User.objects.create_user( + "rat@local.com", + "rat@rat.com", + "ratword", + local=True, + localname="rat", + remote_id="https://example.com/users/rat", + ) request = self.factory.post("", {"user": rat}) request.user = self.local_user self.assertEqual(models.UserFollowRequest.objects.count(), 0) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - views.follow(request) - + views.follow(request) rel = models.UserFollows.objects.get() self.assertEqual(rel.user_subject, self.local_user) self.assertEqual(rel.user_object, rat) self.assertEqual(rel.status, "follows") - @patch("bookwyrm.activitystreams.remove_user_statuses_task.delay") - @patch("bookwyrm.lists_stream.remove_user_lists_task.delay") - def test_handle_unfollow(self, *_): + def test_handle_unfollow(self): """send an unfollow""" request = self.factory.post("", {"user": self.remote_user.username}) request.user = self.local_user @@ -152,7 +128,7 @@ def test_handle_unfollow(self, *_): self.assertEqual(self.remote_user.followers.count(), 0) - def test_handle_accept(self, *_): + def test_handle_accept(self): """accept a follow request""" self.local_user.manually_approves_followers = True self.local_user.save( @@ -164,14 +140,13 @@ def test_handle_accept(self, *_): user_subject=self.remote_user, user_object=self.local_user ) - with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): - views.accept_follow_request(request) + views.accept_follow_request(request) # request should be deleted self.assertEqual(models.UserFollowRequest.objects.filter(id=rel.id).count(), 0) # follow relationship should exist self.assertEqual(self.local_user.followers.first(), self.remote_user) - def test_handle_reject(self, *_): + def test_handle_reject(self): """reject a follow request""" self.local_user.manually_approves_followers = True self.local_user.save( @@ -197,7 +172,7 @@ def test_handle_reject(self, *_): # follow relationship should not exist self.assertEqual(models.UserFollows.objects.filter(id=rel.id).count(), 0) - def test_handle_reject_existing(self, *_): + def test_handle_reject_existing(self): """reject a follow previously approved""" request = self.factory.post("", {"user": self.remote_user.username}) request.user = self.local_user @@ -216,7 +191,7 @@ def test_handle_reject_existing(self, *_): # follow relationship should not exist self.assertEqual(models.UserFollows.objects.filter(id=rel.id).count(), 0) - def test_ostatus_follow_request(self, *_): + def test_ostatus_follow_request(self): """check ostatus subscribe template loads""" request = self.factory.get( "", {"acct": "https%3A%2F%2Fexample.com%2Fusers%2Frat"} @@ -227,7 +202,7 @@ def test_ostatus_follow_request(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - def test_remote_follow_page(self, *_): + def test_remote_follow_page(self): """check remote follow page loads""" request = self.factory.get("", {"acct": "mouse@local.com"}) request.user = self.remote_user @@ -236,7 +211,7 @@ def test_remote_follow_page(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - def test_ostatus_follow_success(self, *_): + def test_ostatus_follow_success(self): """check remote follow success page loads""" request = self.factory.get("") request.user = self.remote_user @@ -246,7 +221,7 @@ def test_ostatus_follow_success(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - def test_remote_follow(self, *_): + def test_remote_follow(self): """check follow from remote page loads""" request = self.factory.post("", {"user": self.remote_user.id}) request.user = self.remote_user diff --git a/bookwyrm/tests/views/test_status.py b/bookwyrm/tests/views/test_status.py index 52582a2357..5ed1852d21 100644 --- a/bookwyrm/tests/views/test_status.py +++ b/bookwyrm/tests/views/test_status.py @@ -12,33 +12,27 @@ from bookwyrm.tests.validate_html import validate_html -@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") class StatusTransactions(TransactionTestCase): """Test full database transactions""" def setUp(self): """we need basic test data and mocks""" self.factory = RequestFactory() - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - self.local_user = models.User.objects.create_user( - "mouse@local.com", - "mouse@mouse.com", - "mouseword", - local=True, - localname="mouse", - remote_id="https://example.com/users/mouse", - ) - self.another_user = models.User.objects.create_user( - f"nutria@{DOMAIN}", - "nutria@nutria.com", - "password", - local=True, - localname="nutria", - ) + self.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.com", + "mouseword", + local=True, + localname="mouse", + remote_id="https://example.com/users/mouse", + ) + self.another_user = models.User.objects.create_user( + f"nutria@{DOMAIN}", + "nutria@nutria.com", + "password", + local=True, + localname="nutria", + ) work = models.Work.objects.create(title="Test Work") self.book = models.Edition.objects.create( @@ -47,7 +41,7 @@ def setUp(self): parent_work=work, ) - def test_create_status_saves(self, *_): + def test_create_status_saves(self): """This view calls save multiple times""" view = views.CreateStatus.as_view() form = forms.CommentForm( @@ -67,11 +61,6 @@ def test_create_status_saves(self, *_): self.assertEqual(mock.call_count, 1) -@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") -@patch("bookwyrm.activitystreams.populate_stream_task.delay") -@patch("bookwyrm.lists_stream.populate_lists_task.delay") -@patch("bookwyrm.activitystreams.remove_status_task.delay") -@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") # pylint: disable=too-many-public-methods class StatusViews(TestCase): """viewing and creating statuses""" @@ -79,37 +68,31 @@ class StatusViews(TestCase): @classmethod def setUpTestData(cls): """we need basic test data and mocks""" - with ( - patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), - patch("bookwyrm.activitystreams.populate_stream_task.delay"), - patch("bookwyrm.lists_stream.populate_lists_task.delay"), - ): - cls.local_user = models.User.objects.create_user( - "mouse@local.com", - "mouse@mouse.com", - "mouseword", - local=True, - localname="mouse", - remote_id="https://example.com/users/mouse", - ) - cls.another_user = models.User.objects.create_user( - f"nutria@{DOMAIN}", - "nutria@nutria.com", - "password", - local=True, - localname="nutria", - ) - cls.existing_hashtag = models.Hashtag.objects.create(name="#existing") - with patch("bookwyrm.models.user.set_remote_server"): - cls.remote_user = models.User.objects.create_user( - "rat", - "rat@email.com", - "ratword", - local=False, - remote_id="https://example.com/users/rat", - inbox="https://example.com/users/rat/inbox", - outbox="https://example.com/users/rat/outbox", - ) + cls.local_user = models.User.objects.create_user( + "mouse@local.com", + "mouse@mouse.com", + "mouseword", + local=True, + localname="mouse", + remote_id="https://example.com/users/mouse", + ) + cls.another_user = models.User.objects.create_user( + f"nutria@{DOMAIN}", + "nutria@nutria.com", + "password", + local=True, + localname="nutria", + ) + cls.existing_hashtag = models.Hashtag.objects.create(name="#existing") + cls.remote_user = models.User.objects.create_user( + "rat", + "rat@email.com", + "ratword", + local=False, + remote_id="https://example.com/users/rat", + inbox="https://example.com/users/rat/inbox", + outbox="https://example.com/users/rat/outbox", + ) work = models.Work.objects.create(title="Test Work") cls.book = models.Edition.objects.create( title="Example Edition", @@ -122,7 +105,7 @@ def setUp(self): """individual test setup""" self.factory = RequestFactory() - def test_create_status_comment(self, *_): + def test_create_status_comment(self): """create a status""" view = views.CreateStatus.as_view() form = forms.CommentForm( @@ -145,7 +128,7 @@ def test_create_status_comment(self, *_): self.assertEqual(status.book, self.book) self.assertIsNone(status.edited_date) - def test_create_status_rating(self, *_): + def test_create_status_rating(self): """create a status""" view = views.CreateStatus.as_view() form = forms.RatingForm( @@ -167,7 +150,7 @@ def test_create_status_rating(self, *_): self.assertEqual(status.rating, 4.0) self.assertIsNone(status.edited_date) - def test_create_status_wrong_user(self, *_): + def test_create_status_wrong_user(self): """You can't compose statuses for someone else""" view = views.CreateStatus.as_view() form = forms.CommentForm( @@ -183,7 +166,7 @@ def test_create_status_wrong_user(self, *_): with self.assertRaises(PermissionDenied): view(request, "comment") - def test_create_status_reply(self, *_): + def test_create_status_reply(self): """create a status in reply to an existing status""" view = views.CreateStatus.as_view() user = models.User.objects.create_user( @@ -210,7 +193,7 @@ def test_create_status_reply(self, *_): self.assertEqual(status.user, user) self.assertEqual(models.Notification.objects.get().user, self.local_user) - def test_create_status_mentions(self, *_): + def test_create_status_mentions(self): """@mention a user in a post""" view = views.CreateStatus.as_view() user = models.User.objects.create_user( @@ -240,7 +223,7 @@ def test_create_status_mentions(self, *_): status.content, f'

hi @rat

' ) - def test_create_status_reply_with_mentions(self, *_): + def test_create_status_reply_with_mentions(self): """reply to a post with an @mention'd user""" view = views.CreateStatus.as_view() user = models.User.objects.create_user( @@ -280,7 +263,7 @@ def test_create_status_reply_with_mentions(self, *_): self.assertFalse(self.remote_user in reply.mention_users.all()) self.assertTrue(self.local_user in reply.mention_users.all()) - def test_find_mentions_local(self, *_): + def test_find_mentions_local(self): """detect and look up @ mentions of users""" result = find_mentions(self.local_user, "@nutria") self.assertEqual(result["@nutria"], self.another_user) @@ -300,14 +283,14 @@ def test_find_mentions_local(self, *_): self.assertEqual(find_mentions(self.local_user, "leading@nutria"), {}) - def test_find_mentions_remote(self, *_): + def test_find_mentions_remote(self): """detect and look up @ mentions of users""" self.assertEqual( find_mentions(self.local_user, "@rat@example.com"), {"@rat@example.com": self.remote_user}, ) - def test_find_mentions_multiple(self, *_): + def test_find_mentions_multiple(self): """detect and look up @ mentions of users""" multiple = find_mentions(self.local_user, "@nutria and @rat@example.com") self.assertEqual(multiple["@nutria"], self.another_user) @@ -315,20 +298,20 @@ def test_find_mentions_multiple(self, *_): self.assertEqual(multiple["@rat@example.com"], self.remote_user) self.assertIsNone(multiple.get("@rat")) - def test_find_mentions_unknown(self, *_): + def test_find_mentions_unknown(self): """detect and look up @ mentions of users""" multiple = find_mentions(self.local_user, "@nutria and @rdkjfgh") self.assertEqual(multiple["@nutria"], self.another_user) self.assertEqual(multiple[f"@nutria@{DOMAIN}"], self.another_user) - def test_find_mentions_blocked(self, *_): + def test_find_mentions_blocked(self): """detect and look up @ mentions of users""" self.another_user.blocks.add(self.local_user) result = find_mentions(self.local_user, "@nutria hello") self.assertEqual(result, {}) - def test_find_mentions_unknown_remote(self, *_): + def test_find_mentions_unknown_remote(self): """mention a user that isn't in the database""" with patch("bookwyrm.views.status.handle_remote_webfinger") as rwf: rwf.return_value = self.another_user @@ -341,7 +324,7 @@ def test_find_mentions_unknown_remote(self, *_): result = find_mentions(self.local_user, "@beep@beep.com") self.assertEqual(result, {}) - def test_create_status_hashtags(self, *_): + def test_create_status_hashtags(self): """#mention a hashtag in a post""" view = views.CreateStatus.as_view() form = forms.CommentForm( @@ -374,7 +357,7 @@ def test_create_status_hashtags(self, *_): + "#NewTag.

", ) - def test_find_or_create_hashtags(self, *_): + def test_find_or_create_hashtags(self): """detect and look up #hashtags""" result = find_or_create_hashtags("no hashtag to be found here") self.assertEqual(result, {}) @@ -406,7 +389,7 @@ def test_find_or_create_hashtags(self, *_): hashtag = models.Hashtag.objects.filter(name="#ひぐま").first() self.assertEqual(result["#ひぐま"], hashtag) - def test_format_links_simple_url(self, *_): + def test_format_links_simple_url(self): """find and format urls into a tags""" url = "http://www.fish.com/" self.assertEqual( @@ -417,7 +400,7 @@ def test_format_links_simple_url(self, *_): f'(www.fish.com/)', ) - def test_format_links_paragraph_break(self, *_): + def test_format_links_paragraph_break(self): """find and format urls into a tags""" url = """okay @@ -427,7 +410,7 @@ def test_format_links_paragraph_break(self, *_): 'okay\n\nwww.fish.com/', ) - def test_format_links_punctuation(self, *_): + def test_format_links_punctuation(self): """test many combinations of brackets, URLs, and punctuation""" url = "https://bookwyrm.social" html = f'bookwyrm.social' @@ -447,7 +430,7 @@ def test_format_links_punctuation(self, *_): with self.subTest(desc=desc): self.assertEqual(views.status.format_links(text), output) - def test_format_links_special_chars(self, *_): + def test_format_links_special_chars(self): """find and format urls into a tags""" url = "https://archive.org/details/dli.granth.72113/page/n25/mode/2up" self.assertEqual( @@ -475,14 +458,14 @@ def test_format_links_special_chars(self, *_): views.status.format_links(url), f'{url[8:]}' ) - def test_format_links_ignore_non_urls(self, *_): + def test_format_links_ignore_non_urls(self): """formating links should leave plain text untouced""" text_elision = "> “The distinction is significant.” [...]" # bookwyrm#2993 text_quoteparens = "some kind of gene-editing technology (?)" # bookwyrm#3049 self.assertEqual(views.status.format_links(text_elision), text_elision) self.assertEqual(views.status.format_links(text_quoteparens), text_quoteparens) - def test_format_mentions_with_at_symbol_links(self, *_): + def test_format_mentions_with_at_symbol_links(self): """A link with an @username shouldn't treat the username as a mention""" content = "a link to https://example.com/user/@mouse" mentions = views.status.find_mentions(self.local_user, content) @@ -491,7 +474,7 @@ def test_format_mentions_with_at_symbol_links(self, *_): "a link to https://example.com/user/@mouse", ) - def test_format_hashtag_with_pound_symbol_links(self, *_): + def test_format_hashtag_with_pound_symbol_links(self): """A link with an @username shouldn't treat the username as a mention""" content = "a link to https://example.com/page#anchor" hashtags = views.status.find_or_create_hashtags(content) @@ -500,7 +483,7 @@ def test_format_hashtag_with_pound_symbol_links(self, *_): "a link to https://example.com/page#anchor", ) - def test_to_markdown(self, *_): + def test_to_markdown(self): """this is mostly handled in other places, but nonetheless""" text = "_hi_ and http://fish.com is rad" result = views.status.to_markdown(text) @@ -509,7 +492,7 @@ def test_to_markdown(self, *_): '

hi and fish.com is rad

', ) - def test_to_markdown_detect_url(self, *_): + def test_to_markdown_detect_url(self): """this is mostly handled in other places, but nonetheless""" text = "http://fish.com/@hello#okay" result = views.status.to_markdown(text) @@ -518,17 +501,17 @@ def test_to_markdown_detect_url(self, *_): '

fish.com/@hello#okay

', ) - def test_to_markdown_link(self, *_): + def test_to_markdown_link(self): """this is mostly handled in other places, but nonetheless""" text = "[hi](http://fish.com) is rad" result = views.status.to_markdown(text) self.assertEqual(result, '

hi is rad

') - def test_delete_status(self, mock, *_): + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") + def test_delete_status(self, mock): """marks a status as deleted""" view = views.DeleteStatus.as_view() - with patch("bookwyrm.activitystreams.add_status_task.delay"): - status = models.Status.objects.create(user=self.local_user, content="hi") + status = models.Status.objects.create(user=self.local_user, content="hi") self.assertFalse(status.deleted) request = self.factory.post("") request.user = self.local_user @@ -542,11 +525,10 @@ def test_delete_status(self, mock, *_): status.refresh_from_db() self.assertTrue(status.deleted) - def test_delete_status_permission_denied(self, *_): + def test_delete_status_permission_denied(self): """marks a status as deleted""" view = views.DeleteStatus.as_view() - with patch("bookwyrm.activitystreams.add_status_task.delay"): - status = models.Status.objects.create(user=self.local_user, content="hi") + status = models.Status.objects.create(user=self.local_user, content="hi") self.assertFalse(status.deleted) request = self.factory.post("") request.user = self.remote_user @@ -557,11 +539,11 @@ def test_delete_status_permission_denied(self, *_): status.refresh_from_db() self.assertFalse(status.deleted) - def test_delete_status_moderator(self, mock, *_): + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") + def test_delete_status_moderator(self, mock): """marks a status as deleted""" view = views.DeleteStatus.as_view() - with patch("bookwyrm.activitystreams.add_status_task.delay"): - status = models.Status.objects.create(user=self.local_user, content="hi") + status = models.Status.objects.create(user=self.local_user, content="hi") self.assertFalse(status.deleted) request = self.factory.post("") request.user = self.remote_user @@ -576,7 +558,7 @@ def test_delete_status_moderator(self, mock, *_): status.refresh_from_db() self.assertTrue(status.deleted) - def test_edit_status_get(self, *_): + def test_edit_status_get(self): """load the edit status view""" view = views.EditStatus.as_view() status = models.Comment.objects.create( @@ -589,7 +571,7 @@ def test_edit_status_get(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - def test_edit_status_get_reply(self, *_): + def test_edit_status_get_reply(self): """load the edit status view""" view = views.EditStatus.as_view() parent = models.Comment.objects.create( @@ -605,7 +587,8 @@ def test_edit_status_get_reply(self, *_): validate_html(result.render()) self.assertEqual(result.status_code, 200) - def test_edit_status_success(self, mock, *_): + @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") + def test_edit_status_success(self, mock): """update an existing status""" status = models.Status.objects.create(content="status", user=self.local_user) self.assertIsNone(status.edited_date) @@ -630,7 +613,7 @@ def test_edit_status_success(self, mock, *_): self.assertEqual(status.content, "

hi

") self.assertIsNotNone(status.edited_date) - def test_edit_status_permission_denied(self, *_): + def test_edit_status_permission_denied(self): """update an existing status""" status = models.Status.objects.create(content="status", user=self.local_user) view = views.CreateStatus.as_view()