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
|
|
|
|
class OutboxView(TestCase):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" sends out activities """
|
|
|
|
|
2021-01-13 21:54:01 +00:00
|
|
|
def setUp(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" we'll need some data """
|
2021-01-13 21:54:01 +00:00
|
|
|
self.factory = RequestFactory()
|
|
|
|
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,
|
|
|
|
localname="mouse",
|
|
|
|
remote_id="https://example.com/users/mouse",
|
2021-01-13 21:54:01 +00:00
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
work = models.Work.objects.create(title="Test Work")
|
2021-01-13 21:54:01 +00:00
|
|
|
self.book = models.Edition.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
title="Example Edition",
|
|
|
|
remote_id="https://example.com/book/1",
|
|
|
|
parent_work=work,
|
2021-01-13 21:54:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_outbox(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" returns user's statuses """
|
|
|
|
request = self.factory.get("")
|
|
|
|
result = views.Outbox.as_view()(request, "mouse")
|
2021-01-13 21:54:01 +00:00
|
|
|
self.assertIsInstance(result, JsonResponse)
|
|
|
|
|
|
|
|
def test_outbox_bad_method(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" can't POST to outbox """
|
|
|
|
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)
|
|
|
|
|
|
|
|
def test_outbox_unknown_user(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" should 404 for unknown and remote users """
|
|
|
|
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)
|
|
|
|
|
|
|
|
def test_outbox_privacy(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" don't show dms et cetera in outbox """
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_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
|
|
|
|
|
|
|
def test_outbox_filter(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" if we only care about reviews, only get reviews """
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-07 06:37:19 +00:00
|
|
|
models.Review.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
content="look at this",
|
|
|
|
name="hi",
|
|
|
|
rating=1,
|
|
|
|
book=self.book,
|
|
|
|
user=self.local_user,
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
def test_outbox_bookwyrm_request_true(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" should differentiate between bookwyrm and outside requests """
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-23 19:34:15 +00:00
|
|
|
models.Review.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
name="hi",
|
|
|
|
content="look at this",
|
2021-02-23 19:34:15 +00:00
|
|
|
user=self.local_user,
|
|
|
|
book=self.book,
|
2021-03-08 16:49:10 +00:00
|
|
|
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
|
|
|
|
|
|
|
def test_outbox_bookwyrm_request_false(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" should differentiate between bookwyrm and outside requests """
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-23 19:34:15 +00:00
|
|
|
models.Review.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
name="hi",
|
|
|
|
content="look at this",
|
2021-02-23 19:34:15 +00:00
|
|
|
user=self.local_user,
|
|
|
|
book=self.book,
|
2021-03-08 16:49:10 +00:00
|
|
|
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")
|