forked from mirrors/bookwyrm
Fixes signing Create activities
and some tests for the base_model
This commit is contained in:
parent
9989641f4c
commit
2e4aff90a3
3 changed files with 51 additions and 10 deletions
|
@ -96,7 +96,7 @@ class ActivitypubMixin:
|
||||||
''' returns the object wrapped in a Create activity '''
|
''' returns the object wrapped in a Create activity '''
|
||||||
activity_object = self.to_activity()
|
activity_object = self.to_activity()
|
||||||
|
|
||||||
signer = pkcs1_15.new(RSA.import_key(user.private_key))
|
signer = pkcs1_15.new(RSA.import_key(user.key_pair.private_key))
|
||||||
content = activity_object['content']
|
content = activity_object['content']
|
||||||
signed_message = signer.sign(SHA256.new(content.encode('utf8')))
|
signed_message = signer.sign(SHA256.new(content.encode('utf8')))
|
||||||
create_id = self.remote_id + '/activity'
|
create_id = self.remote_id + '/activity'
|
||||||
|
|
|
@ -44,11 +44,6 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
||||||
)
|
)
|
||||||
objects = InheritanceManager()
|
objects = InheritanceManager()
|
||||||
|
|
||||||
@property
|
|
||||||
def ap_replies(self):
|
|
||||||
''' structured replies block '''
|
|
||||||
return self.to_replies()
|
|
||||||
|
|
||||||
activity_serializer = activitypub.Note
|
activity_serializer = activitypub.Note
|
||||||
serialize_reverse_fields = ['attachments']
|
serialize_reverse_fields = ['attachments']
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
''' testing models '''
|
''' testing models '''
|
||||||
|
from collections import namedtuple
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
from bookwyrm.models.base_model import BookWyrmModel
|
from bookwyrm.models import base_model
|
||||||
|
from bookwyrm.models.base_model import ActivitypubMixin
|
||||||
from bookwyrm.settings import DOMAIN
|
from bookwyrm.settings import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
class BaseModel(TestCase):
|
class BaseModel(TestCase):
|
||||||
def test_remote_id(self):
|
def test_remote_id(self):
|
||||||
instance = BookWyrmModel()
|
instance = base_model.BookWyrmModel()
|
||||||
instance.id = 1
|
instance.id = 1
|
||||||
expected = instance.get_remote_id()
|
expected = instance.get_remote_id()
|
||||||
self.assertEqual(expected, 'https://%s/bookwyrmmodel/1' % DOMAIN)
|
self.assertEqual(expected, 'https://%s/bookwyrmmodel/1' % DOMAIN)
|
||||||
|
@ -16,10 +17,55 @@ class BaseModel(TestCase):
|
||||||
def test_remote_id_with_user(self):
|
def test_remote_id_with_user(self):
|
||||||
user = models.User.objects.create_user(
|
user = models.User.objects.create_user(
|
||||||
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
|
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
|
||||||
instance = BookWyrmModel()
|
instance = base_model.BookWyrmModel()
|
||||||
instance.user = user
|
instance.user = user
|
||||||
instance.id = 1
|
instance.id = 1
|
||||||
expected = instance.get_remote_id()
|
expected = instance.get_remote_id()
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
expected,
|
expected,
|
||||||
'https://%s/user/mouse/bookwyrmmodel/1' % DOMAIN)
|
'https://%s/user/mouse/bookwyrmmodel/1' % DOMAIN)
|
||||||
|
|
||||||
|
def test_execute_after_save(self):
|
||||||
|
''' this function sets remote ids after creation '''
|
||||||
|
# using Work because it BookWrymModel is abstract and this requires save
|
||||||
|
# Work is a relatively not-fancy model.
|
||||||
|
instance = models.Work.objects.create(title='work title')
|
||||||
|
instance.remote_id = None
|
||||||
|
base_model.execute_after_save(None, instance, True)
|
||||||
|
self.assertEqual(
|
||||||
|
instance.remote_id,
|
||||||
|
'https://%s/book/%d' % (DOMAIN, instance.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
# shouldn't set remote_id if it's not created
|
||||||
|
instance.remote_id = None
|
||||||
|
base_model.execute_after_save(None, instance, False)
|
||||||
|
self.assertIsNone(instance.remote_id)
|
||||||
|
|
||||||
|
def test_to_create_activity(self):
|
||||||
|
user = models.User.objects.create_user(
|
||||||
|
'mouse', 'mouse@mouse.com', 'mouseword', local=True)
|
||||||
|
|
||||||
|
object_activity = {
|
||||||
|
'to': 'to field', 'cc': 'cc field',
|
||||||
|
'content': 'hi',
|
||||||
|
'published': '2020-12-04T17:52:22.623807+00:00',
|
||||||
|
}
|
||||||
|
MockSelf = namedtuple('Self', ('remote_id', 'to_activity'))
|
||||||
|
mock_self = MockSelf(
|
||||||
|
'https://example.com/status/1',
|
||||||
|
lambda *args: object_activity
|
||||||
|
)
|
||||||
|
activity = ActivitypubMixin.to_create_activity(mock_self, user)
|
||||||
|
self.assertEqual(
|
||||||
|
activity['id'],
|
||||||
|
'https://example.com/status/1/activity'
|
||||||
|
)
|
||||||
|
self.assertEqual(activity['actor'], user.remote_id)
|
||||||
|
self.assertEqual(activity['to'], 'to field')
|
||||||
|
self.assertEqual(activity['cc'], 'cc field')
|
||||||
|
self.assertEqual(activity['object'], object_activity)
|
||||||
|
self.assertEqual(
|
||||||
|
activity['signature'].creator,
|
||||||
|
'%s#main-key' % user.remote_id
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue