Use dataclasses to define activitypub (de)serialization (#177)
* Use dataclasses to define activitypub (de)serialization
This commit is contained in:
parent
2c0a07a330
commit
8bbf1fe252
46 changed files with 1449 additions and 1228 deletions
|
@ -16,41 +16,3 @@ class Comment(TestCase):
|
|||
comment = status_builder.create_comment(
|
||||
self.user, self.book, 'commentary')
|
||||
self.assertEqual(comment.content, 'commentary')
|
||||
|
||||
|
||||
def test_comment_from_activity(self):
|
||||
activity = {
|
||||
"id": "https://example.com/user/mouse/comment/6",
|
||||
"url": "https://example.com/user/mouse/comment/6",
|
||||
"inReplyTo": None,
|
||||
"published": "2020-05-08T23:45:44.768012+00:00",
|
||||
"attributedTo": "https://example.com/user/mouse",
|
||||
"to": [
|
||||
"https://www.w3.org/ns/activitystreams#Public"
|
||||
],
|
||||
"cc": [
|
||||
"https://example.com/user/mouse/followers"
|
||||
],
|
||||
"sensitive": False,
|
||||
"content": "commentary",
|
||||
"type": "Note",
|
||||
"attachment": [],
|
||||
"replies": {
|
||||
"id": "https://example.com/user/mouse/comment/6/replies",
|
||||
"type": "Collection",
|
||||
"first": {
|
||||
"type": "CollectionPage",
|
||||
"next": "https://example.com/user/mouse/comment/6/replies?only_other_accounts=true&page=true",
|
||||
"partOf": "https://example.com/user/mouse/comment/6/replies",
|
||||
"items": []
|
||||
}
|
||||
},
|
||||
"inReplyToBook": self.book.remote_id,
|
||||
"fedireadsType": "Comment"
|
||||
}
|
||||
comment = status_builder.create_comment_from_activity(
|
||||
self.user, activity)
|
||||
self.assertEqual(comment.content, 'commentary')
|
||||
self.assertEqual(comment.book, self.book)
|
||||
self.assertEqual(
|
||||
comment.published_date, '2020-05-08T23:45:44.768012+00:00')
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
from django.test import TestCase
|
||||
import json
|
||||
import pathlib
|
||||
|
||||
from fedireads import models
|
||||
from fedireads import activitypub, models
|
||||
from fedireads import status as status_builder
|
||||
|
||||
|
||||
|
@ -8,8 +10,13 @@ class Quotation(TestCase):
|
|||
''' we have hecka ways to create statuses '''
|
||||
def setUp(self):
|
||||
self.user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword')
|
||||
self.book = models.Edition.objects.create(title='Example Edition')
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword',
|
||||
remote_id='https://example.com/user/mouse'
|
||||
)
|
||||
self.book = models.Edition.objects.create(
|
||||
title='Example Edition',
|
||||
remote_id='https://example.com/book/1',
|
||||
)
|
||||
|
||||
|
||||
def test_create_quotation(self):
|
||||
|
@ -17,50 +24,3 @@ class Quotation(TestCase):
|
|||
self.user, self.book, 'commentary', 'a quote')
|
||||
self.assertEqual(quotation.quote, 'a quote')
|
||||
self.assertEqual(quotation.content, 'commentary')
|
||||
|
||||
|
||||
def test_quotation_from_activity(self):
|
||||
activity = {
|
||||
'id': 'https://example.com/user/mouse/quotation/13',
|
||||
'url': 'https://example.com/user/mouse/quotation/13',
|
||||
'inReplyTo': None,
|
||||
'published': '2020-05-10T02:38:31.150343+00:00',
|
||||
'attributedTo': 'https://example.com/user/mouse',
|
||||
'to': [
|
||||
'https://www.w3.org/ns/activitystreams#Public'
|
||||
],
|
||||
'cc': [
|
||||
'https://example.com/user/mouse/followers'
|
||||
],
|
||||
'sensitive': False,
|
||||
'content': 'commentary',
|
||||
'type': 'Note',
|
||||
'attachment': [
|
||||
{
|
||||
'type': 'Document',
|
||||
'mediaType': 'image//images/covers/2b4e4712-5a4d-4ac1-9df4-634cc9c7aff3jpg',
|
||||
'url': 'https://example.com/images/covers/2b4e4712-5a4d-4ac1-9df4-634cc9c7aff3jpg',
|
||||
'name': 'Cover of \'This Is How You Lose the Time War\''
|
||||
}
|
||||
],
|
||||
'replies': {
|
||||
'id': 'https://example.com/user/mouse/quotation/13/replies',
|
||||
'type': 'Collection',
|
||||
'first': {
|
||||
'type': 'CollectionPage',
|
||||
'next': 'https://example.com/user/mouse/quotation/13/replies?only_other_accounts=true&page=true',
|
||||
'partOf': 'https://example.com/user/mouse/quotation/13/replies',
|
||||
'items': []
|
||||
}
|
||||
},
|
||||
'inReplyToBook': self.book.remote_id,
|
||||
'fedireadsType': 'Quotation',
|
||||
'quote': 'quote body'
|
||||
}
|
||||
quotation = status_builder.create_quotation_from_activity(
|
||||
self.user, activity)
|
||||
self.assertEqual(quotation.content, 'commentary')
|
||||
self.assertEqual(quotation.quote, 'quote body')
|
||||
self.assertEqual(quotation.book, self.book)
|
||||
self.assertEqual(
|
||||
quotation.published_date, '2020-05-10T02:38:31.150343+00:00')
|
||||
|
|
|
@ -37,45 +37,3 @@ class Review(TestCase):
|
|||
self.assertEqual(review.name, 'review name')
|
||||
self.assertEqual(review.content, 'content')
|
||||
self.assertEqual(review.rating, None)
|
||||
|
||||
|
||||
def test_review_from_activity(self):
|
||||
activity = {
|
||||
'id': 'https://example.com/user/mouse/review/9',
|
||||
'url': 'https://example.com/user/mouse/review/9',
|
||||
'inReplyTo': None,
|
||||
'published': '2020-05-04T00:00:00.000000+00:00',
|
||||
'attributedTo': 'https://example.com/user/mouse',
|
||||
'to': [
|
||||
'https://www.w3.org/ns/activitystreams#Public'
|
||||
],
|
||||
'cc': [
|
||||
'https://example.com/user/mouse/followers'
|
||||
],
|
||||
'sensitive': False,
|
||||
'content': 'review content',
|
||||
'type': 'Article',
|
||||
'attachment': [],
|
||||
'replies': {
|
||||
'id': 'https://example.com/user/mouse/review/9/replies',
|
||||
'type': 'Collection',
|
||||
'first': {
|
||||
'type': 'CollectionPage',
|
||||
'next': 'https://example.com/user/mouse/review/9/replies?only_other_accounts=true&page=true',
|
||||
'partOf': 'https://example.com/user/mouse/review/9/replies',
|
||||
'items': []
|
||||
}
|
||||
},
|
||||
'inReplyToBook': self.book.remote_id,
|
||||
'fedireadsType': 'Review',
|
||||
'name': 'review title',
|
||||
'rating': 3
|
||||
}
|
||||
review = status_builder.create_review_from_activity(
|
||||
self.user, activity)
|
||||
self.assertEqual(review.content, 'review content')
|
||||
self.assertEqual(review.name, 'review title')
|
||||
self.assertEqual(review.rating, 3)
|
||||
self.assertEqual(review.book, self.book)
|
||||
self.assertEqual(
|
||||
review.published_date, '2020-05-04T00:00:00.000000+00:00')
|
||||
|
|
|
@ -8,7 +8,12 @@ class Status(TestCase):
|
|||
''' we have hecka ways to create statuses '''
|
||||
def setUp(self):
|
||||
self.user = models.User.objects.create_user(
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword')
|
||||
'mouse', 'mouse@mouse.mouse', 'mouseword',
|
||||
local=False,
|
||||
inbox='https://example.com/user/mouse/inbox',
|
||||
outbox='https://example.com/user/mouse/outbox',
|
||||
remote_id='https://example.com/user/mouse'
|
||||
)
|
||||
|
||||
|
||||
def test_create_status(self):
|
||||
|
@ -21,45 +26,3 @@ class Status(TestCase):
|
|||
self.user, content, reply_parent=status)
|
||||
self.assertEqual(reply.content, content)
|
||||
self.assertEqual(reply.reply_parent, status)
|
||||
|
||||
|
||||
def test_create_status_from_activity(self):
|
||||
book = models.Edition.objects.create(title='Example Edition')
|
||||
review = status_builder.create_review(
|
||||
self.user, book, 'review name', 'content', 5)
|
||||
activity = {
|
||||
'id': 'https://example.com/user/mouse/status/12',
|
||||
'url': 'https://example.com/user/mouse/status/12',
|
||||
'inReplyTo': review.remote_id,
|
||||
'published': '2020-05-10T02:15:59.635557+00:00',
|
||||
'attributedTo': 'https://example.com/user/mouse',
|
||||
'to': [
|
||||
'https://www.w3.org/ns/activitystreams#Public'
|
||||
],
|
||||
'cc': [
|
||||
'https://example.com/user/mouse/followers'
|
||||
],
|
||||
'sensitive': False,
|
||||
'content': 'reply to status',
|
||||
'type': 'Note',
|
||||
'attachment': [],
|
||||
'replies': {
|
||||
'id': 'https://example.com/user/mouse/status/12/replies',
|
||||
'type': 'Collection',
|
||||
'first': {
|
||||
'type': 'CollectionPage',
|
||||
'next': 'https://example.com/user/mouse/status/12/replies?only_other_accounts=true&page=true',
|
||||
'partOf': 'https://example.com/user/mouse/status/12/replies',
|
||||
'items': []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
status = status_builder.create_status_from_activity(
|
||||
self.user, activity)
|
||||
self.assertEqual(status.reply_parent, review)
|
||||
self.assertEqual(status.content, 'reply to status')
|
||||
self.assertEqual(
|
||||
status.published_date,
|
||||
'2020-05-10T02:15:59.635557+00:00'
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue