Merge pull request #573 from mouse-reeve/views-tests

Make sure templates render rather than checking template paths
This commit is contained in:
Mouse Reeve 2021-01-30 15:50:47 -08:00 committed by GitHub
commit 7dea363061
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 56 additions and 41 deletions

View file

@ -33,7 +33,7 @@ class AuthenticationViews(TestCase):
result = login(request) result = login(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'login.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
request.user = self.local_user request.user = self.local_user
@ -94,7 +94,7 @@ class AuthenticationViews(TestCase):
}) })
response = view(request) response = view(request)
self.assertEqual(models.User.objects.count(), 1) self.assertEqual(models.User.objects.count(), 1)
self.assertEqual(response.template_name, 'login.html') response.render()
def test_register_invalid_username(self): def test_register_invalid_username(self):
''' gotta have an email ''' ''' gotta have an email '''
@ -109,7 +109,7 @@ class AuthenticationViews(TestCase):
}) })
response = view(request) response = view(request)
self.assertEqual(models.User.objects.count(), 1) self.assertEqual(models.User.objects.count(), 1)
self.assertEqual(response.template_name, 'login.html') response.render()
request = self.factory.post( request = self.factory.post(
'register/', 'register/',
@ -120,7 +120,7 @@ class AuthenticationViews(TestCase):
}) })
response = view(request) response = view(request)
self.assertEqual(models.User.objects.count(), 1) self.assertEqual(models.User.objects.count(), 1)
self.assertEqual(response.template_name, 'login.html') response.render()
request = self.factory.post( request = self.factory.post(
'register/', 'register/',
@ -131,7 +131,7 @@ class AuthenticationViews(TestCase):
}) })
response = view(request) response = view(request)
self.assertEqual(models.User.objects.count(), 1) self.assertEqual(models.User.objects.count(), 1)
self.assertEqual(response.template_name, 'login.html') response.render()
def test_register_closed_instance(self): def test_register_closed_instance(self):

View file

@ -34,6 +34,7 @@ class AuthorViews(TestCase):
remote_id='https://example.com/book/1', remote_id='https://example.com/book/1',
parent_work=self.work parent_work=self.work
) )
models.SiteSettings.objects.create()
def test_author_page(self): def test_author_page(self):
@ -45,7 +46,8 @@ class AuthorViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, author.id) result = view(request, author.id)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'author.html') result.render()
self.assertEqual(result.status_code, 200)
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
request = self.factory.get('') request = self.factory.get('')
@ -66,7 +68,8 @@ class AuthorViews(TestCase):
result = view(request, author.id) result = view(request, author.id)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'edit_author.html') result.render()
self.assertEqual(result.status_code, 200)
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
@ -116,4 +119,5 @@ class AuthorViews(TestCase):
resp = view(request, author.id) resp = view(request, author.id)
author.refresh_from_db() author.refresh_from_db()
self.assertEqual(author.name, 'Test Author') self.assertEqual(author.name, 'Test Author')
self.assertEqual(resp.template_name, 'edit_author.html') resp.render()
self.assertEqual(resp.status_code, 200)

View file

@ -23,6 +23,7 @@ class BlockViews(TestCase):
inbox='https://example.com/users/rat/inbox', inbox='https://example.com/users/rat/inbox',
outbox='https://example.com/users/rat/outbox', outbox='https://example.com/users/rat/outbox',
) )
models.SiteSettings.objects.create()
def test_block_get(self): def test_block_get(self):
@ -32,7 +33,7 @@ class BlockViews(TestCase):
request.user = self.local_user request.user = self.local_user
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'preferences/blocks.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
def test_block_post(self): def test_block_post(self):

View file

