2021-03-08 16:49:10 +00:00
|
|
|
""" tests incoming activities"""
|
2021-02-16 04:49:23 +00:00
|
|
|
from datetime import datetime
|
2021-02-16 00:26:48 +00:00
|
|
|
import json
|
2021-02-16 03:41:22 +00:00
|
|
|
import pathlib
|
2021-02-16 00:26:48 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from django.http import HttpResponseNotAllowed, HttpResponseNotFound
|
|
|
|
from django.test import TestCase, Client
|
2021-04-05 22:54:33 +00:00
|
|
|
from django.test.client import RequestFactory
|
2021-02-16 04:49:23 +00:00
|
|
|
import responses
|
2021-02-16 00:26:48 +00:00
|
|
|
|
2021-02-16 03:41:22 +00:00
|
|
|
from bookwyrm import models, views
|
2021-02-16 00:26:48 +00:00
|
|
|
|
2021-02-16 04:49:23 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
# pylint: disable=too-many-public-methods
|
2021-02-16 00:26:48 +00:00
|
|
|
class Inbox(TestCase):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" readthrough tests """
|
|
|
|
|
2021-02-16 00:26:48 +00:00
|
|
|
def setUp(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" basic user and book data """
|
2021-02-16 00:26:48 +00:00
|
|
|
self.client = Client()
|
2021-04-05 22:54:33 +00:00
|
|
|
self.factory = RequestFactory()
|
2021-02-16 00:26:48 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
2021-03-08 16:49:10 +00:00
|
|
|
"mouse@example.com",
|
|
|
|
"mouse@mouse.com",
|
|
|
|
"mouseword",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
|
|
|
self.local_user.remote_id = "https://example.com/user/mouse"
|
2021-02-16 00:26:48 +00:00
|
|
|
self.local_user.save(broadcast=False)
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
2021-02-16 00:26:48 +00:00
|
|
|
self.remote_user = models.User.objects.create_user(
|
2021-03-08 16:49:10 +00:00
|
|
|
"rat",
|
|
|
|
"rat@rat.com",
|
|
|
|
"ratword",
|
2021-02-16 00:26:48 +00:00
|
|
|
local=False,
|
2021-03-08 16:49:10 +00:00
|
|
|
remote_id="https://example.com/users/rat",
|
|
|
|
inbox="https://example.com/users/rat/inbox",
|
|
|
|
outbox="https://example.com/users/rat/outbox",
|
2021-02-16 00:26:48 +00:00
|
|
|
)
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
|
|
|
|
self.status = models.Status.objects.create(
|
|
|
|
user=self.local_user,
|
|
|
|
content="Test status",
|
|
|
|
remote_id="https://example.com/status/1",
|
|
|
|
)
|
2021-02-16 03:41:22 +00:00
|
|
|
|
|
|
|
self.create_json = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"id": "hi",
|
|
|
|
"type": "Create",
|
|
|
|
"actor": "hi",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"object": {},
|
2021-02-16 03:41:22 +00:00
|
|
|
}
|
2021-02-16 00:26:48 +00:00
|
|
|
models.SiteSettings.objects.create()
|
|
|
|
|
|
|
|
def test_inbox_invalid_get(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" shouldn't try to handle if the user is not found """
|
|
|
|
result = self.client.get("/inbox", content_type="application/json")
|
2021-02-16 00:26:48 +00:00
|
|
|
self.assertIsInstance(result, HttpResponseNotAllowed)
|
|
|
|
|
|
|
|
def test_inbox_invalid_user(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" shouldn't try to handle if the user is not found """
|
2021-02-16 00:26:48 +00:00
|
|
|
result = self.client.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"/user/bleh/inbox",
|
2021-02-16 00:26:48 +00:00
|
|
|
'{"type": "Test", "object": "exists"}',
|
2021-03-08 16:49:10 +00:00
|
|
|
content_type="application/json",
|
2021-02-16 00:26:48 +00:00
|
|
|
)
|
|
|
|
self.assertIsInstance(result, HttpResponseNotFound)
|
|
|
|
|
|
|
|
def test_inbox_invalid_bad_signature(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" bad request for invalid signature """
|
|
|
|
with patch("bookwyrm.views.inbox.has_valid_signature") as mock_valid:
|
2021-02-16 00:26:48 +00:00
|
|
|
mock_valid.return_value = False
|
|
|
|
result = self.client.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"/user/mouse/inbox",
|
2021-03-07 17:45:02 +00:00
|
|
|
'{"type": "Announce", "object": "exists"}',
|
2021-03-08 16:49:10 +00:00
|
|
|
content_type="application/json",
|
2021-02-16 00:26:48 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(result.status_code, 401)
|
|
|
|
|
|
|
|
def test_inbox_invalid_bad_signature_delete(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" invalid signature for Delete is okay though """
|
|
|
|
with patch("bookwyrm.views.inbox.has_valid_signature") as mock_valid:
|
2021-02-16 00:26:48 +00:00
|
|
|
mock_valid.return_value = False
|
|
|
|
result = self.client.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"/user/mouse/inbox",
|
2021-02-16 00:26:48 +00:00
|
|
|
'{"type": "Delete", "object": "exists"}',
|
2021-03-08 16:49:10 +00:00
|
|
|
content_type="application/json",
|
2021-02-16 00:26:48 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(result.status_code, 200)
|
|
|
|
|
|
|
|
def test_inbox_unknown_type(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" never heard of that activity type, don't have a handler for it """
|
|
|
|
with patch("bookwyrm.views.inbox.has_valid_signature") as mock_valid:
|
2021-02-16 00:26:48 +00:00
|
|
|
result = self.client.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"/inbox",
|
2021-02-16 00:26:48 +00:00
|
|
|
'{"type": "Fish", "object": "exists"}',
|
2021-03-08 16:49:10 +00:00
|
|
|
content_type="application/json",
|
2021-02-16 00:26:48 +00:00
|
|
|
)
|
|
|
|
mock_valid.return_value = True
|
|
|
|
self.assertIsInstance(result, HttpResponseNotFound)
|
|
|
|
|
|
|
|
def test_inbox_success(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" a known type, for which we start a task """
|
2021-02-16 03:41:22 +00:00
|
|
|
activity = self.create_json
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["object"] = {
|
2021-02-16 03:41:22 +00:00
|
|
|
"id": "https://example.com/list/22",
|
|
|
|
"type": "BookList",
|
|
|
|
"totalItems": 1,
|
|
|
|
"first": "https://example.com/list/22?page=1",
|
|
|
|
"last": "https://example.com/list/22?page=1",
|
|
|
|
"name": "Test List",
|
|
|
|
"owner": "https://example.com/user/mouse",
|
2021-03-08 16:49:10 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-02-16 03:41:22 +00:00
|
|
|
"summary": "summary text",
|
|
|
|
"curation": "curated",
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2021-02-16 00:26:48 +00:00
|
|
|
}
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.inbox.has_valid_signature") as mock_valid:
|
2021-02-16 00:26:48 +00:00
|
|
|
mock_valid.return_value = True
|
2021-02-16 02:47:08 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.views.inbox.activity_task.delay"):
|
2021-02-16 02:47:08 +00:00
|
|
|
result = self.client.post(
|
2021-03-08 16:49:10 +00:00
|
|
|
"/inbox", json.dumps(activity), content_type="application/json"
|
2021-02-16 02:47:08 +00:00
|
|
|
)
|
2021-02-16 00:26:48 +00:00
|
|
|
self.assertEqual(result.status_code, 200)
|
2021-02-16 03:41:22 +00:00
|
|
|
|
|
|
|
def test_handle_create_status(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" the "it justs works" mode """
|
2021-02-16 03:41:22 +00:00
|
|
|
self.assertEqual(models.Status.objects.count(), 1)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_quotation.json")
|
2021-02-16 03:41:22 +00:00
|
|
|
status_data = json.loads(datafile.read_bytes())
|
|
|
|
models.Edition.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
title="Test Book", remote_id="https://example.com/book/1"
|
|
|
|
)
|
2021-02-16 03:41:22 +00:00
|
|
|
activity = self.create_json
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["object"] = status_data
|
2021-02-16 03:41:22 +00:00
|
|
|
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch("bookwyrm.activitystreams.ActivityStream.add_status") as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
2021-02-16 03:41:22 +00:00
|
|
|
|
|
|
|
status = models.Quotation.objects.get()
|
|
|
|
self.assertEqual(
|
2021-03-08 16:49:10 +00:00
|
|
|
status.remote_id, "https://example.com/user/mouse/quotation/13"
|
|
|
|
)
|
|
|
|
self.assertEqual(status.quote, "quote body")
|
|
|
|
self.assertEqual(status.content, "commentary")
|
2021-02-16 03:41:22 +00:00
|
|
|
self.assertEqual(status.user, self.local_user)
|
|
|
|
self.assertEqual(models.Status.objects.count(), 2)
|
|
|
|
|
|
|
|
# while we're here, lets ensure we avoid dupes
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertEqual(models.Status.objects.count(), 2)
|
|
|
|
|
|
|
|
def test_handle_create_status_remote_note_with_mention(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" should only create it under the right circumstances """
|
2021-02-16 03:41:22 +00:00
|
|
|
self.assertEqual(models.Status.objects.count(), 1)
|
|
|
|
self.assertFalse(
|
2021-03-08 16:49:10 +00:00
|
|
|
models.Notification.objects.filter(user=self.local_user).exists()
|
|
|
|
)
|
2021-02-16 03:41:22 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_note.json")
|
2021-02-16 03:41:22 +00:00
|
|
|
status_data = json.loads(datafile.read_bytes())
|
|
|
|
activity = self.create_json
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["object"] = status_data
|
2021-02-16 03:41:22 +00:00
|
|
|
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch("bookwyrm.activitystreams.ActivityStream.add_status") as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
2021-02-16 03:41:22 +00:00
|
|
|
status = models.Status.objects.last()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(status.content, "test content in note")
|
2021-02-16 03:41:22 +00:00
|
|
|
self.assertEqual(status.mention_users.first(), self.local_user)
|
|
|
|
self.assertTrue(
|
2021-03-08 16:49:10 +00:00
|
|
|
models.Notification.objects.filter(user=self.local_user).exists()
|
|
|
|
)
|
|
|
|
self.assertEqual(models.Notification.objects.get().notification_type, "MENTION")
|
2021-02-16 03:41:22 +00:00
|
|
|
|
|
|
|
def test_handle_create_status_remote_note_with_reply(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" should only create it under the right circumstances """
|
2021-02-16 03:41:22 +00:00
|
|
|
self.assertEqual(models.Status.objects.count(), 1)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertFalse(models.Notification.objects.filter(user=self.local_user))
|
2021-02-16 03:41:22 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_note.json")
|
2021-02-16 03:41:22 +00:00
|
|
|
status_data = json.loads(datafile.read_bytes())
|
2021-03-08 16:49:10 +00:00
|
|
|
del status_data["tag"]
|
|
|
|
status_data["inReplyTo"] = self.status.remote_id
|
2021-02-16 03:41:22 +00:00
|
|
|
activity = self.create_json
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["object"] = status_data
|
2021-02-16 03:41:22 +00:00
|
|
|
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch("bookwyrm.activitystreams.ActivityStream.add_status") as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
2021-02-16 03:41:22 +00:00
|
|
|
status = models.Status.objects.last()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(status.content, "test content in note")
|
2021-02-16 03:41:22 +00:00
|
|
|
self.assertEqual(status.reply_parent, self.status)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertTrue(models.Notification.objects.filter(user=self.local_user))
|
|
|
|
self.assertEqual(models.Notification.objects.get().notification_type, "REPLY")
|
2021-02-16 03:41:22 +00:00
|
|
|
|
|
|
|
def test_handle_create_list(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" a new list """
|
2021-02-16 03:41:22 +00:00
|
|
|
activity = self.create_json
|
2021-03-08 16:49:10 +00:00
|
|
|
activity["object"] = {
|
2021-02-16 03:41:22 +00:00
|
|
|
"id": "https://example.com/list/22",
|
|
|
|
"type": "BookList",
|
|
|
|
"totalItems": 1,
|
|
|
|
"first": "https://example.com/list/22?page=1",
|
|
|
|
"last": "https://example.com/list/22?page=1",
|
|
|
|
"name": "Test List",
|
|
|
|
"owner": "https://example.com/user/mouse",
|
2021-03-08 16:49:10 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-02-16 03:41:22 +00:00
|
|
|
"summary": "summary text",
|
|
|
|
"curation": "curated",
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2021-02-16 03:41:22 +00:00
|
|
|
}
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
book_list = models.List.objects.get()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(book_list.name, "Test List")
|
|
|
|
self.assertEqual(book_list.curation, "curated")
|
|
|
|
self.assertEqual(book_list.description, "summary text")
|
|
|
|
self.assertEqual(book_list.remote_id, "https://example.com/list/22")
|
2021-02-16 04:49:23 +00:00
|
|
|
|
2021-03-23 15:53:28 +00:00
|
|
|
def test_handle_follow(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" remote user wants to follow local user """
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/users/rat/follows/123",
|
|
|
|
"type": "Follow",
|
|
|
|
"actor": "https://example.com/users/rat",
|
2021-03-08 16:49:10 +00:00
|
|
|
"object": "https://example.com/user/mouse",
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 20:23:55 +00:00
|
|
|
self.assertFalse(models.UserFollowRequest.objects.exists())
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay") as mock:
|
2021-02-16 04:49:23 +00:00
|
|
|
views.inbox.activity_task(activity)
|
2021-02-16 20:58:29 +00:00
|
|
|
self.assertEqual(mock.call_count, 1)
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
# notification created
|
|
|
|
notification = models.Notification.objects.get()
|
|
|
|
self.assertEqual(notification.user, self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(notification.notification_type, "FOLLOW")
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
# the request should have been deleted
|
2021-02-16 20:58:29 +00:00
|
|
|
self.assertFalse(models.UserFollowRequest.objects.exists())
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
# the follow relationship should exist
|
|
|
|
follow = models.UserFollows.objects.get(user_object=self.local_user)
|
|
|
|
self.assertEqual(follow.user_subject, self.remote_user)
|
|
|
|
|
|
|
|
def test_handle_follow_manually_approved(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" needs approval before following """
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/users/rat/follows/123",
|
|
|
|
"type": "Follow",
|
|
|
|
"actor": "https://example.com/users/rat",
|
2021-03-08 16:49:10 +00:00
|
|
|
"object": "https://example.com/user/mouse",
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.local_user.manually_approves_followers = True
|
|
|
|
self.local_user.save(broadcast=False)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-16 04:49:23 +00:00
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
|
|
|
# notification created
|
|
|
|
notification = models.Notification.objects.get()
|
|
|
|
self.assertEqual(notification.user, self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(notification.notification_type, "FOLLOW_REQUEST")
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
# the request should exist
|
|
|
|
request = models.UserFollowRequest.objects.get()
|
|
|
|
self.assertEqual(request.user_subject, self.remote_user)
|
|
|
|
self.assertEqual(request.user_object, self.local_user)
|
|
|
|
|
|
|
|
# the follow relationship should not exist
|
|
|
|
follow = models.UserFollows.objects.all()
|
|
|
|
self.assertEqual(list(follow), [])
|
|
|
|
|
2021-03-13 23:32:56 +00:00
|
|
|
def test_handle_undo_follow_request(self):
|
|
|
|
""" the requester cancels a follow request """
|
|
|
|
self.local_user.manually_approves_followers = True
|
|
|
|
self.local_user.save(broadcast=False)
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
request = models.UserFollowRequest.objects.create(
|
2021-03-13 23:36:00 +00:00
|
|
|
user_subject=self.remote_user, user_object=self.local_user
|
2021-03-13 23:32:56 +00:00
|
|
|
)
|
|
|
|
self.assertTrue(self.local_user.follower_requests.exists())
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"type": "Undo",
|
|
|
|
"id": "bleh",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"object": {
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": request.remote_id,
|
|
|
|
"type": "Follow",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"object": "https://example.com/user/mouse",
|
2021-03-13 23:36:00 +00:00
|
|
|
},
|
2021-03-13 23:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
|
|
|
self.assertFalse(self.local_user.follower_requests.exists())
|
|
|
|
|
2021-02-16 04:49:23 +00:00
|
|
|
def test_handle_unfollow(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" remove a relationship """
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-17 00:35:28 +00:00
|
|
|
rel = models.UserFollows.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
user_subject=self.remote_user, user_object=self.local_user
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
|
|
|
"type": "Undo",
|
2021-02-17 00:35:28 +00:00
|
|
|
"id": "bleh",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-03-08 16:49:10 +00:00
|
|
|
"actor": self.remote_user.remote_id,
|
2021-02-16 04:49:23 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"object": {
|
2021-02-17 00:35:28 +00:00
|
|
|
"id": rel.remote_id,
|
2021-02-16 04:49:23 +00:00
|
|
|
"type": "Follow",
|
|
|
|
"actor": "https://example.com/users/rat",
|
2021-03-08 16:49:10 +00:00
|
|
|
"object": "https://example.com/user/mouse",
|
|
|
|
},
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
self.assertEqual(self.remote_user, self.local_user.followers.first())
|
|
|
|
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertIsNone(self.local_user.followers.first())
|
|
|
|
|
|
|
|
def test_handle_follow_accept(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" a remote user approved a follow request from local """
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-16 20:58:29 +00:00
|
|
|
rel = models.UserFollowRequest.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
user_subject=self.local_user, user_object=self.remote_user
|
2021-02-16 20:58:29 +00:00
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/users/rat/follows/123#accepts",
|
|
|
|
"type": "Accept",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"object": {
|
2021-02-16 20:58:29 +00:00
|
|
|
"id": rel.remote_id,
|
2021-02-16 04:49:23 +00:00
|
|
|
"type": "Follow",
|
|
|
|
"actor": "https://example.com/user/mouse",
|
2021-03-08 16:49:10 +00:00
|
|
|
"object": "https://example.com/users/rat",
|
|
|
|
},
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(models.UserFollowRequest.objects.count(), 1)
|
|
|
|
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
|
|
|
# request should be deleted
|
|
|
|
self.assertEqual(models.UserFollowRequest.objects.count(), 0)
|
|
|
|
|
|
|
|
# relationship should be created
|
|
|
|
follows = self.remote_user.followers
|
|
|
|
self.assertEqual(follows.count(), 1)
|
|
|
|
self.assertEqual(follows.first(), self.local_user)
|
|
|
|
|
|
|
|
def test_handle_follow_reject(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" turn down a follow request """
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-16 20:58:29 +00:00
|
|
|
rel = models.UserFollowRequest.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
user_subject=self.local_user, user_object=self.remote_user
|
2021-02-16 20:58:29 +00:00
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/users/rat/follows/123#accepts",
|
|
|
|
"type": "Reject",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"object": {
|
2021-02-16 20:58:29 +00:00
|
|
|
"id": rel.remote_id,
|
2021-02-16 04:49:23 +00:00
|
|
|
"type": "Follow",
|
|
|
|
"actor": "https://example.com/user/mouse",
|
2021-03-08 16:49:10 +00:00
|
|
|
"object": "https://example.com/users/rat",
|
|
|
|
},
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(models.UserFollowRequest.objects.count(), 1)
|
|
|
|
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
|
|
|
# request should be deleted
|
2021-02-17 22:37:20 +00:00
|
|
|
self.assertFalse(models.UserFollowRequest.objects.exists())
|
|
|
|
self.assertFalse(self.remote_user.followers.exists())
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
def test_handle_update_list(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" a new list """
|
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-16 04:49:23 +00:00
|
|
|
book_list = models.List.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
name="hi", remote_id="https://example.com/list/22", user=self.local_user
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"type": "Update",
|
|
|
|
"to": [],
|
|
|
|
"cc": [],
|
|
|
|
"actor": "hi",
|
|
|
|
"id": "sdkjf",
|
|
|
|
"object": {
|
2021-02-16 04:49:23 +00:00
|
|
|
"id": "https://example.com/list/22",
|
|
|
|
"type": "BookList",
|
|
|
|
"totalItems": 1,
|
|
|
|
"first": "https://example.com/list/22?page=1",
|
|
|
|
"last": "https://example.com/list/22?page=1",
|
|
|
|
"name": "Test List",
|
|
|
|
"owner": "https://example.com/user/mouse",
|
2021-03-08 16:49:10 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-02-16 04:49:23 +00:00
|
|
|
"summary": "summary text",
|
|
|
|
"curation": "curated",
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
},
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
book_list.refresh_from_db()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(book_list.name, "Test List")
|
|
|
|
self.assertEqual(book_list.curation, "curated")
|
|
|
|
self.assertEqual(book_list.description, "summary text")
|
|
|
|
self.assertEqual(book_list.remote_id, "https://example.com/list/22")
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
def test_handle_delete_status(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" remove a status """
|
2021-02-16 04:49:23 +00:00
|
|
|
self.status.user = self.remote_user
|
|
|
|
self.status.save(broadcast=False)
|
|
|
|
|
|
|
|
self.assertFalse(self.status.deleted)
|
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"type": "Delete",
|
2021-02-16 17:35:00 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-03-08 16:49:10 +00:00
|
|
|
"id": "%s/activity" % self.status.remote_id,
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": {"id": self.status.remote_id, "type": "Tombstone"},
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch(
|
2021-04-05 21:08:24 +00:00
|
|
|
"bookwyrm.activitystreams.ActivityStream.remove_object_from_related_stores"
|
2021-03-23 15:53:28 +00:00
|
|
|
) as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
2021-02-16 04:49:23 +00:00
|
|
|
# deletion doens't remove the status, it turns it into a tombstone
|
|
|
|
status = models.Status.objects.get()
|
|
|
|
self.assertTrue(status.deleted)
|
|
|
|
self.assertIsInstance(status.deleted_date, datetime)
|
|
|
|
|
|
|
|
def test_handle_delete_status_notifications(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" remove a status with related notifications """
|
2021-02-16 04:49:23 +00:00
|
|
|
self.status.user = self.remote_user
|
|
|
|
self.status.save(broadcast=False)
|
|
|
|
models.Notification.objects.create(
|
|
|
|
related_status=self.status,
|
|
|
|
user=self.local_user,
|
2021-03-08 16:49:10 +00:00
|
|
|
notification_type="MENTION",
|
2021-02-16 04:49:23 +00:00
|
|
|
)
|
|
|
|
# this one is innocent, don't delete it
|
|
|
|
notif = models.Notification.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
user=self.local_user, notification_type="MENTION"
|
2021-02-16 04:49:23 +00:00
|
|
|
)
|
|
|
|
self.assertFalse(self.status.deleted)
|
|
|
|
self.assertEqual(models.Notification.objects.count(), 2)
|
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"type": "Delete",
|
2021-02-16 17:35:00 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-03-08 16:49:10 +00:00
|
|
|
"id": "%s/activity" % self.status.remote_id,
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": {"id": self.status.remote_id, "type": "Tombstone"},
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch(
|
2021-04-05 21:08:24 +00:00
|
|
|
"bookwyrm.activitystreams.ActivityStream.remove_object_from_related_stores"
|
2021-03-23 15:53:28 +00:00
|
|
|
) as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
2021-02-16 04:49:23 +00:00
|
|
|
# deletion doens't remove the status, it turns it into a tombstone
|
|
|
|
status = models.Status.objects.get()
|
|
|
|
self.assertTrue(status.deleted)
|
|
|
|
self.assertIsInstance(status.deleted_date, datetime)
|
|
|
|
|
|
|
|
# notifications should be truly deleted
|
|
|
|
self.assertEqual(models.Notification.objects.count(), 1)
|
|
|
|
self.assertEqual(models.Notification.objects.get(), notif)
|
|
|
|
|
|
|
|
def test_handle_favorite(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" fav a status """
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/fav/1",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"type": "Like",
|
|
|
|
"published": "Mon, 25 May 2020 19:31:20 GMT",
|
|
|
|
"object": self.status.remote_id,
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
fav = models.Favorite.objects.get(remote_id="https://example.com/fav/1")
|
2021-02-16 04:49:23 +00:00
|
|
|
self.assertEqual(fav.status, self.status)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(fav.remote_id, "https://example.com/fav/1")
|
2021-02-16 04:49:23 +00:00
|
|
|
self.assertEqual(fav.user, self.remote_user)
|
|
|
|
|
2021-03-07 17:45:02 +00:00
|
|
|
def test_ignore_favorite(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" don't try to save an unknown status """
|
2021-03-07 17:45:02 +00:00
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/fav/1",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"type": "Like",
|
|
|
|
"published": "Mon, 25 May 2020 19:31:20 GMT",
|
|
|
|
"object": "https://unknown.status/not-found",
|
2021-03-07 17:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
|
|
|
self.assertFalse(models.Favorite.objects.exists())
|
|
|
|
|
2021-02-16 04:49:23 +00:00
|
|
|
def test_handle_unfavorite(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" fav a status """
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"id": "https://example.com/fav/1#undo",
|
|
|
|
"type": "Undo",
|
2021-02-17 00:35:28 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-03-08 16:49:10 +00:00
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": {
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/fav/1",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"type": "Like",
|
|
|
|
"published": "Mon, 25 May 2020 19:31:20 GMT",
|
|
|
|
"object": self.status.remote_id,
|
|
|
|
},
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
models.Favorite.objects.create(
|
|
|
|
status=self.status,
|
|
|
|
user=self.remote_user,
|
2021-03-08 16:49:10 +00:00
|
|
|
remote_id="https://example.com/fav/1",
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
self.assertEqual(models.Favorite.objects.count(), 1)
|
|
|
|
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertEqual(models.Favorite.objects.count(), 0)
|
|
|
|
|
2021-03-23 15:53:28 +00:00
|
|
|
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
|
|
|
|
def test_handle_boost(self, _):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" boost a status """
|
2021-02-16 04:49:23 +00:00
|
|
|
self.assertEqual(models.Notification.objects.count(), 0)
|
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"type": "Announce",
|
|
|
|
"id": "%s/boost" % self.status.remote_id,
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": self.status.remote_id,
|
2021-03-24 21:19:12 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"published": "Mon, 25 May 2020 19:31:20 GMT",
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.models.status.Status.ignore_activity") as discarder:
|
2021-02-16 04:49:23 +00:00
|
|
|
discarder.return_value = False
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
boost = models.Boost.objects.get()
|
|
|
|
self.assertEqual(boost.boosted_status, self.status)
|
|
|
|
notification = models.Notification.objects.get()
|
|
|
|
self.assertEqual(notification.user, self.local_user)
|
|
|
|
self.assertEqual(notification.related_status, self.status)
|
|
|
|
|
2021-04-06 01:02:24 +00:00
|
|
|
@responses.activate
|
|
|
|
@patch("bookwyrm.activitystreams.ActivityStream.add_status")
|
|
|
|
def test_handle_boost_remote_status(self, redis_mock):
|
|
|
|
""" boost a status """
|
|
|
|
work = models.Work.objects.create(title="work title")
|
|
|
|
book = models.Edition.objects.create(
|
|
|
|
title="Test",
|
|
|
|
remote_id="https://bookwyrm.social/book/37292",
|
|
|
|
parent_work=work,
|
|
|
|
)
|
|
|
|
self.assertEqual(models.Notification.objects.count(), 0)
|
|
|
|
activity = {
|
|
|
|
"type": "Announce",
|
|
|
|
"id": "%s/boost" % self.status.remote_id,
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": "https://remote.com/status/1",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"published": "Mon, 25 May 2020 19:31:20 GMT",
|
|
|
|
}
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"https://remote.com/status/1",
|
|
|
|
json={
|
|
|
|
"id": "https://remote.com/status/1",
|
|
|
|
"type": "Comment",
|
|
|
|
"published": "2021-04-05T18:04:59.735190+00:00",
|
|
|
|
"attributedTo": self.remote_user.remote_id,
|
|
|
|
"content": "<p>a comment</p>",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://b875df3d118b.ngrok.io/user/mouse/followers"],
|
|
|
|
"inReplyTo": "",
|
|
|
|
"inReplyToBook": book.remote_id,
|
|
|
|
"summary": "",
|
|
|
|
"tag": [],
|
|
|
|
"sensitive": False,
|
2021-04-06 01:05:06 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
},
|
2021-04-06 01:02:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with patch("bookwyrm.models.status.Status.ignore_activity") as discarder:
|
|
|
|
discarder.return_value = False
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
|
|
|
|
|
|
|
boost = models.Boost.objects.get()
|
|
|
|
self.assertEqual(boost.boosted_status.remote_id, "https://remote.com/status/1")
|
|
|
|
self.assertEqual(boost.boosted_status.comment.status_type, "Comment")
|
|
|
|
self.assertEqual(boost.boosted_status.comment.book, book)
|
|
|
|
|
2021-02-16 04:49:23 +00:00
|
|
|
@responses.activate
|
|
|
|
def test_handle_discarded_boost(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" test a boost of a mastodon status that will be discarded """
|
2021-02-16 20:31:27 +00:00
|
|
|
status = models.Status(
|
2021-03-08 16:49:10 +00:00
|
|
|
content="hi",
|
2021-02-16 20:31:27 +00:00
|
|
|
user=self.remote_user,
|
|
|
|
)
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
|
|
|
|
status.save(broadcast=False)
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"type": "Announce",
|
|
|
|
"id": "http://www.faraway.com/boost/12",
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": status.remote_id,
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
responses.add(
|
2021-03-08 16:49:10 +00:00
|
|
|
responses.GET, status.remote_id, json=status.to_activity(), status=200
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertEqual(models.Boost.objects.count(), 0)
|
|
|
|
|
|
|
|
def test_handle_unboost(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" undo a boost """
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch("bookwyrm.activitystreams.ActivityStream.add_status"):
|
|
|
|
boost = models.Boost.objects.create(
|
|
|
|
boosted_status=self.status, user=self.remote_user
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"type": "Undo",
|
|
|
|
"actor": "hi",
|
|
|
|
"id": "bleh",
|
2021-02-17 00:35:28 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-03-08 16:49:10 +00:00
|
|
|
"object": {
|
|
|
|
"type": "Announce",
|
|
|
|
"id": boost.remote_id,
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": self.status.remote_id,
|
2021-03-24 21:19:12 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"published": "Mon, 25 May 2020 19:31:20 GMT",
|
2021-03-08 16:49:10 +00:00
|
|
|
},
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch(
|
2021-04-05 21:08:24 +00:00
|
|
|
"bookwyrm.activitystreams.ActivityStream.remove_object_from_related_stores"
|
2021-03-23 15:53:28 +00:00
|
|
|
) as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
2021-03-24 21:19:12 +00:00
|
|
|
self.assertFalse(models.Boost.objects.exists())
|
2021-02-16 04:49:23 +00:00
|
|
|
|
2021-03-13 15:15:30 +00:00
|
|
|
def test_handle_unboost_unknown_boost(self):
|
|
|
|
""" undo a boost """
|
|
|
|
activity = {
|
|
|
|
"type": "Undo",
|
|
|
|
"actor": "hi",
|
|
|
|
"id": "bleh",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"object": {
|
|
|
|
"type": "Announce",
|
|
|
|
"id": "http://fake.com/unknown/boost",
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": self.status.remote_id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
2021-02-16 19:04:13 +00:00
|
|
|
def test_handle_add_book_to_shelf(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" shelving a book """
|
|
|
|
work = models.Work.objects.create(title="work title")
|
2021-02-16 04:49:23 +00:00
|
|
|
book = models.Edition.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
title="Test",
|
|
|
|
remote_id="https://bookwyrm.social/book/37292",
|
|
|
|
parent_work=work,
|
|
|
|
)
|
|
|
|
shelf = models.Shelf.objects.create(user=self.remote_user, name="Test Shelf")
|
|
|
|
shelf.remote_id = "https://bookwyrm.social/user/mouse/shelf/to-read"
|
2021-02-16 04:49:23 +00:00
|
|
|
shelf.save()
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"id": "https://bookwyrm.social/shelfbook/6189#add",
|
|
|
|
"type": "Add",
|
|
|
|
"actor": "https://example.com/users/rat",
|
2021-02-16 05:20:00 +00:00
|
|
|
"object": {
|
|
|
|
"type": "Edition",
|
2021-02-16 19:04:13 +00:00
|
|
|
"title": "Test Title",
|
|
|
|
"work": work.remote_id,
|
2021-02-16 05:20:00 +00:00
|
|
|
"id": "https://bookwyrm.social/book/37292",
|
|
|
|
},
|
2021-02-16 04:49:23 +00:00
|
|
|
"target": "https://bookwyrm.social/user/mouse/shelf/to-read",
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
2021-02-16 19:04:13 +00:00
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertEqual(shelf.books.first(), book)
|
2021-02-16 04:49:23 +00:00
|
|
|
|
2021-03-13 16:43:36 +00:00
|
|
|
def test_handle_unshelve_book(self):
|
|
|
|
""" remove a book from a shelf """
|
|
|
|
work = models.Work.objects.create(title="work title")
|
|
|
|
book = models.Edition.objects.create(
|
|
|
|
title="Test",
|
|
|
|
remote_id="https://bookwyrm.social/book/37292",
|
|
|
|
parent_work=work,
|
|
|
|
)
|
|
|
|
shelf = models.Shelf.objects.create(user=self.remote_user, name="Test Shelf")
|
|
|
|
shelf.remote_id = "https://bookwyrm.social/user/mouse/shelf/to-read"
|
|
|
|
shelf.save()
|
|
|
|
|
|
|
|
shelfbook = models.ShelfBook.objects.create(
|
|
|
|
user=self.remote_user, shelf=shelf, book=book
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(shelf.books.first(), book)
|
|
|
|
self.assertEqual(shelf.books.count(), 1)
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"id": shelfbook.remote_id,
|
|
|
|
"type": "Remove",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"object": {
|
|
|
|
"type": "Edition",
|
|
|
|
"title": "Test Title",
|
|
|
|
"work": work.remote_id,
|
|
|
|
"id": "https://bookwyrm.social/book/37292",
|
|
|
|
},
|
|
|
|
"target": "https://bookwyrm.social/user/mouse/shelf/to-read",
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
}
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertFalse(shelf.books.exists())
|
|
|
|
|
2021-02-23 23:51:02 +00:00
|
|
|
@responses.activate
|
|
|
|
def test_handle_add_book_to_list(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" listing a book """
|
|
|
|
work = models.Work.objects.create(title="work title")
|
2021-02-23 23:51:02 +00:00
|
|
|
book = models.Edition.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
title="Test",
|
|
|
|
remote_id="https://bookwyrm.social/book/37292",
|
|
|
|
parent_work=work,
|
|
|
|
)
|
2021-02-23 23:51:02 +00:00
|
|
|
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
2021-03-08 16:49:10 +00:00
|
|
|
"https://bookwyrm.social/user/mouse/list/to-read",
|
2021-02-23 23:51:02 +00:00
|
|
|
json={
|
|
|
|
"id": "https://example.com/list/22",
|
|
|
|
"type": "BookList",
|
|
|
|
"totalItems": 1,
|
|
|
|
"first": "https://example.com/list/22?page=1",
|
|
|
|
"last": "https://example.com/list/22?page=1",
|
|
|
|
"name": "Test List",
|
|
|
|
"owner": "https://example.com/user/mouse",
|
2021-03-08 16:49:10 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-02-23 23:51:02 +00:00
|
|
|
"summary": "summary text",
|
|
|
|
"curation": "curated",
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
},
|
2021-02-23 23:51:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"id": "https://bookwyrm.social/listbook/6189#add",
|
|
|
|
"type": "Add",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"object": {
|
|
|
|
"type": "Edition",
|
|
|
|
"title": "Test Title",
|
|
|
|
"work": work.remote_id,
|
|
|
|
"id": "https://bookwyrm.social/book/37292",
|
|
|
|
},
|
|
|
|
"target": "https://bookwyrm.social/user/mouse/list/to-read",
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2021-02-23 23:51:02 +00:00
|
|
|
}
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
|
|
|
booklist = models.List.objects.get()
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(booklist.name, "Test List")
|
2021-02-23 23:51:02 +00:00
|
|
|
self.assertEqual(booklist.books.first(), book)
|
|
|
|
|
2021-02-24 01:18:25 +00:00
|
|
|
@responses.activate
|
|
|
|
def test_handle_tag_book(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" listing a book """
|
|
|
|
work = models.Work.objects.create(title="work title")
|
2021-02-24 01:18:25 +00:00
|
|
|
book = models.Edition.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
title="Test",
|
|
|
|
remote_id="https://bookwyrm.social/book/37292",
|
|
|
|
parent_work=work,
|
|
|
|
)
|
2021-02-24 01:18:25 +00:00
|
|
|
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
2021-03-08 16:49:10 +00:00
|
|
|
"https://www.example.com/tag/cool-tag",
|
2021-02-24 01:18:25 +00:00
|
|
|
json={
|
|
|
|
"id": "https://1b1a78582461.ngrok.io/tag/tag",
|
|
|
|
"type": "OrderedCollection",
|
|
|
|
"totalItems": 0,
|
|
|
|
"first": "https://1b1a78582461.ngrok.io/tag/tag?page=1",
|
|
|
|
"last": "https://1b1a78582461.ngrok.io/tag/tag?page=1",
|
|
|
|
"name": "cool tag",
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
},
|
2021-02-24 01:18:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"id": "https://bookwyrm.social/listbook/6189#add",
|
|
|
|
"type": "Add",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"object": {
|
|
|
|
"type": "Edition",
|
|
|
|
"title": "Test Title",
|
|
|
|
"work": work.remote_id,
|
|
|
|
"id": "https://bookwyrm.social/book/37292",
|
|
|
|
},
|
|
|
|
"target": "https://www.example.com/tag/cool-tag",
|
2021-03-08 16:49:10 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2021-02-24 01:18:25 +00:00
|
|
|
}
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
|
|
|
tag = models.Tag.objects.get()
|
|
|
|
self.assertFalse(models.List.objects.exists())
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(tag.name, "cool tag")
|
2021-02-24 01:18:25 +00:00
|
|
|
self.assertEqual(tag.books.first(), book)
|
|
|
|
|
2021-02-16 04:49:23 +00:00
|
|
|
def test_handle_update_user(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" update an existing user """
|
2021-02-16 04:49:23 +00:00
|
|
|
# we only do this with remote users
|
|
|
|
self.local_user.local = False
|
|
|
|
self.local_user.save()
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath("../data/ap_user.json")
|
2021-02-16 04:49:23 +00:00
|
|
|
userdata = json.loads(datafile.read_bytes())
|
2021-03-08 16:49:10 +00:00
|
|
|
del userdata["icon"]
|
2021-02-16 04:49:23 +00:00
|
|
|
self.assertIsNone(self.local_user.name)
|
2021-03-08 16:49:10 +00:00
|
|
|
views.inbox.activity_task(
|
|
|
|
{
|
|
|
|
"type": "Update",
|
|
|
|
"to": [],
|
|
|
|
"cc": [],
|
|
|
|
"actor": "hi",
|
|
|
|
"id": "sdkjf",
|
|
|
|
"object": userdata,
|
|
|
|
}
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
user = models.User.objects.get(id=self.local_user.id)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(user.name, "MOUSE?? MOUSE!!")
|
|
|
|
self.assertEqual(user.username, "mouse@example.com")
|
|
|
|
self.assertEqual(user.localname, "mouse")
|
2021-03-21 21:50:36 +00:00
|
|
|
self.assertTrue(user.discoverable)
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
def test_handle_update_edition(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" update an existing edition """
|
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath("../data/bw_edition.json")
|
2021-02-16 04:49:23 +00:00
|
|
|
bookdata = json.loads(datafile.read_bytes())
|
|
|
|
|
|
|
|
models.Work.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
title="Test Work", remote_id="https://bookwyrm.social/book/5988"
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
book = models.Edition.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
title="Test Book", remote_id="https://bookwyrm.social/book/5989"
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
del bookdata["authors"]
|
|
|
|
self.assertEqual(book.title, "Test Book")
|
|
|
|
|
|
|
|
with patch("bookwyrm.activitypub.base_activity.set_related_field.delay"):
|
|
|
|
views.inbox.activity_task(
|
|
|
|
{
|
|
|
|
"type": "Update",
|
|
|
|
"to": [],
|
|
|
|
"cc": [],
|
|
|
|
"actor": "hi",
|
|
|
|
"id": "sdkjf",
|
|
|
|
"object": bookdata,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
book = models.Edition.objects.get(id=book.id)
|
|
|
|
self.assertEqual(book.title, "Piranesi")
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
def test_handle_update_work(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" update an existing edition """
|
|
|
|
datafile = pathlib.Path(__file__).parent.joinpath("../data/bw_work.json")
|
2021-02-16 04:49:23 +00:00
|
|
|
bookdata = json.loads(datafile.read_bytes())
|
|
|
|
|
|
|
|
book = models.Work.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
title="Test Book", remote_id="https://bookwyrm.social/book/5988"
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
del bookdata["authors"]
|
|
|
|
self.assertEqual(book.title, "Test Book")
|
|
|
|
with patch("bookwyrm.activitypub.base_activity.set_related_field.delay"):
|
|
|
|
views.inbox.activity_task(
|
|
|
|
{
|
|
|
|
"type": "Update",
|
|
|
|
"to": [],
|
|
|
|
"cc": [],
|
|
|
|
"actor": "hi",
|
|
|
|
"id": "sdkjf",
|
|
|
|
"object": bookdata,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
book = models.Work.objects.get(id=book.id)
|
|
|
|
self.assertEqual(book.title, "Piranesi")
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
def test_handle_blocks(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" create a "block" database entry from an activity """
|
2021-02-16 04:49:23 +00:00
|
|
|
self.local_user.followers.add(self.remote_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
2021-02-16 04:49:23 +00:00
|
|
|
models.UserFollowRequest.objects.create(
|
2021-03-08 16:49:10 +00:00
|
|
|
user_subject=self.local_user, user_object=self.remote_user
|
|
|
|
)
|
2021-02-16 04:49:23 +00:00
|
|
|
self.assertTrue(models.UserFollows.objects.exists())
|
|
|
|
self.assertTrue(models.UserFollowRequest.objects.exists())
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/9e1f41ac-9ddd-4159",
|
|
|
|
"type": "Block",
|
|
|
|
"actor": "https://example.com/users/rat",
|
2021-03-08 16:49:10 +00:00
|
|
|
"object": "https://example.com/user/mouse",
|
2021-02-16 04:49:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.activitystreams.ActivityStream.remove_user_statuses"
|
|
|
|
) as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
2021-02-16 04:49:23 +00:00
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
block = models.UserBlocks.objects.get()
|
|
|
|
self.assertEqual(block.user_subject, self.remote_user)
|
|
|
|
self.assertEqual(block.user_object, self.local_user)
|
2021-03-08 16:49:10 +00:00
|
|
|
self.assertEqual(block.remote_id, "https://example.com/9e1f41ac-9ddd-4159")
|
2021-02-16 04:49:23 +00:00
|
|
|
|
|
|
|
self.assertFalse(models.UserFollows.objects.exists())
|
|
|
|
self.assertFalse(models.UserFollowRequest.objects.exists())
|
|
|
|
|
|
|
|
def test_handle_unblock(self):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" unblock a user """
|
2021-02-16 04:49:23 +00:00
|
|
|
self.remote_user.blocks.add(self.local_user)
|
|
|
|
|
|
|
|
block = models.UserBlocks.objects.get()
|
2021-03-08 16:49:10 +00:00
|
|
|
block.remote_id = "https://example.com/9e1f41ac-9ddd-4159"
|
2021-02-16 04:49:23 +00:00
|
|
|
block.save()
|
|
|
|
|
|
|
|
self.assertEqual(block.user_subject, self.remote_user)
|
|
|
|
self.assertEqual(block.user_object, self.local_user)
|
2021-02-17 00:35:28 +00:00
|
|
|
activity = {
|
2021-03-08 16:49:10 +00:00
|
|
|
"type": "Undo",
|
|
|
|
"actor": "hi",
|
|
|
|
"id": "bleh",
|
2021-02-17 00:35:28 +00:00
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
2021-03-08 16:49:10 +00:00
|
|
|
"object": {
|
2021-02-17 00:35:28 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/9e1f41ac-9ddd-4159",
|
|
|
|
"type": "Block",
|
|
|
|
"actor": "https://example.com/users/rat",
|
2021-03-08 16:49:10 +00:00
|
|
|
"object": "https://example.com/user/mouse",
|
|
|
|
},
|
|
|
|
}
|
2021-03-23 15:53:28 +00:00
|
|
|
with patch(
|
|
|
|
"bookwyrm.activitystreams.ActivityStream.add_user_statuses"
|
|
|
|
) as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
2021-02-16 04:49:23 +00:00
|
|
|
self.assertFalse(models.UserBlocks.objects.exists())
|
2021-04-05 22:54:33 +00:00
|
|
|
|
|
|
|
def test_is_blocked_user_agent(self):
|
|
|
|
""" check for blocked servers """
|
|
|
|
request = self.factory.post(
|
|
|
|
"",
|
|
|
|
HTTP_USER_AGENT="http.rb/4.4.1 (Mastodon/3.3.0; +https://mastodon.social/)",
|
|
|
|
)
|
|
|
|
self.assertFalse(views.inbox.is_blocked_user_agent(request))
|
|
|
|
|
|
|
|
models.FederatedServer.objects.create(
|
|
|
|
server_name="mastodon.social", status="blocked"
|
|
|
|
)
|
|
|
|
self.assertTrue(views.inbox.is_blocked_user_agent(request))
|
|
|
|
|
|
|
|
def test_is_blocked_activity(self):
|
|
|
|
""" check for blocked servers """
|
|
|
|
activity = {"actor": "https://mastodon.social/user/whaatever/else"}
|
2021-04-05 23:47:48 +00:00
|
|
|
self.assertFalse(views.inbox.is_blocked_activity(activity))
|
2021-04-05 22:54:33 +00:00
|
|
|
|
|
|
|
models.FederatedServer.objects.create(
|
|
|
|
server_name="mastodon.social", status="blocked"
|
|
|
|
)
|
2021-04-05 23:47:48 +00:00
|
|
|
self.assertTrue(views.inbox.is_blocked_activity(activity))
|