Updates mock paradigm in lists tests

This commit is contained in:
Mouse Reeve 2021-04-08 14:20:16 -07:00
parent 31077a9790
commit af58455ae9

View file

@ -1,4 +1,5 @@
""" test for app action functionality """
import json
from unittest.mock import patch
from django.contrib.auth.models import AnonymousUser
@ -71,16 +72,6 @@ class ListViews(TestCase):
def test_lists_create(self):
""" create list view """
real_broadcast = models.List.broadcast
def mock_broadcast(_, activity, user, **kwargs):
""" 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(
"",
@ -93,13 +84,19 @@ class ListViews(TestCase):
},
)
request.user = self.local_user
result = view(request)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
result = view(request)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args_list[1][0][1])
self.assertEqual(activity["type"], "Create")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(result.status_code, 302)
new_list = models.List.objects.filter(name="A list").get()
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):
""" there are so many views, this just makes sure it LOADS """
@ -138,17 +135,6 @@ class ListViews(TestCase):
def test_list_edit(self):
""" edit a list """
real_broadcast = models.List.broadcast
def mock_broadcast(_, activity, user, **kwargs):
""" 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(
"",
@ -162,7 +148,15 @@ class ListViews(TestCase):
)
request.user = self.local_user
result = view(request, self.list.id)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
result = view(request, self.list.id)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args_list[1][0][1])
self.assertEqual(activity["type"], "Update")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["object"]["id"], self.list.remote_id)
self.assertEqual(result.status_code, 302)
self.list.refresh_from_db()
@ -170,7 +164,6 @@ 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):
""" there are so many views, this just makes sure it LOADS """
@ -194,17 +187,6 @@ class ListViews(TestCase):
def test_curate_approve(self):
""" approve a pending item """
real_broadcast = models.List.broadcast
def mock_broadcast(_, activity, user, **kwargs):
""" 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()
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
pending = models.ListItem.objects.create(
@ -223,12 +205,19 @@ class ListViews(TestCase):
)
request.user = self.local_user
view(request, self.list.id)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
view(request, self.list.id)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args_list[1][0][1])
self.assertEqual(activity["type"], "Add")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["target"], self.list.remote_id)
pending.refresh_from_db()
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):
""" approve a pending item """
@ -250,23 +239,15 @@ class ListViews(TestCase):
)
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
view(request, self.list.id)
self.assertEqual(mock.call_count, 1)
#activity = json.loads(mock.call_args_list[1][0][1])
self.assertFalse(self.list.books.exists())
self.assertFalse(models.ListItem.objects.exists())
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(
"",
{
@ -276,25 +257,21 @@ class ListViews(TestCase):
)
request.user = self.local_user
views.list.add_book(request)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
views.list.add_book(request)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args_list[1][0][1])
self.assertEqual(activity["type"], "Add")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["target"], self.list.remote_id)
item = self.list.listitem_set.get()
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):
""" 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(broadcast=False)
request = self.factory.post(
@ -306,26 +283,21 @@ class ListViews(TestCase):
)
request.user = self.rat
views.list.add_book(request)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
views.list.add_book(request)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args_list[1][0][1])
self.assertEqual(activity["type"], "Add")
self.assertEqual(activity["actor"], self.rat.remote_id)
self.assertEqual(activity["target"], self.list.remote_id)
item = self.list.listitem_set.get()
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 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(broadcast=False)
request = self.factory.post(
@ -337,26 +309,21 @@ class ListViews(TestCase):
)
request.user = self.rat
views.list.add_book(request)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
views.list.add_book(request)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args_list[1][0][1])
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)
item = self.list.listitem_set.get()
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 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(broadcast=False)
request = self.factory.post(
@ -368,16 +335,21 @@ class ListViews(TestCase):
)
request.user = self.local_user
views.list.add_book(request)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
views.list.add_book(request)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args_list[1][0][1])
self.assertEqual(activity["type"], "Add")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["target"], self.list.remote_id)
item = self.list.listitem_set.get()
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):
""" take an item off a list """
real_broadcast = models.ListItem.broadcast
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
item = models.ListItem.objects.create(
@ -387,14 +359,6 @@ class ListViews(TestCase):
)
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(
"",
{
@ -403,10 +367,15 @@ class ListViews(TestCase):
)
request.user = self.local_user
views.list.remove_book(request, self.list.id)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
views.list.remove_book(request, self.list.id)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args_list[1][0][1])
self.assertEqual(activity["type"], "Remove")
self.assertEqual(activity["actor"], self.local_user.remote_id)
self.assertEqual(activity["target"], self.list.remote_id)
self.assertFalse(self.list.listitem_set.exists())
models.ListItem.broadcast = real_broadcast
def test_remove_book_unauthorized(self):
""" take an item off a list """
@ -425,6 +394,10 @@ class ListViews(TestCase):
)
request.user = self.rat
views.list.remove_book(request, self.list.id)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
views.list.remove_book(request, self.list.id)
self.assertEqual(mock.call_count, 1)
activity = json.loads(mock.call_args_list[1][0][1])
self.assertEqual(activity["type"], "Remove")
self.assertTrue(self.list.listitem_set.exists())