moviewyrm/bookwyrm/tests/views/test_goal.py

138 lines
4.7 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-01-16 21:29:28 +00:00
from unittest.mock import patch
from django.contrib.auth.models import AnonymousUser
2021-09-28 00:52:10 +00:00
from django.http import Http404
2021-01-16 21:29:28 +00:00
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from django.utils import timezone
2021-01-16 21:29:28 +00:00
from bookwyrm import models, views
2021-10-01 05:22:20 +00:00
from bookwyrm.tests.validate_html import validate_html
2021-01-16 21:29:28 +00:00
class GoalViews(TestCase):
2021-04-26 16:15:42 +00:00
"""viewing and creating statuses"""
2021-03-08 16:49:10 +00:00
2021-01-16 21:29:28 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""we need basic test data and mocks"""
2021-01-16 21:29:28 +00:00
self.factory = RequestFactory()
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
2021-08-03 17:25:53 +00:00
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.rat = models.User.objects.create_user(
"rat@local.com",
"rat@rat.com",
"ratword",
local=True,
localname="rat",
remote_id="https://example.com/users/rat",
)
2021-08-02 23:05:40 +00:00
self.book = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
)
2021-01-16 21:29:28 +00:00
self.anonymous_user = AnonymousUser
self.anonymous_user.is_authenticated = False
2021-08-04 19:59:06 +00:00
self.year = timezone.now().year
2021-08-02 23:05:40 +00:00
models.SiteSettings.objects.create()
2021-01-16 21:29:28 +00:00
def test_goal_page_no_goal(self):
2021-04-26 16:15:42 +00:00
"""view a reading goal page for another's unset goal"""
2021-01-16 21:29:28 +00:00
view = views.Goal.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-16 21:29:28 +00:00
request.user = self.rat
2021-08-04 19:59:06 +00:00
result = view(request, self.local_user.localname, self.year)
2021-01-16 21:29:28 +00:00
self.assertEqual(result.status_code, 404)
def test_goal_page_no_goal_self(self):
2021-04-26 16:15:42 +00:00
"""view a reading goal page for your own unset goal"""
2021-01-16 21:29:28 +00:00
view = views.Goal.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-16 21:29:28 +00:00
request.user = self.local_user
2021-08-04 19:59:06 +00:00
result = view(request, self.local_user.localname, self.year)
2021-10-01 05:22:20 +00:00
validate_html(result.render())
2021-01-16 21:29:28 +00:00
self.assertIsInstance(result, TemplateResponse)
def test_goal_page_anonymous(self):
2021-04-26 16:15:42 +00:00
"""can't view it without login"""
2021-01-16 21:29:28 +00:00
view = views.Goal.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-16 21:29:28 +00:00
request.user = self.anonymous_user
2021-08-04 19:59:06 +00:00
result = view(request, self.local_user.localname, self.year)
2021-01-16 21:29:28 +00:00
self.assertEqual(result.status_code, 302)
def test_goal_page_public(self):
2021-04-26 16:15:42 +00:00
"""view a user's public goal"""
2021-02-24 06:07:13 +00:00
models.ReadThrough.objects.create(
finish_date=timezone.now(),
user=self.local_user,
book=self.book,
)
2021-01-16 21:29:28 +00:00
models.AnnualGoal.objects.create(
user=self.local_user,
2021-02-24 06:07:13 +00:00
year=timezone.now().year,
2021-01-16 21:29:28 +00:00
goal=128937123,
2021-03-08 16:49:10 +00:00
privacy="public",
)
2021-01-16 21:29:28 +00:00
view = views.Goal.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-16 21:29:28 +00:00
request.user = self.rat
2021-02-24 06:07:13 +00:00
result = view(request, self.local_user.localname, timezone.now().year)
2021-10-01 05:22:20 +00:00
validate_html(result.render())
2021-01-16 21:29:28 +00:00
self.assertIsInstance(result, TemplateResponse)
def test_goal_page_private(self):
2021-04-26 16:15:42 +00:00
"""view a user's private goal"""
2021-01-16 21:29:28 +00:00
models.AnnualGoal.objects.create(
2021-08-04 19:59:06 +00:00
user=self.local_user, year=self.year, goal=15, privacy="followers"
2021-03-08 16:49:10 +00:00
)
2021-01-16 21:29:28 +00:00
view = views.Goal.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("")
2021-01-16 21:29:28 +00:00
request.user = self.rat
2021-09-28 00:52:10 +00:00
with self.assertRaises(Http404):
view(request, self.local_user.localname, self.year)
2021-01-16 21:29:28 +00:00
2021-09-06 20:53:49 +00:00
@patch("bookwyrm.activitystreams.add_status_task.delay")
2021-03-23 18:27:00 +00:00
def test_create_goal(self, _):
2021-04-26 16:15:42 +00:00
"""create a new goal"""
2021-01-16 21:29:28 +00:00
view = views.Goal.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.post(
"",
{
"user": self.local_user.id,
"goal": 10,
2021-08-04 19:59:06 +00:00
"year": self.year,
2021-03-08 16:49:10 +00:00
"privacy": "unlisted",
"post-status": True,
},
)
2021-01-16 21:29:28 +00:00
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-08-04 19:59:06 +00:00
view(request, self.local_user.localname, self.year)
2021-01-16 21:29:28 +00:00
goal = models.AnnualGoal.objects.get()
self.assertEqual(goal.user, self.local_user)
self.assertEqual(goal.goal, 10)
2021-08-04 19:59:06 +00:00
self.assertEqual(goal.year, self.year)
2021-03-08 16:49:10 +00:00
self.assertEqual(goal.privacy, "unlisted")
2021-01-16 21:29:28 +00:00
status = models.GeneratedNote.objects.get()
self.assertEqual(status.user, self.local_user)
2021-03-08 16:49:10 +00:00
self.assertEqual(status.privacy, "unlisted")