pyssb/tests/test_feed.py

171 lines
5.6 KiB
Python
Raw Permalink Normal View History

"""Tests for the feed functionality"""
2017-05-25 10:47:01 +00:00
from base64 import b64decode
from collections import OrderedDict
2017-08-05 17:32:01 +00:00
from nacl.signing import SigningKey, VerifyKey
import pytest
2017-05-25 10:47:01 +00:00
from ssb.feed import Feed, LocalFeed, LocalMessage, Message, NoPrivateKeyException
2017-08-06 10:16:22 +00:00
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"
}"""
2017-05-25 10:47:01 +00:00
@pytest.fixture
def local_feed() -> LocalFeed:
"""Fixture providing a local feed"""
secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=")
2017-08-05 16:21:14 +00:00
return LocalFeed(SigningKey(secret))
2017-05-25 10:47:01 +00:00
@pytest.fixture
def remote_feed() -> Feed:
"""Fixture providing a remote feed"""
public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
2017-08-05 17:32:01 +00:00
return Feed(VerifyKey(public))
def test_local_feed() -> None:
"""Test a local feed"""
secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=")
2017-08-05 16:21:14 +00:00
feed = LocalFeed(SigningKey(secret))
assert bytes(feed.private_key) == secret
assert bytes(feed.public_key) == b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
assert feed.id == "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519"
2017-05-25 10:47:01 +00:00
def test_remote_feed() -> None:
"""Test a remote feed"""
public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
2017-08-05 17:32:01 +00:00
feed = Feed(VerifyKey(public))
assert bytes(feed.public_key) == public
assert feed.id == "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519"
2017-08-05 17:32:01 +00:00
m1 = Message(
feed,
OrderedDict([("type", "about"), ("about", feed.id), ("name", "neo"), ("description", "The Chosen One")]),
"foo",
timestamp=1495706260190,
)
2017-08-06 10:16:22 +00:00
with pytest.raises(NoPrivateKeyException):
feed.sign(m1)
2017-08-05 17:32:01 +00:00
def test_local_message(local_feed: LocalFeed) -> None: # pylint: disable=redefined-outer-name
"""Test a local message"""
m1 = LocalMessage(
local_feed,
OrderedDict([("type", "about"), ("about", local_feed.id), ("name", "neo"), ("description", "The Chosen One")]),
timestamp=1495706260190,
)
2017-05-25 10:47:01 +00:00
assert m1.timestamp == 1495706260190
assert m1.previous is None
assert m1.sequence == 1
assert m1.signature == (
"lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519"
)
assert m1.key == "%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256"
m2 = LocalMessage(
local_feed,
OrderedDict(
[("type", "about"), ("about", local_feed.id), ("name", "morpheus"), ("description", "Dude with big jaw")]
),
previous=m1,
timestamp=1495706447426,
)
2017-05-25 10:47:01 +00:00
assert m2.timestamp == 1495706447426
assert m2.previous is m1
assert m2.sequence == 2
assert m2.signature == (
"3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519"
)
assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256"
2017-08-05 17:32:01 +00:00
def test_remote_message(remote_feed: Feed) -> None: # pylint: disable=redefined-outer-name
"""Test a remote message"""
signature = "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519"
m1 = Message(
remote_feed,
OrderedDict([("type", "about"), ("about", remote_feed.id), ("name", "neo"), ("description", "The Chosen One")]),
signature,
timestamp=1495706260190,
)
2017-08-05 17:32:01 +00:00
assert m1.timestamp == 1495706260190
assert m1.previous is None
assert m1.sequence == 1
assert m1.signature == signature
assert m1.key == "%xRDqws/TrQmOd4aEwZ32jdLhP873ZKjIgHlggPR0eoo=.sha256"
signature = "3SY85LX6/ppOfP4SbfwZbKfd6DccbLRiB13pwpzbSK0nU52OEJxOqcJ2Uensr6RkrWztWLIq90sNOn1zRAoOAw==.sig.ed25519"
m2 = Message(
remote_feed,
OrderedDict(
[("type", "about"), ("about", remote_feed.id), ("name", "morpheus"), ("description", "Dude with big jaw")]
),
signature,
previous=m1,
timestamp=1495706447426,
)
2017-08-05 17:32:01 +00:00
assert m2.timestamp == 1495706447426
assert m2.previous is m1
assert m2.sequence == 2
assert m2.signature == signature
m2.verify(signature)
assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256"
2017-08-05 17:32:01 +00:00
def test_remote_no_signature(remote_feed: Feed) -> None: # pylint: disable=redefined-outer-name
"""Test remote feed without a signature"""
2017-08-05 17:32:01 +00:00
with pytest.raises(ValueError):
Message(
remote_feed,
OrderedDict(
[("type", "about"), ("about", remote_feed.id), ("name", "neo"), ("description", "The Chosen One")]
),
None,
timestamp=1495706260190,
)
2017-08-05 18:01:17 +00:00
def test_serialize(local_feed: LocalFeed) -> None: # pylint: disable=redefined-outer-name
"""Test feed serialization"""
m1 = LocalMessage(
local_feed,
OrderedDict([("type", "about"), ("about", local_feed.id), ("name", "neo"), ("description", "The Chosen One")]),
timestamp=1495706260190,
)
2017-08-05 18:01:17 +00:00
2017-08-06 10:16:22 +00:00
assert m1.serialize() == SERIALIZED_M1
def test_parse(local_feed: LocalFeed) -> None: # pylint: disable=redefined-outer-name
"""Test feed parsing"""
2017-08-06 10:16:22 +00:00
m1 = LocalMessage.parse(SERIALIZED_M1, local_feed)
assert m1.content == {"type": "about", "about": local_feed.id, "name": "neo", "description": "The Chosen One"}
2017-08-06 10:16:22 +00:00
assert m1.timestamp == 1495706260190