moviewyrm/bookwyrm/tests/models/test_base_model.py

127 lines
4.9 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
2021-04-11 17:55:13 +00:00
from unittest.mock import patch
2020-05-12 21:45:30 +00:00
from django.test import TestCase
from bookwyrm import models
from bookwyrm.models import base_model
from bookwyrm.settings import DOMAIN
2020-05-12 21:45:30 +00:00
2021-03-08 16:49:10 +00:00
2021-08-02 23:05:40 +00:00
# pylint: disable=attribute-defined-outside-init
2020-05-12 21:45:30 +00:00
class BaseModel(TestCase):
2021-04-26 16:15:42 +00:00
"""functionality shared across models"""
2021-03-08 16:49:10 +00:00
2021-04-11 17:55:13 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""shared data"""
2021-08-03 17:25:53 +00:00
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"):
self.local_user = models.User.objects.create_user(
"mouse", "mouse@mouse.com", "mouseword", local=True, localname="mouse"
)
2021-08-02 23:05:40 +00:00
with patch("bookwyrm.models.user.set_remote_server.delay"):
self.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",
2021-05-26 21:57:29 +00:00
)
2021-04-11 17:55:13 +00:00
2021-04-26 18:51:17 +00:00
class BookWyrmTestModel(base_model.BookWyrmModel):
2021-04-26 18:37:07 +00:00
"""just making it not abstract"""
2021-04-26 18:34:04 +00:00
2021-04-26 18:51:17 +00:00
self.test_model = BookWyrmTestModel()
2021-04-26 18:28:33 +00:00
def test_remote_id(self):
2021-04-26 16:15:42 +00:00
"""these should be generated"""
2021-04-26 18:28:33 +00:00
self.test_model.id = 1
expected = self.test_model.get_remote_id()
2021-04-26 20:50:43 +00:00
self.assertEqual(expected, "https://%s/bookwyrmtestmodel/1" % DOMAIN)
2020-05-12 21:59:14 +00:00
def test_remote_id_with_user(self):
2021-04-26 16:15:42 +00:00
"""format of remote id when there's a user object"""
2021-04-26 18:28:33 +00:00
self.test_model.user = self.local_user
self.test_model.id = 1
expected = self.test_model.get_remote_id()
2021-04-26 20:50:43 +00:00
self.assertEqual(expected, "https://%s/user/mouse/bookwyrmtestmodel/1" % DOMAIN)
2021-03-23 17:41:18 +00:00
def test_set_remote_id(self):
2021-04-26 16:15:42 +00:00
"""this function sets remote ids after creation"""
# using Work because it BookWrymModel is abstract and this requires save
# Work is a relatively not-fancy model.
2021-03-08 16:49:10 +00:00
instance = models.Work.objects.create(title="work title")
instance.remote_id = None
2021-03-23 17:41:18 +00:00
base_model.set_remote_id(None, instance, True)
self.assertEqual(
2021-03-08 16:49:10 +00:00
instance.remote_id, "https://%s/book/%d" % (DOMAIN, instance.id)
)
# shouldn't set remote_id if it's not created
instance.remote_id = None
2021-03-23 17:41:18 +00:00
base_model.set_remote_id(None, instance, False)
self.assertIsNone(instance.remote_id)
2021-04-12 13:44:50 +00:00
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
def test_object_visible_to_user(self, _):
2021-04-26 16:15:42 +00:00
"""does a user have permission to view an object"""
obj = models.Status.objects.create(
content="hi", user=self.remote_user, privacy="public"
)
self.assertTrue(obj.visible_to_user(self.local_user))
obj = models.Shelf.objects.create(
name="test", user=self.remote_user, privacy="unlisted"
)
self.assertTrue(obj.visible_to_user(self.local_user))
obj = models.Status.objects.create(
content="hi", user=self.remote_user, privacy="followers"
)
self.assertFalse(obj.visible_to_user(self.local_user))
obj = models.Status.objects.create(
content="hi", user=self.remote_user, privacy="direct"
)
self.assertFalse(obj.visible_to_user(self.local_user))
obj = models.Status.objects.create(
content="hi", user=self.remote_user, privacy="direct"
)
obj.mention_users.add(self.local_user)
self.assertTrue(obj.visible_to_user(self.local_user))
2021-04-12 13:44:50 +00:00
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
def test_object_visible_to_user_follower(self, _):
2021-04-26 16:15:42 +00:00
"""what you can see if you follow a user"""
self.remote_user.followers.add(self.local_user)
obj = models.Status.objects.create(
content="hi", user=self.remote_user, privacy="followers"
)
self.assertTrue(obj.visible_to_user(self.local_user))
obj = models.Status.objects.create(
content="hi", user=self.remote_user, privacy="direct"
)
self.assertFalse(obj.visible_to_user(self.local_user))
obj = models.Status.objects.create(
content="hi", user=self.remote_user, privacy="direct"
)
obj.mention_users.add(self.local_user)
self.assertTrue(obj.visible_to_user(self.local_user))
2021-04-12 13:44:50 +00:00
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
def test_object_visible_to_user_blocked(self, _):
2021-04-26 16:15:42 +00:00
"""you can't see it if they block you"""
self.remote_user.blocks.add(self.local_user)
obj = models.Status.objects.create(
content="hi", user=self.remote_user, privacy="public"
)
self.assertFalse(obj.visible_to_user(self.local_user))
obj = models.Shelf.objects.create(
name="test", user=self.remote_user, privacy="unlisted"
)
self.assertFalse(obj.visible_to_user(self.local_user))