From 797315e96782d75dd861f6c85627ee290cf02663 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Sun, 6 Aug 2017 12:16:22 +0200 Subject: [PATCH] Add some additional coverage --- ssb/feed.py | 4 ++-- ssb/tests/test_feed.py | 54 ++++++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/ssb/feed.py b/ssb/feed.py index 667f33a..565e3d5 100644 --- a/ssb/feed.py +++ b/ssb/feed.py @@ -28,7 +28,7 @@ class Feed(object): def id(self): return tag(self.public_key).decode('ascii') - def sign(self): + def sign(self, msg): raise NoPrivateKeyException('Cannot use remote identity to sign (no private key!)') @@ -65,7 +65,7 @@ class Message(object): def parse(cls, data, feed): obj = loads(data, object_pairs_hook=OrderedDict) msg = cls(feed, obj['content'], timestamp=obj['timestamp']) - return msg, obj['signature'] + return msg def serialize(self, add_signature=True): return dumps(self.to_dict(add_signature=add_signature), indent=2).encode('utf-8') diff --git a/ssb/tests/test_feed.py b/ssb/tests/test_feed.py index c24dbb1..78f499a 100644 --- a/ssb/tests/test_feed.py +++ b/ssb/tests/test_feed.py @@ -4,7 +4,23 @@ from collections import OrderedDict import pytest from nacl.signing import SigningKey, VerifyKey -from ssb.feed import LocalMessage, LocalFeed, Feed, Message +from ssb.feed import LocalMessage, LocalFeed, Feed, Message, NoPrivateKeyException + + +SERIALIZED_M1 = b"""{ + "previous": null, + "author": "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519", + "sequence": 1, + "timestamp": 1495706260190, + "hash": "sha256", + "content": { + "type": "about", + "about": "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519", + "name": "neo", + "description": "The Chosen One" + }, + "signature": "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519" +}""" @pytest.fixture() @@ -33,6 +49,16 @@ def test_remote_feed(): assert bytes(feed.public_key) == public assert feed.id == '@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519' + m1 = Message(feed, OrderedDict([ + ('type', 'about'), + ('about', feed.id), + ('name', 'neo'), + ('description', 'The Chosen One') + ]), 'foo', timestamp=1495706260190) + + with pytest.raises(NoPrivateKeyException): + feed.sign(m1) + def test_local_message(local_feed): m1 = LocalMessage(local_feed, OrderedDict([ @@ -109,17 +135,15 @@ def test_serialize(local_feed): ('description', 'The Chosen One') ]), timestamp=1495706260190) - assert m1.serialize() == b"""{ - "previous": null, - "author": "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519", - "sequence": 1, - "timestamp": 1495706260190, - "hash": "sha256", - "content": { - "type": "about", - "about": "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519", - "name": "neo", - "description": "The Chosen One" - }, - "signature": "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519" -}""" + assert m1.serialize() == SERIALIZED_M1 + + +def test_parse(local_feed): + m1 = LocalMessage.parse(SERIALIZED_M1, local_feed) + assert m1.content == { + 'type': 'about', + 'about': local_feed.id, + 'name': 'neo', + 'description': 'The Chosen One' + } + assert m1.timestamp == 1495706260190