Skip to content

Commit

Permalink
celery inmem test suite: tests/{activitypub,activitystreams,lists_str…
Browse files Browse the repository at this point in the history
…eam}
  • Loading branch information
dato committed Jul 29, 2024
1 parent 2133c41 commit d0212c5
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 473 deletions.
81 changes: 30 additions & 51 deletions bookwyrm/tests/activitypub/test_base_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,15 @@
from bookwyrm import models


@patch("bookwyrm.activitystreams.add_status_task.delay")
@patch("bookwyrm.suggested_users.rerank_user_task.delay")
@patch("bookwyrm.suggested_users.remove_user_task.delay")
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
class BaseActivity(TestCase):
"""the super class for model-linked activitypub dataclasses"""

@classmethod
def setUpTestData(cls):
"""we're probably going to re-use this so why copy/paste"""
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.user = models.User.objects.create_user(
"mouse", "[email protected]", "mouseword", local=True, localname="mouse"
)
cls.user = models.User.objects.create_user(
"mouse", "[email protected]", "mouseword", local=True, localname="mouse"
)
cls.user.remote_id = "http://example.com/a/b"
cls.user.save(broadcast=False, update_fields=["remote_id"])

Expand All @@ -52,29 +42,29 @@ def setUp(self):
with open(image_path, "rb") as image_file:
self.image_data = image_file.read()

def test_get_representative_not_existing(self, *_):
def test_get_representative_not_existing(self):
"""test that an instance representative actor is created if it does not exist"""
representative = get_representative()
self.assertIsInstance(representative, models.User)

def test_init(self, *_):
def test_init(self):
"""simple successfully init"""
instance = ActivityObject(id="a", type="b")
self.assertTrue(hasattr(instance, "id"))
self.assertTrue(hasattr(instance, "type"))

def test_init_missing(self, *_):
def test_init_missing(self):
"""init with missing required params"""
with self.assertRaises(ActivitySerializerError):
ActivityObject()

def test_init_extra_fields(self, *_):
def test_init_extra_fields(self):
"""init ignoring additional fields"""
instance = ActivityObject(id="a", type="b", fish="c")
self.assertTrue(hasattr(instance, "id"))
self.assertTrue(hasattr(instance, "type"))

def test_init_default_field(self, *_):
def test_init_default_field(self):
"""replace an existing required field with a default field"""

@dataclass(init=False)
Expand All @@ -87,7 +77,7 @@ class TestClass(ActivityObject):
self.assertEqual(instance.id, "a")
self.assertEqual(instance.type, "TestObject")

def test_serialize(self, *_):
def test_serialize(self):
"""simple function for converting dataclass to dict"""
instance = ActivityObject(id="a", type="b")
serialized = instance.serialize()
Expand All @@ -96,7 +86,7 @@ def test_serialize(self, *_):
self.assertEqual(serialized["type"], "b")

@responses.activate
def test_resolve_remote_id(self, *_):
def test_resolve_remote_id(self):
"""look up or load remote data"""
# existing item
result = resolve_remote_id("http://example.com/a/b", model=models.User)
Expand All @@ -110,22 +100,19 @@ def test_resolve_remote_id(self, *_):
status=200,
)

with patch("bookwyrm.models.user.set_remote_server.delay"):
result = resolve_remote_id(
"https://example.com/user/mouse", model=models.User
)
result = resolve_remote_id("https://example.com/user/mouse", model=models.User)
self.assertIsInstance(result, models.User)
self.assertEqual(result.remote_id, "https://example.com/user/mouse")
self.assertEqual(result.name, "MOUSE?? MOUSE!!")

def test_to_model_invalid_model(self, *_):
def test_to_model_invalid_model(self):
"""catch mismatch between activity type and model type"""
instance = ActivityObject(id="a", type="b")
with self.assertRaises(ActivitySerializerError):
instance.to_model(model=models.User)

