moviewyrm/bookwyrm/tests/views/test_get_started.py

144 lines
5.4 KiB
Python
Raw Normal View History

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
from bookwyrm.tests.validate_html import validate_html
2021-04-01 15:20:57 +00:00
2021-09-06 22:09:04 +00:00
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
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()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
2021-12-09 23:03:01 +00:00
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
2021-08-03 17:25:53 +00:00
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",
"password",
local=True,
localname="mouse",
)
2022-01-07 18:06:11 +00:00
self.local_user = models.User.objects.create_user(
"rat@local.com",
"rat@rat.rat",
"password",
local=True,
localname="rat",
)
2021-08-02 23:05:40 +00:00
self.book = models.Edition.objects.create(
parent_work=models.Work.objects.create(title="hi"),
title="Example Edition",
remote_id="https://example.com/book/1",
)
models.SiteSettings.objects.create()
2021-04-01 15:20:57 +00:00
2021-09-06 23:59:58 +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)
validate_html(result.render())
2021-04-01 15:20:57 +00:00
self.assertEqual(result.status_code, 200)
2021-08-03 23:21:29 +00:00
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
@patch("bookwyrm.suggested_users.rerank_user_task.delay")
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(
2021-11-12 17:17:00 +00:00
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
2021-08-02 23:05:40 +00:00
) 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
2021-09-06 23:59:58 +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)
validate_html(result.render())
2021-04-01 15:20:57 +00:00
self.assertEqual(result.status_code, 200)
2021-09-06 23:59:58 +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"""
view = views.GetStartedBooks.as_view()
request = self.factory.get("?query=Example")
request.user = self.local_user
result = view(request)
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
self.assertEqual(result.status_code, 200)
2021-08-03 23:21:29 +00:00
@patch("bookwyrm.suggested_users.rerank_suggestions_task.delay")
2021-09-06 23:59:58 +00:00
@patch("bookwyrm.activitystreams.add_book_statuses_task.delay")
def test_books_view_post(self, *_):
2021-04-26 16:15:42 +00:00
"""shelve some books"""
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(
2021-11-12 17:17:00 +00:00
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
) 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-08-04 15:50:50 +00:00
@patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions")
2021-09-06 23:59:58 +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)
validate_html(result.render())
2021-04-01 15:20:57 +00:00
self.assertEqual(result.status_code, 200)
2021-04-01 16:39:57 +00:00
2021-09-06 23:59:58 +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
2022-01-07 18:06:11 +00:00
with patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions") as mock:
mock.return_value = models.User.objects.all()
result = view(request)
2021-04-01 16:39:57 +00:00
self.assertIsInstance(result, TemplateResponse)
validate_html(result.render())
2021-04-01 16:39:57 +00:00
self.assertEqual(result.status_code, 200)