@ -33,6 +33,7 @@ class BookViews(TestCase):
remote_id='https://example.com/book/1', remote_id='https://example.com/book/1',
parent_work=self.work parent_work=self.work
) )
models.SiteSettings.objects.create()
def test_book_page(self): def test_book_page(self):
@ -44,7 +45,7 @@ class BookViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, self.book.id) result = view(request, self.book.id)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'book.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
request = self.factory.get('') request = self.factory.get('')
@ -63,7 +64,7 @@ class BookViews(TestCase):
request.user.is_superuser = True request.user.is_superuser = True
result = view(request, self.book.id) result = view(request, self.book.id)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'edit_book.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
@ -116,7 +117,7 @@ class BookViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, self.work.id) result = view(request, self.work.id)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'editions.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
request = self.factory.get('') request = self.factory.get('')

View file

@ -15,6 +15,7 @@ class FederationViews(TestCase):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
'mouse@local.com', 'mouse@mouse.mouse', 'password', 'mouse@local.com', 'mouse@mouse.mouse', 'password',
local=True, localname='mouse') local=True, localname='mouse')
models.SiteSettings.objects.create()
def test_federation_page(self): def test_federation_page(self):
@ -25,5 +26,5 @@ class FederationViews(TestCase):
request.user.is_superuser = True request.user.is_superuser = True
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'settings/federation.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)

View file

@ -21,6 +21,7 @@ class FeedMessageViews(TestCase):
title='Example Edition', title='Example Edition',
remote_id='https://example.com/book/1', remote_id='https://example.com/book/1',
) )
models.SiteSettings.objects.create()
def test_feed(self): def test_feed(self):
@ -30,7 +31,7 @@ class FeedMessageViews(TestCase):
request.user = self.local_user request.user = self.local_user
result = view(request, 'local') result = view(request, 'local')
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'feed/feed.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
@ -45,7 +46,7 @@ class FeedMessageViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, 'mouse', status.id) result = view(request, 'mouse', status.id)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'feed/status.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
with patch('bookwyrm.views.feed.is_api_request') as is_api: with patch('bookwyrm.views.feed.is_api_request') as is_api:
@ -66,7 +67,7 @@ class FeedMessageViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, 'mouse', status.id) result = view(request, 'mouse', status.id)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'feed/status.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
with patch('bookwyrm.views.feed.is_api_request') as is_api: with patch('bookwyrm.views.feed.is_api_request') as is_api:
@ -83,7 +84,7 @@ class FeedMessageViews(TestCase):
request.user = self.local_user request.user = self.local_user
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'feed/direct_messages.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)

View file

@ -16,6 +16,7 @@ class ImportViews(TestCase):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
'mouse@local.com', 'mouse@mouse.mouse', 'password', 'mouse@local.com', 'mouse@mouse.mouse', 'password',
local=True, localname='mouse') local=True, localname='mouse')
models.SiteSettings.objects.create()
def test_import_page(self): def test_import_page(self):
@ -25,7 +26,7 @@ class ImportViews(TestCase):
request.user = self.local_user request.user = self.local_user
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'import.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
@ -39,5 +40,5 @@ class ImportViews(TestCase):
async_result.return_value = [] async_result.return_value = []
result = view(request, import_job.id) result = view(request, import_job.id)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'import_status.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)

View file

@ -18,6 +18,7 @@ class InviteViews(TestCase):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
'mouse@local.com', 'mouse@mouse.mouse', 'password', 'mouse@local.com', 'mouse@mouse.mouse', 'password',
local=True, localname='mouse') local=True, localname='mouse')
models.SiteSettings.objects.create()
def test_invite_page(self): def test_invite_page(self):
@ -32,7 +33,7 @@ class InviteViews(TestCase):
invite.return_value = True invite.return_value = True
result = view(request, 'hi') result = view(request, 'hi')
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'invite.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
@ -44,5 +45,5 @@ class InviteViews(TestCase):
request.user.is_superuser = True request.user.is_superuser = True
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'settings/manage_invites.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)

View file

