2021-04-01 15:20:57 +00:00
|
|
|
""" test for app action functionality """
|
2021-04-01 15:32:06 +00:00
|
|
|
from unittest.mock import patch
|
2021-04-01 15:20:57 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
2021-04-01 15:32:06 +00:00
|
|
|
from bookwyrm import forms, models, views
|
2021-04-01 15:20:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GetStartedViews(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""helping new users get oriented"""
|
2021-04-01 15:20:57 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need basic test data and mocks"""
|
2021-04-01 15:20:57 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-08-02 23:05:40 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse@local.com",
|
|
|
|
"mouse@mouse.mouse",
|
|
|
|
"password",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
parent_work=models.Work.objects.create(title="hi"),
|
|
|
|
title="Example Edition",
|
|
|
|
remote_id="https://example.com/book/1",
|
|
|
|
)
|
2021-04-01 16:02:48 +00:00
|
|
|
models.Connector.objects.create(
|
|
|
|
identifier="self", connector_file="self_connector", local=True
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
models.SiteSettings.objects.create()
|
2021-04-01 15:20:57 +00:00
|
|
|
|
|
|
|
def test_profile_view(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-04-01 15:20:57 +00:00
|
|
|
view = views.GetStartedProfile.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
result = view(request)
|
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
result.render()
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_profile_view_post(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""save basic user details"""
|
2021-04-01 15:32:06 +00:00
|
|
|
view = views.GetStartedProfile.as_view()
|
|
|
|
form = forms.LimitedEditUserForm(instance=self.local_user)
|
|
|
|
form.data["name"] = "New Name"
|
|
|
|
form.data["discoverable"] = "True"
|
|
|
|
request = self.factory.post("", form.data)
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
self.assertIsNone(self.local_user.name)
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
|
|
|
|
) as delay_mock:
|
|
|
|
view(request)
|
|
|
|
self.assertEqual(delay_mock.call_count, 1)
|
2021-04-01 15:32:06 +00:00
|
|
|
self.assertEqual(self.local_user.name, "New Name")
|
|
|
|
self.assertTrue(self.local_user.discoverable)
|
2021-04-01 15:20:57 +00:00
|
|
|
|
|
|
|
def test_books_view(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-04-01 15:20:57 +00:00
|
|
|
view = views.GetStartedBooks.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
result = view(request)
|
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
result.render()
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-04-01 16:02:48 +00:00
|
|
|
def test_books_view_with_query(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-04-01 16:02:48 +00:00
|
|
|
view = views.GetStartedBooks.as_view()
|
|
|
|
request = self.factory.get("?query=Example")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
result = view(request)
|
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
result.render()
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_books_view_post(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""shelve some books"""
|
2021-04-01 16:02:48 +00:00
|
|
|
view = views.GetStartedBooks.as_view()
|
|
|
|
data = {self.book.id: self.local_user.shelf_set.first().id}
|
|
|
|
request = self.factory.post("", data)
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
self.assertFalse(self.local_user.shelfbook_set.exists())
|
|
|
|
with patch(
|
|
|
|
"bookwyrm.models.activitypub_mixin.broadcast_task.delay"
|
|
|
|
) as delay_mock:
|
|
|
|
view(request)
|
|
|
|
self.assertEqual(delay_mock.call_count, 1)
|
|
|
|
|
|
|
|
shelfbook = self.local_user.shelfbook_set.first()
|
|
|
|
self.assertEqual(shelfbook.book, self.book)
|
|
|
|
self.assertEqual(shelfbook.user, self.local_user)
|
|
|
|
|
2021-04-01 15:20:57 +00:00
|
|
|
def test_users_view(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-04-01 15:20:57 +00:00
|
|
|
view = views.GetStartedUsers.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
result = view(request)
|
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
result.render()
|
|
|
|
self.assertEqual(result.status_code, 200)
|
2021-04-01 16:39:57 +00:00
|
|
|
|
|
|
|
def test_users_view_with_query(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-04-01 16:39:57 +00:00
|
|
|
view = views.GetStartedUsers.as_view()
|
|
|
|
request = self.factory.get("?query=rat")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
result = view(request)
|
|
|
|
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
result.render()
|
|
|
|
self.assertEqual(result.status_code, 200)
|