@responses.activate
def test_to_model_image(self, *_):
def test_to_model_image(self):
"""update an image field"""
activity = activitypub.Person(
id=self.user.remote_id,
Expand All @@ -151,20 +138,17 @@ def test_to_model_image(self, *_):
with self.assertRaises(ValueError):
self.user.avatar.file # pylint: disable=pointless-statement

# this would trigger a broadcast because it's a local user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
activity.to_model(model=models.User, instance=self.user)
activity.to_model(model=models.User, instance=self.user)
self.assertIsNotNone(self.user.avatar.file)
self.assertEqual(self.user.name, "New Name")
self.assertEqual(self.user.key_pair.public_key, "hi")

def test_to_model_many_to_many(self, *_):
def test_to_model_many_to_many(self):
"""annoying that these all need special handling"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
status = models.Status.objects.create(
content="test status",
user=self.user,
)
status = models.Status.objects.create(
content="test status",
user=self.user,
)
book = models.Edition.objects.create(
title="Test Edition", remote_id="http://book.com/book"
)
Expand Down Expand Up @@ -198,14 +182,13 @@ def test_to_model_many_to_many(self, *_):
self.assertEqual(status.mention_hashtags.first(), hashtag)

@responses.activate
def test_to_model_one_to_many(self, *_):
def test_to_model_one_to_many(self):
"""these are reversed relationships, where the secondary object
keys the primary object but not vice versa"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
status = models.Status.objects.create(
content="test status",
user=self.user,
)
status = models.Status.objects.create(
content="test status",
user=self.user,
)
update_data = activitypub.Note(
id=status.remote_id,
content=status.content,
Expand All @@ -230,22 +213,18 @@ def test_to_model_one_to_many(self, *_):
)

# sets the celery task call to the function call
with (
patch("bookwyrm.activitypub.base_activity.set_related_field.delay"),
patch("bookwyrm.models.status.Status.ignore_activity") as discarder,
):
with patch("bookwyrm.models.status.Status.ignore_activity") as discarder:
discarder.return_value = False
update_data.to_model(model=models.Status, instance=status)
self.assertIsNone(status.attachments.first())

@responses.activate
def test_set_related_field(self, *_):
def test_set_related_field(self):
"""celery task to add back-references to created objects"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
status = models.Status.objects.create(
content="test status",
user=self.user,
)
status = models.Status.objects.create(
content="test status",
user=self.user,
)
data = {
"url": "http://www.example.com/image.jpg",
"name": "alt text",
Expand Down
13 changes: 3 additions & 10 deletions bookwyrm/tests/activitypub/test_note.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
""" tests functionality specifically for the Note ActivityPub dataclass"""
from unittest.mock import patch

from django.test import TestCase

from bookwyrm import activitypub
Expand All @@ -13,14 +11,9 @@ class Note(TestCase):
@classmethod
def setUpTestData(cls):
"""create a shared user"""
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.user = models.User.objects.create_user(
"mouse", "[email protected]", "mouseword", local=True, localname="mouse"
)
cls.user = models.User.objects.create_user(
"mouse", "[email protected]", "mouseword", local=True, localname="mouse"
)
cls.user.remote_id = "https://test-instance.org/user/critic"
cls.user.save(broadcast=False, update_fields=["remote_id"])

Expand Down
4 changes: 1 addition & 3 deletions bookwyrm/tests/activitypub/test_person.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# pylint: disable=missing-module-docstring, missing-class-docstring, missing-function-docstring
import json
import pathlib
from unittest.mock import patch
from django.test import TestCase

from bookwyrm import activitypub, models
Expand All @@ -20,8 +19,7 @@ def test_load_user_data(self):

def test_user_to_model(self):
activity = activitypub.Person(**self.user_data)
with patch("bookwyrm.models.user.set_remote_server.delay"):
user = activity.to_model(model=models.User)
user = activity.to_model(model=models.User)
self.assertEqual(user.username, "[email protected]")
self.assertEqual(user.remote_id, "https://example.com/user/mouse")
self.assertFalse(user.local)
Expand Down
20 changes: 9 additions & 11 deletions bookwyrm/tests/activitypub/test_quotation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
""" quotation activity object serializer class """
import json
import pathlib
from unittest.mock import patch

from django.test import TestCase
from bookwyrm import activitypub, models
Expand All @@ -13,16 +12,15 @@ class Quotation(TestCase):
@classmethod
def setUpTestData(cls):
"""model objects we'll need"""
with patch("bookwyrm.models.user.set_remote_server.delay"):
cls.user = models.User.objects.create_user(
"mouse",
"[email protected]",
"mouseword",
local=False,
inbox="https://example.com/user/mouse/inbox",
outbox="https://example.com/user/mouse/outbox",
remote_id="https://example.com/user/mouse",
)
cls.user = models.User.objects.create_user(
"mouse",
"[email protected]",
"mouseword",
local=False,
inbox="https://example.com/user/mouse/inbox",
outbox="https://example.com/user/mouse/outbox",
remote_id="https://example.com/user/mouse",
)
cls.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
Expand Down
Loading

0 comments on commit d0212c5

Please sign in to comment.