2021-03-08 16:49:10 +00:00
|
|
|
""" test for app action functionality """
|
2021-01-12 20:05:30 +00:00
|
|
|
from unittest.mock import patch
|
2021-01-26 16:03:16 +00:00
|
|
|
|
2021-02-11 02:01:11 +00:00
|
|
|
from django.contrib.auth.models import AnonymousUser
|
2021-04-30 17:16:24 +00:00
|
|
|
from django.http.response import Http404
|
2021-01-12 20:05:30 +00:00
|
|
|
from django.template.response import TemplateResponse
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
2021-06-14 16:57:51 +00:00
|
|
|
from bookwyrm import models, views
|
2021-01-12 20:05:30 +00:00
|
|
|
from bookwyrm.activitypub import ActivitypubResponse
|
2021-10-01 05:22:20 +00:00
|
|
|
from bookwyrm.tests.validate_html import validate_html
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserViews(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""view user and edit profile"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we need basic test data and mocks"""
|
2021-08-02 23:05:40 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-09-06 21:48:45 +00:00
|
|
|
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",
|
|
|
|
)
|
|
|
|
self.rat = models.User.objects.create_user(
|
|
|
|
"rat@local.com", "rat@rat.rat", "password", local=True, localname="rat"
|
|
|
|
)
|
2021-08-06 00:47:28 +00:00
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
title="test", parent_work=models.Work.objects.create(title="test work")
|
|
|
|
)
|
2021-11-12 17:17:00 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"
|
|
|
|
), patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
|
|
|
"bookwyrm.activitystreams.add_book_statuses_task.delay"
|
|
|
|
):
|
2021-09-06 23:33:21 +00:00
|
|
|
models.ShelfBook.objects.create(
|
|
|
|
book=self.book,
|
|
|
|
user=self.local_user,
|
|
|
|
shelf=self.local_user.shelf_set.first(),
|
|
|
|
)
|
2021-04-20 19:31:45 +00:00
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
models.SiteSettings.objects.create()
|
2021-02-11 02:01:11 +00:00
|
|
|
self.anonymous_user = AnonymousUser
|
|
|
|
self.anonymous_user.is_authenticated = False
|
2021-01-12 20:05:30 +00:00
|
|
|
|
|
|
|
def test_user_page(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-12-10 02:38:05 +00:00
|
|
|
# extras that are rendered on the user page
|
|
|
|
models.AnnualGoal.objects.create(
|
|
|
|
user=self.local_user, goal=12, privacy="followers"
|
|
|
|
)
|
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
view = views.User.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-12 20:05:30 +00:00
|
|
|
request.user = self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-01-12 20:05:30 +00:00
|
|
|
is_api.return_value = False
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse")
|
2021-01-12 20:05:30 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-10-01 05:22:20 +00:00
|
|
|
validate_html(result.render())
|
2021-01-12 20:05:30 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-02-11 02:01:11 +00:00
|
|
|
request.user = self.anonymous_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-02-11 02:01:11 +00:00
|
|
|
is_api.return_value = False
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse")
|
2021-02-11 02:01:11 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-10-01 05:22:20 +00:00
|
|
|
validate_html(result.render())
|
2021-02-11 02:01:11 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-01-12 20:05:30 +00:00
|
|
|
is_api.return_value = True
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse")
|
2021-01-12 20:05:30 +00:00
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-12-28 23:52:35 +00:00
|
|
|
def test_user_page_domain(self):
|
|
|
|
"""when the user domain has dashes in it"""
|
|
|
|
with patch("bookwyrm.models.user.set_remote_server"):
|
|
|
|
self.remote_user = models.User.objects.create_user(
|
|
|
|
"nutria",
|
|
|
|
"",
|
|
|
|
"nutriaword",
|
|
|
|
local=False,
|
|
|
|
remote_id="https://ex--ample.co----m/users/nutria",
|
|
|
|
inbox="https://ex--ample.co----m/users/nutria/inbox",
|
|
|
|
outbox="https://ex--ample.co----m/users/nutria/outbox",
|
|
|
|
)
|
|
|
|
|
|
|
|
view = views.User.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.local_user
|
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
|
|
|
is_api.return_value = False
|
|
|
|
result = view(request, "nutria@ex--ample.co----m")
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-01-26 16:31:55 +00:00
|
|
|
def test_user_page_blocked(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-26 16:31:55 +00:00
|
|
|
view = views.User.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-26 16:31:55 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
self.rat.blocks.add(self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-01-26 16:31:55 +00:00
|
|
|
is_api.return_value = False
|
2021-04-30 17:16:24 +00:00
|
|
|
with self.assertRaises(Http404):
|
|
|
|
view(request, "rat")
|
2021-01-26 16:31:55 +00:00
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
def test_followers_page(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-12 20:05:30 +00:00
|
|
|
view = views.Followers.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-12 20:05:30 +00:00
|
|
|
request.user = self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-01-12 20:05:30 +00:00
|
|
|
is_api.return_value = False
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse")
|
2021-01-12 20:05:30 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-10-01 05:22:20 +00:00
|
|
|
validate_html(result.render())
|
2021-01-12 20:05:30 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-01-12 20:05:30 +00:00
|
|
|
is_api.return_value = True
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse")
|
2021-01-12 20:05:30 +00:00
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-12-10 02:38:05 +00:00
|
|
|
def test_followers_page_anonymous(self):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
view = views.Followers.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.anonymous_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-12-10 02:38:05 +00:00
|
|
|
is_api.return_value = False
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse")
|
2021-12-10 02:38:05 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
2021-01-12 20:05:30 +00:00
|
|
|
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 22:09:04 +00:00
|
|
|
@patch("bookwyrm.activitystreams.populate_stream_task.delay")
|
|
|
|
def test_followers_page_blocked(self, *_):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-26 16:31:55 +00:00
|
|
|
view = views.Followers.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-26 16:31:55 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
self.rat.blocks.add(self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-01-26 16:31:55 +00:00
|
|
|
is_api.return_value = False
|
2021-04-30 17:16:24 +00:00
|
|
|
with self.assertRaises(Http404):
|
|
|
|
view(request, "rat")
|
2021-01-26 16:31:55 +00:00
|
|
|
|
2021-01-12 20:05:30 +00:00
|
|
|
def test_following_page(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-12 20:05:30 +00:00
|
|
|
view = views.Following.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-12 20:05:30 +00:00
|
|
|
request.user = self.local_user
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-01-12 20:05:30 +00:00
|
|
|
is_api.return_value = False
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse")
|
2021-01-12 20:05:30 +00:00
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
2021-10-01 05:22:20 +00:00
|
|
|
validate_html(result.render())
|
2021-01-12 20:05:30 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-01-12 20:05:30 +00:00
|
|
|
is_api.return_value = True
|
2021-03-08 16:49:10 +00:00
|
|
|
result = view(request, "mouse")
|
2021-01-12 20:05:30 +00:00
|
|
|
self.assertIsInstance(result, ActivitypubResponse)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-12-10 02:38:05 +00:00
|
|
|
def test_following_page_anonymous(self):
|
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
|
|
|
view = views.Following.as_view()
|
|
|
|
request = self.factory.get("")
|
|
|
|
request.user = self.anonymous_user
|
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
|
|
|
is_api.return_value = False
|
|
|
|
result = view(request, "mouse")
|
|
|
|
self.assertIsInstance(result, TemplateResponse)
|
|
|
|
validate_html(result.render())
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
2021-01-26 16:31:55 +00:00
|
|
|
def test_following_page_blocked(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""there are so many views, this just makes sure it LOADS"""
|
2021-01-26 16:31:55 +00:00
|
|
|
view = views.Following.as_view()
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
2021-01-26 16:31:55 +00:00
|
|
|
request.user = self.local_user
|
|
|
|
self.rat.blocks.add(self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.user.is_api_request") as is_api:
|
2021-01-26 16:31:55 +00:00
|
|
|
is_api.return_value = False
|
2021-04-30 18:26:02 +00:00
|
|
|
with self.assertRaises(Http404):
|
2021-06-18 16:28:24 +00:00
|
|
|
view(request, "rat")
|
2021-12-10 02:38:05 +00:00
|
|
|
|
|
|
|
def test_hide_suggestions(self):
|
|
|
|
"""update suggestions settings"""
|
|
|
|
self.assertTrue(self.local_user.show_suggested_users)
|
|
|
|
request = self.factory.post("")
|
|
|
|
request.user = self.local_user
|
|
|
|
|
|
|
|
result = views.hide_suggestions(request)
|
2021-12-10 02:42:09 +00:00
|
|
|
self.assertEqual(result.status_code, 302)
|
2021-12-10 02:38:05 +00:00
|
|
|
|
|
|
|
self.local_user.refresh_from_db()
|
|
|
|
self.assertFalse(self.local_user.show_suggested_users)
|
2021-12-28 20:49:41 +00:00
|
|
|
|
|
|
|
def test_user_redirect(self):
|
|
|
|
"""test the basic redirect"""
|
|
|
|
request = self.factory.get("@mouse")
|
|
|
|
request.user = self.anonymous_user
|
|
|
|
result = views.user_redirect(request, "mouse")
|
|
|
|
|
|
|
|
self.assertEqual(result.status_code, 302)
|