pyssb/tests/test_util.py

42 lines
1.4 KiB
Python
Raw Permalink Normal View History

"""Test for utility functions"""
2017-08-05 10:24:46 +00:00
from base64 import b64decode
from unittest.mock import mock_open, patch
import pytest
from ssb.util import ConfigException, load_ssb_secret
2017-08-05 10:24:46 +00:00
CONFIG_FILE = """
## Comments should be supported too
{
"curve": "ed25519",
"public": "rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=ed25519",
"private": "/bqDBI/vGLD5qy3GxMsgHFgYIrrY08JfTzUaCYT6x0GuxikEhxezGNAB/Qk16z4wepPYMv4R+ilYoCnisZ4Q9A==",
"id": "@rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=.ed25519"
}
"""
CONFIG_FILE_INVALID = CONFIG_FILE.replace("ed25519", "foo")
2017-08-05 10:24:46 +00:00
def test_load_secret() -> None:
"""Test loading the SSB secret from a file"""
with patch("ssb.util.open", mock_open(read_data=CONFIG_FILE), create=True):
2017-08-05 10:24:46 +00:00
secret = load_ssb_secret()
priv_key = b'\xfd\xba\x83\x04\x8f\xef\x18\xb0\xf9\xab-\xc6\xc4\xcb \x1cX\x18"\xba\xd8\xd3\xc2_O5\x1a\t\x84\xfa\xc7A'
assert secret["id"] == "@rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=.ed25519"
assert bytes(secret["keypair"]) == priv_key
assert bytes(secret["keypair"].verify_key) == b64decode("rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=")
2017-08-05 10:24:46 +00:00
def test_load_exception() -> None:
"""Test configuration loading if there is a problem with the file"""
2017-08-05 10:24:46 +00:00
with pytest.raises(ConfigException):
with patch("ssb.util.open", mock_open(read_data=CONFIG_FILE_INVALID), create=True):
2017-08-05 10:24:46 +00:00
load_ssb_secret()