2020-12-14 18:25:43 +00:00
|
|
|
''' testing models '''
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from bookwyrm import models, settings
|
|
|
|
|
|
|
|
|
|
|
|
class Shelf(TestCase):
|
|
|
|
''' some activitypub oddness ahead '''
|
|
|
|
def setUp(self):
|
|
|
|
''' look, a shelf '''
|
2021-02-07 05:00:08 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
2020-12-27 23:18:48 +00:00
|
|
|
'mouse', 'mouse@mouse.mouse', 'mouseword',
|
|
|
|
local=True, localname='mouse')
|
2021-02-07 05:00:08 +00:00
|
|
|
work = models.Work.objects.create(title='Test Work')
|
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
title='test book',
|
|
|
|
parent_work=work)
|
2020-12-14 18:25:43 +00:00
|
|
|
|
|
|
|
def test_remote_id(self):
|
|
|
|
''' shelves use custom remote ids '''
|
2021-02-08 17:38:28 +00:00
|
|
|
real_broadcast = models.Shelf.broadcast
|
2021-02-09 20:48:46 +00:00
|
|
|
def broadcast_mock(_, activity, user, **kwargs):
|
|
|
|
''' nah '''
|
|
|
|
models.Shelf.broadcast = broadcast_mock
|
2021-02-07 05:00:08 +00:00
|
|
|
shelf = models.Shelf.objects.create(
|
|
|
|
name='Test Shelf', identifier='test-shelf',
|
|
|
|
user=self.local_user)
|
2020-12-14 18:25:43 +00:00
|
|
|
expected_id = 'https://%s/user/mouse/shelf/test-shelf' % settings.DOMAIN
|
2021-02-07 05:00:08 +00:00
|
|
|
self.assertEqual(shelf.get_remote_id(), expected_id)
|
2021-02-08 17:38:28 +00:00
|
|
|
models.Shelf.broadcast = real_broadcast
|
2020-12-14 18:25:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_to_activity(self):
|
|
|
|
''' jsonify it '''
|
2021-02-08 17:38:28 +00:00
|
|
|
real_broadcast = models.Shelf.broadcast
|
2021-02-09 20:48:46 +00:00
|
|
|
def empty_mock(_, activity, user, **kwargs):
|
|
|
|
''' nah '''
|
|
|
|
models.Shelf.broadcast = empty_mock
|
2021-02-07 05:00:08 +00:00
|
|
|
shelf = models.Shelf.objects.create(
|
|
|
|
name='Test Shelf', identifier='test-shelf',
|
|
|
|
user=self.local_user)
|
|
|
|
activity_json = shelf.to_activity()
|
2020-12-14 18:25:43 +00:00
|
|
|
self.assertIsInstance(activity_json, dict)
|
2021-02-07 05:00:08 +00:00
|
|
|
self.assertEqual(activity_json['id'], shelf.remote_id)
|
2020-12-14 18:25:43 +00:00
|
|
|
self.assertEqual(activity_json['totalItems'], 0)
|
2021-02-02 22:59:40 +00:00
|
|
|
self.assertEqual(activity_json['type'], 'Shelf')
|
2020-12-14 18:25:43 +00:00
|
|
|
self.assertEqual(activity_json['name'], 'Test Shelf')
|
2021-02-07 05:00:08 +00:00
|
|
|
self.assertEqual(activity_json['owner'], self.local_user.remote_id)
|
2021-02-08 17:38:28 +00:00
|
|
|
models.Shelf.broadcast = real_broadcast
|
2021-02-07 05:00:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_create_update_shelf(self):
|
|
|
|
''' create and broadcast shelf creation '''
|
2021-02-08 17:38:28 +00:00
|
|
|
real_broadcast = models.Shelf.broadcast
|
2021-02-09 20:48:46 +00:00
|
|
|
def create_mock(_, activity, user, **kwargs):
|
2021-02-07 05:00:08 +00:00
|
|
|
''' ok '''
|
|
|
|
self.assertEqual(user.remote_id, self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity['type'], 'Create')
|
|
|
|
self.assertEqual(activity['actor'], self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity['object']['name'], 'Test Shelf')
|
|
|
|
models.Shelf.broadcast = create_mock
|
|
|
|
|
|
|
|
shelf = models.Shelf.objects.create(
|
|
|
|
name='Test Shelf', identifier='test-shelf', user=self.local_user)
|
|
|
|
|
2021-02-09 20:48:46 +00:00
|
|
|
def update_mock(_, activity, user, **kwargs):
|
2021-02-07 05:00:08 +00:00
|
|
|
''' ok '''
|
|
|
|
self.assertEqual(user.remote_id, self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity['type'], 'Update')
|
|
|
|
self.assertEqual(activity['actor'], self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity['object']['name'], 'arthur russel')
|
|
|
|
models.Shelf.broadcast = update_mock
|
|
|
|
|
|
|
|
shelf.name = 'arthur russel'
|
|
|
|
shelf.save()
|
|
|
|
self.assertEqual(shelf.name, 'arthur russel')
|
2021-02-08 17:38:28 +00:00
|
|
|
models.Shelf.broadcast = real_broadcast
|
2021-02-07 05:00:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_shelve(self):
|
|
|
|
''' create and broadcast shelf creation '''
|
2021-02-08 17:38:28 +00:00
|
|
|
real_broadcast = models.Shelf.broadcast
|
|
|
|
real_shelfbook_broadcast = models.ShelfBook.broadcast
|
|
|
|
|
2021-02-09 20:48:46 +00:00
|
|
|
def add_mock(_, activity, user, **kwargs):
|
2021-02-07 05:00:08 +00:00
|
|
|
''' ok '''
|
|
|
|
self.assertEqual(user.remote_id, self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity['type'], 'Add')
|
|
|
|
self.assertEqual(activity['actor'], self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity['object']['id'], self.book.remote_id)
|
|
|
|
self.assertEqual(activity['target'], shelf.remote_id)
|
|
|
|
|
2021-02-09 20:48:46 +00:00
|
|
|
def remove_mock(_, activity, user, **kwargs):
|
2021-02-07 05:00:08 +00:00
|
|
|
''' ok '''
|
|
|
|
self.assertEqual(user.remote_id, self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity['type'], 'Remove')
|
|
|
|
self.assertEqual(activity['actor'], self.local_user.remote_id)
|
|
|
|
self.assertEqual(activity['object']['id'], self.book.remote_id)
|
|
|
|
self.assertEqual(activity['target'], shelf.remote_id)
|
|
|
|
|
2021-02-09 20:48:46 +00:00
|
|
|
def empty_mock(_, activity, user, **kwargs):
|
|
|
|
''' nah '''
|
|
|
|
|
|
|
|
models.Shelf.broadcast = empty_mock
|
2021-02-07 05:00:08 +00:00
|
|
|
shelf = models.Shelf.objects.create(
|
|
|
|
name='Test Shelf', identifier='test-shelf', user=self.local_user)
|
|
|
|
|
|
|
|
models.ShelfBook.broadcast = add_mock
|
|
|
|
shelf_book = models.ShelfBook.objects.create(
|
|
|
|
shelf=shelf,
|
|
|
|
user=self.local_user,
|
|
|
|
book=self.book)
|
|
|
|
self.assertEqual(shelf.books.first(), self.book)
|
|
|
|
|
|
|
|
models.ShelfBook.broadcast = remove_mock
|
|
|
|
shelf_book.delete()
|
|
|
|
self.assertFalse(shelf.books.exists())
|
2021-02-08 17:38:28 +00:00
|
|
|
|
|
|
|
models.ShelfBook.broadcast = real_shelfbook_broadcast
|
|
|
|
models.Shelf.broadcast = real_broadcast
|