@ -18,6 +18,7 @@ class LandingViews(TestCase):
local=True, localname='mouse') local=True, localname='mouse')
self.anonymous_user = AnonymousUser self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False self.anonymous_user.is_authenticated = False
models.SiteSettings.objects.create()
def test_home_page(self): def test_home_page(self):
@ -27,13 +28,13 @@ class LandingViews(TestCase):
request.user = self.local_user request.user = self.local_user
result = view(request) result = view(request)
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assertEqual(result.template_name, 'feed/feed.html') result.render()
request.user = self.anonymous_user request.user = self.anonymous_user
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assertEqual(result.template_name, 'discover.html') result.render()
def test_about_page(self): def test_about_page(self):
@ -43,7 +44,7 @@ class LandingViews(TestCase):
request.user = self.local_user request.user = self.local_user
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'about.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
@ -53,5 +54,3 @@ class LandingViews(TestCase):
request = self.factory.get('') request = self.factory.get('')
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'discover.html')
self.assertEqual(result.status_code, 200)

View file

@ -15,6 +15,7 @@ class NotificationViews(TestCase):
self.local_user = models.User.objects.create_user( self.local_user = models.User.objects.create_user(
'mouse@local.com', 'mouse@mouse.mouse', 'password', 'mouse@local.com', 'mouse@mouse.mouse', 'password',
local=True, localname='mouse') local=True, localname='mouse')
models.SiteSettings.objects.create()
def test_notifications_page(self): def test_notifications_page(self):
''' there are so many views, this just makes sure it LOADS ''' ''' there are so many views, this just makes sure it LOADS '''
@ -23,7 +24,7 @@ class NotificationViews(TestCase):
request.user = self.local_user request.user = self.local_user
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'notifications.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
def test_clear_notifications(self): def test_clear_notifications(self):

View file

@ -19,6 +19,7 @@ class PasswordViews(TestCase):
local=True, localname='mouse') local=True, localname='mouse')
self.anonymous_user = AnonymousUser self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False self.anonymous_user.is_authenticated = False
models.SiteSettings.objects.create(id=1)
def test_password_reset_request(self): def test_password_reset_request(self):
@ -29,7 +30,7 @@ class PasswordViews(TestCase):
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'password_reset_request.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
@ -43,7 +44,7 @@ class PasswordViews(TestCase):
request = self.factory.post('', {'email': 'mouse@mouse.com'}) request = self.factory.post('', {'email': 'mouse@mouse.com'})
with patch('bookwyrm.emailing.send_email.delay'): with patch('bookwyrm.emailing.send_email.delay'):
resp = view(request) resp = view(request)
self.assertEqual(resp.template_name, 'password_reset_request.html') resp.render()
self.assertEqual( self.assertEqual(
models.PasswordReset.objects.get().user, self.local_user) models.PasswordReset.objects.get().user, self.local_user)
@ -56,7 +57,7 @@ class PasswordViews(TestCase):
request.user = self.anonymous_user request.user = self.anonymous_user
result = view(request, code.code) result = view(request, code.code)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'password_reset.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
@ -82,7 +83,7 @@ class PasswordViews(TestCase):
'confirm-password': 'hi' 'confirm-password': 'hi'
}) })
resp = view(request, 'jhgdkfjgdf') resp = view(request, 'jhgdkfjgdf')
self.assertEqual(resp.template_name, 'password_reset.html') resp.render()
self.assertTrue(models.PasswordReset.objects.exists()) self.assertTrue(models.PasswordReset.objects.exists())
def test_password_reset_mismatch(self): def test_password_reset_mismatch(self):
@ -94,7 +95,7 @@ class PasswordViews(TestCase):
'confirm-password': 'hihi' 'confirm-password': 'hihi'
}) })
resp = view(request, code.code) resp = view(request, code.code)
self.assertEqual(resp.template_name, 'password_reset.html') resp.render()
self.assertTrue(models.PasswordReset.objects.exists()) self.assertTrue(models.PasswordReset.objects.exists())
@ -106,7 +107,7 @@ class PasswordViews(TestCase):
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'preferences/change_password.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)

