moviewyrm/bookwyrm/tests/views/test_discover.py

79 lines
2.9 KiB
Python
Raw Normal View History

2021-08-08 15:03:04 +00:00
""" test for app action functionality """
from unittest.mock import patch
from django.contrib.auth.models import AnonymousUser
from django.test import TestCase
from django.test.client import RequestFactory
2021-12-29 18:44:04 +00:00
from bookwyrm import models, views
2021-12-09 23:03:01 +00:00
from bookwyrm.tests.validate_html import validate_html
2021-08-08 15:03:04 +00:00
class DiscoverViews(TestCase):
"""pages you land on without really trying"""
def setUp(self):
"""we need basic test data and mocks"""
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-08 15:03:04 +00:00
self.local_user = models.User.objects.create_user(
"mouse@local.com",
"mouse@mouse.mouse",
"password",
local=True,
localname="mouse",
)
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
models.SiteSettings.objects.create()
def test_discover_page_empty(self):
"""there are so many views, this just makes sure it LOADS"""
2021-08-08 21:17:02 +00:00
view = views.Discover.as_view()
2021-08-08 15:03:04 +00:00
request = self.factory.get("")
request.user = self.local_user
with patch(
"bookwyrm.activitystreams.ActivityStream.get_activity_stream"
) as mock:
result = view(request)
self.assertEqual(mock.call_count, 1)
self.assertEqual(result.status_code, 200)
2021-12-09 23:03:01 +00:00
validate_html(result.render())
2021-08-08 15:03:04 +00:00
2021-11-12 17:17:00 +00:00
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
2021-09-06 20:53:49 +00:00
@patch("bookwyrm.activitystreams.add_status_task.delay")
2021-08-08 15:37:07 +00:00
def test_discover_page(self, *_):
2021-08-08 15:03:04 +00:00
"""there are so many views, this just makes sure it LOADS"""
2021-08-08 21:17:02 +00:00
view = views.Discover.as_view()
2021-08-08 15:03:04 +00:00
request = self.factory.get("")
request.user = self.local_user
book = models.Edition.objects.create(
title="hi", parent_work=models.Work.objects.create(title="work")
)
models.Comment.objects.create(
book=book,
user=self.local_user,
content="hello",
)
models.Status.objects.create(user=self.local_user, content="beep")
with patch(
"bookwyrm.activitystreams.ActivityStream.get_activity_stream"
) as mock:
mock.return_value = models.Status.objects.all()
result = view(request)
self.assertEqual(mock.call_count, 1)
self.assertEqual(result.status_code, 200)
2021-12-09 23:03:01 +00:00
validate_html(result.render())
2021-08-08 15:03:04 +00:00
def test_discover_page_logged_out(self):
"""there are so many views, this just makes sure it LOADS"""
2021-08-08 21:17:02 +00:00
view = views.Discover.as_view()
2021-08-08 15:03:04 +00:00
request = self.factory.get("")
request.user = self.anonymous_user
result = view(request)
2021-08-08 21:43:17 +00:00
self.assertEqual(result.status_code, 302)