Removes old views file

This commit is contained in:
Mouse Reeve 2021-01-13 12:18:19 -08:00
parent 12bf34e242
commit 2e61bd0e7a
4 changed files with 33 additions and 152 deletions

View file

@ -211,54 +211,6 @@ class Outgoing(TestCase):
self.assertEqual(result.username, 'mouse@example.com') self.assertEqual(result.username, 'mouse@example.com')
def test_handle_shelve(self):
''' shelve a book '''
with patch('bookwyrm.broadcast.broadcast_task.delay'):
outgoing.handle_shelve(self.local_user, self.book, self.shelf)
# make sure the book is on the shelf
self.assertEqual(self.shelf.books.get(), self.book)
def test_handle_shelve_to_read(self):
''' special behavior for the to-read shelf '''
shelf = models.Shelf.objects.get(identifier='to-read')
with patch('bookwyrm.broadcast.broadcast_task.delay'):
outgoing.handle_shelve(self.local_user, self.book, shelf)
# make sure the book is on the shelf
self.assertEqual(shelf.books.get(), self.book)
def test_handle_shelve_reading(self):
''' special behavior for the reading shelf '''
shelf = models.Shelf.objects.get(identifier='reading')
with patch('bookwyrm.broadcast.broadcast_task.delay'):
outgoing.handle_shelve(self.local_user, self.book, shelf)
# make sure the book is on the shelf
self.assertEqual(shelf.books.get(), self.book)
def test_handle_shelve_read(self):
''' special behavior for the read shelf '''
shelf = models.Shelf.objects.get(identifier='read')
with patch('bookwyrm.broadcast.broadcast_task.delay'):
outgoing.handle_shelve(self.local_user, self.book, shelf)
# make sure the book is on the shelf
self.assertEqual(shelf.books.get(), self.book)
def test_handle_unshelve(self):
''' remove a book from a shelf '''
self.shelf.books.add(self.book)
self.shelf.save()
self.assertEqual(self.shelf.books.count(), 1)
with patch('bookwyrm.broadcast.broadcast_task.delay'):
outgoing.handle_unshelve(self.local_user, self.book, self.shelf)
self.assertEqual(self.shelf.books.count(), 0)
def test_handle_reading_status_to_read(self): def test_handle_reading_status_to_read(self):
''' posts shelve activities ''' ''' posts shelve activities '''
shelf = self.local_user.shelf_set.get(identifier='to-read') shelf = self.local_user.shelf_set.get(identifier='to-read')

View file

