Skip to content

Commit

Permalink
Adds more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mouse-reeve committed Aug 27, 2024
1 parent aee089f commit 6b622ba
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 17 deletions.
42 changes: 42 additions & 0 deletions bookwyrm/tests/views/books/test_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def setUpTestData(cls):
remote_id="https://example.com/book/1",
parent_work=cls.work,
)
cls.another_book = models.Edition.objects.create(
title="Another Example Edition",
remote_id="https://example.com/book/1",
parent_work=models.Work.objects.create(title="Another Work"),
)

models.SiteSettings.objects.create()

Expand Down Expand Up @@ -134,6 +139,43 @@ def test_book_page_statuses(self, *_):
self.assertEqual(result.status_code, 200)
self.assertEqual(result.context_data["statuses"].object_list[0], quote)

@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
def test_book_page_suggestions(self, *_):
"""there are so many views, this just makes sure it LOADS"""
view = views.Book.as_view()
suggestion_list = models.SuggestionList.objects.create(suggests_for=self.work)

request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.book.id, user_statuses="review")
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())

self.assertEqual(result.status_code, 200)
self.assertEqual(result.context_data["suggestion_list"], suggestion_list)

@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
def test_book_page_suggestions_with_items(self, *_):
"""there are so many views, this just makes sure it LOADS"""
view = views.Book.as_view()
suggestion_list = models.SuggestionList.objects.create(suggests_for=self.work)
models.SuggestionListItem.objects.create(
book_list=suggestion_list, user=self.local_user, book=self.another_book
)

request = self.factory.get("")
request.user = self.local_user
with patch("bookwyrm.views.books.books.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.book.id, user_statuses="review")
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())

self.assertEqual(result.status_code, 200)
self.assertEqual(result.context_data["suggestion_list"], suggestion_list)

