Signals tests
This commit is contained in:
parent
157d891681
commit
3bf1121fa6
|
@ -5,7 +5,7 @@ from django.db.models import signals, Count, Q
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
from bookwyrm.redis_store import RedisStore
|
from bookwyrm.redis_store import RedisStore
|
||||||
from bookwyrm.tasks import app, LOW, MEDIUM, HIGH
|
from bookwyrm.tasks import app, MEDIUM, HIGH
|
||||||
|
|
||||||
|
|
||||||
class ListsStream(RedisStore):
|
class ListsStream(RedisStore):
|
||||||
|
@ -98,31 +98,22 @@ class ListsStream(RedisStore):
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def add_list_on_create(sender, instance, created, *args, **kwargs):
|
def add_list_on_create(sender, instance, created, *args, **kwargs):
|
||||||
"""add newly created lists to activity feeds"""
|
"""add newly created lists to activity feeds"""
|
||||||
|
if not created:
|
||||||
|
return
|
||||||
# when creating new things, gotta wait on the transaction
|
# when creating new things, gotta wait on the transaction
|
||||||
transaction.on_commit(lambda: add_list_on_create_command(instance, created))
|
transaction.on_commit(lambda: add_list_on_create_command(instance))
|
||||||
|
|
||||||
|
|
||||||
@receiver(signals.pre_delete, sender=models.List)
|
@receiver(signals.pre_delete, sender=models.List)
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def remove_list_on_delete(sender, instance, created, *args, **kwargs):
|
def remove_list_on_delete(sender, instance, *args, **kwargs):
|
||||||
"""add newly created lists to activity feeds"""
|
"""add newly created lists to activity feeds"""
|
||||||
remove_list_task.delay(instance.id)
|
remove_list_task.delay(instance.id)
|
||||||
|
|
||||||
|
|
||||||
def add_list_on_create_command(instance, created):
|
def add_list_on_create_command(instance):
|
||||||
"""runs this code only after the database commit completes"""
|
"""runs this code only after the database commit completes"""
|
||||||
priority = HIGH
|
add_list_task.delay(instance.id)
|
||||||
# check if this is an old list, de-prioritize if so
|
|
||||||
# (this will happen if federation is very slow, or, more expectedly, on csv import)
|
|
||||||
one_day = 60 * 60 * 24
|
|
||||||
if (instance.created_date - instance.published_date).seconds > one_day:
|
|
||||||
priority = LOW
|
|
||||||
|
|
||||||
add_list_task.apply_async(
|
|
||||||
args=(instance.id,),
|
|
||||||
kwargs={"increment_unread": created},
|
|
||||||
queue=priority,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@receiver(signals.post_save, sender=models.UserFollows)
|
@receiver(signals.post_save, sender=models.UserFollows)
|
||||||
|
@ -188,17 +179,17 @@ def add_lists_on_unblock(sender, instance, *args, **kwargs):
|
||||||
|
|
||||||
@receiver(signals.post_save, sender=models.User)
|
@receiver(signals.post_save, sender=models.User)
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def populate_streams_on_account_create(sender, instance, created, *args, **kwargs):
|
def populate_lists_on_account_create(sender, instance, created, *args, **kwargs):
|
||||||
"""build a user's feeds when they join"""
|
"""build a user's feeds when they join"""
|
||||||
if not created or not instance.local:
|
if not created or not instance.local:
|
||||||
return
|
return
|
||||||
|
|
||||||
populate_lists_stream_task.delay(instance.id)
|
populate_lists_task.delay(instance.id)
|
||||||
|
|
||||||
|
|
||||||
# ---- TASKS
|
# ---- TASKS
|
||||||
@app.task(queue=MEDIUM)
|
@app.task(queue=MEDIUM)
|
||||||
def populate_lists_stream_task(user_id):
|
def populate_lists_task(user_id):
|
||||||
"""background task for populating an empty activitystream"""
|
"""background task for populating an empty activitystream"""
|
||||||
user = models.User.objects.get(id=user_id)
|
user = models.User.objects.get(id=user_id)
|
||||||
ListsStream().populate_streams(user)
|
ListsStream().populate_streams(user)
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
""" testing activitystreams """
|
""" testing lists_stream """
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from bookwyrm import activitystreams, models
|
from bookwyrm import lists_stream, models
|
||||||
|
|
||||||
|
|
||||||
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
||||||
class ActivitystreamsSignals(TestCase):
|
class ListsStreamSignals(TestCase):
|
||||||
"""using redis to build activity streams"""
|
"""using redis to build activity streams"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -29,51 +29,41 @@ class ActivitystreamsSignals(TestCase):
|
||||||
inbox="https://example.com/users/rat/inbox",
|
inbox="https://example.com/users/rat/inbox",
|
||||||
outbox="https://example.com/users/rat/outbox",
|
outbox="https://example.com/users/rat/outbox",
|
||||||
)
|
)
|
||||||
work = models.Work.objects.create(title="test work")
|
|
||||||
self.book = models.Edition.objects.create(title="test book", parent_work=work)
|
|
||||||
|
|
||||||
def test_add_status_on_create_ignore(self, _):
|
def test_add_list_on_create(self, _):
|
||||||
"""a new statuses has entered"""
|
"""a new lists has entered"""
|
||||||
activitystreams.add_status_on_create(models.User, self.local_user, False)
|
book_list = models.List.objects.create(
|
||||||
|
user=self.remote_user, name="hi", privacy="public"
|
||||||
def test_add_status_on_create_deleted(self, _):
|
)
|
||||||
"""a new statuses has entered"""
|
with patch("bookwyrm.lists_stream.add_list_task.delay") as mock:
|
||||||
with patch("bookwyrm.activitystreams.remove_status_task.delay"):
|
lists_stream.add_list_on_create_command(book_list)
|
||||||
status = models.Status.objects.create(
|
|
||||||
user=self.remote_user, content="hi", privacy="public", deleted=True
|
|
||||||
)
|
|
||||||
with patch("bookwyrm.activitystreams.remove_status_task.delay") as mock:
|
|
||||||
activitystreams.add_status_on_create(models.Status, status, False)
|
|
||||||
self.assertEqual(mock.call_count, 1)
|
self.assertEqual(mock.call_count, 1)
|
||||||
args = mock.call_args[0]
|
args = mock.call_args[0]
|
||||||
self.assertEqual(args[0], status.id)
|
self.assertEqual(args[0], book_list.id)
|
||||||
|
|
||||||
def test_add_status_on_create_created(self, _):
|
def test_remove_list_on_delete(self, _):
|
||||||
"""a new statuses has entered"""
|
"""delete a list"""
|
||||||
status = models.Status.objects.create(
|
book_list = models.List.objects.create(
|
||||||
user=self.remote_user, content="hi", privacy="public"
|
user=self.remote_user, name="hi", privacy="public"
|
||||||
)
|
)
|
||||||
with patch("bookwyrm.activitystreams.add_status_task.apply_async") as mock:
|
with patch("bookwyrm.lists_stream.remove_list_task.delay") as mock:
|
||||||
activitystreams.add_status_on_create_command(models.Status, status, False)
|
lists_stream.remove_list_on_delete(models.List, book_list)
|
||||||
self.assertEqual(mock.call_count, 1)
|
args = mock.call_args[0]
|
||||||
args = mock.call_args[1]
|
self.assertEqual(args[0], book_list.id)
|
||||||
self.assertEqual(args["args"][0], status.id)
|
|
||||||
self.assertEqual(args["queue"], "high_priority")
|
|
||||||
|
|
||||||
def test_populate_streams_on_account_create(self, _):
|
def test_populate_lists_on_account_create(self, _):
|
||||||
"""create streams for a user"""
|
"""create streams for a user"""
|
||||||
with patch("bookwyrm.activitystreams.populate_stream_task.delay") as mock:
|
with patch("bookwyrm.lists_stream.populate_lists_task.delay") as mock:
|
||||||
activitystreams.populate_streams_on_account_create(
|
lists_stream.populate_lists_on_account_create(
|
||||||
models.User, self.local_user, True
|
models.User, self.local_user, True
|
||||||
)
|
)
|
||||||
self.assertEqual(mock.call_count, 3)
|
self.assertEqual(mock.call_count, 1)
|
||||||
args = mock.call_args[0]
|
args = mock.call_args[0]
|
||||||
self.assertEqual(args[0], "books")
|
self.assertEqual(args[0], self.local_user.id)
|
||||||
self.assertEqual(args[1], self.local_user.id)
|
|
||||||
|
|
||||||
def test_remove_statuses_on_block(self, _):
|
def test_remove_lists_on_block(self, _):
|
||||||
"""don't show statuses from blocked users"""
|
"""don't show lists from blocked users"""
|
||||||
with patch("bookwyrm.activitystreams.remove_user_statuses_task.delay") as mock:
|
with patch("bookwyrm.lists_stream.remove_user_lists_task.delay") as mock:
|
||||||
models.UserBlocks.objects.create(
|
models.UserBlocks.objects.create(
|
||||||
user_subject=self.local_user,
|
user_subject=self.local_user,
|
||||||
user_object=self.remote_user,
|
user_object=self.remote_user,
|
||||||
|
@ -83,26 +73,24 @@ class ActivitystreamsSignals(TestCase):
|
||||||
self.assertEqual(args[0], self.local_user.id)
|
self.assertEqual(args[0], self.local_user.id)
|
||||||
self.assertEqual(args[1], self.remote_user.id)
|
self.assertEqual(args[1], self.remote_user.id)
|
||||||
|
|
||||||
def test_add_statuses_on_unblock(self, _):
|
def test_add_lists_on_unblock(self, _):
|
||||||
"""re-add statuses on unblock"""
|
"""re-add lists on unblock"""
|
||||||
with patch("bookwyrm.activitystreams.remove_user_statuses_task.delay"):
|
with patch("bookwyrm.lists_stream.remove_user_lists_task.delay"):
|
||||||
block = models.UserBlocks.objects.create(
|
block = models.UserBlocks.objects.create(
|
||||||
user_subject=self.local_user,
|
user_subject=self.local_user,
|
||||||
user_object=self.remote_user,
|
user_object=self.remote_user,
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch("bookwyrm.activitystreams.add_user_statuses_task.delay") as mock:
|
with patch("bookwyrm.lists_stream.add_user_lists_task.delay") as mock:
|
||||||
block.delete()
|
block.delete()
|
||||||
|
|
||||||
args = mock.call_args[0]
|
args = mock.call_args[0]
|
||||||
kwargs = mock.call_args.kwargs
|
|
||||||
self.assertEqual(args[0], self.local_user.id)
|
self.assertEqual(args[0], self.local_user.id)
|
||||||
self.assertEqual(args[1], self.remote_user.id)
|
self.assertEqual(args[1], self.remote_user.id)
|
||||||
self.assertEqual(kwargs["stream_list"], ["local", "books"])
|
|
||||||
|
|
||||||
def test_add_statuses_on_unblock_reciprocal_block(self, _):
|
def test_add_lists_on_unblock_reciprocal_block(self, _):
|
||||||
"""re-add statuses on unblock"""
|
"""dont' re-add lists on unblock if there's a block the other way"""
|
||||||
with patch("bookwyrm.activitystreams.remove_user_statuses_task.delay"):
|
with patch("bookwyrm.lists_stream.remove_user_lists_task.delay"):
|
||||||
block = models.UserBlocks.objects.create(
|
block = models.UserBlocks.objects.create(
|
||||||
user_subject=self.local_user,
|
user_subject=self.local_user,
|
||||||
user_object=self.remote_user,
|
user_object=self.remote_user,
|
||||||
|
@ -112,7 +100,7 @@ class ActivitystreamsSignals(TestCase):
|
||||||
user_object=self.local_user,
|
user_object=self.local_user,
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch("bookwyrm.activitystreams.add_user_statuses_task.delay") as mock:
|
with patch("bookwyrm.lists_stream.add_user_lists_task.delay") as mock:
|
||||||
block.delete()
|
block.delete()
|
||||||
|
|
||||||
self.assertEqual(mock.call_count, 0)
|
self.assertFalse(mock.called)
|
||||||
|
|
Loading…
Reference in New Issue