@ -48,61 +48,6 @@ class ViewActions(TestCase):
self.factory = RequestFactory() self.factory = RequestFactory()
def test_edit_shelf_privacy(self):
''' 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,
'name': 'To Read',
})
request.user = self.local_user
actions.edit_shelf(request, shelf.id)
shelf.refresh_from_db()
self.assertEqual(shelf.privacy, 'unlisted')
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')
def test_edit_readthrough(self): def test_edit_readthrough(self):
''' adding dates to an ongoing readthrough ''' ''' adding dates to an ongoing readthrough '''
start = timezone.make_aware(dateutil.parser.parse('2021-01-03')) start = timezone.make_aware(dateutil.parser.parse('2021-01-03'))

View file

@ -1,31 +1,27 @@
''' test for app action functionality ''' ''' test for app action functionality '''
from unittest.mock import patch from unittest.mock import patch
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
from bookwyrm import models from bookwyrm import models, views
from bookwyrm import vviews as views
from bookwyrm.settings import USER_AGENT from bookwyrm.settings import USER_AGENT
class ViewsHelpers(TestCase):
# pylint: disable=too-many-public-methods ''' viewing and creating statuses '''
class Views(TestCase):
''' every response to a get request, html or json '''
def setUp(self): def setUp(self):
''' we need basic test data and mocks ''' ''' we need basic test data and mocks '''
self.factory = RequestFactory() 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.work = models.Work.objects.create(title='Test Work')
self.book = models.Edition.objects.create( self.book = models.Edition.objects.create(
title='Test Book', parent_work=self.work) title='Test Book',
models.Connector.objects.create( remote_id='https://example.com/book/1',
identifier='self', parent_work=self.work
connector_file='self_connector',
local=True
) )
self.local_user = models.User.objects.create_user(
'mouse@local.com', 'mouse@mouse.mouse', 'password',
local=True, localname='mouse')
with patch('bookwyrm.models.user.set_remote_server.delay'): with patch('bookwyrm.models.user.set_remote_server.delay'):
self.remote_user = models.User.objects.create_user( self.remote_user = models.User.objects.create_user(
'rat', 'rat@rat.com', 'ratword', 'rat', 'rat@rat.com', 'ratword',
@ -35,30 +31,37 @@ class Views(TestCase):
outbox='https://example.com/users/rat/outbox', outbox='https://example.com/users/rat/outbox',
) )
def test_get_edition(self):
''' given an edition or a work, returns an edition '''
self.assertEqual(
views.helpers.get_edition(self.book.id), self.book)
self.assertEqual(
views.helpers.get_edition(self.work.id), self.book)
def test_get_user_from_username(self): def test_get_user_from_username(self):
''' works for either localname or username ''' ''' works for either localname or username '''
self.assertEqual( self.assertEqual(
views.get_user_from_username('mouse'), self.local_user) views.helpers.get_user_from_username('mouse'), self.local_user)
self.assertEqual( self.assertEqual(
views.get_user_from_username('mouse@local.com'), self.local_user) views.helpers.get_user_from_username(
'mouse@local.com'), self.local_user)
with self.assertRaises(models.User.DoesNotExist): with self.assertRaises(models.User.DoesNotExist):
views.get_user_from_username('mojfse@example.com') views.helpers.get_user_from_username('mojfse@example.com')
def test_is_api_request(self): def test_is_api_request(self):
''' should it return html or json ''' ''' should it return html or json '''
request = self.factory.get('/path') request = self.factory.get('/path')
request.headers = {'Accept': 'application/json'} request.headers = {'Accept': 'application/json'}
self.assertTrue(views.is_api_request(request)) self.assertTrue(views.helpers.is_api_request(request))
request = self.factory.get('/path.json') request = self.factory.get('/path.json')
request.headers = {'Accept': 'Praise'} request.headers = {'Accept': 'Praise'}
self.assertTrue(views.is_api_request(request)) self.assertTrue(views.helpers.is_api_request(request))
request = self.factory.get('/path') request = self.factory.get('/path')
request.headers = {'Accept': 'Praise'} request.headers = {'Accept': 'Praise'}
self.assertFalse(views.is_api_request(request)) self.assertFalse(views.helpers.is_api_request(request))
def test_get_activity_feed(self): def test_get_activity_feed(self):
@ -83,7 +86,7 @@ class Views(TestCase):
content='blah blah blah', user=rat, privacy='followers') content='blah blah blah', user=rat, privacy='followers')
rat_mention.mention_users.set([self.local_user]) rat_mention.mention_users.set([self.local_user])
statuses = views.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'unlisted', 'followers'], ['public', 'unlisted', 'followers'],
following_only=True, following_only=True,
@ -92,7 +95,7 @@ class Views(TestCase):
self.assertEqual(len(statuses), 1) self.assertEqual(len(statuses), 1)
self.assertEqual(statuses[0], public_status) self.assertEqual(statuses[0], public_status)
statuses = views.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'followers'], ['public', 'followers'],
local_only=True local_only=True
@ -101,11 +104,11 @@ class Views(TestCase):
self.assertEqual(statuses[1], public_status) self.assertEqual(statuses[1], public_status)
self.assertEqual(statuses[0], rat_public) self.assertEqual(statuses[0], rat_public)
statuses = views.get_activity_feed(self.local_user, 'direct') statuses = views.helpers.get_activity_feed(self.local_user, 'direct')
self.assertEqual(len(statuses), 1) self.assertEqual(len(statuses), 1)
self.assertEqual(statuses[0], direct_status) self.assertEqual(statuses[0], direct_status)
statuses = views.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'followers'], ['public', 'followers'],
) )
@ -114,7 +117,7 @@ class Views(TestCase):
self.assertEqual(statuses[1], rat_public) self.assertEqual(statuses[1], rat_public)
self.assertEqual(statuses[0], remote_status) self.assertEqual(statuses[0], remote_status)
statuses = views.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'unlisted', 'followers'], ['public', 'unlisted', 'followers'],
following_only=True following_only=True
@ -124,7 +127,7 @@ class Views(TestCase):
self.assertEqual(statuses[0], rat_mention) self.assertEqual(statuses[0], rat_mention)
rat.followers.add(self.local_user) rat.followers.add(self.local_user)
statuses = views.get_activity_feed( statuses = views.helpers.get_activity_feed(
self.local_user, self.local_user,
['public', 'unlisted', 'followers'], ['public', 'unlisted', 'followers'],
following_only=True following_only=True
@ -140,15 +143,15 @@ class Views(TestCase):
def test_is_bookwyrm_request(self): def test_is_bookwyrm_request(self):
''' checks if a request came from a bookwyrm instance ''' ''' checks if a request came from a bookwyrm instance '''
request = self.factory.get('', {'q': 'Test Book'}) request = self.factory.get('', {'q': 'Test Book'})
self.assertFalse(views.is_bookworm_request(request)) self.assertFalse(views.helpers.is_bookworm_request(request))
request = self.factory.get( request = self.factory.get(
'', {'q': 'Test Book'}, '', {'q': 'Test Book'},
HTTP_USER_AGENT=\ HTTP_USER_AGENT=\
"http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)" "http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)"
) )
self.assertFalse(views.is_bookworm_request(request)) self.assertFalse(views.helpers.is_bookworm_request(request))
request = self.factory.get( request = self.factory.get(
'', {'q': 'Test Book'}, HTTP_USER_AGENT=USER_AGENT) '', {'q': 'Test Book'}, HTTP_USER_AGENT=USER_AGENT)
self.assertTrue(views.is_bookworm_request(request)) self.assertTrue(views.helpers.is_bookworm_request(request))

View file

@ -1,19 +0,0 @@
''' views for pages you can go to in the application '''
from django.template.response import TemplateResponse
def is_api_request(request):
''' check whether a request is asking for html or data '''
return 'json' in request.headers.get('Accept') or \
request.path[-5:] == '.json'
def server_error_page(request):
''' 500 errors '''
return TemplateResponse(
request, 'error.html', {'title': 'Oops!'}, status=500)
def not_found_page(request, _):
''' 404s '''
return TemplateResponse(
request, 'notfound.html', {'title': 'Not found'}, status=404)