2021-04-08 18:18:32 +00:00
|
|
|
""" tests incoming activities"""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from bookwyrm import models, views
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-public-methods
|
2021-04-08 22:23:56 +00:00
|
|
|
class InboxRemove(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-04-08 22:23:56 +00:00
|
|
|
self.local_user = models.User.objects.create_user(
|
|
|
|
"mouse@example.com",
|
|
|
|
"mouse@mouse.com",
|
|
|
|
"mouseword",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
|
|
|
self.local_user.remote_id = "https://example.com/user/mouse"
|
|
|
|
self.local_user.save(broadcast=False)
|
2021-04-08 18:18:32 +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-04-08 22:23:56 +00:00
|
|
|
self.work = models.Work.objects.create(title="work title")
|
|
|
|
self.book = models.Edition.objects.create(
|
|
|
|
title="Test",
|
|
|
|
remote_id="https://bookwyrm.social/book/37292",
|
|
|
|
parent_work=self.work,
|
|
|
|
)
|
2021-04-08 18:18:32 +00:00
|
|
|
|
|
|
|
models.SiteSettings.objects.create()
|
|
|
|
|
|
|
|
def test_handle_unshelve_book(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""remove a book from a shelf"""
|
2021-04-08 18:18:32 +00:00
|
|
|
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(
|
2021-04-08 22:23:56 +00:00
|
|
|
user=self.remote_user, shelf=shelf, book=self.book
|
2021-04-08 18:18:32 +00:00
|
|
|
)
|
|
|
|
|
2021-04-08 22:23:56 +00:00
|
|
|
self.assertEqual(shelf.books.first(), self.book)
|
2021-04-08 18:18:32 +00:00
|
|
|
self.assertEqual(shelf.books.count(), 1)
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"id": shelfbook.remote_id,
|
|
|
|
"type": "Remove",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"object": {
|
2021-04-08 22:50:21 +00:00
|
|
|
"actor": self.remote_user.remote_id,
|
2021-04-08 22:46:06 +00:00
|
|
|
"type": "ShelfItem",
|
|
|
|
"book": self.book.remote_id,
|
|
|
|
"id": shelfbook.remote_id,
|
2021-04-08 18:18:32 +00:00
|
|
|
},
|
|
|
|
"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-04-08 22:23:56 +00:00
|
|
|
|
|
|
|
def test_handle_remove_book_from_list(self):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""listing a book"""
|
2021-04-08 22:23:56 +00:00
|
|
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.delay"):
|
|
|
|
booklist = models.List.objects.create(
|
|
|
|
name="test list",
|
|
|
|
user=self.local_user,
|
|
|
|
)
|
|
|
|
listitem = models.ListItem.objects.create(
|
|
|
|
user=self.local_user,
|
|
|
|
book=self.book,
|
|
|
|
book_list=booklist,
|
2021-04-08 16:05:21 +00:00
|
|
|
order=1,
|
2021-04-08 22:23:56 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(booklist.books.count(), 1)
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"id": listitem.remote_id,
|
|
|
|
"type": "Remove",
|
|
|
|
"actor": "https://example.com/users/rat",
|
|
|
|
"object": {
|
2021-04-08 22:50:21 +00:00
|
|
|
"actor": self.remote_user.remote_id,
|
2021-04-08 22:46:06 +00:00
|
|
|
"type": "ListItem",
|
|
|
|
"book": self.book.remote_id,
|
|
|
|
"id": listitem.remote_id,
|
2021-04-08 22:23:56 +00:00
|
|
|
},
|
|
|
|
"target": booklist.remote_id,
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
}
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
|
|
|
self.assertEqual(booklist.books.count(), 0)
|