Fix tests (for realz)

This commit is contained in:
Joachim 2021-05-27 23:19:17 +02:00
parent d9d34d8220
commit f8df501239
13 changed files with 112 additions and 95 deletions

View file

@ -98,10 +98,11 @@ class BaseActivity(TestCase):
status=200,
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
result = resolve_remote_id(
"https://example.com/user/mouse", model=models.User
)
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
with patch("bookwyrm.models.user.set_remote_server.delay"):
result = resolve_remote_id(
"https://example.com/user/mouse", model=models.User
)
self.assertIsInstance(result, models.User)
self.assertEqual(result.remote_id, "https://example.com/user/mouse")
self.assertEqual(result.name, "MOUSE?? MOUSE!!")

View file

@ -111,8 +111,9 @@ class AbstractConnector(TestCase):
responses.add(
responses.GET, "https://example.com/book/abcd", json=self.edition_data
)
with patch("bookwyrm.connectors.abstract_connector.load_more_data.delay"):
result = self.connector.get_or_create_book("https://example.com/book/abcd")
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
with patch("bookwyrm.connectors.abstract_connector.load_more_data.delay"):
result = self.connector.get_or_create_book("https://example.com/book/abcd")
self.assertEqual(result, self.book)
self.assertEqual(models.Edition.objects.count(), 1)
self.assertEqual(models.Edition.objects.count(), 1)

View file

@ -230,11 +230,12 @@ class Openlibrary(TestCase):
json={"hi": "there"},
status=200,
)
with patch(
"bookwyrm.connectors.openlibrary.Connector." "get_authors_from_data"
) as mock:
mock.return_value = []
result = self.connector.create_edition_from_data(work, self.edition_data)
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
with patch(
"bookwyrm.connectors.openlibrary.Connector." "get_authors_from_data"
) as mock:
mock.return_value = []
result = self.connector.create_edition_from_data(work, self.edition_data)
self.assertEqual(result.parent_work, work)
self.assertEqual(result.title, "Sabriel")
self.assertEqual(result.isbn_10, "0060273224")

View file

@ -36,7 +36,7 @@ class SelfConnector(TestCase):
published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc),
)
edition.authors.add(author)
result = self.connector.search("Edition of Example")[0]
result = self.connector.search("Edition of Example")[0]
self.assertEqual(result.title, "Edition of Example Work")
self.assertEqual(result.key, edition.remote_id)
self.assertEqual(result.author, "Anonymous")
@ -46,7 +46,7 @@ class SelfConnector(TestCase):
def test_search_rank(self):
"""prioritize certain results"""
author = models.Author.objects.create(name="Anonymous")
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
edition = models.Edition.objects.create(
title="Edition of Example Work",
published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc),
@ -74,7 +74,7 @@ class SelfConnector(TestCase):
title="An Edition", parent_work=models.Work.objects.create(title="")
)
results = self.connector.search("Anonymous")
results = self.connector.search("Anonymous")
self.assertEqual(len(results), 3)
self.assertEqual(results[0].title, "Anonymous")
self.assertEqual(results[1].title, "More Editions")
@ -94,17 +94,17 @@ class SelfConnector(TestCase):
)
edition_3 = models.Edition.objects.create(title="Fish", parent_work=work)
# pick the best edition
results = self.connector.search("Edition 1 Title")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_1.remote_id)
# pick the best edition
results = self.connector.search("Edition 1 Title")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_1.remote_id)
# pick the default edition when no match is best
results = self.connector.search("Edition Title")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_2.remote_id)
# pick the default edition when no match is best
results = self.connector.search("Edition Title")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_2.remote_id)
# only matches one edition, so no deduplication takes place
results = self.connector.search("Fish")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_3.remote_id)
# only matches one edition, so no deduplication takes place
results = self.connector.search("Fish")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_3.remote_id)

View file

