moviewyrm/bookwyrm/tests/views/test_helpers.py

341 lines
13 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-01-13 21:54:01 +00:00
import json
2020-12-30 19:37:49 +00:00
from unittest.mock import patch
2021-01-13 21:54:01 +00:00
import pathlib
from django.db.models import Q
2020-12-30 19:37:49 +00:00
from django.test import TestCase
from django.test.client import RequestFactory
2021-01-13 21:54:01 +00:00
import responses
2020-12-30 19:37:49 +00:00
2021-01-13 20:18:19 +00:00
from bookwyrm import models, views
2021-01-13 20:03:27 +00:00
from bookwyrm.settings import USER_AGENT
2020-12-30 19:37:49 +00:00
2021-03-08 16:49:10 +00:00
2021-03-27 16:56:46 +00:00
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
2021-01-13 20:18:19 +00:00
class ViewsHelpers(TestCase):
2021-03-08 16:49:10 +00:00
""" viewing and creating statuses """
2020-12-30 19:37:49 +00:00
def setUp(self):
2021-03-08 16:49:10 +00:00
""" we need basic test data and mocks """
2020-12-30 19:37:49 +00:00
self.factory = RequestFactory()
2021-01-13 20:18:19 +00:00
self.local_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"mouse@local.com",
"mouse@mouse.com",
"mouseword",
local=True,
2021-03-27 16:56:46 +00:00
discoverable=True,
2021-03-08 16:49:10 +00:00
localname="mouse",
remote_id="https://example.com/users/mouse",
2021-01-13 20:18:19 +00:00
)
2021-03-08 16:49:10 +00:00
self.work = models.Work.objects.create(title="Test Work")
2020-12-31 15:52:08 +00:00
self.book = models.Edition.objects.create(
2021-03-08 16:49:10 +00:00
title="Test Book",
remote_id="https://example.com/book/1",
parent_work=self.work,
2020-12-30 19:37:49 +00:00
)
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.models.user.set_remote_server.delay"):
2020-12-30 22:57:57 +00:00
self.remote_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"rat",
"rat@rat.com",
"ratword",
2020-12-30 22:57:57 +00:00
local=False,
2021-03-08 16:49:10 +00:00
remote_id="https://example.com/users/rat",
2021-03-27 16:56:46 +00:00
discoverable=True,
2021-03-08 16:49:10 +00:00
inbox="https://example.com/users/rat/inbox",
outbox="https://example.com/users/rat/outbox",
2020-12-30 22:57:57 +00:00
)
2021-03-08 16:49:10 +00:00
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_user.json")
2021-01-13 21:54:01 +00:00
self.userdata = json.loads(datafile.read_bytes())
2021-03-08 16:49:10 +00:00
del self.userdata["icon"]
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-02-07 06:37:19 +00:00
self.shelf = models.Shelf.objects.create(
2021-03-08 16:49:10 +00:00
name="Test Shelf", identifier="test-shelf", user=self.local_user
2021-02-07 06:37:19 +00:00
)
2021-01-13 21:54:01 +00:00
2021-03-27 16:56:46 +00:00
def test_get_edition(self, _):
2021-03-08 16:49:10 +00:00
""" given an edition or a work, returns an edition """
self.assertEqual(views.helpers.get_edition(self.book.id), self.book)
self.assertEqual(views.helpers.get_edition(self.work.id), self.book)
2021-03-27 16:56:46 +00:00
def test_get_user_from_username(self, _):
2021-03-08 16:49:10 +00:00
""" works for either localname or username """
self.assertEqual(
2021-03-08 16:49:10 +00:00
views.helpers.get_user_from_username(self.local_user, "mouse"),
self.local_user,
)
self.assertEqual(
2021-03-08 16:49:10 +00:00
views.helpers.get_user_from_username(self.local_user, "mouse@local.com"),
self.local_user,
)
with self.assertRaises(models.User.DoesNotExist):
2021-03-08 16:49:10 +00:00
views.helpers.get_user_from_username(self.local_user, "mojfse@example.com")
2020-12-30 19:37:49 +00:00
2021-03-27 16:56:46 +00:00
def test_is_api_request(self, _):
2021-03-08 16:49:10 +00:00
""" should it return html or json """
request = self.factory.get("/path")
request.headers = {"Accept": "application/json"}
2021-01-13 20:18:19 +00:00
self.assertTrue(views.helpers.is_api_request(request))
2020-12-30 22:57:57 +00:00
2021-03-08 16:49:10 +00:00
request = self.factory.get("/path.json")
request.headers = {"Accept": "Praise"}
2021-01-13 20:18:19 +00:00
self.assertTrue(views.helpers.is_api_request(request))
2020-12-30 22:57:57 +00:00
2021-03-08 16:49:10 +00:00
request = self.factory.get("/path")
request.headers = {"Accept": "Praise"}
2021-01-13 20:18:19 +00:00
self.assertFalse(views.helpers.is_api_request(request))
def test_is_api_request_no_headers(self, _):
""" should it return html or json """
request = self.factory.get("/path")
self.assertFalse(views.helpers.is_api_request(request))
2020-12-30 22:57:57 +00:00
2021-03-27 16:56:46 +00:00
def test_is_bookwyrm_request(self, _):
2021-03-08 16:49:10 +00:00
""" checks if a request came from a bookwyrm instance """
request = self.factory.get("", {"q": "Test Book"})
2021-02-23 19:34:15 +00:00
self.assertFalse(views.helpers.is_bookwyrm_request(request))
2021-01-04 18:08:45 +00:00
request = self.factory.get(
2021-03-08 16:49:10 +00:00
"",
{"q": "Test Book"},
HTTP_USER_AGENT="http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)",
2021-01-04 18:08:45 +00:00
)
2021-02-23 19:34:15 +00:00
self.assertFalse(views.helpers.is_bookwyrm_request(request))
2021-03-08 16:49:10 +00:00
request = self.factory.get("", {"q": "Test Book"}, HTTP_USER_AGENT=USER_AGENT)
2021-02-23 19:34:15 +00:00
self.assertTrue(views.helpers.is_bookwyrm_request(request))
2021-01-13 21:54:01 +00:00
2021-03-27 16:56:46 +00:00
def test_existing_user(self, _):
2021-03-08 16:49:10 +00:00
""" simple database lookup by username """
result = views.helpers.handle_remote_webfinger("@mouse@local.com")
2021-01-13 21:54:01 +00:00
self.assertEqual(result, self.local_user)
2021-03-08 16:49:10 +00:00
result = views.helpers.handle_remote_webfinger("mouse@local.com")
2021-01-13 21:54:01 +00:00
self.assertEqual(result, self.local_user)
@responses.activate
2021-03-27 16:56:46 +00:00
def test_load_user(self, _):
2021-03-08 16:49:10 +00:00
""" find a remote user using webfinger """
username = "mouse@example.com"
2021-01-13 21:54:01 +00:00
wellknown = {
"subject": "acct:mouse@example.com",
2021-03-08 16:49:10 +00:00
"links": [
{
"rel": "self",
"type": "application/activity+json",
"href": "https://example.com/user/mouse",
}
],
2021-01-13 21:54:01 +00:00
}
responses.add(
responses.GET,
2021-03-08 16:49:10 +00:00
"https://example.com/.well-known/webfinger?resource=acct:%s" % username,
2021-01-13 21:54:01 +00:00
json=wellknown,
2021-03-08 16:49:10 +00:00
status=200,
)
2021-01-13 21:54:01 +00:00
responses.add(
responses.GET,
2021-03-08 16:49:10 +00:00
"https://example.com/user/mouse",
2021-01-13 21:54:01 +00:00
json=self.userdata,
2021-03-08 16:49:10 +00:00
status=200,
)
with patch("bookwyrm.models.user.set_remote_server.delay"):
result = views.helpers.handle_remote_webfinger("@mouse@example.com")
2021-01-13 21:54:01 +00:00
self.assertIsInstance(result, models.User)
2021-03-08 16:49:10 +00:00
self.assertEqual(result.username, "mouse@example.com")
2021-01-13 21:54:01 +00:00
2021-03-23 16:00:04 +00:00
def test_handle_reading_status_to_read(self, _):
2021-03-08 16:49:10 +00:00
""" posts shelve activities """
shelf = self.local_user.shelf_set.get(identifier="to-read")
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-01-13 21:54:01 +00:00
views.helpers.handle_reading_status(
2021-03-08 16:49:10 +00:00
self.local_user, shelf, self.book, "public"
)
2021-01-13 21:54:01 +00:00
status = models.GeneratedNote.objects.get()
self.assertEqual(status.user, self.local_user)
self.assertEqual(status.mention_books.first(), self.book)
2021-03-08 16:49:10 +00:00
self.assertEqual(status.content, "wants to read")
2021-01-13 21:54:01 +00:00
2021-03-23 16:00:04 +00:00
def test_handle_reading_status_reading(self, _):
2021-03-08 16:49:10 +00:00
""" posts shelve activities """
shelf = self.local_user.shelf_set.get(identifier="reading")
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-01-13 21:54:01 +00:00
views.helpers.handle_reading_status(
2021-03-08 16:49:10 +00:00
self.local_user, shelf, self.book, "public"
)
2021-01-13 21:54:01 +00:00
status = models.GeneratedNote.objects.get()
self.assertEqual(status.user, self.local_user)
self.assertEqual(status.mention_books.first(), self.book)
2021-03-08 16:49:10 +00:00
self.assertEqual(status.content, "started reading")
2021-01-13 21:54:01 +00:00
2021-03-23 16:00:04 +00:00
def test_handle_reading_status_read(self, _):
2021-03-08 16:49:10 +00:00
""" posts shelve activities """
shelf = self.local_user.shelf_set.get(identifier="read")
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-01-13 21:54:01 +00:00
views.helpers.handle_reading_status(
2021-03-08 16:49:10 +00:00
self.local_user, shelf, self.book, "public"
)
2021-01-13 21:54:01 +00:00
status = models.GeneratedNote.objects.get()
self.assertEqual(status.user, self.local_user)
self.assertEqual(status.mention_books.first(), self.book)
2021-03-08 16:49:10 +00:00
self.assertEqual(status.content, "finished reading")
2021-01-13 21:54:01 +00:00
2021-03-23 16:00:04 +00:00
def test_handle_reading_status_other(self, _):
2021-03-08 16:49:10 +00:00
""" posts shelve activities """
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
2021-01-13 21:54:01 +00:00
views.helpers.handle_reading_status(
2021-03-08 16:49:10 +00:00
self.local_user, self.shelf, self.book, "public"
)
2021-01-13 21:54:01 +00:00
self.assertFalse(models.GeneratedNote.objects.exists())
2021-01-23 19:40:41 +00:00
2021-03-23 16:00:04 +00:00
def test_object_visible_to_user(self, _):
2021-03-08 16:49:10 +00:00
""" does a user have permission to view an object """
2021-01-23 19:40:41 +00:00
obj = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
content="hi", user=self.remote_user, privacy="public"
)
self.assertTrue(views.helpers.object_visible_to_user(self.local_user, obj))
2021-01-23 19:40:41 +00:00
obj = models.Shelf.objects.create(
2021-03-08 16:49:10 +00:00
name="test", user=self.remote_user, privacy="unlisted"
)
self.assertTrue(views.helpers.object_visible_to_user(self.local_user, obj))
2021-01-23 19:40:41 +00:00
obj = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
content="hi", user=self.remote_user, privacy="followers"
)
self.assertFalse(views.helpers.object_visible_to_user(self.local_user, obj))
2021-01-23 19:40:41 +00:00
obj = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
content="hi", user=self.remote_user, privacy="direct"
)
self.assertFalse(views.helpers.object_visible_to_user(self.local_user, obj))
2021-01-23 19:40:41 +00:00
obj = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
content="hi", user=self.remote_user, privacy="direct"
)
2021-01-23 19:40:41 +00:00
obj.mention_users.add(self.local_user)
2021-03-08 16:49:10 +00:00
self.assertTrue(views.helpers.object_visible_to_user(self.local_user, obj))
2021-01-23 19:40:41 +00:00
2021-03-23 16:00:04 +00:00
def test_object_visible_to_user_follower(self, _):
2021-03-08 16:49:10 +00:00
""" what you can see if you follow a user """
2021-01-23 19:40:41 +00:00
self.remote_user.followers.add(self.local_user)
obj = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
content="hi", user=self.remote_user, privacy="followers"
)
self.assertTrue(views.helpers.object_visible_to_user(self.local_user, obj))
2021-01-23 19:40:41 +00:00
obj = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
content="hi", user=self.remote_user, privacy="direct"
)
self.assertFalse(views.helpers.object_visible_to_user(self.local_user, obj))
2021-01-23 19:40:41 +00:00
obj = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
content="hi", user=self.remote_user, privacy="direct"
)
2021-01-23 19:40:41 +00:00
obj.mention_users.add(self.local_user)
2021-03-08 16:49:10 +00:00
self.assertTrue(views.helpers.object_visible_to_user(self.local_user, obj))
2021-01-23 19:40:41 +00:00
2021-03-23 16:00:04 +00:00
def test_object_visible_to_user_blocked(self, _):
2021-03-08 16:49:10 +00:00
""" you can't see it if they block you """
2021-01-23 19:40:41 +00:00
self.remote_user.blocks.add(self.local_user)
obj = models.Status.objects.create(
2021-03-08 16:49:10 +00:00
content="hi", user=self.remote_user, privacy="public"
)
self.assertFalse(views.helpers.object_visible_to_user(self.local_user, obj))
2021-01-23 19:40:41 +00:00
obj = models.Shelf.objects.create(
2021-03-08 16:49:10 +00:00
name="test", user=self.remote_user, privacy="unlisted"
)
self.assertFalse(views.helpers.object_visible_to_user(self.local_user, obj))
2021-03-27 16:56:46 +00:00
def test_get_suggested_users(self, _):
""" list of people you might know """
user_1 = models.User.objects.create_user(
"nutria@local.com",
"nutria@nutria.com",
"nutriaword",
local=True,
localname="nutria",
discoverable=True,
)
user_2 = models.User.objects.create_user(
"fish@local.com",
"fish@fish.com",
"fishword",
local=True,
localname="fish",
)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
# 1 shared follow
self.local_user.following.add(user_2)
user_1.following.add(user_2)
# 1 shared book
models.ShelfBook.objects.create(
user=self.local_user,
book=self.book,
2021-03-27 17:00:32 +00:00
shelf=self.local_user.shelf_set.first(),
2021-03-27 16:56:46 +00:00
)
models.ShelfBook.objects.create(
2021-03-27 17:00:32 +00:00
user=user_1, book=self.book, shelf=user_1.shelf_set.first()
)
2021-03-27 16:56:46 +00:00
result = views.helpers.get_suggested_users(self.local_user)
self.assertEqual(result.count(), 3)
self.assertTrue(user_1 in result)
self.assertFalse(user_2 in result)
self.assertTrue(self.local_user in result)
self.assertTrue(self.remote_user in result)
user_1_annotated = result.get(id=user_1.id)
self.assertEqual(user_1_annotated.mutuals, 1)
self.assertEqual(user_1_annotated.shared_books, 1)
remote_user_annotated = result.get(id=self.remote_user.id)
self.assertEqual(remote_user_annotated.mutuals, 0)
self.assertEqual(remote_user_annotated.shared_books, 0)
def test_get_suggested_users_counts(self, _):
""" correct counting for multiple shared attributed """
user_1 = models.User.objects.create_user(
"nutria@local.com",
"nutria@nutria.com",
"nutriaword",
local=True,
localname="nutria",
discoverable=True,
)
for i in range(3):
user = models.User.objects.create_user(
"{:d}@local.com".format(i),
"{:d}@nutria.com".format(i),
"password",
local=True,
localname=i,
)
user.followers.add(user_1)
user.followers.add(self.local_user)
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
for i in range(3):
book = models.Edition.objects.create(
title=i,
parent_work=models.Work.objects.create(title=i),
)
models.ShelfBook.objects.create(
user=self.local_user,
book=book,
shelf=self.local_user.shelf_set.first(),
)
models.ShelfBook.objects.create(
user=user_1, book=book, shelf=user_1.shelf_set.first()
)
result = views.helpers.get_suggested_users(
self.local_user,
~Q(id=self.local_user.id),
~Q(followers=self.local_user),
)
self.assertEqual(result.count(), 2)
user_1_annotated = result.get(id=user_1.id)
self.assertEqual(user_1_annotated.mutuals, 3)