From 0fd2885e7e026374f25cfdd323d9619f56b88e2b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 9 Feb 2021 09:41:04 -0800 Subject: [PATCH] Tests broadcasting from list views more thoroughly --- bookwyrm/tests/views/test_list.py | 176 ++++++++++++++++++++++-------- bookwyrm/views/list.py | 2 +- 2 files changed, 130 insertions(+), 48 deletions(-) diff --git a/bookwyrm/tests/views/test_list.py b/bookwyrm/tests/views/test_list.py index 8869aa15..763c3b34 100644 --- a/bookwyrm/tests/views/test_list.py +++ b/bookwyrm/tests/views/test_list.py @@ -10,7 +10,6 @@ from bookwyrm import models, views from bookwyrm.activitypub import ActivitypubResponse -@patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay') class ListViews(TestCase): ''' tag views''' def setUp(self): @@ -40,11 +39,11 @@ class ListViews(TestCase): 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 ''' 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'): + models.List.objects.create(name='Public list', user=self.local_user) models.List.objects.create( name='Private list', privacy='private', user=self.local_user) request = self.factory.get('') @@ -63,8 +62,16 @@ class ListViews(TestCase): self.assertEqual(result.status_code, 200) - def test_lists_create(self, _): + def test_lists_create(self): ''' 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() request = self.factory.post('', { 'name': 'A list', @@ -80,9 +87,10 @@ class ListViews(TestCase): self.assertEqual(new_list.description, 'wow') self.assertEqual(new_list.privacy, 'unlisted') 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 ''' view = views.List.as_view() request = self.factory.get('') @@ -118,8 +126,17 @@ class ListViews(TestCase): self.assertEqual(result.status_code, 200) - def test_list_edit(self, _): + def test_list_edit(self): ''' 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() request = self.factory.post('', { 'name': 'New Name', @@ -130,7 +147,6 @@ class ListViews(TestCase): }) request.user = self.local_user - # TODO should broadcast? result = view(request, self.list.id) self.assertEqual(result.status_code, 302) @@ -139,14 +155,16 @@ class ListViews(TestCase): self.assertEqual(self.list.description, 'wow') self.assertEqual(self.list.privacy, 'direct') 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 ''' view = views.Curate.as_view() - models.List.objects.create(name='Public list', user=self.local_user) - models.List.objects.create( - name='Private list', privacy='private', user=self.local_user) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + models.List.objects.create(name='Public list', user=self.local_user) + models.List.objects.create( + name='Private list', privacy='private', user=self.local_user) request = self.factory.get('') request.user = self.local_user @@ -160,15 +178,25 @@ class ListViews(TestCase): self.assertEqual(result.status_code, 302) - def test_curate_approve(self, _): + def test_curate_approve(self): ''' 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() - pending = models.ListItem.objects.create( - book_list=self.list, - user=self.local_user, - book=self.book, - approved=False - ) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + pending = models.ListItem.objects.create( + book_list=self.list, + user=self.local_user, + book=self.book, + approved=False + ) request = self.factory.post('', { 'item': pending.id, @@ -181,17 +209,19 @@ class ListViews(TestCase): self.assertEqual(self.list.books.count(), 1) self.assertEqual(self.list.listitem_set.first(), pending) self.assertTrue(pending.approved) + models.ListItem.broadcast = real_broadcast - def test_curate_reject(self, _): + def test_curate_reject(self): ''' approve a pending item ''' view = views.Curate.as_view() - pending = models.ListItem.objects.create( - book_list=self.list, - user=self.local_user, - book=self.book, - approved=False - ) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + pending = models.ListItem.objects.create( + book_list=self.list, + user=self.local_user, + book=self.book, + approved=False + ) request = self.factory.post('', { 'item': pending.id, @@ -199,13 +229,22 @@ class ListViews(TestCase): }) 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(models.ListItem.objects.exists()) - def test_add_book(self, _): + def test_add_book(self): ''' 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('', { 'book': self.book.id, }) @@ -216,12 +255,21 @@ class ListViews(TestCase): self.assertEqual(item.book, self.book) self.assertEqual(item.user, self.local_user) 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 ''' + 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.save() + self.list.save(broadcast=False) request = self.factory.post('', { 'book': self.book.id, }) @@ -232,12 +280,22 @@ class ListViews(TestCase): self.assertEqual(item.book, self.book) self.assertEqual(item.user, self.rat) self.assertTrue(item.approved) + models.ListItem.broadcast = real_broadcast - def test_add_book_pending(self, _): - ''' put a book on a list ''' + def test_add_book_pending(self): + ''' 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.save() + self.list.save(broadcast=False) request = self.factory.post('', { 'book': self.book.id, }) @@ -248,12 +306,22 @@ class ListViews(TestCase): self.assertEqual(item.book, self.book) self.assertEqual(item.user, self.rat) self.assertFalse(item.approved) + models.ListItem.broadcast = real_broadcast - def test_add_book_self_curated(self, _): - ''' put a book on a list ''' + def test_add_book_self_curated(self): + ''' 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.save() + self.list.save(broadcast=False) request = self.factory.post('', { 'book': self.book.id, }) @@ -264,16 +332,28 @@ class ListViews(TestCase): self.assertEqual(item.book, self.book) self.assertEqual(item.user, self.local_user) 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 ''' - item = models.ListItem.objects.create( - book_list=self.list, - user=self.local_user, - book=self.book, - ) + real_broadcast = models.ListItem.broadcast + + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + item = models.ListItem.objects.create( + book_list=self.list, + user=self.local_user, + book=self.book, + ) 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('', { 'item': item.id, }) @@ -282,15 +362,17 @@ class ListViews(TestCase): views.list.remove_book(request, self.list.id) 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 ''' - item = models.ListItem.objects.create( - book_list=self.list, - user=self.local_user, - book=self.book, - ) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + item = models.ListItem.objects.create( + book_list=self.list, + user=self.local_user, + book=self.book, + ) self.assertTrue(self.list.listitem_set.exists()) request = self.factory.post('', { 'item': item.id, diff --git a/bookwyrm/views/list.py b/bookwyrm/views/list.py index 888b2d40..8e315f30 100644 --- a/bookwyrm/views/list.py +++ b/bookwyrm/views/list.py @@ -124,7 +124,7 @@ class List(View): @method_decorator(login_required, name='dispatch') # pylint: disable=unused-argument def post(self, request, list_id): - ''' edit a book_list ''' + ''' edit a list ''' book_list = get_object_or_404(models.List, id=list_id) form = forms.ListForm(request.POST, instance=book_list) if not form.is_valid():