Add some additional coverage

This commit is contained in:
Pedro Ferreira 2017-08-06 12:16:22 +02:00
parent 73dccb8b15
commit 797315e967
2 changed files with 41 additions and 17 deletions

View File

@ -28,7 +28,7 @@ class Feed(object):
def id(self): def id(self):
return tag(self.public_key).decode('ascii') 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!)') raise NoPrivateKeyException('Cannot use remote identity to sign (no private key!)')
@ -65,7 +65,7 @@ class Message(object):
def parse(cls, data, feed): def parse(cls, data, feed):
obj = loads(data, object_pairs_hook=OrderedDict) obj = loads(data, object_pairs_hook=OrderedDict)
msg = cls(feed, obj['content'], timestamp=obj['timestamp']) msg = cls(feed, obj['content'], timestamp=obj['timestamp'])
return msg, obj['signature'] return msg
def serialize(self, add_signature=True): def serialize(self, add_signature=True):
return dumps(self.to_dict(add_signature=add_signature), indent=2).encode('utf-8') return dumps(self.to_dict(add_signature=add_signature), indent=2).encode('utf-8')

View File

@ -4,7 +4,23 @@ from collections import OrderedDict
import pytest import pytest
from nacl.signing import SigningKey, VerifyKey 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() @pytest.fixture()
@ -33,6 +49,16 @@ def test_remote_feed():
assert bytes(feed.public_key) == public assert bytes(feed.public_key) == public
assert feed.id == '@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519' 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): def test_local_message(local_feed):
m1 = LocalMessage(local_feed, OrderedDict([ m1 = LocalMessage(local_feed, OrderedDict([
@ -109,17 +135,15 @@ def test_serialize(local_feed):
('description', 'The Chosen One') ('description', 'The Chosen One')
]), timestamp=1495706260190) ]), timestamp=1495706260190)
assert m1.serialize() == b"""{ assert m1.serialize() == SERIALIZED_M1
"previous": null,
"author": "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519",
"sequence": 1, def test_parse(local_feed):
"timestamp": 1495706260190, m1 = LocalMessage.parse(SERIALIZED_M1, local_feed)
"hash": "sha256", assert m1.content == {
"content": { 'type': 'about',
"type": "about", 'about': local_feed.id,
"about": "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519", 'name': 'neo',
"name": "neo", 'description': 'The Chosen One'
"description": "The Chosen One" }
}, assert m1.timestamp == 1495706260190
"signature": "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519"
}"""