From e066c53dd04264222b84401eb95ca186a6d90f38 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 31 Dec 2020 13:45:13 -0800 Subject: [PATCH 1/2] tests hella views --- bookwyrm/tests/test_views.py | 294 +++++++++++++++++++++++++++++++++++ 1 file changed, 294 insertions(+) diff --git a/bookwyrm/tests/test_views.py b/bookwyrm/tests/test_views.py index fa8c99e4..51c6f502 100644 --- a/bookwyrm/tests/test_views.py +++ b/bookwyrm/tests/test_views.py @@ -2,6 +2,7 @@ import json from unittest.mock import patch +from django.contrib.auth.models import AnonymousUser from django.http import JsonResponse from django.template.response import TemplateResponse from django.test import TestCase @@ -12,6 +13,7 @@ from bookwyrm.connectors import abstract_connector from bookwyrm.settings import DOMAIN +# pylint: disable=too-many-public-methods class Views(TestCase): ''' every response to a get request, html or json ''' def setUp(self): @@ -62,6 +64,26 @@ class Views(TestCase): self.assertFalse(views.is_api_request(request)) + def test_home_tab(self): + ''' there are so many views, this just makes sure it LOADS ''' + request = self.factory.get('') + request.user = self.local_user + result = views.home_tab(request, 'local') + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'feed.html') + self.assertEqual(result.status_code, 200) + + + def test_direct_messages_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + request = self.factory.get('') + request.user = self.local_user + result = views.direct_messages_page(request) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'direct_messages.html') + self.assertEqual(result.status_code, 200) + + def test_get_activity_feed(self): ''' loads statuses ''' rat = models.User.objects.create_user( @@ -180,6 +202,257 @@ class Views(TestCase): response.context_data['book_results'][0].title, 'Gideon the Ninth') + def test_import_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + request = self.factory.get('') + request.user = self.local_user + result = views.import_page(request) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'import.html') + self.assertEqual(result.status_code, 200) + + + def test_import_status(self): + ''' there are so many views, this just makes sure it LOADS ''' + import_job = models.ImportJob.objects.create(user=self.local_user) + request = self.factory.get('') + request.user = self.local_user + with patch('bookwyrm.tasks.app.AsyncResult') as async_result: + async_result.return_value = [] + result = views.import_status(request, import_job.id) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'import_status.html') + self.assertEqual(result.status_code, 200) + + + def test_login_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + request = self.factory.get('') + request.user = AnonymousUser + result = views.login_page(request) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'login.html') + self.assertEqual(result.status_code, 200) + + request.user = self.local_user + result = views.login_page(request) + self.assertEqual(result.url, '/') + self.assertEqual(result.status_code, 302) + + + def test_about_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + request = self.factory.get('') + request.user = self.local_user + result = views.about_page(request) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'about.html') + self.assertEqual(result.status_code, 200) + + + def test_password_reset_request(self): + ''' there are so many views, this just makes sure it LOADS ''' + request = self.factory.get('') + request.user = self.local_user + result = views.password_reset_request(request) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'password_reset_request.html') + self.assertEqual(result.status_code, 200) + + + def test_password_reset(self): + ''' there are so many views, this just makes sure it LOADS ''' + code = models.PasswordReset.objects.create(user=self.local_user) + request = self.factory.get('') + request.user = AnonymousUser + result = views.password_reset(request, code.code) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'password_reset.html') + self.assertEqual(result.status_code, 200) + + + def test_invite_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + models.SiteInvite.objects.create(code='hi', user=self.local_user) + request = self.factory.get('') + request.user = AnonymousUser + # why?? this is annoying. + request.user.is_authenticated = False + with patch('bookwyrm.models.site.SiteInvite.valid') as invite: + invite.return_value = True + result = views.invite_page(request, 'hi') + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'invite.html') + self.assertEqual(result.status_code, 200) + + + def test_manage_invites(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.manage_invites(request) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'manage_invites.html') + self.assertEqual(result.status_code, 200) + + + def test_notifications_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + request = self.factory.get('') + request.user = self.local_user + result = views.notifications_page(request) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'notifications.html') + self.assertEqual(result.status_code, 200) + + + def test_user_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.user_page(request, 'mouse') + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'user.html') + self.assertEqual(result.status_code, 200) + + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = True + result = views.user_page(request, 'mouse') + self.assertIsInstance(result, JsonResponse) + self.assertEqual(result.status_code, 200) + + + def test_followers_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.followers_page(request, 'mouse') + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'followers.html') + self.assertEqual(result.status_code, 200) + + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = True + result = views.followers_page(request, 'mouse') + self.assertIsInstance(result, JsonResponse) + self.assertEqual(result.status_code, 200) + + + def test_following_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.following_page(request, 'mouse') + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'following.html') + self.assertEqual(result.status_code, 200) + + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = True + result = views.following_page(request, 'mouse') + self.assertIsInstance(result, JsonResponse) + self.assertEqual(result.status_code, 200) + + + def test_status_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + status = models.Status.objects.create( + content='hi', user=self.local_user) + 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.status_page(request, 'mouse', status.id) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'status.html') + self.assertEqual(result.status_code, 200) + + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = True + result = views.status_page(request, 'mouse', status.id) + self.assertIsInstance(result, JsonResponse) + self.assertEqual(result.status_code, 200) + + + def test_replies_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + status = models.Status.objects.create( + content='hi', user=self.local_user) + 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.replies_page(request, 'mouse', status.id) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'status.html') + self.assertEqual(result.status_code, 200) + + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = True + result = views.replies_page(request, 'mouse', status.id) + self.assertIsInstance(result, JsonResponse) + self.assertEqual(result.status_code, 200) + + + def test_edit_profile_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + request = self.factory.get('') + request.user = self.local_user + result = views.edit_profile_page(request) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'edit_user.html') + self.assertEqual(result.status_code, 200) + + + 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, JsonResponse) + 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('') @@ -236,3 +509,24 @@ class Views(TestCase): result = views.tag_page(request, tag.identifier) self.assertIsInstance(result, JsonResponse) self.assertEqual(result.status_code, 200) + + + def test_shelf_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + shelf = self.local_user.shelf_set.first() + 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.shelf_page( + request, self.local_user.username, shelf.identifier) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'shelf.html') + self.assertEqual(result.status_code, 200) + + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = True + result = views.shelf_page( + request, self.local_user.username, shelf.identifier) + self.assertIsInstance(result, JsonResponse) + self.assertEqual(result.status_code, 200) From 2f9123c20660bb661318ba57092b4e39d78f3ece Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 31 Dec 2020 13:47:26 -0800 Subject: [PATCH 2/2] Add sort order to followers/following ordered collection querysets --- bookwyrm/models/user.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 686af24d..f9120ff0 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -130,14 +130,22 @@ class User(OrderedCollectionPageMixin, AbstractUser): def to_following_activity(self, **kwargs): ''' activitypub following list ''' remote_id = '%s/following' % self.remote_id - return self.to_ordered_collection(self.following.all(), \ - remote_id=remote_id, id_only=True, **kwargs) + return self.to_ordered_collection( + self.following.order_by('-updated_date').all(), + remote_id=remote_id, + id_only=True, + **kwargs + ) def to_followers_activity(self, **kwargs): ''' activitypub followers list ''' remote_id = '%s/followers' % self.remote_id - return self.to_ordered_collection(self.followers.all(), \ - remote_id=remote_id, id_only=True, **kwargs) + return self.to_ordered_collection( + self.followers.order_by('-updated_date').all(), + remote_id=remote_id, + id_only=True, + **kwargs + ) def to_activity(self): ''' override default AP serializer to add context object