@ -254,12 +254,12 @@ class ActivitypubFields(TestCase):
"rat", "rat@rat.rat", "ratword", local=True, localname="rat"
)
# test receiving an unknown remote id and loading data
responses.add(
responses.GET, "https://example.com/user/mouse", json=userdata, status=200
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
value = instance.field_from_activity("https://example.com/user/mouse")
# test receiving an unknown remote id and loading data
responses.add(
responses.GET, "https://example.com/user/mouse", json=userdata, status=200
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
value = instance.field_from_activity("https://example.com/user/mouse")
self.assertIsInstance(value, User)
self.assertNotEqual(value, unrelated_user)
self.assertEqual(value.remote_id, "https://example.com/user/mouse")
@ -278,8 +278,8 @@ class ActivitypubFields(TestCase):
unrelated_user = User.objects.create_user(
"rat", "rat@rat.rat", "ratword", local=True, localname="rat"
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
value = instance.field_from_activity(activitypub.Person(**userdata))
with patch("bookwyrm.models.user.set_remote_server.delay"):
value = instance.field_from_activity(activitypub.Person(**userdata))
self.assertIsInstance(value, User)
self.assertNotEqual(value, unrelated_user)
self.assertEqual(value.remote_id, "https://example.com/user/mouse")
@ -302,8 +302,8 @@ class ActivitypubFields(TestCase):
"rat", "rat@rat.rat", "ratword", local=True, localname="rat"
)
with patch("bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast"):
value = instance.field_from_activity(activitypub.Person(**userdata))
with patch("bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast"):
value = instance.field_from_activity(activitypub.Person(**userdata))
self.assertEqual(value, user)
def test_foreign_key_from_activity_str_existing(self):
@ -357,10 +357,11 @@ class ActivitypubFields(TestCase):
responses.add(
responses.GET, "https://example.com/user/mouse", json=userdata, status=200
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
value = instance.field_from_activity(
["https://example.com/user/mouse", "bleh"]
)
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
with patch("bookwyrm.models.user.set_remote_server.delay"):
value = instance.field_from_activity(
["https://example.com/user/mouse", "bleh"]
)
self.assertIsInstance(value, list)
self.assertEqual(len(value), 1)
self.assertIsInstance(value[0], User)

View file

@ -175,6 +175,7 @@ class ImportJob(TestCase):
with patch(
"bookwyrm.connectors.openlibrary.Connector." "get_authors_from_data"
):
book = self.item_1.get_book_from_isbn()
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
book = self.item_1.get_book_from_isbn()
self.assertEqual(book.title, "Sabriel")

View file

@ -60,7 +60,7 @@ class Status(TestCase):
child = models.Status.objects.create(
content="hello", reply_parent=parent, user=self.local_user
)
with patch("bookwyrm.preview_images.generate_edition_image_task.delay"):
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
models.Review.objects.create(
content="hey", reply_parent=parent, user=self.local_user, book=self.book
)

View file

@ -162,9 +162,10 @@ class InboxCreate(TestCase):
"rating": 3,
"@context": "https://www.w3.org/ns/activitystreams",
}
with patch("bookwyrm.activitystreams.ActivityStream.add_status") as redis_mock:
views.inbox.activity_task(activity)
self.assertTrue(redis_mock.called)
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
with patch("bookwyrm.activitystreams.ActivityStream.add_status") as redis_mock:
views.inbox.activity_task(activity)
self.assertTrue(redis_mock.called)
rating = models.ReviewRating.objects.first()
self.assertEqual(rating.book, book)
self.assertEqual(rating.rating, 3.0)

View file

@ -98,16 +98,18 @@ class InboxUpdate(TestCase):
del userdata["icon"]
self.assertIsNone(self.remote_user.name)
self.assertFalse(self.remote_user.discoverable)
views.inbox.activity_task(
{
"type": "Update",
"to": [],
"cc": [],
"actor": "hi",
"id": "sdkjf",
"object": userdata,
}
)
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
views.inbox.activity_task(
{
"type": "Update",
"to": [],
"cc": [],
"actor": "hi",
"id": "sdkjf",
"object": userdata,
}
)
user = models.User.objects.get(id=self.remote_user.id)
self.assertEqual(user.name, "RAT???")
self.assertEqual(user.username, "rat@example.com")
@ -130,20 +132,20 @@ class InboxUpdate(TestCase):
title="Test Book", remote_id="https://bookwyrm.social/book/5989"
)
del bookdata["authors"]
self.assertEqual(book.title, "Test Book")
del bookdata["authors"]
self.assertEqual(book.title, "Test Book")
with patch("bookwyrm.activitypub.base_activity.set_related_field.delay"):
views.inbox.activity_task(
{
"type": "Update",
"to": [],
"cc": [],
"actor": "hi",
"id": "sdkjf",
"object": bookdata,
}
)
with patch("bookwyrm.activitypub.base_activity.set_related_field.delay"):
views.inbox.activity_task(
{
"type": "Update",
"to": [],
"cc": [],
"actor": "hi",
"id": "sdkjf",
"object": bookdata,
}
)
book = models.Edition.objects.get(id=book.id)
self.assertEqual(book.title, "Piranesi")
self.assertEqual(book.last_edited_by, self.remote_user)

View file

@ -74,7 +74,8 @@ class BookViews(TestCase):
request = self.factory.get("")
request.user = self.local_user
request.user.is_superuser = True
result = view(request, self.book.id)
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
result = view(request, self.book.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
@ -89,7 +90,7 @@ class BookViews(TestCase):
request = self.factory.post("", form.data)
request.user = self.local_user
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
view(request, self.book.id)
@ -127,7 +128,7 @@ class BookViews(TestCase):
request = self.factory.post("", form.data)
request.user = self.local_user
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
view(request, self.book.id)
@ -149,8 +150,9 @@ class BookViews(TestCase):
request = self.factory.post("", form.data)
request.user = self.local_user
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
view(request, self.book.id)
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
view(request, self.book.id)
self.book.refresh_from_db()
self.assertEqual(self.book.title, "New Title")
self.assertFalse(self.book.authors.exists())
@ -165,7 +167,8 @@ class BookViews(TestCase):
request = self.factory.post("", form.data)
request.user = self.local_user
view(request)
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
view(request)
book = models.Edition.objects.get(title="New Title")
self.assertEqual(book.parent_work.title, "New Title")
@ -180,7 +183,8 @@ class BookViews(TestCase):
request = self.factory.post("", form.data)
request.user = self.local_user
view(request)
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
view(request)
book = models.Edition.objects.get(title="New Title")
self.assertEqual(book.parent_work, self.work)
@ -196,7 +200,8 @@ class BookViews(TestCase):
request = self.factory.post("", form.data)
request.user = self.local_user
view(request)
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
view(request)
book = models.Edition.objects.get(title="New Title")
self.assertEqual(book.parent_work.title, "New Title")
self.assertEqual(book.authors.first().name, "Sappho")

View file

@ -55,11 +55,12 @@ class GetStartedViews(TestCase):
request.user = self.local_user
self.assertIsNone(self.local_user.name)
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
view(request)
self.assertEqual(delay_mock.call_count, 1)
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
view(request)
self.assertEqual(delay_mock.call_count, 1)
self.assertEqual(self.local_user.name, "New Name")
self.assertTrue(self.local_user.discoverable)

View file

@ -144,10 +144,11 @@ class ViewsHelpers(TestCase):
json=self.userdata,
status=200,
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
result = views.helpers.handle_remote_webfinger("@mouse@example.com")
self.assertIsInstance(result, models.User)
self.assertEqual(result.username, "mouse@example.com")
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
with patch("bookwyrm.models.user.set_remote_server.delay"):
result = views.helpers.handle_remote_webfinger("@mouse@example.com")
self.assertIsInstance(result, models.User)
self.assertEqual(result.username, "mouse@example.com")
def test_user_on_blocked_server(self, _):
"""find a remote user using webfinger"""

View file

@ -162,11 +162,12 @@ class UserViews(TestCase):
request.user = self.local_user
self.assertIsNone(self.local_user.name)
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
view(request)
self.assertEqual(delay_mock.call_count, 1)
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
view(request)
self.assertEqual(delay_mock.call_count, 1)
self.assertEqual(self.local_user.name, "New Name")
self.assertEqual(self.local_user.email, "wow@email.com")
@ -186,11 +187,12 @@ class UserViews(TestCase):
request = self.factory.post("", form.data)
request.user = self.local_user
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
view(request)
self.assertEqual(delay_mock.call_count, 1)
with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
with patch(
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
) as delay_mock:
view(request)
self.assertEqual(delay_mock.call_count, 1)
self.assertEqual(self.local_user.name, "New Name")
self.assertEqual(self.local_user.email, "wow@email.com")
self.assertIsNotNone(self.local_user.avatar)