list views tests

This commit is contained in:
Mouse Reeve 2021-01-31 13:12:56 -08:00
parent 944503dde5
commit 19b56c26f7

View file

@ -20,11 +20,14 @@ class ListViews(TestCase):
local=True, localname='mouse', local=True, localname='mouse',
remote_id='https://example.com/users/mouse', remote_id='https://example.com/users/mouse',
) )
self.work = models.Work.objects.create(title='Test Work') self.rat = models.User.objects.create_user(
'rat@local.com', 'rat@rat.com', 'ratword',
local=True, localname='rat',
remote_id='https://example.com/users/rat',
)
self.book = models.Edition.objects.create( self.book = models.Edition.objects.create(
title='Example Edition', title='Example Edition',
remote_id='https://example.com/book/1', remote_id='https://example.com/book/1',
parent_work=self.work
) )
self.list = models.List.objects.create( self.list = models.List.objects.create(
name='Test List', user=self.local_user) name='Test List', user=self.local_user)
@ -55,6 +58,25 @@ class ListViews(TestCase):
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
def test_lists_create(self):
''' create list view '''
view = views.Lists.as_view()
request = self.factory.post('', {
'name': 'A list',
'description': 'wow',
'privacy': 'unlisted',
'curation': 'open',
'user': self.local_user.id,
})
request.user = self.local_user
result = view(request)
self.assertEqual(result.status_code, 302)
new_list = models.List.objects.filter(name='A list').get()
self.assertEqual(new_list.description, 'wow')
self.assertEqual(new_list.privacy, 'unlisted')
self.assertEqual(new_list.curation, 'open')
def test_list_page(self): def test_list_page(self):
''' there are so many views, this just makes sure it LOADS ''' ''' there are so many views, this just makes sure it LOADS '''
view = views.List.as_view() view = views.List.as_view()
@ -81,3 +103,186 @@ class ListViews(TestCase):
result = view(request, self.list.id) result = view(request, self.list.id)
self.assertIsInstance(result, ActivitypubResponse) self.assertIsInstance(result, ActivitypubResponse)
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
def test_list_edit(self):
''' edit a list '''
view = views.List.as_view()
request = self.factory.post('', {
'name': 'New Name',
'description': 'wow',
'privacy': 'direct',
'curation': 'curated',
'user': self.local_user.id,
})
request.user = self.local_user
result = view(request, self.list.id)
self.assertEqual(result.status_code, 302)
self.list.refresh_from_db()
self.assertEqual(self.list.name, 'New Name')
self.assertEqual(self.list.description, 'wow')
self.assertEqual(self.list.privacy, 'direct')
self.assertEqual(self.list.curation, 'curated')
def test_curate_page(self):
''' there are so many views, this just makes sure it LOADS '''
view = views.Curate.as_view()
models.List.objects.create(name='Public list', user=self.local_user)
models.List.objects.create(
name='Private list', privacy='private', user=self.local_user)
request = self.factory.get('')
request.user = self.local_user
result = view(request, self.list.id)
self.assertIsInstance(result, TemplateResponse)
result.render()
self.assertEqual(result.status_code, 200)
request.user = self.anonymous_user
result = view(request, self.list.id)
self.assertEqual(result.status_code, 302)
def test_curate_approve(self):
''' approve a pending item '''
view = views.Curate.as_view()
pending = models.ListItem.objects.create(
book_list=self.list,
added_by=self.local_user,
book=self.book,
approved=False
)
request = self.factory.post('', {
'item': pending.id,
'approved': 'true',
})
request.user = self.local_user
view(request, self.list.id)
pending.refresh_from_db()
self.assertEqual(self.list.books.count(), 1)
self.assertEqual(self.list.listitem_set.first(), pending)
self.assertTrue(pending.approved)
def test_curate_reject(self):
''' approve a pending item '''
view = views.Curate.as_view()
pending = models.ListItem.objects.create(
book_list=self.list,
added_by=self.local_user,
book=self.book,
approved=False
)
request = self.factory.post('', {
'item': pending.id,
'approved': 'false',
})
request.user = self.local_user
view(request, self.list.id)
self.assertFalse(self.list.books.exists())
self.assertFalse(models.ListItem.objects.exists())
def test_add_book(self):
''' put a book on a list '''
request = self.factory.post('', {
'book': self.book.id,
})
request.user = self.local_user
views.list.add_book(request, self.list.id)
item = self.list.listitem_set.get()
self.assertEqual(item.book, self.book)
self.assertEqual(item.added_by, self.local_user)
self.assertTrue(item.approved)
def test_add_book_outsider(self):
''' put a book on a list '''
self.list.curation = 'open'
self.list.save()
request = self.factory.post('', {
'book': self.book.id,
})
request.user = self.rat
views.list.add_book(request, self.list.id)
item = self.list.listitem_set.get()
self.assertEqual(item.book, self.book)
self.assertEqual(item.added_by, self.rat)
self.assertTrue(item.approved)
def test_add_book_pending(self):
''' put a book on a list '''
self.list.curation = 'curated'
self.list.save()
request = self.factory.post('', {
'book': self.book.id,
})
request.user = self.rat
views.list.add_book(request, self.list.id)
item = self.list.listitem_set.get()
self.assertEqual(item.book, self.book)
self.assertEqual(item.added_by, self.rat)
self.assertFalse(item.approved)
def test_add_book_self_curated(self):
''' put a book on a list '''
self.list.curation = 'curated'
self.list.save()
request = self.factory.post('', {
'book': self.book.id,
})
request.user = self.local_user
views.list.add_book(request, self.list.id)
item = self.list.listitem_set.get()
self.assertEqual(item.book, self.book)
self.assertEqual(item.added_by, self.local_user)
self.assertTrue(item.approved)
def test_remove_book(self):
''' take an item off a list '''
item = models.ListItem.objects.create(
book_list=self.list,
added_by=self.local_user,
book=self.book,
)
self.assertTrue(self.list.listitem_set.exists())
request = self.factory.post('', {
'item': item.id,
})
request.user = self.local_user
views.list.remove_book(request, self.list.id)
self.assertFalse(self.list.listitem_set.exists())
def test_remove_book_unauthorized(self):
''' take an item off a list '''
item = models.ListItem.objects.create(
book_list=self.list,
added_by=self.local_user,
book=self.book,
)
self.assertTrue(self.list.listitem_set.exists())
request = self.factory.post('', {
'item': item.id,
})
request.user = self.rat
views.list.remove_book(request, self.list.id)
self.assertTrue(self.list.listitem_set.exists())