ci: Add and configure PyLint, and make it happy
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
"""Tests for the feed functionality"""
|
||||
|
||||
from base64 import b64decode
|
||||
from collections import OrderedDict
|
||||
|
||||
@@ -25,17 +27,23 @@ SERIALIZED_M1 = b"""{
|
||||
|
||||
@pytest.fixture
|
||||
def local_feed():
|
||||
"""Fixture providing a local feed"""
|
||||
|
||||
secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=")
|
||||
return LocalFeed(SigningKey(secret))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def remote_feed():
|
||||
"""Fixture providing a remote feed"""
|
||||
|
||||
public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
|
||||
return Feed(VerifyKey(public))
|
||||
|
||||
|
||||
def test_local_feed():
|
||||
"""Test a local feed"""
|
||||
|
||||
secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=")
|
||||
feed = LocalFeed(SigningKey(secret))
|
||||
assert bytes(feed.private_key) == secret
|
||||
@@ -44,6 +52,8 @@ def test_local_feed():
|
||||
|
||||
|
||||
def test_remote_feed():
|
||||
"""Test a remote feed"""
|
||||
|
||||
public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
|
||||
feed = Feed(VerifyKey(public))
|
||||
assert bytes(feed.public_key) == public
|
||||
@@ -60,7 +70,9 @@ def test_remote_feed():
|
||||
feed.sign(m1)
|
||||
|
||||
|
||||
def test_local_message(local_feed):
|
||||
def test_local_message(local_feed): # 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")]),
|
||||
@@ -91,7 +103,9 @@ def test_local_message(local_feed):
|
||||
assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256"
|
||||
|
||||
|
||||
def test_remote_message(remote_feed):
|
||||
def test_remote_message(remote_feed): # pylint: disable=redefined-outer-name
|
||||
"""Test a remote message"""
|
||||
|
||||
signature = "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519"
|
||||
m1 = Message(
|
||||
remote_feed,
|
||||
@@ -123,7 +137,9 @@ def test_remote_message(remote_feed):
|
||||
assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256"
|
||||
|
||||
|
||||
def test_remote_no_signature(remote_feed):
|
||||
def test_remote_no_signature(remote_feed): # pylint: disable=redefined-outer-name
|
||||
"""Test remote feed without a signature"""
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
Message(
|
||||
remote_feed,
|
||||
@@ -135,7 +151,9 @@ def test_remote_no_signature(remote_feed):
|
||||
)
|
||||
|
||||
|
||||
def test_serialize(local_feed):
|
||||
def test_serialize(local_feed): # 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")]),
|
||||
@@ -145,7 +163,9 @@ def test_serialize(local_feed):
|
||||
assert m1.serialize() == SERIALIZED_M1
|
||||
|
||||
|
||||
def test_parse(local_feed):
|
||||
def test_parse(local_feed): # pylint: disable=redefined-outer-name
|
||||
"""Test feed parsing"""
|
||||
|
||||
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
|
||||
|
@@ -1,8 +1,9 @@
|
||||
"""Tests for the packet stream"""
|
||||
|
||||
import json
|
||||
from asyncio import ensure_future, gather, Event
|
||||
|
||||
import pytest
|
||||
from nacl.signing import SigningKey
|
||||
|
||||
from secret_handshake.network import SHSDuplexStream
|
||||
from ssb.packet_stream import PacketStream, PSMessageType
|
||||
@@ -36,63 +37,94 @@ MSG_BODY_2 = (
|
||||
|
||||
|
||||
class MockSHSSocket(SHSDuplexStream):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(MockSHSSocket, self).__init__()
|
||||
"""A mocked SHS socket"""
|
||||
|
||||
def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
|
||||
super().__init__()
|
||||
|
||||
self.input = []
|
||||
self.output = []
|
||||
self.is_connected = False
|
||||
self._on_connect = []
|
||||
|
||||
def on_connect(self, cb):
|
||||
"""Set the on_connect callback"""
|
||||
|
||||
self._on_connect.append(cb)
|
||||
|
||||
async def read(self):
|
||||
"""Read data from the socket"""
|
||||
|
||||
if not self.input:
|
||||
raise StopAsyncIteration
|
||||
return self.input.pop(0)
|
||||
|
||||
def write(self, data):
|
||||
"""Write data to the socket"""
|
||||
|
||||
self.output.append(data)
|
||||
|
||||
def feed(self, input):
|
||||
self.input += input
|
||||
def feed(self, input_):
|
||||
"""Get the connection’s feed"""
|
||||
|
||||
self.input += input_
|
||||
|
||||
def get_output(self):
|
||||
"""Get the output of a call"""
|
||||
|
||||
while True:
|
||||
if not self.output:
|
||||
break
|
||||
yield self.output.pop(0)
|
||||
|
||||
def disconnect(self):
|
||||
"""Disconnect from the remote party"""
|
||||
|
||||
self.is_connected = False
|
||||
|
||||
|
||||
class MockSHSClient(MockSHSSocket):
|
||||
"""A mocked SHS client"""
|
||||
|
||||
async def connect(self):
|
||||
"""Connect to a SHS server"""
|
||||
|
||||
self.is_connected = True
|
||||
|
||||
for cb in self._on_connect:
|
||||
await cb()
|
||||
|
||||
|
||||
class MockSHSServer(MockSHSSocket):
|
||||
"""A mocked SHS server"""
|
||||
|
||||
def listen(self):
|
||||
"""Listen for new connections"""
|
||||
|
||||
self.is_connected = True
|
||||
|
||||
for cb in self._on_connect:
|
||||
ensure_future(cb())
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ps_client(event_loop):
|
||||
def ps_client(event_loop): # pylint: disable=unused-argument
|
||||
"""Fixture to provide a mocked SHS client"""
|
||||
|
||||
return MockSHSClient()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ps_server(event_loop):
|
||||
def ps_server(event_loop): # pylint: disable=unused-argument
|
||||
"""Fixture to provide a mocked SHS server"""
|
||||
|
||||
return MockSHSServer()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_on_connect(ps_server):
|
||||
async def test_on_connect(ps_server): # pylint: disable=redefined-outer-name
|
||||
"""Test the on_connect callback functionality"""
|
||||
|
||||
called = Event()
|
||||
|
||||
async def _on_connect():
|
||||
@@ -105,7 +137,9 @@ async def test_on_connect(ps_server):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_decoding(ps_client):
|
||||
async def test_message_decoding(ps_client): # pylint: disable=redefined-outer-name
|
||||
"""Test message decoding"""
|
||||
|
||||
await ps_client.connect()
|
||||
|
||||
ps = PacketStream(ps_client)
|
||||
@@ -133,7 +167,9 @@ async def test_message_decoding(ps_client):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_encoding(ps_client):
|
||||
async def test_message_encoding(ps_client): # pylint: disable=redefined-outer-name
|
||||
"""Test message encoding"""
|
||||
|
||||
await ps_client.connect()
|
||||
|
||||
ps = PacketStream(ps_client)
|
||||
@@ -164,7 +200,9 @@ async def test_message_encoding(ps_client):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_stream(ps_client, mocker):
|
||||
async def test_message_stream(ps_client, mocker): # pylint: disable=redefined-outer-name
|
||||
"""Test requesting a history stream"""
|
||||
|
||||
await ps_client.connect()
|
||||
|
||||
ps = PacketStream(ps_client)
|
||||
@@ -184,8 +222,8 @@ async def test_message_stream(ps_client, mocker):
|
||||
)
|
||||
|
||||
assert ps.req_counter == 2
|
||||
assert ps.register_handler.call_count == 1
|
||||
handler = list(ps._event_map.values())[0][1]
|
||||
assert ps.register_handler.call_count == 1 # pylint: disable=no-member
|
||||
handler = list(ps._event_map.values())[0][1] # pylint: disable=protected-access
|
||||
|
||||
mock_process = mocker.patch.object(handler, "process")
|
||||
|
||||
@@ -211,8 +249,8 @@ async def test_message_stream(ps_client, mocker):
|
||||
)
|
||||
|
||||
assert ps.req_counter == 3
|
||||
assert ps.register_handler.call_count == 2
|
||||
handler = list(ps._event_map.values())[1][1]
|
||||
assert ps.register_handler.call_count == 2 # pylint: disable=no-member
|
||||
handler = list(ps._event_map.values())[1][1] # pylint: disable=protected-access
|
||||
|
||||
mock_process = mocker.patch.object(handler, "process", wraps=handler.process)
|
||||
|
||||
@@ -234,7 +272,9 @@ async def test_message_stream(ps_client, mocker):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_request(ps_server, mocker):
|
||||
async def test_message_request(ps_server, mocker): # pylint: disable=redefined-outer-name
|
||||
"""Test message sending"""
|
||||
|
||||
ps_server.listen()
|
||||
|
||||
ps = PacketStream(ps_server)
|
||||
@@ -248,8 +288,8 @@ async def test_message_request(ps_server, mocker):
|
||||
assert json.loads(body.decode("utf-8")) == {"name": ["whoami"], "args": []}
|
||||
|
||||
assert ps.req_counter == 2
|
||||
assert ps.register_handler.call_count == 1
|
||||
handler = list(ps._event_map.values())[0][1]
|
||||
assert ps.register_handler.call_count == 1 # pylint: disable=no-member
|
||||
handler = list(ps._event_map.values())[0][1] # pylint: disable=protected-access
|
||||
|
||||
mock_process = mocker.patch.object(handler, "process")
|
||||
|
||||
|
@@ -1,3 +1,5 @@
|
||||
"""Test for utility functions"""
|
||||
|
||||
from base64 import b64decode
|
||||
from unittest.mock import mock_open, patch
|
||||
|
||||
@@ -20,6 +22,8 @@ CONFIG_FILE_INVALID = CONFIG_FILE.replace("ed25519", "foo")
|
||||
|
||||
|
||||
def test_load_secret():
|
||||
"""Test loading the SSB secret from a file"""
|
||||
|
||||
with patch("ssb.util.open", mock_open(read_data=CONFIG_FILE), create=True):
|
||||
secret = load_ssb_secret()
|
||||
|
||||
@@ -31,6 +35,7 @@ def test_load_secret():
|
||||
|
||||
|
||||
def test_load_exception():
|
||||
"""Test configuration loading if there is a problem with the file"""
|
||||
with pytest.raises(ConfigException):
|
||||
with patch("ssb.util.open", mock_open(read_data=CONFIG_FILE_INVALID), create=True):
|
||||
load_ssb_secret()
|
||||
|
Reference in New Issue
Block a user