2021-04-08 18:18:32 +00:00
|
|
|
""" tests incoming activities"""
|
|
|
|
from datetime import datetime
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from bookwyrm import models, views
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-public-methods
|
|
|
|
class InboxActivities(TestCase):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""inbox tests"""
|
2021-04-08 18:18:32 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""basic user and book data"""
|
2021-08-03 20:27:32 +00:00
|
|
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"):
|
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse@example.com",
|
|
|
|
"mouse@mouse.com",
|
|
|
|
"mouseword",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
2021-08-02 23:05:40 +00:00
|
|
|
self.local_user.remote_id = "https://example.com/user/mouse"
|
2021-08-03 20:27:32 +00:00
|
|
|
self.local_user.save(broadcast=False, update_fields=["remote_id"])
|
2021-08-02 23:05:40 +00:00
|
|
|
with patch("bookwyrm.models.user.set_remote_server.delay"):
|
|
|
|
self.remote_user = models.User.objects.create_user(
|
|
|
|
"rat",
|
|
|
|
"rat@rat.com",
|
|
|
|
"ratword",
|
|
|
|
local=False,
|
|
|
|
remote_id="https://example.com/users/rat",
|
|
|
|
inbox="https://example.com/users/rat/inbox",
|
|
|
|
outbox="https://example.com/users/rat/outbox",
|
2021-05-26 21:57:29 +00:00
|
|
|
)
|
2021-09-06 20:53:49 +00:00
|
|
|
with patch("bookwyrm.activitystreams.add_status_task.delay"):
|
2021-04-08 18:18:32 +00:00
|
|
|
self.status = models.Status.objects.create(
|
|
|
|
user=self.remote_user,
|
|
|
|
content="Test status",
|
|
|
|
remote_id="https://example.com/status/1",
|
|
|
|
)
|
|
|
|
|
|
|
|
self.create_json = {
|
|
|
|
"id": "hi",
|
|
|
|
"type": "Create",
|
|
|
|
"actor": "hi",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"object": {},
|
|
|
|
}
|
2021-08-02 23:05:40 +00:00
|
|
|
models.SiteSettings.objects.create()
|
2021-04-08 18:18:32 +00:00
|
|
|
|
2021-04-17 19:39:16 +00:00
|
|
|
def test_delete_status(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove a status"""
|
2021-04-08 18:18:32 +00:00
|
|
|
self.assertFalse(self.status.deleted)
|
|
|
|
activity = {
|
|
|
|
"type": "Delete",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"id": "%s/activity" % self.status.remote_id,
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": {"id": self.status.remote_id, "type": "Tombstone"},
|
|
|
|
}
|
|
|
|
with patch(
|
|
|
|
"bookwyrm.activitystreams.ActivityStream.remove_object_from_related_stores"
|
|
|
|
) as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
|
|
|
# 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)
|
|
|
|
|
2021-04-17 19:39:16 +00:00
|
|
|
def test_delete_status_notifications(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove a status with related notifications"""
|
2021-04-08 18:18:32 +00:00
|
|
|
models.Notification.objects.create(
|
|
|
|
related_status=self.status,
|
|
|
|
user=self.local_user,
|
|
|
|
notification_type="MENTION",
|
|
|
|
)
|
|
|
|
# this one is innocent, don't delete it
|
|
|
|
notif = models.Notification.objects.create(
|
|
|
|
user=self.local_user, notification_type="MENTION"
|
|
|
|
)
|
|
|
|
self.assertFalse(self.status.deleted)
|
|
|
|
self.assertEqual(models.Notification.objects.count(), 2)
|
|
|
|
activity = {
|
|
|
|
"type": "Delete",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"id": "%s/activity" % self.status.remote_id,
|
|
|
|
"actor": self.remote_user.remote_id,
|
|
|
|
"object": {"id": self.status.remote_id, "type": "Tombstone"},
|
|
|
|
}
|
|
|
|
with patch(
|
|
|
|
"bookwyrm.activitystreams.ActivityStream.remove_object_from_related_stores"
|
|
|
|
) as redis_mock:
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertTrue(redis_mock.called)
|
|
|
|
# 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)
|
2021-04-17 19:39:16 +00:00
|
|
|
|
2021-08-04 15:50:50 +00:00
|
|
|
@patch("bookwyrm.suggested_users.remove_user_task.delay")
|
|
|
|
def test_delete_user(self, _):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""delete a user"""
|
2021-04-17 20:19:23 +00:00
|
|
|
self.assertTrue(models.User.objects.get(username="rat@example.com").is_active)
|
2021-04-17 19:39:16 +00:00
|
|
|
activity = {
|
2021-04-17 20:19:23 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/users/test-user#delete",
|
|
|
|
"type": "Delete",
|
|
|
|
"actor": "https://example.com/users/test-user",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"object": self.remote_user.remote_id,
|
2021-04-17 19:39:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 23:05:40 +00:00
|
|
|
views.inbox.activity_task(activity)
|
2021-04-17 20:19:23 +00:00
|
|
|
self.assertFalse(models.User.objects.get(username="rat@example.com").is_active)
|
2021-04-17 19:39:16 +00:00
|
|
|
|
|
|
|
def test_delete_user_unknown(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""don't worry about it if we don't know the user"""
|
2021-04-17 19:39:16 +00:00
|
|
|
self.assertEqual(models.User.objects.filter(is_active=True).count(), 2)
|
|
|
|
activity = {
|
2021-04-17 20:19:23 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
"id": "https://example.com/users/test-user#delete",
|
|
|
|
"type": "Delete",
|
|
|
|
"actor": "https://example.com/users/test-user",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"object": "https://example.com/users/test-user",
|
2021-04-17 19:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# nothing happens.
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
self.assertEqual(models.User.objects.filter(is_active=True).count(), 2)
|