diff --git a/bookwyrm/tests/test_view_actions.py b/bookwyrm/tests/test_view_actions.py index c0c613ac6..e7c5e0cb6 100644 --- a/bookwyrm/tests/test_view_actions.py +++ b/bookwyrm/tests/test_view_actions.py @@ -2,14 +2,13 @@ from unittest.mock import patch import dateutil -from django.core.exceptions import PermissionDenied from django.contrib.auth.models import Group, Permission from django.contrib.contenttypes.models import ContentType from django.test import TestCase from django.test.client import RequestFactory from django.utils import timezone -from bookwyrm import forms, models, view_actions as actions +from bookwyrm import models, view_actions as actions #pylint: disable=too-many-public-methods @@ -49,87 +48,6 @@ class ViewActions(TestCase): self.factory = RequestFactory() - 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') - - - 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) - - - 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') def test_edit_shelf_privacy(self): diff --git a/bookwyrm/tests/test_views.py b/bookwyrm/tests/test_views.py index fcf886102..b060de9f6 100644 --- a/bookwyrm/tests/test_views.py +++ b/bookwyrm/tests/test_views.py @@ -41,14 +41,6 @@ class Views(TestCase): ) - def test_get_edition(self): - ''' given an edition or a work, returns an edition ''' - self.assertEqual( - views.get_edition(self.book.id), self.book) - self.assertEqual( - views.get_edition(self.work.id), self.book) - - def test_get_user_from_username(self): ''' works for either localname or username ''' self.assertEqual( @@ -219,84 +211,6 @@ class Views(TestCase): self.assertEqual( response.context_data['user_results'][0], self.local_user) - def test_book_page(self): - ''' there are so many views, this just makes sure it LOADS ''' - request = self.factory.get('') - request.user = self.local_user - with patch('bookwyrm.views.is_api_request') as is_api: - is_api.return_value = False - result = views.book_page(request, self.book.id) - self.assertIsInstance(result, TemplateResponse) - self.assertEqual(result.template_name, 'book.html') - self.assertEqual(result.status_code, 200) - - request = self.factory.get('') - with patch('bookwyrm.views.is_api_request') as is_api: - is_api.return_value = True - result = views.book_page(request, self.book.id) - self.assertIsInstance(result, ActivitypubResponse) - self.assertEqual(result.status_code, 200) - - - def test_edit_book_page(self): - ''' there are so many views, this just makes sure it LOADS ''' - request = self.factory.get('') - request.user = self.local_user - request.user.is_superuser = True - result = views.edit_book_page(request, self.book.id) - self.assertIsInstance(result, TemplateResponse) - self.assertEqual(result.template_name, 'edit_book.html') - self.assertEqual(result.status_code, 200) - - - def test_edit_author_page(self): - ''' there are so many views, this just makes sure it LOADS ''' - author = models.Author.objects.create(name='Test Author') - request = self.factory.get('') - request.user = self.local_user - request.user.is_superuser = True - result = views.edit_author_page(request, author.id) - self.assertIsInstance(result, TemplateResponse) - self.assertEqual(result.template_name, 'edit_author.html') - self.assertEqual(result.status_code, 200) - - - def test_editions_page(self): - ''' there are so many views, this just makes sure it LOADS ''' - request = self.factory.get('') - with patch('bookwyrm.views.is_api_request') as is_api: - is_api.return_value = False - result = views.editions_page(request, self.work.id) - self.assertIsInstance(result, TemplateResponse) - self.assertEqual(result.template_name, 'editions.html') - self.assertEqual(result.status_code, 200) - - request = self.factory.get('') - with patch('bookwyrm.views.is_api_request') as is_api: - is_api.return_value = True - result = views.editions_page(request, self.work.id) - self.assertIsInstance(result, ActivitypubResponse) - self.assertEqual(result.status_code, 200) - - - def test_author_page(self): - ''' there are so many views, this just makes sure it LOADS ''' - author = models.Author.objects.create(name='Jessica') - request = self.factory.get('') - with patch('bookwyrm.views.is_api_request') as is_api: - is_api.return_value = False - result = views.author_page(request, author.id) - self.assertIsInstance(result, TemplateResponse) - self.assertEqual(result.template_name, 'author.html') - self.assertEqual(result.status_code, 200) - - request = self.factory.get('') - with patch('bookwyrm.views.is_api_request') as is_api: - is_api.return_value = True - result = views.author_page(request, author.id) - self.assertIsInstance(result, ActivitypubResponse) - self.assertEqual(result.status_code, 200) - def test_tag_page(self): ''' there are so many views, this just makes sure it LOADS ''' diff --git a/bookwyrm/tests/views/test_author.py b/bookwyrm/tests/views/test_author.py new file mode 100644 index 000000000..c00972f39 --- /dev/null +++ b/bookwyrm/tests/views/test_author.py @@ -0,0 +1,119 @@ +''' test for app action functionality ''' +from unittest.mock import patch +from django.contrib.auth.models import Group, Permission +from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import PermissionDenied +from django.template.response import TemplateResponse +from django.test import TestCase +from django.test.client import RequestFactory + +from bookwyrm import forms, models, views +from bookwyrm.activitypub import ActivitypubResponse + + +class AuthorViews(TestCase): + ''' author 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.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 + ) + 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 + ) + + + def test_author_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + view = views.Author.as_view() + author = models.Author.objects.create(name='Jessica') + request = self.factory.get('') + with patch('bookwyrm.views.author.is_api_request') as is_api: + is_api.return_value = False + result = view(request, author.id) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'author.html') + self.assertEqual(result.status_code, 200) + + request = self.factory.get('') + with patch('bookwyrm.views.author.is_api_request') as is_api: + is_api.return_value = True + result = view(request, author.id) + self.assertIsInstance(result, ActivitypubResponse) + self.assertEqual(result.status_code, 200) + + + def test_edit_author_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + view = views.EditAuthor.as_view() + author = models.Author.objects.create(name='Test Author') + request = self.factory.get('') + request.user = self.local_user + request.user.is_superuser = True + + result = view(request, author.id) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'edit_author.html') + self.assertEqual(result.status_code, 200) + + + def test_edit_author(self): + ''' edit an author ''' + view = views.EditAuthor.as_view() + 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'): + view(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''' + view = views.EditAuthor.as_view() + 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): + view(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''' + view = views.EditAuthor.as_view() + 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 = view(request, author.id) + author.refresh_from_db() + self.assertEqual(author.name, 'Test Author') + self.assertEqual(resp.template_name, 'edit_author.html') diff --git a/bookwyrm/tests/views/test_book.py b/bookwyrm/tests/views/test_book.py index a94757724..8306b8037 100644 --- a/bookwyrm/tests/views/test_book.py +++ b/bookwyrm/tests/views/test_book.py @@ -10,8 +10,8 @@ from bookwyrm import forms, models, views from bookwyrm.activitypub import ActivitypubResponse -class InteractionViews(TestCase): - ''' viewing and creating statuses ''' +class BookViews(TestCase): + ''' books books books ''' def setUp(self): ''' we need basic test data and mocks ''' self.factory = RequestFactory()