moviewyrm/bookwyrm/tests/views/test_goal.py

133 lines
4.5 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
2021-02-24 06:07:13 +00:00
from django.utils import timezone
2021-01-16 21:29:28 +00:00
from django.contrib.auth.models import AnonymousUser
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
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()
2021-08-03 17:25:53 +00:00
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"):
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-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
result = view(request, self.local_user.localname, 2020)
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
result = view(request, self.local_user.localname, 2020)
2021-02-24 06:07:13 +00:00
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
result = view(request, self.local_user.localname, 2020)
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)
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-03-08 16:49:10 +00:00
user=self.local_user, year=2020, goal=15, privacy="followers"
)
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
result = view(request, self.local_user.localname, 2020)
self.assertEqual(result.status_code, 404)
2021-03-23 18:27:00 +00:00
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
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,
"year": 2020,
"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-01-16 21:29:28 +00:00
view(request, self.local_user.localname, 2020)
goal = models.AnnualGoal.objects.get()
self.assertEqual(goal.user, self.local_user)
self.assertEqual(goal.goal, 10)
self.assertEqual(goal.year, 2020)
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")