Updates models tests

This commit is contained in:
Mouse Reeve 2021-12-09 12:42:07 -08:00
parent e9c58295f3
commit eb4a399472
13 changed files with 68 additions and 38 deletions

View file

@ -9,7 +9,7 @@ from bookwyrm.tasks import app, MEDIUM, HIGH
class ListsStream(RedisStore): class ListsStream(RedisStore):
"""a category of activity stream (like home, local, books)""" """all the lists you can see"""
def stream_id(self, user): # pylint: disable=no-self-use def stream_id(self, user): # pylint: disable=no-self-use
"""the redis key for this user's instance of this stream""" """the redis key for this user's instance of this stream"""
@ -122,9 +122,7 @@ def add_lists_on_follow(sender, instance, created, *args, **kwargs):
"""add a newly followed user's lists to feeds""" """add a newly followed user's lists to feeds"""
if not created or not instance.user_subject.local: if not created or not instance.user_subject.local:
return return
add_user_lists_task.delay( add_user_lists_task.delay(instance.user_subject.id, instance.user_object.id)
instance.user_subject.id, instance.user_object.id, stream_list=["home"]
)
@receiver(signals.post_delete, sender=models.UserFollows) @receiver(signals.post_delete, sender=models.UserFollows)
@ -133,9 +131,7 @@ def remove_lists_on_unfollow(sender, instance, *args, **kwargs):
"""remove lists from a feed on unfollow""" """remove lists from a feed on unfollow"""
if not instance.user_subject.local: if not instance.user_subject.local:
return return
remove_user_lists_task.delay( remove_user_lists_task.delay(instance.user_subject.id, instance.user_object.id)
instance.user_subject.id, instance.user_object.id, stream_list=["home"]
)
@receiver(signals.post_save, sender=models.UserBlocks) @receiver(signals.post_save, sender=models.UserBlocks)

View file

@ -29,7 +29,7 @@ class ActivitypubMixins(TestCase):
"""shared data""" """shared data"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse" "mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
) )
@ -332,7 +332,7 @@ class ActivitypubMixins(TestCase):
self.assertEqual(activity["id"], "https://example.com/status/1/activity") self.assertEqual(activity["id"], "https://example.com/status/1/activity")
self.assertEqual(activity["actor"], self.local_user.remote_id) self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["type"], "Delete") self.assertEqual(activity["type"], "Delete")
self.assertEqual(activity["to"], ["%s/followers" % self.local_user.remote_id]) self.assertEqual(activity["to"], [f"{self.local_user.remote_id}/followers"])
self.assertEqual( self.assertEqual(
activity["cc"], ["https://www.w3.org/ns/activitystreams#Public"] activity["cc"], ["https://www.w3.org/ns/activitystreams#Public"]
) )
@ -374,7 +374,7 @@ class ActivitypubMixins(TestCase):
for number in range(0, 2 * PAGE_LENGTH): for number in range(0, 2 * PAGE_LENGTH):
models.Status.objects.create( models.Status.objects.create(
user=self.local_user, user=self.local_user,
content="test status {:d}".format(number), content=f"test status {number}",
) )
page_1 = to_ordered_collection_page( page_1 = to_ordered_collection_page(
models.Status.objects.all(), "http://fish.com/", page=1 models.Status.objects.all(), "http://fish.com/", page=1
@ -400,7 +400,7 @@ class ActivitypubMixins(TestCase):
for number in range(0, 2 * PAGE_LENGTH): for number in range(0, 2 * PAGE_LENGTH):
models.Status.objects.create( models.Status.objects.create(
user=self.local_user, user=self.local_user,
content="test status {:d}".format(number), content=f"test status {number}",
) )
MockSelf = namedtuple("Self", ("remote_id")) MockSelf = namedtuple("Self", ("remote_id"))

View file

@ -16,7 +16,7 @@ class BaseModel(TestCase):
"""shared data""" """shared data"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse" "mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
) )

