moviewyrm/bookwyrm/tests/models/test_base_model.py

203 lines
7.5 KiB
Python
Raw Normal View History

2020-05-12 21:45:30 +00:00
''' testing models '''
from collections import namedtuple
2020-12-04 23:14:26 +00:00
from dataclasses import dataclass
2020-12-04 19:46:16 +00:00
import re
2020-05-12 21:45:30 +00:00
from django.test import TestCase
2020-12-04 23:14:26 +00:00
from bookwyrm.activitypub.base_activity import ActivityObject
from bookwyrm import models
from bookwyrm.models import base_model
from bookwyrm.models.base_model import ActivitypubMixin
from bookwyrm.settings import DOMAIN
2020-05-12 21:45:30 +00:00
class BaseModel(TestCase):
2020-12-12 21:39:55 +00:00
''' functionality shared across models '''
def test_remote_id(self):
2020-12-12 21:39:55 +00:00
''' these should be generated '''
instance = base_model.BookWyrmModel()
2020-05-12 21:45:30 +00:00
instance.id = 1
expected = instance.get_remote_id()
self.assertEqual(expected, 'https://%s/bookwyrmmodel/1' % DOMAIN)
2020-05-12 21:59:14 +00:00
def test_remote_id_with_user(self):
2020-12-12 21:39:55 +00:00
''' format of remote id when there's a user object '''
2020-05-12 22:07:31 +00:00
user = models.User.objects.create_user(
2020-12-04 01:18:23 +00:00
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
instance = base_model.BookWyrmModel()
2020-05-12 22:07:31 +00:00
instance.user = user
instance.id = 1
expected = instance.get_remote_id()
2020-05-12 22:07:31 +00:00
self.assertEqual(
expected,
'https://%s/user/mouse/bookwyrmmodel/1' % DOMAIN)
def test_execute_after_save(self):
''' 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.
instance = models.Work.objects.create(title='work title')
instance.remote_id = None
base_model.execute_after_save(None, instance, True)
self.assertEqual(
instance.remote_id,
'https://%s/book/%d' % (DOMAIN, instance.id)
)
# shouldn't set remote_id if it's not created
instance.remote_id = None
base_model.execute_after_save(None, instance, False)
self.assertIsNone(instance.remote_id)
def test_to_create_activity(self):
2020-12-12 21:39:55 +00:00
''' wrapper for ActivityPub "create" action '''
user = models.User.objects.create_user(
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
object_activity = {
'to': 'to field', 'cc': 'cc field',
'content': 'hi',
'published': '2020-12-04T17:52:22.623807+00:00',
}
MockSelf = namedtuple('Self', ('remote_id', 'to_activity'))
mock_self = MockSelf(
'https://example.com/status/1',
lambda *args: object_activity
)
activity = ActivitypubMixin.to_create_activity(mock_self, user)
self.assertEqual(
activity['id'],
'https://example.com/status/1/activity'
)
self.assertEqual(activity['actor'], user.remote_id)
2020-12-04 19:46:16 +00:00
self.assertEqual(activity['type'], 'Create')
self.assertEqual(activity['to'], 'to field')
self.assertEqual(activity['cc'], 'cc field')
self.assertEqual(activity['object'], object_activity)
self.assertEqual(
activity['signature'].creator,
'%s#main-key' % user.remote_id
)
2020-12-04 19:46:16 +00:00
def test_to_delete_activity(self):
2020-12-12 21:39:55 +00:00
''' wrapper for Delete activity '''
2020-12-04 19:46:16 +00:00
user = models.User.objects.create_user(
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
MockSelf = namedtuple('Self', ('remote_id', 'to_activity'))
mock_self = MockSelf(
'https://example.com/status/1',
lambda *args: {}
)
activity = ActivitypubMixin.to_delete_activity(mock_self, user)
self.assertEqual(
activity['id'],
'https://example.com/status/1/activity'
)
self.assertEqual(activity['actor'], user.remote_id)
self.assertEqual(activity['type'], 'Delete')
self.assertEqual(
activity['to'],
['%s/followers' % user.remote_id])
self.assertEqual(
activity['cc'],
['https://www.w3.org/ns/activitystreams#Public'])
def test_to_update_activity(self):
2020-12-12 21:39:55 +00:00
''' ditto above but for Update '''
2020-12-04 19:46:16 +00:00
user = models.User.objects.create_user(
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
MockSelf = namedtuple('Self', ('remote_id', 'to_activity'))
mock_self = MockSelf(
'https://example.com/status/1',
lambda *args: {}
)
activity = ActivitypubMixin.to_update_activity(mock_self, user)
self.assertIsNotNone(
re.match(
r'^https:\/\/example\.com\/status\/1#update\/.*',
activity['id']
)
)
self.assertEqual(activity['actor'], user.remote_id)
self.assertEqual(activity['type'], 'Update')
self.assertEqual(
activity['to'],
['https://www.w3.org/ns/activitystreams#Public'])
self.assertEqual(activity['object'], {})
def test_to_undo_activity(self):
2020-12-12 21:39:55 +00:00
''' and again, for Undo '''
user = models.User.objects.create_user(
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
MockSelf = namedtuple('Self', ('remote_id', 'to_activity'))
mock_self = MockSelf(
'https://example.com/status/1',
lambda *args: {}
)
activity = ActivitypubMixin.to_undo_activity(mock_self, user)
self.assertEqual(
activity['id'],
'https://example.com/status/1#undo'
)
self.assertEqual(activity['actor'], user.remote_id)
self.assertEqual(activity['type'], 'Undo')
self.assertEqual(activity['object'], {})
2020-12-04 23:14:26 +00:00
def test_to_activity(self):
2020-12-12 21:39:55 +00:00
''' model to ActivityPub json '''
2020-12-04 23:14:26 +00:00
@dataclass(init=False)
class TestActivity(ActivityObject):
2020-12-12 21:39:55 +00:00
''' real simple mock '''
2020-12-04 23:14:26 +00:00
type: str = 'Test'
class TestModel(ActivitypubMixin, base_model.BookWyrmModel):
2020-12-12 21:39:55 +00:00
''' real simple mock model because BookWyrmModel is abstract '''
2020-12-04 23:14:26 +00:00
instance = TestModel()
instance.remote_id = 'https://www.example.com/test'
instance.activity_serializer = TestActivity
activity = instance.to_activity()
self.assertIsInstance(activity, dict)
self.assertEqual(activity['id'], 'https://www.example.com/test')
self.assertEqual(activity['type'], 'Test')
2020-12-12 21:39:55 +00:00
def test_find_existing_by_remote_id(self):
''' attempt to match a remote id to an object in the db '''
# uses a different remote id scheme
# this isn't really part of this test directly but it's helpful to state
2020-12-12 22:15:10 +00:00
book = models.Edition.objects.create(
title='Test Edition', remote_id='http://book.com/book')
user = models.User.objects.create_user(
'mouse', 'mouse@mouse.mouse', 'mouseword', local=True)
user.remote_id = 'http://example.com/a/b'
user.save()
self.assertEqual(book.origin_id, 'http://book.com/book')
self.assertNotEqual(book.remote_id, 'http://book.com/book')
2020-12-12 21:39:55 +00:00
# uses subclasses
models.Comment.objects.create(
2020-12-12 22:15:10 +00:00
user=user, content='test status', book=book, \
2020-12-12 21:39:55 +00:00
remote_id='https://comment.net')
result = models.User.find_existing_by_remote_id('hi')
self.assertIsNone(result)
result = models.User.find_existing_by_remote_id(
'http://example.com/a/b')
2020-12-12 22:15:10 +00:00
self.assertEqual(result, user)
2020-12-12 21:39:55 +00:00
# test using origin id
result = models.Edition.find_existing_by_remote_id(
'http://book.com/book')
2020-12-12 22:15:10 +00:00
self.assertEqual(result, book)
2020-12-12 21:39:55 +00:00
# test subclass match
result = models.Status.find_existing_by_remote_id(
'https://comment.net')