moviewyrm/bookwyrm/tests/views/inbox/test_inbox_delete.py

159 lines
6.5 KiB
Python
Raw Normal View History

2021-09-07 01:39:14 +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"""
def setUp(self):
2021-04-26 16:15:42 +00:00
"""basic user and book data"""
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
2021-12-09 23:02:53 +00:00
), patch("bookwyrm.lists_stream.populate_lists_task.delay"):
2021-08-03 20:27:32 +00:00
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"):
self.status = models.Status.objects.create(
user=self.remote_user,
content="Test status",
remote_id="https://example.com/status/1",
)
2021-08-02 23:05:40 +00:00
models.SiteSettings.objects.create()
def test_delete_status(self):
2021-04-26 16:15:42 +00:00
"""remove a status"""
self.assertFalse(self.status.deleted)
activity = {
"type": "Delete",
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"cc": ["https://example.com/user/mouse/followers"],
2021-12-09 23:02:53 +00:00
"id": f"{self.status.remote_id}/activity",
"actor": self.remote_user.remote_id,
"object": {"id": self.status.remote_id, "type": "Tombstone"},
}
2021-09-07 01:39:14 +00:00
with patch("bookwyrm.activitystreams.remove_status_task.delay") 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)
def test_delete_status_notifications(self):
2021-04-26 16:15:42 +00:00
"""remove a status with related notifications"""
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"],
2021-12-09 23:02:53 +00:00
"id": f"{self.status.remote_id}/activity",
"actor": self.remote_user.remote_id,
"object": {"id": self.status.remote_id, "type": "Tombstone"},
}
2021-09-07 01:39:14 +00:00
with patch("bookwyrm.activitystreams.remove_status_task.delay") 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-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)
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-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)
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"""
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",
}
# nothing happens.
views.inbox.activity_task(activity)
self.assertEqual(models.User.objects.filter(is_active=True).count(), 2)
2021-09-06 18:55:04 +00:00
def test_delete_list(self):
"""delete a list"""
book_list = models.List.objects.create(
name="test list",
user=self.remote_user,
2021-09-06 19:30:11 +00:00
remote_id="https://example.com/list/1",
2021-09-06 18:55:04 +00:00
)
activity = {
"@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"],
2021-09-06 19:30:11 +00:00
"object": {
"id": book_list.remote_id,
"owner": self.remote_user.remote_id,
"type": "BookList",
"totalItems": 0,
"first": "",
"name": "test list",
"to": [],
"cc": [],
},
2021-09-06 18:55:04 +00:00
}
2021-12-09 23:02:53 +00:00
with patch("bookwyrm.lists_stream.remove_list_task.delay") as mock:
views.inbox.activity_task(activity)
self.assertTrue(mock.called)
2021-09-06 18:55:04 +00:00
self.assertFalse(models.List.objects.exists())