2021-03-08 16:49:10 +00:00
|
|
|
""" sending out activities """
|
2021-02-07 06:37:19 +00:00
|
|
|
from unittest.mock import patch
|
2021-01-13 21:54:01 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from django.http import JsonResponse
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
|
|
|
from bookwyrm import models, views
|
2021-02-23 19:34:15 +00:00
|
|
|
from bookwyrm.settings import USER_AGENT
|
2021-01-13 21:54:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-public-methods
|
2021-11-12 17:17:00 +00:00
|
|
|
@patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async")
|
2021-01-13 21:54:01 +00:00
|
|
|
class OutboxView(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""sends out activities"""
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2021-01-13 21:54:01 +00:00
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""we'll need some data"""
|
2021-01-13 21:54:01 +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.com",
|
|
|
|
"mouseword",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
remote_id="https://example.com/users/mouse",
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
work = models.Work.objects.create(title="Test Work")
|
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
title="Example Edition",
|
|
|
|
remote_id="https://example.com/book/1",
|
|
|
|
parent_work=work,
|
|
|
|
)
|
2021-01-13 21:54:01 +00:00
|
|
|
|
2021-03-23 15:19:25 +00:00
|
|
|
def test_outbox(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""returns user's statuses"""
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
|
|
|
result = views.Outbox.as_view()(request, "mouse")
|
2021-01-13 21:54:01 +00:00
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
|
2021-03-23 15:19:25 +00:00
|
|
|
def test_outbox_bad_method(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""can't POST to outbox"""
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("")
|
|
|
|
result = views.Outbox.as_view()(request, "mouse")
|
2021-01-13 21:54:01 +00:00
|
|
|
self.assertEqual(result.status_code, 405)
|
|
|
|
|
2021-03-23 15:19:25 +00:00
|
|
|
def test_outbox_unknown_user(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""should 404 for unknown and remote users"""
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.post("")
|
|
|
|
result = views.Outbox.as_view()(request, "beepboop")
|
2021-01-13 21:54:01 +00:00
|
|
|
self.assertEqual(result.status_code, 405)
|
2021-03-08 16:49:10 +00:00
|
|
|
result = views.Outbox.as_view()(request, "rat")
|
2021-01-13 21:54:01 +00:00
|
|
|
self.assertEqual(result.status_code, 405)
|
|
|
|
|
2021-03-23 15:19:25 +00:00
|
|
|
def test_outbox_privacy(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""don't show dms et cetera in outbox"""
|
2021-09-06 20:53:49 +00:00
|
|
|
with patch("bookwyrm.activitystreams.add_status_task.delay"):
|
2021-02-07 06:37:19 +00:00
|
|
|
models.Status.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
content="PRIVATE!!", user=self.local_user, privacy="direct"
|
|
|
|
)
|
2021-02-07 06:37:19 +00:00
|
|
|
models.Status.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
content="bffs ONLY", user=self.local_user, privacy="followers"
|
|
|
|
)
|
2021-02-07 06:37:19 +00:00
|
|
|
models.Status.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
content="unlisted status", user=self.local_user, privacy="unlisted"
|
|
|
|
)
|
2021-02-07 06:37:19 +00:00
|
|
|
models.Status.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
content="look at this", user=self.local_user, privacy="public"
|
|
|
|
)
|
2021-01-13 21:54:01 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("")
|
|
|
|
result = views.Outbox.as_view()(request, "mouse")
|
2021-01-13 21:54:01 +00:00
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.content)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(data["type"], "OrderedCollection")
|
|
|
|
self.assertEqual(data["totalItems"], 2)
|
2021-01-13 21:54:01 +00:00
|
|
|
|
2021-03-23 15:19:25 +00:00
|
|
|
def test_outbox_filter(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""if we only care about reviews, only get reviews"""
|
2021-09-06 20:53:49 +00:00
|
|
|
with patch("bookwyrm.activitystreams.add_status_task.delay"):
|
2021-08-02 23:05:40 +00:00
|
|
|
models.Review.objects.create(
|
|
|
|
content="look at this",
|
|
|
|
name="hi",
|
|
|
|
rating=1,
|
|
|
|
book=self.book,
|
|
|
|
user=self.local_user,
|
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
models.Status.objects.create(content="look at this", user=self.local_user)
|
2021-01-13 21:54:01 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("", {"type": "bleh"})
|
|
|
|
result = views.Outbox.as_view()(request, "mouse")
|
2021-01-13 21:54:01 +00:00
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.content)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(data["type"], "OrderedCollection")
|
|
|
|
self.assertEqual(data["totalItems"], 2)
|
2021-01-13 21:54:01 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("", {"type": "Review"})
|
|
|
|
result = views.Outbox.as_view()(request, "mouse")
|
2021-01-13 21:54:01 +00:00
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
data = json.loads(result.content)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(data["type"], "OrderedCollection")
|
|
|
|
self.assertEqual(data["totalItems"], 1)
|
2021-02-23 19:34:15 +00:00
|
|
|
|
2021-03-23 15:19:25 +00:00
|
|
|
def test_outbox_bookwyrm_request_true(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""should differentiate between bookwyrm and outside requests"""
|
2021-09-06 20:53:49 +00:00
|
|
|
with patch("bookwyrm.activitystreams.add_status_task.delay"):
|
2021-08-02 23:05:40 +00:00
|
|
|
models.Review.objects.create(
|
|
|
|
name="hi",
|
|
|
|
content="look at this",
|
|
|
|
user=self.local_user,
|
|
|
|
book=self.book,
|
|
|
|
privacy="public",
|
|
|
|
)
|
2021-02-23 19:34:15 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("", {"page": 1}, HTTP_USER_AGENT=USER_AGENT)
|
|
|
|
result = views.Outbox.as_view()(request, "mouse")
|
2021-02-23 19:34:15 +00:00
|
|
|
|
|
|
|
data = json.loads(result.content)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(len(data["orderedItems"]), 1)
|
|
|
|
self.assertEqual(data["orderedItems"][0]["type"], "Review")
|
2021-02-23 19:34:15 +00:00
|
|
|
|
2021-03-23 15:19:25 +00:00
|
|
|
def test_outbox_bookwyrm_request_false(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""should differentiate between bookwyrm and outside requests"""
|
2021-09-06 20:53:49 +00:00
|
|
|
with patch("bookwyrm.activitystreams.add_status_task.delay"):
|
2021-08-02 23:05:40 +00:00
|
|
|
models.Review.objects.create(
|
|
|
|
name="hi",
|
|
|
|
content="look at this",
|
|
|
|
user=self.local_user,
|
|
|
|
book=self.book,
|
|
|
|
privacy="public",
|
|
|
|
)
|
2021-02-23 19:34:15 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
request = self.factory.get("", {"page": 1})
|
|
|
|
result = views.Outbox.as_view()(request, "mouse")
|
2021-02-23 19:34:15 +00:00
|
|
|
|
|
|
|
data = json.loads(result.content)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(len(data["orderedItems"]), 1)
|
|
|
|
self.assertEqual(data["orderedItems"][0]["type"], "Article")
|