2021-01-13 19:45:08 +00:00
|
|
|
''' test for app action functionality '''
|
|
|
|
from unittest.mock import patch
|
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
|
|
|
from bookwyrm import models, views
|
|
|
|
from bookwyrm.activitypub import ActivitypubResponse
|
|
|
|
|
|
|
|
|
2021-02-07 06:37:19 +00:00
|
|
|
@patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay')
|
2021-01-13 19:45:08 +00:00
|
|
|
class ShelfViews(TestCase):
|
|
|
|
''' tag views'''
|
|
|
|
def setUp(self):
|
|
|
|
''' we need basic test data and mocks '''
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
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',
|
|
|
|
)
|
|
|
|
self.work = models.Work.objects.create(title='Test Work')
|
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
title='Example Edition',
|
|
|
|
remote_id='https://example.com/book/1',
|
|
|
|
parent_work=self.work
|
|
|
|
)
|
2021-02-07 19:29:13 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
self.shelf = models.Shelf.objects.create(
|
|
|
|
name='Test Shelf',
|
|
|
|
identifier='test-shelf',
|
|
|
|
user=self.local_user
|
|
|
|
)
|
2021-01-30 20:16:57 +00:00
|
|
|
models.SiteSettings.objects.create()
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
|
2021-02-02 23:19:44 +00:00
|
|
|
def test_shelf_page(self, _):
|
2021-01-13 19:45:08 +00:00
|
|
|
''' there are so many views, this just makes sure it LOADS '''
|
|
|
|
view = views.Shelf.as_view()
|
|
|
|
shelf = self.local_user.shelf_set.first()
|
|
|
|
request = self.factory.get('')
|
|
|
|
request.user = self.local_user
|
|
|
|
with patch('bookwyrm.views.shelf.is_api_request') as is_api:
|
|
|
|
is_api.return_value = False
|
|
|
|
result = view(request, self.local_user.username, shelf.identifier)
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-01-30 20:16:57 +00:00
|
|
|
result.render()
|
2021-01-13 19:45:08 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
with patch('bookwyrm.views.shelf.is_api_request') as is_api:
|
|
|
|
is_api.return_value = True
|
|
|
|
result = view(
|
|
|
|
request, self.local_user.username, shelf.identifier)
|
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
request = self.factory.get('/?page=1')
|
|
|
|
request.user = self.local_user
|
|
|
|
with patch('bookwyrm.views.shelf.is_api_request') as is_api:
|
|
|
|
is_api.return_value = True
|
|
|
|
result = view(
|
|
|
|
request, self.local_user.username, shelf.identifier)
|
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
|
2021-02-02 23:19:44 +00:00
|
|
|
def test_edit_shelf_privacy(self, _):
|
2021-01-13 19:45:08 +00:00
|
|
|
''' set name or privacy on shelf '''
|
|
|
|
view = views.Shelf.as_view()
|
|
|
|
shelf = self.local_user.shelf_set.get(identifier='to-read')
|
|
|
|
self.assertEqual(shelf.privacy, 'public')
|
|
|
|
|
|
|
|
request = self.factory.post(
|
|
|
|
'', {
|
|
|
|
'privacy': 'unlisted',
|
|
|
|
'user': self.local_user.id,
|
|
|
|
'name': 'To Read',
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
2021-01-27 17:27:29 +00:00
|
|
|
view(request, self.local_user.username, shelf.identifier)
|
2021-01-13 19:45:08 +00:00
|
|
|
shelf.refresh_from_db()
|
|
|
|
|
|
|
|
self.assertEqual(shelf.privacy, 'unlisted')
|
|
|
|
|
|
|
|
|
2021-02-02 23:19:44 +00:00
|
|
|
def test_edit_shelf_name(self, _):
|
2021-01-13 19:45:08 +00:00
|
|
|
''' change the name of an editable shelf '''
|
|
|
|
view = views.Shelf.as_view()
|
|
|
|
shelf = models.Shelf.objects.create(
|
|
|
|
name='Test Shelf', user=self.local_user)
|
|
|
|
self.assertEqual(shelf.privacy, 'public')
|
|
|
|
|
|
|
|
request = self.factory.post(
|
|
|
|
'', {
|
|
|
|
'privacy': 'public',
|
|
|
|
'user': self.local_user.id,
|
|
|
|
'name': 'cool name'
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
2021-02-07 19:29:13 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
view(request, request.user.username, shelf.identifier)
|
2021-01-13 19:45:08 +00:00
|
|
|
shelf.refresh_from_db()
|
|
|
|
|
|
|
|
self.assertEqual(shelf.name, 'cool name')
|
|
|
|
self.assertEqual(shelf.identifier, 'testshelf-%d' % shelf.id)
|
|
|
|
|
|
|
|
|
2021-02-02 23:19:44 +00:00
|
|
|
def test_edit_shelf_name_not_editable(self, _):
|
2021-01-13 19:45:08 +00:00
|
|
|
''' can't change the name of an non-editable shelf '''
|
|
|
|
view = views.Shelf.as_view()
|
|
|
|
shelf = self.local_user.shelf_set.get(identifier='to-read')
|
|
|
|
self.assertEqual(shelf.privacy, 'public')
|
|
|
|
|
|
|
|
request = self.factory.post(
|
|
|
|
'', {
|
|
|
|
'privacy': 'public',
|
|
|
|
'user': self.local_user.id,
|
|
|
|
'name': 'cool name'
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
2021-02-07 19:29:13 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
view(request, request.user.username, shelf.identifier)
|
2021-01-13 19:45:08 +00:00
|
|
|
|
|
|
|
self.assertEqual(shelf.name, 'To Read')
|
|
|
|
|
|
|
|
|
2021-02-02 23:19:44 +00:00
|
|
|
def test_handle_shelve(self, _):
|
2021-01-13 19:45:08 +00:00
|
|
|
''' shelve a book '''
|
|
|
|
request = self.factory.post('', {
|
|
|
|
'book': self.book.id,
|
|
|
|
'shelf': self.shelf.identifier
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
2021-02-07 19:29:13 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
views.shelve(request)
|
2021-01-13 19:45:08 +00:00
|
|
|
# make sure the book is on the shelf
|
|
|
|
self.assertEqual(self.shelf.books.get(), self.book)
|
|
|
|
|
|
|
|
|
2021-02-02 23:19:44 +00:00
|
|
|
def test_handle_shelve_to_read(self, _):
|
2021-01-13 19:45:08 +00:00
|
|
|
''' special behavior for the to-read shelf '''
|
|
|
|
shelf = models.Shelf.objects.get(identifier='to-read')
|
|
|
|
request = self.factory.post('', {
|
|
|
|
'book': self.book.id,
|
|
|
|
'shelf': shelf.identifier
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-02-07 19:29:13 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
views.shelve(request)
|
2021-01-13 19:45:08 +00:00
|
|
|
# make sure the book is on the shelf
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
|
|
|
|
2021-02-02 23:19:44 +00:00
|
|
|
def test_handle_shelve_reading(self, _):
|
2021-01-13 19:45:08 +00:00
|
|
|
''' special behavior for the reading shelf '''
|
|
|
|
shelf = models.Shelf.objects.get(identifier='reading')
|
|
|
|
request = self.factory.post('', {
|
|
|
|
'book': self.book.id,
|
|
|
|
'shelf': shelf.identifier
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-02-07 19:29:13 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
views.shelve(request)
|
2021-01-13 19:45:08 +00:00
|
|
|
# make sure the book is on the shelf
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
|
|
|
|
2021-02-02 23:19:44 +00:00
|
|
|
def test_handle_shelve_read(self, _):
|
2021-01-13 19:45:08 +00:00
|
|
|
''' special behavior for the read shelf '''
|
|
|
|
shelf = models.Shelf.objects.get(identifier='read')
|
|
|
|
request = self.factory.post('', {
|
|
|
|
'book': self.book.id,
|
|
|
|
'shelf': shelf.identifier
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
|
2021-02-07 06:37:19 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
2021-01-13 19:45:08 +00:00
|
|
|
views.shelve(request)
|
|
|
|
# make sure the book is on the shelf
|
|
|
|
self.assertEqual(shelf.books.get(), self.book)
|
|
|
|
|
|
|
|
|
2021-02-02 23:19:44 +00:00
|
|
|
def test_handle_unshelve(self, _):
|
2021-01-13 19:45:08 +00:00
|
|
|
''' remove a book from a shelf '''
|
2021-02-07 19:29:13 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
|
|
|
models.ShelfBook.objects.create(
|
|
|
|
book=self.book,
|
|
|
|
user=self.local_user,
|
|
|
|
shelf=self.shelf
|
|
|
|
)
|
2021-01-13 19:45:08 +00:00
|
|
|
self.shelf.save()
|
|
|
|
self.assertEqual(self.shelf.books.count(), 1)
|
|
|
|
request = self.factory.post('', {
|
|
|
|
'book': self.book.id,
|
|
|
|
'shelf': self.shelf.id
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
2021-02-07 06:37:19 +00:00
|
|
|
with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'):
|
2021-01-13 19:45:08 +00:00
|
|
|
views.unshelve(request)
|
|
|
|
self.assertEqual(self.shelf.books.count(), 0)
|