Skip to content

Commit

Permalink
celery inmem test suite: tests/views/{inbox,landing,lists,preferences}
Browse files Browse the repository at this point in the history
  • Loading branch information
dato committed Jul 29, 2024
1 parent c8873bd commit 2133c41
Show file tree
Hide file tree
Showing 28 changed files with 739 additions and 1,098 deletions.
49 changes: 20 additions & 29 deletions bookwyrm/tests/views/inbox/test_inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,24 @@ def setUp(self):
@classmethod
def setUpTestData(cls):
"""basic user and book data"""
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
local_user = models.User.objects.create_user(
"mouse@example.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
)
local_user = models.User.objects.create_user(
"mouse@example.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
)
local_user.remote_id = "https://example.com/user/mouse"
local_user.save(broadcast=False, update_fields=["remote_id"])
with patch("bookwyrm.models.user.set_remote_server.delay"):
cls.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.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.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.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",
)
models.SiteSettings.objects.create()

def test_inbox_invalid_get(self):
Expand Down Expand Up @@ -122,11 +116,9 @@ def test_inbox_success(self):
}
with patch("bookwyrm.views.inbox.has_valid_signature") as mock_valid:
mock_valid.return_value = True

with patch("bookwyrm.views.inbox.activity_task.apply_async"):
result = self.client.post(
"/inbox", json.dumps(activity), content_type="application/json"
)
result = self.client.post(
"/inbox", json.dumps(activity), content_type="application/json"
)
self.assertEqual(result.status_code, 200)

def test_is_blocked_user_agent(self):
Expand Down Expand Up @@ -157,8 +149,7 @@ def test_is_blocked_activity(self):
with self.assertRaises(PermissionDenied):
views.inbox.raise_is_blocked_activity(activity)

@patch("bookwyrm.suggested_users.remove_user_task.delay")
def test_create_by_deactivated_user(self, _):
def test_create_by_deactivated_user(self):
"""don't let deactivated users post"""
self.remote_user.delete(broadcast=False)
self.assertTrue(self.remote_user.deleted)
Expand Down
41 changes: 16 additions & 25 deletions bookwyrm/tests/views/inbox/test_inbox_add.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
""" tests incoming activities"""
from unittest.mock import patch

from django.test import TestCase
import responses

Expand All @@ -13,31 +11,24 @@ class InboxAdd(TestCase):
@classmethod
def setUpTestData(cls):
"""basic user and book data"""
with (
patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"),
patch("bookwyrm.activitystreams.populate_stream_task.delay"),
patch("bookwyrm.lists_stream.populate_lists_task.delay"),
):
local_user = models.User.objects.create_user(
"mouse@example.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
)
local_user = models.User.objects.create_user(
"mouse@example.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
)
local_user.remote_id = "https://example.com/user/mouse"
local_user.save(broadcast=False, update_fields=["remote_id"])
with patch("bookwyrm.models.user.set_remote_server.delay"):
cls.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.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.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.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="work title")
cls.book = models.Edition.objects.create(
title="Test",
Expand Down
67 changes: 25 additions & 42 deletions bookwyrm/tests/views/inbox/test_inbox_announce.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,29 @@ class InboxActivities(TestCase):
@classmethod
def setUpTestData(cls):
"""basic user and book data"""
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@example.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
)
cls.local_user = models.User.objects.create_user(
"mouse@example.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
)
cls.local_user.remote_id = "https://example.com/user/mouse"
cls.local_user.save(broadcast=False, update_fields=["remote_id"])
with patch("bookwyrm.models.user.set_remote_server.delay"):
cls.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.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",
)

with (
patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"),
patch("bookwyrm.activitystreams.add_status_task.delay"),
):
cls.status = models.Status.objects.create(
user=cls.local_user,
content="Test status",
remote_id="https://example.com/status/1",
)
cls.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.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.status = models.Status.objects.create(
user=cls.local_user,
content="Test status",
remote_id="https://example.com/status/1",
)

models.SiteSettings.objects.create()

