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, status=200,
) )
with patch("bookwyrm.models.user.set_remote_server.delay"): with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
result = resolve_remote_id( with patch("bookwyrm.models.user.set_remote_server.delay"):
"https://example.com/user/mouse", model=models.User result = resolve_remote_id(
) "https://example.com/user/mouse", model=models.User
)
self.assertIsInstance(result, models.User) self.assertIsInstance(result, models.User)
self.assertEqual(result.remote_id, "https://example.com/user/mouse") self.assertEqual(result.remote_id, "https://example.com/user/mouse")
self.assertEqual(result.name, "MOUSE?? MOUSE!!") self.assertEqual(result.name, "MOUSE?? MOUSE!!")

View file

@ -111,8 +111,9 @@ class AbstractConnector(TestCase):
responses.add( responses.add(
responses.GET, "https://example.com/book/abcd", json=self.edition_data responses.GET, "https://example.com/book/abcd", json=self.edition_data
) )
with patch("bookwyrm.connectors.abstract_connector.load_more_data.delay"): with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
result = self.connector.get_or_create_book("https://example.com/book/abcd") 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(result, self.book)
self.assertEqual(models.Edition.objects.count(), 1) self.assertEqual(models.Edition.objects.count(), 1)
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"}, json={"hi": "there"},
status=200, status=200,
) )
with patch( with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
"bookwyrm.connectors.openlibrary.Connector." "get_authors_from_data" with patch(
) as mock: "bookwyrm.connectors.openlibrary.Connector." "get_authors_from_data"
mock.return_value = [] ) as mock:
result = self.connector.create_edition_from_data(work, self.edition_data) mock.return_value = []
result = self.connector.create_edition_from_data(work, self.edition_data)
self.assertEqual(result.parent_work, work) self.assertEqual(result.parent_work, work)
self.assertEqual(result.title, "Sabriel") self.assertEqual(result.title, "Sabriel")
self.assertEqual(result.isbn_10, "0060273224") 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), published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc),
) )
edition.authors.add(author) 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.title, "Edition of Example Work")
self.assertEqual(result.key, edition.remote_id) self.assertEqual(result.key, edition.remote_id)
self.assertEqual(result.author, "Anonymous") self.assertEqual(result.author, "Anonymous")
@ -46,7 +46,7 @@ class SelfConnector(TestCase):
def test_search_rank(self): def test_search_rank(self):
"""prioritize certain results""" """prioritize certain results"""
author = models.Author.objects.create(name="Anonymous") 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( edition = models.Edition.objects.create(
title="Edition of Example Work", title="Edition of Example Work",
published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc), 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="") 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(len(results), 3)
self.assertEqual(results[0].title, "Anonymous") self.assertEqual(results[0].title, "Anonymous")
self.assertEqual(results[1].title, "More Editions") 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) edition_3 = models.Edition.objects.create(title="Fish", parent_work=work)
# pick the best edition # pick the best edition
results = self.connector.search("Edition 1 Title") results = self.connector.search("Edition 1 Title")
self.assertEqual(len(results), 1) self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_1.remote_id) self.assertEqual(results[0].key, edition_1.remote_id)
# pick the default edition when no match is best # pick the default edition when no match is best
results = self.connector.search("Edition Title") results = self.connector.search("Edition Title")
self.assertEqual(len(results), 1) self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_2.remote_id) self.assertEqual(results[0].key, edition_2.remote_id)
# only matches one edition, so no deduplication takes place # only matches one edition, so no deduplication takes place
results = self.connector.search("Fish") results = self.connector.search("Fish")
self.assertEqual(len(results), 1) self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_3.remote_id) 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" "rat", "rat@rat.rat", "ratword", local=True, localname="rat"
) )
# test receiving an unknown remote id and loading data # test receiving an unknown remote id and loading data
responses.add( responses.add(
responses.GET, "https://example.com/user/mouse", json=userdata, status=200 responses.GET, "https://example.com/user/mouse", json=userdata, status=200
) )
with patch("bookwyrm.models.user.set_remote_server.delay"): with patch("bookwyrm.models.user.set_remote_server.delay"):
value = instance.field_from_activity("https://example.com/user/mouse") value = instance.field_from_activity("https://example.com/user/mouse")
self.assertIsInstance(value, User) self.assertIsInstance(value, User)
self.assertNotEqual(value, unrelated_user) self.assertNotEqual(value, unrelated_user)
self.assertEqual(value.remote_id, "https://example.com/user/mouse") self.assertEqual(value.remote_id, "https://example.com/user/mouse")
@ -278,8 +278,8 @@ class ActivitypubFields(TestCase):
unrelated_user = User.objects.create_user( unrelated_user = User.objects.create_user(
"rat", "rat@rat.rat", "ratword", local=True, localname="rat" "rat", "rat@rat.rat", "ratword", local=True, localname="rat"
) )
with patch("bookwyrm.models.user.set_remote_server.delay"): with patch("bookwyrm.models.user.set_remote_server.delay"):
value = instance.field_from_activity(activitypub.Person(**userdata)) value = instance.field_from_activity(activitypub.Person(**userdata))
self.assertIsInstance(value, User) self.assertIsInstance(value, User)
self.assertNotEqual(value, unrelated_user) self.assertNotEqual(value, unrelated_user)
self.assertEqual(value.remote_id, "https://example.com/user/mouse") 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" "rat", "rat@rat.rat", "ratword", local=True, localname="rat"
) )
with patch("bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast"): with patch("bookwyrm.models.activitypub_mixin.ObjectMixin.broadcast"):
value = instance.field_from_activity(activitypub.Person(**userdata)) value = instance.field_from_activity(activitypub.Person(**userdata))
self.assertEqual(value, user) self.assertEqual(value, user)
def test_foreign_key_from_activity_str_existing(self): def test_foreign_key_from_activity_str_existing(self):
@ -357,10 +357,11 @@ class ActivitypubFields(TestCase):
responses.add( responses.add(
responses.GET, "https://example.com/user/mouse", json=userdata, status=200 responses.GET, "https://example.com/user/mouse", json=userdata, status=200
) )
with patch("bookwyrm.models.user.set_remote_server.delay"): with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"):
value = instance.field_from_activity( with patch("bookwyrm.models.user.set_remote_server.delay"):
["https://example.com/user/mouse", "bleh"] value = instance.field_from_activity(
) ["https://example.com/user/mouse", "bleh"]
)
self.assertIsInstance(value, list) self.assertIsInstance(value, list)
self.assertEqual(len(value), 1) self.assertEqual(len(value), 1)
self.assertIsInstance(value[0], User) self.assertIsInstance(value[0], User)

View file

@ -175,6 +175,7 @@ class ImportJob(TestCase):
with patch( with patch(
"bookwyrm.connectors.openlibrary.Connector." "get_authors_from_data" "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") self.assertEqual(book.title, "Sabriel")

View file

@ -60,7 +60,7 @@ class Status(TestCase):
child = models.Status.objects.create( child = models.Status.objects.create(
content="hello", reply_parent=parent, user=self.local_user 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( models.Review.objects.create(
content="hey", reply_parent=parent, user=self.local_user, book=self.book content="hey", reply_parent=parent, user=self.local_user, book=self.book
) )

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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