def test_book_page_invalid_id(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Book.as_view()
Expand Down
95 changes: 85 additions & 10 deletions bookwyrm/tests/views/test_suggestion_list.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" test for app action functionality """
from unittest.mock import patch

from django.core.exceptions import PermissionDenied
from django.test import TestCase
from django.test.client import RequestFactory

Expand Down Expand Up @@ -28,6 +29,14 @@ def setUpTestData(cls):
localname="mouse",
remote_id="https://example.com/users/mouse",
)
cls.another_user = models.User.objects.create_user(
"[email protected]",
"[email protected]",
"ratword",
local=True,
localname="rat",
remote_id="https://example.com/users/rat",
)
cls.work = models.Work.objects.create(title="Test Work")
cls.book = models.Edition.objects.create(
title="Example Edition",
Expand All @@ -48,7 +57,7 @@ def setUp(self):

def test_suggestion_list_get(self, *_):
"""start a suggestion list for a book"""
models.SuggestionList.objects.create(suggests_for=self.book)
models.SuggestionList.objects.create(suggests_for=self.work)
view = views.SuggestionList.as_view()
request = self.factory.get("")
request.user = self.local_user
Expand All @@ -58,7 +67,7 @@ def test_suggestion_list_get(self, *_):

def test_suggestion_list_get_json(self, *_):
"""start a suggestion list for a book"""
models.SuggestionList.objects.create(suggests_for=self.book)
models.SuggestionList.objects.create(suggests_for=self.work)
view = views.SuggestionList.as_view()
request = self.factory.get("")
request.user = self.local_user
Expand All @@ -70,40 +79,106 @@ def test_suggestion_list_get_json(self, *_):

def test_suggestion_create(self, *_):
"""start a suggestion list for a book"""
self.assertFalse(hasattr(self.book, "suggestion_list"))
self.assertFalse(hasattr(self.work, "suggestion_list"))

view = views.SuggestionList.as_view()
form = forms.SuggestionListForm()
form.data["suggests_for"] = self.book.id
form.data["suggests_for"] = self.work.id
request = self.factory.post("", form.data)
request.user = self.local_user

view(request, self.book.id)

self.book.refresh_from_db()
self.assertTrue(hasattr(self.book, "suggestion_list"))
self.work.refresh_from_db()
self.assertTrue(hasattr(self.work, "suggestion_list"))

suggestion_list = self.book.suggestion_list
self.assertEqual(suggestion_list.suggests_for, self.book)
suggestion_list = self.work.suggestion_list
self.assertEqual(suggestion_list.suggests_for, self.work)
self.assertEqual(suggestion_list.privacy, "public")
self.assertEqual(suggestion_list.user, get_representative())

def test_book_add_suggestion(self, *_):
"""Add a book to the recommendation list"""
suggestion_list = models.SuggestionList.objects.create(suggests_for=self.book)
suggestion_list = models.SuggestionList.objects.create(suggests_for=self.work)
view = views.book_add_suggestion

form = forms.SuggestionListItemForm()
form.data["user"] = self.local_user.id
form.data["book"] = self.another_book.id
form.data["book_list"] = suggestion_list.id
form.data["notes"] = "hello"

request = self.factory.post("", form.data)
request.user = self.local_user

view(request, self.book.id)
view(request, self.work.id)

self.assertEqual(suggestion_list.suggestionlistitem_set.count(), 1)
item = suggestion_list.suggestionlistitem_set.first()
self.assertEqual(item.book, self.another_book)
self.assertEqual(item.user, self.local_user)
self.assertEqual(item.notes, "hello")

def test_book_remove_suggestion(self, *_):
"""Remove a book from the recommendation list"""
suggestion_list = models.SuggestionList.objects.create(suggests_for=self.work)
item = models.SuggestionListItem.objects.create(
book_list=suggestion_list, user=self.local_user, book=self.another_book
)
self.assertEqual(suggestion_list.suggestionlistitem_set.count(), 1)

view = views.book_remove_suggestion
request = self.factory.post("", {"item": item.id})
request.user = self.local_user

view(request, self.work.id)

self.assertEqual(suggestion_list.suggestionlistitem_set.count(), 0)

def test_book_remove_suggestion_without_permission(self, *_):
"""Remove a book from the recommendation list"""
suggestion_list = models.SuggestionList.objects.create(suggests_for=self.work)
item = models.SuggestionListItem.objects.create(
book_list=suggestion_list, user=self.local_user, book=self.another_book
)
self.assertEqual(suggestion_list.suggestionlistitem_set.count(), 1)

view = views.book_remove_suggestion
request = self.factory.post("", {"item": item.id})
request.user = self.another_user

with self.assertRaises(PermissionDenied):
view(request, self.work.id)

self.assertEqual(suggestion_list.suggestionlistitem_set.count(), 1)

def test_endorse_suggestion(self, *_):
"""Endorse a suggestion"""
suggestion_list = models.SuggestionList.objects.create(suggests_for=self.work)
item = models.SuggestionListItem.objects.create(
book_list=suggestion_list, user=self.local_user, book=self.another_book
)
self.assertEqual(item.endorsement.count(), 0)
view = views.endorse_suggestion
request = self.factory.post("")
request.user = self.another_user

view(request, self.work.id, item.id)

self.assertEqual(item.endorsement.count(), 1)

def test_endorse_suggestion_by_self(self, *_):
"""Endorse a suggestion error handling"""
suggestion_list = models.SuggestionList.objects.create(suggests_for=self.work)
item = models.SuggestionListItem.objects.create(
book_list=suggestion_list, user=self.local_user, book=self.another_book
)
self.assertEqual(item.endorsement.count(), 0)
view = views.endorse_suggestion
request = self.factory.post("")
request.user = self.local_user

view(request, self.work.id, item.id)

# no impact
self.assertEqual(item.endorsement.count(), 0)
8 changes: 4 additions & 4 deletions bookwyrm/views/books/books.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,18 @@ def get(self, request, book_id, **kwargs):
"quotation_count": book.quotation_set.filter(**filters).count(),
}
if hasattr(book.parent_work, "suggestion_list"):
suggestion_list = book.parent_work.suggestion_list
data["suggestion_list"] = suggestion_list
data["suggestion_list"] = book.parent_work.suggestion_list
data["items"] = (
suggestion_list.suggestionlistitem_set.prefetch_related(
data["suggestion_list"]
.suggestionlistitem_set.prefetch_related(
"user", "book", "book__authors", "endorsement"
)
.annotate(endorsement_count=Count("endorsement"))
.order_by("-endorsement_count")[:3]
)

data["suggested_books"] = get_list_suggestions(
suggestion_list,
data["suggestion_list"],
request.user,
query=request.GET.get("suggestion_query", ""),
ignore_book=book.parent_work,
Expand Down
4 changes: 1 addition & 3 deletions bookwyrm/views/suggestion_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def post(self, request, book_id): # pylint: disable=unused-argument

@login_required
@require_POST
@transaction.atomic
def book_add_suggestion(request, book_id):
"""put a book on the suggestion list"""
_ = get_object_or_404(
Expand All @@ -106,8 +105,7 @@ def book_add_suggestion(request, book_id):
if not form.is_valid():
return Book().get(request, book_id, add_failed=True)

item = form.save(request, commit=False)
item.save()
form.save(request)

return redirect_to_referer(request)

Expand Down

0 comments on commit 6b622ba

Please sign in to comment.