2020-12-16 00:36:22 +00:00
|
|
|
''' test for app action functionality '''
|
|
|
|
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.core.exceptions import PermissionDenied
|
2020-12-22 18:10:36 +00:00
|
|
|
from django.contrib.auth.models import Group, Permission
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
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
|
|
|
|
2020-12-22 00:19:36 +00:00
|
|
|
from bookwyrm import forms, models, view_actions as actions
|
2020-12-16 00:36:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
#pylint: disable=too-many-public-methods
|
|
|
|
class ViewActions(TestCase):
|
|
|
|
''' a lot here: all handlers for receiving activitypub requests '''
|
|
|
|
def setUp(self):
|
|
|
|
''' we need basic things, like users '''
|
|
|
|
self.local_user = models.User.objects.create_user(
|
2020-12-27 23:18:48 +00:00
|
|
|
'mouse', 'mouse@mouse.com', 'mouseword',
|
|
|
|
local=True, localname='mouse')
|
2020-12-16 00:36:22 +00:00
|
|
|
self.local_user.remote_id = 'https://example.com/user/mouse'
|
|
|
|
self.local_user.save()
|
2020-12-22 18:10:36 +00:00
|
|
|
self.group = Group.objects.create(name='editor')
|
|
|
|
self.group.permissions.add(
|
|
|
|
Permission.objects.create(
|
|
|
|
name='edit_book',
|
|
|
|
codename='edit_book',
|
|
|
|
content_type=ContentType.objects.get_for_model(models.User)).id
|
|
|
|
)
|
2020-12-16 00:36:22 +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',
|
|
|
|
)
|
|
|
|
self.status = models.Status.objects.create(
|
|
|
|
user=self.local_user,
|
|
|
|
content='Test status',
|
|
|
|
remote_id='https://example.com/status/1',
|
|
|
|
)
|
2020-12-31 16:30:51 +00:00
|
|
|
self.work = models.Work.objects.create(title='Test Work')
|
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
title='Test Book', parent_work=self.work)
|
2020-12-16 00:36:22 +00:00
|
|
|
self.settings = models.SiteSettings.objects.create(id=1)
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
|
|
|
|
|
2020-12-21 22:54:45 +00:00
|
|
|
def test_edit_user(self):
|
|
|
|
''' use a form to update a user '''
|
2020-12-22 00:19:36 +00:00
|
|
|
form = forms.EditUserForm(instance=self.local_user)
|
|
|
|
form.data['name'] = 'New Name'
|
|
|
|
request = self.factory.post('', form.data)
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
actions.edit_profile(request)
|
|
|
|
self.assertEqual(self.local_user.name, 'New Name')
|
2020-12-21 22:54:45 +00:00
|
|
|
|
|
|
|
|
2021-01-12 04:21:32 +00:00
|
|
|
def test_edit_book(self):
|
|
|
|
''' lets a user edit a book '''
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.EditionForm(instance=self.book)
|
|
|
|
form.data['title'] = 'New Title'
|
|
|
|
form.data['last_edited_by'] = self.local_user.id
|
|
|
|
request = self.factory.post('', form.data)
|
|
|
|
request.user = self.local_user
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
actions.edit_book(request, self.book.id)
|
|
|
|
self.book.refresh_from_db()
|
|
|
|
self.assertEqual(self.book.title, 'New Title')
|
|
|
|
|
|
|
|
|
2020-12-16 21:59:18 +00:00
|
|
|
def test_switch_edition(self):
|
|
|
|
''' updates user's relationships to a book '''
|
|
|
|
work = models.Work.objects.create(title='test work')
|
|
|
|
edition1 = models.Edition.objects.create(
|
|
|
|
title='first ed', parent_work=work)
|
|
|
|
edition2 = models.Edition.objects.create(
|
|
|
|
title='second ed', parent_work=work)
|
|
|
|
shelf = models.Shelf.objects.create(
|
|
|
|
name='Test Shelf', user=self.local_user)
|
|
|
|
shelf.books.add(edition1)
|
|
|
|
models.ReadThrough.objects.create(
|
|
|
|
user=self.local_user, book=edition1)
|
|
|
|
|
|
|
|
self.assertEqual(models.ShelfBook.objects.get().book, edition1)
|
|
|
|
self.assertEqual(models.ReadThrough.objects.get().book, edition1)
|
|
|
|
request = self.factory.post('', {
|
|
|
|
'edition': edition2.id
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
actions.switch_edition(request)
|
|
|
|
|
|
|
|
self.assertEqual(models.ShelfBook.objects.get().book, edition2)
|
|
|
|
self.assertEqual(models.ReadThrough.objects.get().book, edition2)
|
2020-12-22 18:10:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_edit_author(self):
|
|
|
|
''' edit an author '''
|
|
|
|
author = models.Author.objects.create(name='Test Author')
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.AuthorForm(instance=author)
|
|
|
|
form.data['name'] = 'New Name'
|
|
|
|
form.data['last_edited_by'] = self.local_user.id
|
|
|
|
request = self.factory.post('', form.data)
|
|
|
|
request.user = self.local_user
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
actions.edit_author(request, author.id)
|
|
|
|
author.refresh_from_db()
|
|
|
|
self.assertEqual(author.name, 'New Name')
|
|
|
|
self.assertEqual(author.last_edited_by, self.local_user)
|
|
|
|
|
|
|
|
def test_edit_author_non_editor(self):
|
|
|
|
''' edit an author with invalid post data'''
|
|
|
|
author = models.Author.objects.create(name='Test Author')
|
|
|
|
form = forms.AuthorForm(instance=author)
|
|
|
|
form.data['name'] = 'New Name'
|
|
|
|
form.data['last_edited_by'] = self.local_user.id
|
|
|
|
request = self.factory.post('', form.data)
|
|
|
|
request.user = self.local_user
|
|
|
|
with self.assertRaises(PermissionDenied):
|
|
|
|
actions.edit_author(request, author.id)
|
|
|
|
author.refresh_from_db()
|
|
|
|
self.assertEqual(author.name, 'Test Author')
|
|
|
|
|
|
|
|
def test_edit_author_invalid_form(self):
|
|
|
|
''' edit an author with invalid post data'''
|
|
|
|
author = models.Author.objects.create(name='Test Author')
|
|
|
|
self.local_user.groups.add(self.group)
|
|
|
|
form = forms.AuthorForm(instance=author)
|
|
|
|
form.data['name'] = ''
|
|
|
|
form.data['last_edited_by'] = self.local_user.id
|
|
|
|
request = self.factory.post('', form.data)
|
|
|
|
request.user = self.local_user
|
|
|
|
resp = actions.edit_author(request, author.id)
|
|
|
|
author.refresh_from_db()
|
|
|
|
self.assertEqual(author.name, 'Test Author')
|
|
|
|
self.assertEqual(resp.template_name, 'edit_author.html')
|
2020-12-31 16:30:51 +00:00
|
|
|
|
|
|
|
|
2021-01-11 22:32:04 +00:00
|
|
|
def test_edit_shelf_privacy(self):
|
2021-01-11 22:23:56 +00:00
|
|
|
''' set name or privacy on shelf '''
|
|
|
|
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,
|
2021-01-11 22:32:04 +00:00
|
|
|
'name': 'To Read',
|
2021-01-11 22:23:56 +00:00
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
actions.edit_shelf(request, shelf.id)
|
2021-01-11 22:32:04 +00:00
|
|
|
shelf.refresh_from_db()
|
2021-01-11 22:23:56 +00:00
|
|
|
|
|
|
|
self.assertEqual(shelf.privacy, 'unlisted')
|
|
|
|
|
|
|
|
|
2021-01-11 22:32:04 +00:00
|
|
|
def test_edit_shelf_name(self):
|
|
|
|
''' change the name of an editable shelf '''
|
|
|
|
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
|
|
|
|
actions.edit_shelf(request, shelf.id)
|
|
|
|
shelf.refresh_from_db()
|
|
|
|
|
|
|
|
self.assertEqual(shelf.name, 'cool name')
|
|
|
|
self.assertEqual(shelf.identifier, 'testshelf-%d' % shelf.id)
|
|
|
|
|
|
|
|
|
|
|
|
def test_edit_shelf_name_not_editable(self):
|
|
|
|
''' can't change the name of an non-editable shelf '''
|
|
|
|
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
|
|
|
|
actions.edit_shelf(request, shelf.id)
|
|
|
|
|
|
|
|
self.assertEqual(shelf.name, 'To Read')
|
|
|
|
|
|
|
|
|
2021-01-10 23:56:57 +00:00
|
|
|
def test_edit_readthrough(self):
|
|
|
|
''' adding dates to an ongoing readthrough '''
|
2021-01-11 00:11:02 +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(
|
|
|
|
book=self.book, user=self.local_user, start_date=start)
|
|
|
|
request = self.factory.post(
|
|
|
|
'', {
|
|
|
|
'start_date': '2017-01-01',
|
|
|
|
'finish_date': '2018-03-07',
|
|
|
|
'book': '',
|
|
|
|
'id': readthrough.id,
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
actions.edit_readthrough(request)
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_readthrough(self):
|
|
|
|
''' remove a readthrough '''
|
|
|
|
readthrough = models.ReadThrough.objects.create(
|
|
|
|
book=self.book, user=self.local_user)
|
|
|
|
models.ReadThrough.objects.create(
|
|
|
|
book=self.book, user=self.local_user)
|
|
|
|
request = self.factory.post(
|
|
|
|
'', {
|
|
|
|
'id': readthrough.id,
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
actions.delete_readthrough(request)
|
|
|
|
self.assertFalse(
|
|
|
|
models.ReadThrough.objects.filter(id=readthrough.id).exists())
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_readthrough(self):
|
|
|
|
''' adding new read dates '''
|
|
|
|
request = self.factory.post(
|
|
|
|
'', {
|
|
|
|
'start_date': '2017-01-01',
|
|
|
|
'finish_date': '2018-03-07',
|
|
|
|
'book': self.book.id,
|
|
|
|
'id': '',
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
actions.create_readthrough(request)
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-12-31 16:30:51 +00:00
|
|
|
def test_tag(self):
|
|
|
|
''' add a tag to a book '''
|
|
|
|
request = self.factory.post(
|
|
|
|
'', {
|
|
|
|
'name': 'A Tag!?',
|
|
|
|
'book': self.book.id,
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
actions.tag(request)
|
|
|
|
|
|
|
|
tag = models.Tag.objects.get()
|
|
|
|
user_tag = models.UserTag.objects.get()
|
|
|
|
self.assertEqual(tag.name, 'A Tag!?')
|
|
|
|
self.assertEqual(tag.identifier, 'A+Tag%21%3F')
|
|
|
|
self.assertEqual(user_tag.user, self.local_user)
|
|
|
|
self.assertEqual(user_tag.book, self.book)
|
2020-12-31 16:42:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_untag(self):
|
|
|
|
''' remove a tag from a book '''
|
|
|
|
tag = models.Tag.objects.create(name='A Tag!?')
|
2021-01-04 19:14:10 +00:00
|
|
|
models.UserTag.objects.create(
|
2020-12-31 16:42:09 +00:00
|
|
|
user=self.local_user, book=self.book, tag=tag)
|
|
|
|
request = self.factory.post(
|
|
|
|
'', {
|
|
|
|
'user': self.local_user.id,
|
|
|
|
'book': self.book.id,
|
|
|
|
'name': tag.name,
|
|
|
|
})
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
with patch('bookwyrm.broadcast.broadcast_task.delay'):
|
|
|
|
actions.untag(request)
|
|
|
|
|
|
|
|
self.assertTrue(models.Tag.objects.filter(name='A Tag!?').exists())
|
|
|
|
self.assertFalse(models.UserTag.objects.exists())
|