2021-04-08 18:18:32 +00:00
|
|
|
""" tests incoming activities"""
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
import responses
|
|
|
|
|
|
|
|
from bookwyrm import models, views
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-public-methods
|
2021-04-08 22:40:02 +00:00
|
|
|
class InboxAdd(TestCase):
|
2021-04-08 18:18:32 +00:00
|
|
|
""" inbox tests """
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
""" basic user and book data """
|
2021-04-08 22:50:21 +00:00
|
|
|
local_user = models.User.objects.create_user(
|
2021-04-08 18:18:32 +00:00
|
|
|
"mouse@example.com",
|
|
|
|
"mouse@mouse.com",
|
|
|
|
"mouseword",
|
|
|
|
local=True,
|
|
|
|
localname="mouse",
|
|
|
|
)
|
2021-04-08 22:50:21 +00:00
|
|
|
local_user.remote_id = "https://example.com/user/mouse"
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
work = models.Work.objects.create(title="work title")
|
2021-04-08 22:40:02 +00:00
|
|
|
self.book = models.Edition.objects.create(
|
2021-04-08 18:18:32 +00:00
|
|
|
title="Test",
|
|
|
|
remote_id="https://bookwyrm.social/book/37292",
|
|
|
|
parent_work=work,
|
|
|
|
)
|
2021-04-08 22:40:02 +00:00
|
|
|
|
|
|
|
models.SiteSettings.objects.create()
|
|
|
|
|
|
|
|
def test_handle_add_book_to_shelf(self):
|
|
|
|
""" shelving a book """
|
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()
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"id": "https://bookwyrm.social/shelfbook/6189#add",
|
|
|
|
"type": "Add",
|
|
|
|
"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:40:02 +00:00
|
|
|
"type": "ShelfItem",
|
|
|
|
"book": self.book.remote_id,
|
2021-04-08 22:50:21 +00:00
|
|
|
"id": "https://bookwyrm.social/shelfbook/6189",
|
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)
|
2021-04-08 22:40:02 +00:00
|
|
|
self.assertEqual(shelf.books.first(), self.book)
|
2021-04-08 18:18:32 +00:00
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_handle_add_book_to_list(self):
|
|
|
|
""" listing a book """
|
|
|
|
responses.add(
|
|
|
|
responses.GET,
|
|
|
|
"https://bookwyrm.social/user/mouse/list/to-read",
|
|
|
|
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",
|
|
|
|
"to": ["https://www.w3.org/ns/activitystreams#Public"],
|
|
|
|
"cc": ["https://example.com/user/mouse/followers"],
|
|
|
|
"summary": "summary text",
|
|
|
|
"curation": "curated",
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
activity = {
|
|
|
|
"id": "https://bookwyrm.social/listbook/6189#add",
|
|
|
|
"type": "Add",
|
|
|
|
"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:23:56 +00:00
|
|
|
"type": "ListItem",
|
2021-04-08 22:40:02 +00:00
|
|
|
"book": self.book.remote_id,
|
2021-04-08 22:23:56 +00:00
|
|
|
"id": "https://bookwyrm.social/listbook/6189",
|
2021-04-08 18:18:32 +00:00
|
|
|
},
|
|
|
|
"target": "https://bookwyrm.social/user/mouse/list/to-read",
|
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
|
|
|
}
|
|
|
|
views.inbox.activity_task(activity)
|
|
|
|
|
|
|
|
booklist = models.List.objects.get()
|
2021-04-08 22:23:56 +00:00
|
|
|
listitem = models.ListItem.objects.get()
|
2021-04-08 18:18:32 +00:00
|
|
|
self.assertEqual(booklist.name, "Test List")
|
2021-04-08 22:40:02 +00:00
|
|
|
self.assertEqual(booklist.books.first(), self.book)
|
2021-04-08 22:23:56 +00:00
|
|
|
self.assertEqual(listitem.remote_id, "https://bookwyrm.social/listbook/6189")
|