View file

@ -33,6 +33,7 @@ class ShelfViews(TestCase):
connector_file='self_connector', connector_file='self_connector',
local=True local=True
) )
models.SiteSettings.objects.create()
def test_search_json_response(self): def test_search_json_response(self):
@ -89,7 +90,7 @@ class ShelfViews(TestCase):
manager.return_value = [search_result] manager.return_value = [search_result]
response = view(request) response = view(request)
self.assertIsInstance(response, TemplateResponse) self.assertIsInstance(response, TemplateResponse)
self.assertEqual(response.template_name, 'search_results.html') response.render()
self.assertEqual( self.assertEqual(
response.context_data['book_results'][0].title, 'Gideon the Ninth') response.context_data['book_results'][0].title, 'Gideon the Ninth')
@ -103,6 +104,6 @@ class ShelfViews(TestCase):
with patch('bookwyrm.connectors.connector_manager.search'): with patch('bookwyrm.connectors.connector_manager.search'):
response = view(request) response = view(request)
self.assertIsInstance(response, TemplateResponse) self.assertIsInstance(response, TemplateResponse)
self.assertEqual(response.template_name, 'search_results.html') response.render()
self.assertEqual( self.assertEqual(
response.context_data['user_results'][0], self.local_user) response.context_data['user_results'][0], self.local_user)

View file

@ -29,6 +29,7 @@ class ShelfViews(TestCase):
identifier='test-shelf', identifier='test-shelf',
user=self.local_user user=self.local_user
) )
models.SiteSettings.objects.create()
def test_shelf_page(self): def test_shelf_page(self):
@ -41,7 +42,7 @@ class ShelfViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, self.local_user.username, shelf.identifier) result = view(request, self.local_user.username, shelf.identifier)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'user/shelf.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
with patch('bookwyrm.views.shelf.is_api_request') as is_api: with patch('bookwyrm.views.shelf.is_api_request') as is_api:

View file

@ -33,6 +33,7 @@ class TagViews(TestCase):
remote_id='https://example.com/book/1', remote_id='https://example.com/book/1',
parent_work=self.work parent_work=self.work
) )
models.SiteSettings.objects.create()
def test_tag_page(self): def test_tag_page(self):
@ -46,7 +47,7 @@ class TagViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, tag.identifier) result = view(request, tag.identifier)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'tag.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
request = self.factory.get('') request = self.factory.get('')

View file

@ -23,6 +23,7 @@ class UserViews(TestCase):
self.rat = models.User.objects.create_user( self.rat = models.User.objects.create_user(
'rat@local.com', 'rat@rat.rat', 'password', 'rat@local.com', 'rat@rat.rat', 'password',
local=True, localname='rat') local=True, localname='rat')
models.SiteSettings.objects.create()
def test_user_page(self): def test_user_page(self):
@ -34,7 +35,7 @@ class UserViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, 'mouse') result = view(request, 'mouse')
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'user/user.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
with patch('bookwyrm.views.user.is_api_request') as is_api: with patch('bookwyrm.views.user.is_api_request') as is_api:
@ -65,7 +66,7 @@ class UserViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, 'mouse') result = view(request, 'mouse')
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'user/followers.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
with patch('bookwyrm.views.user.is_api_request') as is_api: with patch('bookwyrm.views.user.is_api_request') as is_api:
@ -96,7 +97,7 @@ class UserViews(TestCase):
is_api.return_value = False is_api.return_value = False
result = view(request, 'mouse') result = view(request, 'mouse')
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'user/following.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
with patch('bookwyrm.views.user.is_api_request') as is_api: with patch('bookwyrm.views.user.is_api_request') as is_api:
@ -125,7 +126,7 @@ class UserViews(TestCase):
request.user = self.local_user request.user = self.local_user
result = view(request) result = view(request)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
self.assertEqual(result.template_name, 'preferences/edit_user.html') result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)