Expand All @@ -61,8 +50,7 @@ def setUp(self):
"object": {},
}

@patch("bookwyrm.activitystreams.handle_boost_task.delay")
def test_boost(self, _):
def test_boost(self):
"""boost a status"""
self.assertEqual(models.Notification.objects.count(), 0)
activity = {
Expand All @@ -88,8 +76,7 @@ def test_boost(self, _):
self.assertEqual(notification.related_status, self.status)

@responses.activate
@patch("bookwyrm.activitystreams.handle_boost_task.delay")
def test_boost_remote_status(self, _):
def test_boost_remote_status(self):
"""boost a status from a remote server"""
work = models.Work.objects.create(title="work title")
book = models.Edition.objects.create(
Expand Down Expand Up @@ -143,8 +130,7 @@ def test_discarded_boost(self):
content="hi",
user=self.remote_user,
)
with patch("bookwyrm.activitystreams.add_status_task.delay"):
status.save(broadcast=False)
status.save(broadcast=False)
activity = {
"type": "Announce",
"id": "http://www.faraway.com/boost/12",
Expand All @@ -160,10 +146,7 @@ def test_discarded_boost(self):
views.inbox.activity_task(activity)
self.assertEqual(models.Boost.objects.count(), 0)

@patch("bookwyrm.activitystreams.add_status_task.delay")
@patch("bookwyrm.activitystreams.handle_boost_task.delay")
@patch("bookwyrm.activitystreams.remove_status_task.delay")
def test_unboost(self, *_):
def test_unboost(self):
"""undo a boost"""
boost = models.Boost.objects.create(
boosted_status=self.status, user=self.remote_user
Expand Down
59 changes: 23 additions & 36 deletions bookwyrm/tests/views/inbox/test_inbox_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,33 @@ class InboxBlock(TestCase):
@classmethod
def setUpTestData(cls):
"""basic user and book data"""
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@example.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
)
cls.local_user = models.User.objects.create_user(
"mouse@example.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
)
cls.local_user.remote_id = "https://example.com/user/mouse"
cls.local_user.save(broadcast=False, update_fields=["remote_id"])
with patch("bookwyrm.models.user.set_remote_server.delay"):
cls.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.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.remote_user = models.User.objects.create_user(
"rat",
"rat@rat.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",
)

models.SiteSettings.objects.create()

def test_handle_blocks(self):
"""create a "block" database entry from an activity"""
self.local_user.followers.add(self.remote_user)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
models.UserFollowRequest.objects.create(
user_subject=self.local_user, user_object=self.remote_user
)
models.UserFollowRequest.objects.create(
user_subject=self.local_user, user_object=self.remote_user
)
self.assertTrue(models.UserFollows.objects.exists())
self.assertTrue(models.UserFollowRequest.objects.exists())

Expand All @@ -57,12 +50,9 @@ def test_handle_blocks(self):
"object": "https://example.com/user/mouse",
}

with (
patch(
"bookwyrm.activitystreams.remove_user_statuses_task.delay"
) as redis_mock,
patch("bookwyrm.lists_stream.remove_user_lists_task.delay"),
):
with patch(
"bookwyrm.activitystreams.remove_user_statuses_task.delay"
) as redis_mock:
views.inbox.activity_task(activity)
self.assertTrue(redis_mock.called)
views.inbox.activity_task(activity)
Expand All @@ -74,10 +64,7 @@ def test_handle_blocks(self):
self.assertFalse(models.UserFollows.objects.exists())
self.assertFalse(models.UserFollowRequest.objects.exists())

@patch("bookwyrm.activitystreams.remove_user_statuses_task.delay")
@patch("bookwyrm.lists_stream.add_user_lists_task.delay")
@patch("bookwyrm.lists_stream.remove_user_lists_task.delay")
def test_handle_unblock(self, *_):
def test_handle_unblock(self):
"""unblock a user"""
self.remote_user.blocks.add(self.local_user)

Expand Down
Loading

0 comments on commit 2133c41

Please sign in to comment.