2021-03-08 16:49:10 +00:00
|
|
|
""" test for app action functionality """
|
2020-12-16 00:36:22 +00:00
|
|
|
from unittest.mock import patch
|
2021-01-10 23:56:57 +00:00
|
|
|
import dateutil
|
2020-12-16 00:36:22 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
2021-01-11 00:11:02 +00:00
|
|
|
from django.utils import timezone
|
2020-12-16 00:36:22 +00:00
|
|
|
|
2021-01-13 21:05:16 +00:00
|
|
|
from bookwyrm import models, views
|
2020-12-16 00:36:22 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-09-06 20:53:49 +00:00
|
|
|
@patch("bookwyrm.activitystreams.add_status_task.delay")
|
2021-08-03 19:41:41 +00:00
|
|
|
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
|
2021-09-06 21:50:33 +00:00
|
|
|
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
|
2021-09-07 01:39:14 +00:00
|
|
|
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
|
2021-01-13 21:05:16 +00:00
|
|
|
class ReadingViews(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""viewing and creating statuses"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-12-16 00:36:22 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need basic test data and mocks"""
|
2021-01-13 21:05:16 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-09-06 21:48:45 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.populate_stream_task.delay"
|
2021-12-09 23:03:01 +00:00
|
|
|
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
|
2021-08-03 17:25:53 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse@local.com",
|
|
|
|
"mouse@mouse.com",
|
|
|
|
"mouseword",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
remote_id="https://example.com/users/mouse",
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
|
|
|
self.remote_user = models.User.objects.create_user(
|
|
|
|
"rat",
|
|
|
|
"rat@rat.com",
|
|
|
|
"ratword",
|
|
|
|
local=False,
|
|
|
|
remote_id="https://example.com/users/rat",
|
|
|
|
inbox="https://example.com/users/rat/inbox",
|
|
|
|
outbox="https://example.com/users/rat/outbox",
|
2021-05-26 21:57:29 +00:00
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
self.work = models.Work.objects.create(title="Test Work")
|
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
title="Test Book",
|
|
|
|
remote_id="https://example.com/book/1",
|
|
|
|
parent_work=self.work,
|
|
|
|
)
|
2020-12-16 00:36:22 +00:00
|
|
|
|
2021-08-03 19:41:41 +00:00
|
|
|
def test_start_reading(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""begin a book"""
|
2021-03-25 23:57:20 +00:00
|
|
|
shelf = self.local_user.shelf_set.get(identifier=models.Shelf.READING)
|
2021-01-14 02:02:01 +00:00
|
|
|
self.assertFalse(shelf.books.exists())
|
|
|
|
self.assertFalse(models.Status.objects.exists())
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post(
|
|
|
|
"",
|
|
|
|
{
|
|
|
|
"post-status": True,
|
|
|
|
"privacy": "followers",
|
|
|
|
"start_date": "2020-01-05",
|
2021-10-01 17:57:56 +00:00
|
|
|
"book": self.book.id,
|
|
|
|
"mention_books": self.book.id,
|
|
|
|
"user": self.local_user.id,
|
2021-03-08 16:49:10 +00:00
|
|
|
},
|
|
|
|
)
|
2021-01-14 02:02:01 +00:00
|
|
|
request.user = self.local_user
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
2021-06-09 17:36:52 +00:00
|
|
|
views.ReadingStatus.as_view()(request, "start", self.book.id)
|
2021-01-14 02:02:01 +00:00
|
|
|
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
|
|
|
status = models.GeneratedNote.objects.get()
|
|
|
|
self.assertEqual(status.user, self.local_user)
|
|
|
|
self.assertEqual(status.mention_books.get(), self.book)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(status.privacy, "followers")
|
2021-01-14 02:02:01 +00:00
|
|
|
|
|
|
|
readthrough = models.ReadThrough.objects.get()
|
|
|
|
self.assertIsNotNone(readthrough.start_date)
|
|
|
|
self.assertIsNone(readthrough.finish_date)
|
|
|
|
self.assertEqual(readthrough.user, self.local_user)
|
|
|
|
self.assertEqual(readthrough.book, self.book)
|
|
|
|
|
2021-10-01 17:57:56 +00:00
|
|
|
def test_start_reading_with_comment(self, *_):
|
|
|
|
"""begin a book"""
|
|
|
|
shelf = self.local_user.shelf_set.get(identifier=models.Shelf.READING)
|
|
|
|
self.assertFalse(shelf.books.exists())
|
|
|
|
self.assertFalse(models.Status.objects.exists())
|
|
|
|
|
|
|
|
request = self.factory.post(
|
|
|
|
"",
|
|
|
|
{
|
|
|
|
"post-status": True,
|
|
|
|
"privacy": "followers",
|
|
|
|
"start_date": "2020-01-05",
|
|
|
|
"content": "hello hello",
|
|
|
|
"book": self.book.id,
|
|
|
|
"mention_books": self.book.id,
|
|
|
|
"user": self.local_user.id,
|
|
|
|
"reading_status": "reading",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
request.user = self.local_user
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
2021-10-01 17:57:56 +00:00
|
|
|
views.ReadingStatus.as_view()(request, "start", self.book.id)
|
|
|
|
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
|
|
|
status = models.Comment.objects.get()
|
|
|
|
self.assertEqual(status.user, self.local_user)
|
|
|
|
self.assertEqual(status.book, self.book)
|
|
|
|
self.assertFalse(status.mention_books.exists())
|
|
|
|
self.assertEqual(status.privacy, "followers")
|
|
|
|
self.assertEqual(status.content, "<p>hello hello</p>")
|
|
|
|
self.assertEqual(status.reading_status, "reading")
|
|
|
|
|
|
|
|
readthrough = models.ReadThrough.objects.get()
|
|
|
|
self.assertIsNotNone(readthrough.start_date)
|
|
|
|
self.assertIsNone(readthrough.finish_date)
|
|
|
|
self.assertEqual(readthrough.user, self.local_user)
|
|
|
|
self.assertEqual(readthrough.book, self.book)
|
|
|
|
|
2021-09-07 01:55:48 +00:00
|
|
|
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
|
2021-09-07 03:37:09 +00:00
|
|
|
@patch("bookwyrm.activitystreams.remove_book_statuses_task.delay")
|
2021-09-07 01:55:48 +00:00
|
|
|
def test_start_reading_reshelve(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""begin a book"""
|
2021-03-25 23:57:20 +00:00
|
|
|
to_read_shelf = self.local_user.shelf_set.get(identifier=models.Shelf.TO_READ)
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
2021-02-07 19:29:24 +00:00
|
|
|
models.ShelfBook.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf=to_read_shelf, book=self.book, user=self.local_user
|
|
|
|
)
|
|
|
|
shelf = self.local_user.shelf_set.get(identifier="reading")
|
2021-01-14 02:02:01 +00:00
|
|
|
self.assertEqual(to_read_shelf.books.get(), self.book)
|
|
|
|
self.assertFalse(shelf.books.exists())
|
|
|
|
self.assertFalse(models.Status.objects.exists())
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("")
|
2021-01-14 02:02:01 +00:00
|
|
|
request.user = self.local_user
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
2021-06-09 18:27:14 +00:00
|
|
|
views.ReadingStatus.as_view()(request, "start", self.book.id)
|
2021-01-14 02:02:01 +00:00
|
|
|
|
|
|
|
self.assertFalse(to_read_shelf.books.exists())
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
2021-08-03 19:41:41 +00:00
|
|
|
def test_finish_reading(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""begin a book"""
|
2021-03-25 23:57:20 +00:00
|
|
|
shelf = self.local_user.shelf_set.get(identifier=models.Shelf.READ_FINISHED)
|
2021-01-14 02:02:01 +00:00
|
|
|
self.assertFalse(shelf.books.exists())
|
|
|
|
self.assertFalse(models.Status.objects.exists())
|
|
|
|
readthrough = models.ReadThrough.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
user=self.local_user, start_date=timezone.now(), book=self.book
|
|
|
|
)
|
|
|
|
|
|
|
|
request = self.factory.post(
|
|
|
|
"",
|
|
|
|
{
|
|
|
|
"post-status": True,
|
|
|
|
"privacy": "followers",
|
2021-09-22 23:41:43 +00:00
|
|
|
"start_date": readthrough.start_date,
|
2021-08-27 18:38:34 +00:00
|
|
|
"finish_date": timezone.now().isoformat(),
|
2021-03-08 16:49:10 +00:00
|
|
|
"id": readthrough.id,
|
|
|
|
},
|
|
|
|
)
|
2021-01-14 02:02:01 +00:00
|
|
|
request.user = self.local_user
|
2021-01-27 18:48:59 +00:00
|
|
|
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
2021-06-09 17:36:52 +00:00
|
|
|
views.ReadingStatus.as_view()(request, "finish", self.book.id)
|
2021-01-14 02:02:01 +00:00
|
|
|
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
|
|
|
status = models.GeneratedNote.objects.get()
|
|
|
|
self.assertEqual(status.user, self.local_user)
|
|
|
|
self.assertEqual(status.mention_books.get(), self.book)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(status.privacy, "followers")
|
2021-01-14 02:02:01 +00:00
|
|
|
|
|
|
|
readthrough = models.ReadThrough.objects.get()
|
|
|
|
self.assertIsNotNone(readthrough.start_date)
|
|
|
|
self.assertIsNotNone(readthrough.finish_date)
|
|
|
|
self.assertEqual(readthrough.user, self.local_user)
|
|
|
|
self.assertEqual(readthrough.book, self.book)
|
|
|
|
|
2021-08-03 19:41:41 +00:00
|
|
|
def test_edit_readthrough(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""adding dates to an ongoing readthrough"""
|
2021-03-08 16:49:10 +00:00
|
|
|
start = timezone.make_aware(dateutil.parser.parse("2021-01-03"))
|
2021-01-10 23:56:57 +00:00
|
|
|
readthrough = models.ReadThrough.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
book=self.book, user=self.local_user, start_date=start
|
|
|
|
)
|
2021-01-10 23:56:57 +00:00
|
|
|
request = self.factory.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"",
|
|
|
|
{
|
|
|
|
"start_date": "2017-01-01",
|
|
|
|
"finish_date": "2018-03-07",
|
|
|
|
"book": "",
|
|
|
|
"id": readthrough.id,
|
|
|
|
},
|
|
|
|
)
|
2021-01-10 23:56:57 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-01-13 21:05:16 +00:00
|
|
|
views.edit_readthrough(request)
|
2021-01-10 23:56:57 +00:00
|
|
|
readthrough.refresh_from_db()
|
|
|
|
self.assertEqual(readthrough.start_date.year, 2017)
|
|
|
|
self.assertEqual(readthrough.start_date.month, 1)
|
|
|
|
self.assertEqual(readthrough.start_date.day, 1)
|
|
|
|
self.assertEqual(readthrough.finish_date.year, 2018)
|
|
|
|
self.assertEqual(readthrough.finish_date.month, 3)
|
|
|
|
self.assertEqual(readthrough.finish_date.day, 7)
|
|
|
|
self.assertEqual(readthrough.book, self.book)
|
|
|
|
|
2021-08-03 19:41:41 +00:00
|
|
|
def test_delete_readthrough(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove a readthrough"""
|
2021-01-10 23:56:57 +00:00
|
|
|
readthrough = models.ReadThrough.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
book=self.book, user=self.local_user
|
|
|
|
)
|
|
|
|
models.ReadThrough.objects.create(book=self.book, user=self.local_user)
|
2021-01-10 23:56:57 +00:00
|
|
|
request = self.factory.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"",
|
|
|
|
{
|
|
|
|
"id": readthrough.id,
|
|
|
|
},
|
|
|
|
)
|
2021-01-10 23:56:57 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-01-13 21:05:16 +00:00
|
|
|
views.delete_readthrough(request)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertFalse(models.ReadThrough.objects.filter(id=readthrough.id).exists())
|
2021-01-10 23:56:57 +00:00
|
|
|
|
2021-08-03 19:41:41 +00:00
|
|
|
def test_create_readthrough(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""adding new read dates"""
|
2021-01-10 23:56:57 +00:00
|
|
|
request = self.factory.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"",
|
|
|
|
{
|
|
|
|
"start_date": "2017-01-01",
|
|
|
|
"finish_date": "2018-03-07",
|
|
|
|
"book": self.book.id,
|
|
|
|
"id": "",
|
2022-01-11 19:38:26 +00:00
|
|
|
"user": self.local_user.id,
|
2021-03-08 16:49:10 +00:00
|
|
|
},
|
|
|
|
)
|
2021-01-10 23:56:57 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
|
2022-01-11 19:38:26 +00:00
|
|
|
views.ReadThrough.as_view()(request)
|
2021-01-10 23:56:57 +00:00
|
|
|
readthrough = models.ReadThrough.objects.get()
|
|
|
|
self.assertEqual(readthrough.start_date.year, 2017)
|
|
|
|
self.assertEqual(readthrough.start_date.month, 1)
|
|
|
|
self.assertEqual(readthrough.start_date.day, 1)
|
|
|
|
self.assertEqual(readthrough.finish_date.year, 2018)
|
|
|
|
self.assertEqual(readthrough.finish_date.month, 3)
|
|
|
|
self.assertEqual(readthrough.finish_date.day, 7)
|
|
|
|
self.assertEqual(readthrough.book, self.book)
|
|
|
|
self.assertEqual(readthrough.user, self.local_user)
|
2021-10-01 18:23:35 +00:00
|
|
|
|
|
|
|
def test_update_progress_comment(self, *_):
|
|
|
|
"""update progress with commentary"""
|
|
|
|
readthrough = models.ReadThrough.objects.create(
|
|
|
|
user=self.local_user, start_date=timezone.now(), book=self.book
|
|
|
|
)
|
|
|
|
request = self.factory.post(
|
|
|
|
"",
|
|
|
|
{
|
|
|
|
"post-status": True,
|
|
|
|
"privacy": "followers",
|
|
|
|
"start_date": "2020-01-05",
|
|
|
|
"content": "hello hello",
|
|
|
|
"book": self.book.id,
|
|
|
|
"mention_books": self.book.id,
|
|
|
|
"user": self.local_user.id,
|
|
|
|
"id": readthrough.id,
|
|
|
|
"progress": 23,
|
|
|
|
"progress_mode": "PCT",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
request.user = self.local_user
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
2021-10-01 18:23:35 +00:00
|
|
|
views.update_progress(request, self.book.id)
|
|
|
|
|
|
|
|
status = models.Comment.objects.get()
|
|
|
|
self.assertEqual(status.user, self.local_user)
|
|
|
|
self.assertEqual(status.book, self.book)
|
|
|
|
self.assertFalse(status.mention_books.exists())
|
|
|
|
self.assertEqual(status.privacy, "followers")
|
|
|
|
self.assertEqual(status.content, "<p>hello hello</p>")
|
|
|
|
self.assertIsNone(status.reading_status)
|
|
|
|
self.assertEqual(status.progress, 23)
|
|
|
|
self.assertEqual(status.progress_mode, "PCT")
|
|
|
|
|
|
|
|
self.assertIsNotNone(readthrough.start_date)
|
|
|
|
self.assertIsNone(readthrough.finish_date)
|
|
|
|
self.assertEqual(readthrough.user, self.local_user)
|
|
|
|
self.assertEqual(readthrough.book, self.book)
|