View file

@ -27,6 +27,7 @@ from bookwyrm.settings import DOMAIN
# pylint: disable=too-many-public-methods # pylint: disable=too-many-public-methods
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") @patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.activitystreams.populate_stream_task.delay") @patch("bookwyrm.activitystreams.populate_stream_task.delay")
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
class ModelFields(TestCase): class ModelFields(TestCase):
"""overwrites standard model feilds to work with activitypub""" """overwrites standard model feilds to work with activitypub"""

View file

@ -2,7 +2,7 @@
from unittest.mock import patch from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
from bookwyrm import models, settings from bookwyrm import models
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
@ -14,21 +14,21 @@ class Group(TestCase):
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.owner_user = models.User.objects.create_user( self.owner_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse" "mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
) )
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.rat = models.User.objects.create_user( self.rat = models.User.objects.create_user(
"rat", "rat@rat.rat", "ratword", local=True, localname="rat" "rat", "rat@rat.rat", "ratword", local=True, localname="rat"
) )
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.badger = models.User.objects.create_user( self.badger = models.User.objects.create_user(
"badger", "badger",
"badger@badger.badger", "badger@badger.badger",
@ -39,7 +39,7 @@ class Group(TestCase):
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.capybara = models.User.objects.create_user( self.capybara = models.User.objects.create_user(
"capybara", "capybara",
"capybara@capybara.capybara", "capybara@capybara.capybara",
@ -76,7 +76,8 @@ class Group(TestCase):
models.GroupMember.objects.create(group=self.public_group, user=self.capybara) models.GroupMember.objects.create(group=self.public_group, user=self.capybara)
def test_group_members_can_see_private_groups(self, _): def test_group_members_can_see_private_groups(self, _):
"""direct privacy group should not be excluded from group listings for group members viewing""" """direct privacy group should not be excluded from group listings for group
members viewing"""
rat_groups = models.Group.privacy_filter(self.rat).all() rat_groups = models.Group.privacy_filter(self.rat).all()
badger_groups = models.Group.privacy_filter(self.badger).all() badger_groups = models.Group.privacy_filter(self.badger).all()
@ -85,7 +86,8 @@ class Group(TestCase):
self.assertTrue(self.private_group in badger_groups) self.assertTrue(self.private_group in badger_groups)
def test_group_members_can_see_followers_only_lists(self, _): def test_group_members_can_see_followers_only_lists(self, _):
"""follower-only group booklists should not be excluded from group booklist listing for group members who do not follower list owner""" """follower-only group booklists should not be excluded from group booklist
listing for group members who do not follower list owner"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
followers_list = models.List.objects.create( followers_list = models.List.objects.create(
@ -105,7 +107,8 @@ class Group(TestCase):
self.assertTrue(followers_list in capybara_lists) self.assertTrue(followers_list in capybara_lists)
def test_group_members_can_see_private_lists(self, _): def test_group_members_can_see_private_lists(self, _):
"""private group booklists should not be excluded from group booklist listing for group members""" """private group booklists should not be excluded from group booklist listing
for group members"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):

View file

@ -20,7 +20,7 @@ class ImportJob(TestCase):
"""data is from a goodreads export of The Raven Tower""" """data is from a goodreads export of The Raven Tower"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "password", local=True "mouse", "mouse@mouse.mouse", "password", local=True
) )

View file

@ -1,7 +1,7 @@
""" testing models """ """ testing models """
from uuid import UUID
from unittest.mock import patch from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
from uuid import UUID
from bookwyrm import models, settings from bookwyrm import models, settings
@ -14,7 +14,7 @@ class List(TestCase):
"""look, a list""" """look, a list"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse" "mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
) )
@ -27,7 +27,7 @@ class List(TestCase):
book_list = models.List.objects.create( book_list = models.List.objects.create(
name="Test List", user=self.local_user name="Test List", user=self.local_user
) )
expected_id = "https://%s/list/%d" % (settings.DOMAIN, book_list.id) expected_id = f"https://{settings.DOMAIN}/list/{book_list.id}"
self.assertEqual(book_list.get_remote_id(), expected_id) self.assertEqual(book_list.get_remote_id(), expected_id)
def test_to_activity(self, _): def test_to_activity(self, _):

View file

@ -15,7 +15,7 @@ class ReadThrough(TestCase):
"""look, a shelf""" """look, a shelf"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.user = models.User.objects.create_user( self.user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse" "mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
) )

View file

@ -1,12 +1,16 @@
""" testing models """ """ testing models """
import json import json
from unittest.mock import patch from unittest.mock import patch
from django.db import IntegrityError
from django.test import TestCase from django.test import TestCase
from bookwyrm import models from bookwyrm import models
@patch("bookwyrm.activitystreams.add_user_statuses_task.delay") @patch("bookwyrm.activitystreams.add_user_statuses_task.delay")
@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")
class Relationship(TestCase): class Relationship(TestCase):
"""following, blocking, stuff like that""" """following, blocking, stuff like that"""
@ -24,14 +28,39 @@ class Relationship(TestCase):
) )
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse" "mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
) )
self.local_user.remote_id = "http://local.com/user/mouse" self.local_user.remote_id = "http://local.com/user/mouse"
self.local_user.save(broadcast=False, update_fields=["remote_id"]) self.local_user.save(broadcast=False, update_fields=["remote_id"])
def test_user_follows_from_request(self, _): def test_user_follows(self, *_):
"""basic functionality of user follows"""
relationship = models.UserFollows.objects.create(
user_subject=self.local_user, user_object=self.remote_user
)
self.assertEqual(relationship.status, "follows")
activity = relationship.to_activity()
self.assertEqual(activity.type, "Follow")
self.assertEqual(
relationship.remote_id,
f"http://local.com/user/mouse#follows/{relationship.id}",
)
def test_user_follows_blocks(self, *_):
"""can't follow if you're blocked"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
models.UserBlocks.objects.create(
user_subject=self.local_user, user_object=self.remote_user
)
with self.assertRaises(IntegrityError):
models.UserFollows.objects.create(
user_subject=self.local_user, user_object=self.remote_user
)
def test_user_follows_from_request(self, *_):
"""convert a follow request into a follow""" """convert a follow request into a follow"""
with patch( with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
@ -42,19 +71,19 @@ class Relationship(TestCase):
activity = json.loads(mock.call_args[1]["args"][1]) activity = json.loads(mock.call_args[1]["args"][1])
self.assertEqual(activity["type"], "Follow") self.assertEqual(activity["type"], "Follow")
self.assertEqual( self.assertEqual(
request.remote_id, "http://local.com/user/mouse#follows/%d" % request.id request.remote_id, f"http://local.com/user/mouse#follows/{request.id}"
) )
self.assertEqual(request.status, "follow_request") self.assertEqual(request.status, "follow_request")
rel = models.UserFollows.from_request(request) rel = models.UserFollows.from_request(request)
self.assertEqual( self.assertEqual(
rel.remote_id, "http://local.com/user/mouse#follows/%d" % request.id rel.remote_id, f"http://local.com/user/mouse#follows/{request.id}"
) )
self.assertEqual(rel.status, "follows") self.assertEqual(rel.status, "follows")
self.assertEqual(rel.user_subject, self.local_user) self.assertEqual(rel.user_subject, self.local_user)
self.assertEqual(rel.user_object, self.remote_user) self.assertEqual(rel.user_object, self.remote_user)
def test_user_follows_from_request_custom_remote_id(self, _): def test_user_follows_from_request_custom_remote_id(self, *_):
"""store a specific remote id for a relationship provided by remote""" """store a specific remote id for a relationship provided by remote"""
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"): with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
request = models.UserFollowRequest.objects.create( request = models.UserFollowRequest.objects.create(
@ -72,7 +101,7 @@ class Relationship(TestCase):
self.assertEqual(rel.user_object, self.remote_user) self.assertEqual(rel.user_object, self.remote_user)
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
def test_follow_request_activity(self, broadcast_mock, _): def test_follow_request_activity(self, broadcast_mock, *_):
"""accept a request and make it a relationship""" """accept a request and make it a relationship"""
models.UserFollowRequest.objects.create( models.UserFollowRequest.objects.create(
user_subject=self.local_user, user_subject=self.local_user,
@ -84,7 +113,7 @@ class Relationship(TestCase):
self.assertEqual(activity["type"], "Follow") self.assertEqual(activity["type"], "Follow")
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
def test_follow_request_accept(self, broadcast_mock, _): def test_follow_request_accept(self, broadcast_mock, *_):
"""accept a request and make it a relationship""" """accept a request and make it a relationship"""
self.local_user.manually_approves_followers = True self.local_user.manually_approves_followers = True
self.local_user.save( self.local_user.save(
@ -110,7 +139,7 @@ class Relationship(TestCase):
self.assertEqual(rel.user_object, self.local_user) self.assertEqual(rel.user_object, self.local_user)
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async") @patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
def test_follow_request_reject(self, broadcast_mock, _): def test_follow_request_reject(self, broadcast_mock, *_):
"""accept a request and make it a relationship""" """accept a request and make it a relationship"""
self.local_user.manually_approves_followers = True self.local_user.manually_approves_followers = True
self.local_user.save( self.local_user.save(

View file

@ -9,6 +9,7 @@ from bookwyrm import models, settings
# pylint: disable=unused-argument # pylint: disable=unused-argument
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay") @patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.activitystreams.populate_stream_task.delay") @patch("bookwyrm.activitystreams.populate_stream_task.delay")
@patch("bookwyrm.lists_stream.populate_lists_task.delay")
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay") @patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
@patch("bookwyrm.activitystreams.remove_book_statuses_task.delay") @patch("bookwyrm.activitystreams.remove_book_statuses_task.delay")
class Shelf(TestCase): class Shelf(TestCase):
@ -18,7 +19,7 @@ class Shelf(TestCase):
"""look, a shelf""" """look, a shelf"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse" "mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
) )
@ -31,7 +32,7 @@ class Shelf(TestCase):
shelf = models.Shelf.objects.create( shelf = models.Shelf.objects.create(
name="Test Shelf", identifier="test-shelf", user=self.local_user name="Test Shelf", identifier="test-shelf", user=self.local_user
) )
expected_id = "https://%s/user/mouse/books/test-shelf" % settings.DOMAIN expected_id = f"https://{settings.DOMAIN}/user/mouse/books/test-shelf"
self.assertEqual(shelf.get_remote_id(), expected_id) self.assertEqual(shelf.get_remote_id(), expected_id)
def test_to_activity(self, *_): def test_to_activity(self, *_):

View file

@ -16,7 +16,7 @@ class SiteModels(TestCase):
"""we need basic test data and mocks""" """we need basic test data and mocks"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
"mouse@local.com", "mouse@local.com",
"mouse@mouse.com", "mouse@mouse.com",

View file

@ -28,7 +28,7 @@ class Status(TestCase):
"""useful things for creating a status""" """useful things for creating a status"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse" "mouse", "mouse@mouse.mouse", "mouseword", local=True, localname="mouse"
) )

View file

@ -15,7 +15,7 @@ class User(TestCase):
def setUp(self): def setUp(self):
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch( with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay" "bookwyrm.activitystreams.populate_stream_task.delay"
): ), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
self.user = models.User.objects.create_user( self.user = models.User.objects.create_user(
"mouse@%s" % DOMAIN, "mouse@%s" % DOMAIN,
"mouse@mouse.mouse", "mouse@mouse.mouse",