Tests broadcasting from list views more thoroughly

This commit is contained in:
Mouse Reeve 2021-02-09 09:41:04 -08:00
parent 76e90cb3f6
commit 0fd2885e7e
2 changed files with 130 additions and 48 deletions

View file

@ -10,7 +10,6 @@ from bookwyrm import models, views
from bookwyrm.activitypub import ActivitypubResponse from bookwyrm.activitypub import ActivitypubResponse
@patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay')
class ListViews(TestCase): class ListViews(TestCase):
''' tag views''' ''' tag views'''
def setUp(self): def setUp(self):
@ -40,11 +39,11 @@ class ListViews(TestCase):
models.SiteSettings.objects.create() models.SiteSettings.objects.create()
def test_lists_page(self, _): def test_lists_page(self):
''' there are so many views, this just makes sure it LOADS ''' ''' there are so many views, this just makes sure it LOADS '''
view = views.Lists.as_view() view = views.Lists.as_view()
models.List.objects.create(name='Public list', user=self.local_user)
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
models.List.objects.create(name='Public list', user=self.local_user)
models.List.objects.create( models.List.objects.create(
name='Private list', privacy='private', user=self.local_user) name='Private list', privacy='private', user=self.local_user)
request = self.factory.get('') request = self.factory.get('')
@ -63,8 +62,16 @@ class ListViews(TestCase):
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
def test_lists_create(self, _): def test_lists_create(self):
''' create list view ''' ''' create list view '''
real_broadcast = models.List.broadcast
def mock_broadcast(_, activity, user):
''' 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)
models.List.broadcast = mock_broadcast
view = views.Lists.as_view() view = views.Lists.as_view()
request = self.factory.post('', { request = self.factory.post('', {
'name': 'A list', 'name': 'A list',
@ -80,9 +87,10 @@ class ListViews(TestCase):
self.assertEqual(new_list.description, 'wow') self.assertEqual(new_list.description, 'wow')
self.assertEqual(new_list.privacy, 'unlisted') self.assertEqual(new_list.privacy, 'unlisted')
self.assertEqual(new_list.curation, 'open') self.assertEqual(new_list.curation, 'open')
models.List.broadcast = real_broadcast
def test_list_page(self, _): def test_list_page(self):
''' there are so many views, this just makes sure it LOADS ''' ''' there are so many views, this just makes sure it LOADS '''
view = views.List.as_view() view = views.List.as_view()
request = self.factory.get('') request = self.factory.get('')
@ -118,8 +126,17 @@ class ListViews(TestCase):
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
def test_list_edit(self, _): def test_list_edit(self):
''' edit a list ''' ''' edit a list '''
real_broadcast = models.List.broadcast
def mock_broadcast(_, activity, user):
''' 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']['id'], self.list.remote_id)
models.List.broadcast = mock_broadcast
view = views.List.as_view() view = views.List.as_view()
request = self.factory.post('', { request = self.factory.post('', {
'name': 'New Name', 'name': 'New Name',
@ -130,7 +147,6 @@ class ListViews(TestCase):
}) })
request.user = self.local_user request.user = self.local_user
# TODO should broadcast?
result = view(request, self.list.id) result = view(request, self.list.id)
self.assertEqual(result.status_code, 302) self.assertEqual(result.status_code, 302)
@ -139,14 +155,16 @@ class ListViews(TestCase):
self.assertEqual(self.list.description, 'wow') self.assertEqual(self.list.description, 'wow')
self.assertEqual(self.list.privacy, 'direct') self.assertEqual(self.list.privacy, 'direct')
self.assertEqual(self.list.curation, 'curated') self.assertEqual(self.list.curation, 'curated')
models.List.broadcast = real_broadcast
def test_curate_page(self, _): def test_curate_page(self):
''' there are so many views, this just makes sure it LOADS ''' ''' there are so many views, this just makes sure it LOADS '''
view = views.Curate.as_view() view = views.Curate.as_view()
models.List.objects.create(name='Public list', user=self.local_user) with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
models.List.objects.create( models.List.objects.create(name='Public list', user=self.local_user)
name='Private list', privacy='private', user=self.local_user) models.List.objects.create(
name='Private list', privacy='private', user=self.local_user)
request = self.factory.get('') request = self.factory.get('')
request.user = self.local_user request.user = self.local_user
@ -160,15 +178,25 @@ class ListViews(TestCase):
self.assertEqual(result.status_code, 302) self.assertEqual(result.status_code, 302)
def test_curate_approve(self, _): def test_curate_approve(self):
''' approve a pending item ''' ''' approve a pending item '''
real_broadcast = models.List.broadcast
def mock_broadcast(_, activity, user):
''' 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['target'], self.list.remote_id)
models.ListItem.broadcast = mock_broadcast
view = views.Curate.as_view() view = views.Curate.as_view()
pending = models.ListItem.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
book_list=self.list, pending = models.ListItem.objects.create(
user=self.local_user, book_list=self.list,
book=self.book, user=self.local_user,
approved=False book=self.book,
) approved=False
)
request = self.factory.post('', { request = self.factory.post('', {
'item': pending.id, 'item': pending.id,
@ -181,17 +209,19 @@ class ListViews(TestCase):
self.assertEqual(self.list.books.count(), 1) self.assertEqual(self.list.books.count(), 1)
self.assertEqual(self.list.listitem_set.first(), pending) self.assertEqual(self.list.listitem_set.first(), pending)
self.assertTrue(pending.approved) self.assertTrue(pending.approved)
models.ListItem.broadcast = real_broadcast
def test_curate_reject(self, _): def test_curate_reject(self):
''' approve a pending item ''' ''' approve a pending item '''
view = views.Curate.as_view() view = views.Curate.as_view()
pending = models.ListItem.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
book_list=self.list, pending = models.ListItem.objects.create(
user=self.local_user, book_list=self.list,
book=self.book, user=self.local_user,
approved=False book=self.book,
) approved=False
)
request = self.factory.post('', { request = self.factory.post('', {
'item': pending.id, 'item': pending.id,
@ -199,13 +229,22 @@ class ListViews(TestCase):
}) })
request.user = self.local_user request.user = self.local_user
view(request, self.list.id) with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
view(request, self.list.id)
self.assertFalse(self.list.books.exists()) self.assertFalse(self.list.books.exists())
self.assertFalse(models.ListItem.objects.exists()) self.assertFalse(models.ListItem.objects.exists())
def test_add_book(self, _): def test_add_book(self):
''' put a book on a list ''' ''' put a book on a list '''
real_broadcast = models.List.broadcast
def mock_broadcast(_, activity, user):
''' 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['target'], self.list.remote_id)
models.ListItem.broadcast = mock_broadcast
request = self.factory.post('', { request = self.factory.post('', {
'book': self.book.id, 'book': self.book.id,
}) })
@ -216,12 +255,21 @@ class ListViews(TestCase):
self.assertEqual(item.book, self.book) self.assertEqual(item.book, self.book)
self.assertEqual(item.user, self.local_user) self.assertEqual(item.user, self.local_user)
self.assertTrue(item.approved) self.assertTrue(item.approved)
models.ListItem.broadcast = real_broadcast
def test_add_book_outsider(self, _): def test_add_book_outsider(self):
''' put a book on a list ''' ''' put a book on a list '''
real_broadcast = models.List.broadcast
def mock_broadcast(_, activity, user):
''' ok '''
self.assertEqual(user.remote_id, self.rat.remote_id)
self.assertEqual(activity['type'], 'Add')
self.assertEqual(activity['actor'], self.rat.remote_id)
self.assertEqual(activity['target'], self.list.remote_id)
models.ListItem.broadcast = mock_broadcast
self.list.curation = 'open' self.list.curation = 'open'
self.list.save() self.list.save(broadcast=False)
request = self.factory.post('', { request = self.factory.post('', {
'book': self.book.id, 'book': self.book.id,
}) })
@ -232,12 +280,22 @@ class ListViews(TestCase):
self.assertEqual(item.book, self.book) self.assertEqual(item.book, self.book)
self.assertEqual(item.user, self.rat) self.assertEqual(item.user, self.rat)
self.assertTrue(item.approved) self.assertTrue(item.approved)
models.ListItem.broadcast = real_broadcast
def test_add_book_pending(self, _): def test_add_book_pending(self):
''' put a book on a list ''' ''' put a book on a list awaiting approval '''
real_broadcast = models.List.broadcast
def mock_broadcast(_, activity, user):
''' ok '''
self.assertEqual(user.remote_id, self.rat.remote_id)
self.assertEqual(activity['type'], 'Add')
self.assertEqual(activity['actor'], self.rat.remote_id)
self.assertEqual(activity['target'], self.list.remote_id)
self.assertEqual(activity['object']['id'], self.book.remote_id)
models.ListItem.broadcast = mock_broadcast
self.list.curation = 'curated' self.list.curation = 'curated'
self.list.save() self.list.save(broadcast=False)
request = self.factory.post('', { request = self.factory.post('', {
'book': self.book.id, 'book': self.book.id,
}) })
@ -248,12 +306,22 @@ class ListViews(TestCase):
self.assertEqual(item.book, self.book) self.assertEqual(item.book, self.book)
self.assertEqual(item.user, self.rat) self.assertEqual(item.user, self.rat)
self.assertFalse(item.approved) self.assertFalse(item.approved)
models.ListItem.broadcast = real_broadcast
def test_add_book_self_curated(self, _): def test_add_book_self_curated(self):
''' put a book on a list ''' ''' put a book on a list automatically approved '''
real_broadcast = models.ListItem.broadcast
def mock_broadcast(_, activity, user):
''' 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['target'], self.list.remote_id)
models.ListItem.broadcast = mock_broadcast
self.list.curation = 'curated' self.list.curation = 'curated'
self.list.save() self.list.save(broadcast=False)
request = self.factory.post('', { request = self.factory.post('', {
'book': self.book.id, 'book': self.book.id,
}) })
@ -264,16 +332,28 @@ class ListViews(TestCase):
self.assertEqual(item.book, self.book) self.assertEqual(item.book, self.book)
self.assertEqual(item.user, self.local_user) self.assertEqual(item.user, self.local_user)
self.assertTrue(item.approved) self.assertTrue(item.approved)
models.ListItem.broadcast = real_broadcast
def test_remove_book(self, _): def test_remove_book(self):
''' take an item off a list ''' ''' take an item off a list '''
item = models.ListItem.objects.create( real_broadcast = models.ListItem.broadcast
book_list=self.list,
user=self.local_user, with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
book=self.book, item = models.ListItem.objects.create(
) book_list=self.list,
user=self.local_user,
book=self.book,
)
self.assertTrue(self.list.listitem_set.exists()) self.assertTrue(self.list.listitem_set.exists())
def mock_broadcast(_, activity, user):
''' 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['target'], self.list.remote_id)
models.ListItem.broadcast = mock_broadcast
request = self.factory.post('', { request = self.factory.post('', {
'item': item.id, 'item': item.id,
}) })
@ -282,15 +362,17 @@ class ListViews(TestCase):
views.list.remove_book(request, self.list.id) views.list.remove_book(request, self.list.id)
self.assertFalse(self.list.listitem_set.exists()) self.assertFalse(self.list.listitem_set.exists())
models.ListItem.broadcast = real_broadcast
def test_remove_book_unauthorized(self, _): def test_remove_book_unauthorized(self):
''' take an item off a list ''' ''' take an item off a list '''
item = models.ListItem.objects.create( with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
book_list=self.list, item = models.ListItem.objects.create(
user=self.local_user, book_list=self.list,
book=self.book, user=self.local_user,
) book=self.book,
)
self.assertTrue(self.list.listitem_set.exists()) self.assertTrue(self.list.listitem_set.exists())
request = self.factory.post('', { request = self.factory.post('', {
'item': item.id, 'item': item.id,

View file

@ -124,7 +124,7 @@ class List(View):
@method_decorator(login_required, name='dispatch') @method_decorator(login_required, name='dispatch')
# pylint: disable=unused-argument # pylint: disable=unused-argument
def post(self, request, list_id): def post(self, request, list_id):
''' edit a book_list ''' ''' edit a list '''
book_list = get_object_or_404(models.List, id=list_id) book_list = get_object_or_404(models.List, id=list_id)
form = forms.ListForm(request.POST, instance=book_list) form = forms.ListForm(request.POST, instance=book_list)
if not form.is_valid(): if not form.is_valid():