ci: Add and configure mypy, and make it happy
This commit is contained in:
@@ -25,7 +25,7 @@ SERIALIZED_M1 = b"""{
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def local_feed():
|
||||
def local_feed() -> LocalFeed:
|
||||
"""Fixture providing a local feed"""
|
||||
|
||||
secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=")
|
||||
@@ -33,14 +33,14 @@ def local_feed():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def remote_feed():
|
||||
def remote_feed() -> Feed:
|
||||
"""Fixture providing a remote feed"""
|
||||
|
||||
public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
|
||||
return Feed(VerifyKey(public))
|
||||
|
||||
|
||||
def test_local_feed():
|
||||
def test_local_feed() -> None:
|
||||
"""Test a local feed"""
|
||||
|
||||
secret = b64decode("Mz2qkNOP2K6upnqibWrR+z8pVUI1ReA1MLc7QMtF2qQ=")
|
||||
@@ -50,7 +50,7 @@ def test_local_feed():
|
||||
assert feed.id == "@I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=.ed25519"
|
||||
|
||||
|
||||
def test_remote_feed():
|
||||
def test_remote_feed() -> None:
|
||||
"""Test a remote feed"""
|
||||
|
||||
public = b64decode("I/4cyN/jPBbDsikbHzAEvmaYlaJK33lW3UhWjNXjyrU=")
|
||||
@@ -69,7 +69,7 @@ def test_remote_feed():
|
||||
feed.sign(m1)
|
||||
|
||||
|
||||
def test_local_message(local_feed): # pylint: disable=redefined-outer-name
|
||||
def test_local_message(local_feed: LocalFeed) -> None: # pylint: disable=redefined-outer-name
|
||||
"""Test a local message"""
|
||||
|
||||
m1 = LocalMessage(
|
||||
@@ -102,7 +102,7 @@ def test_local_message(local_feed): # pylint: disable=redefined-outer-name
|
||||
assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256"
|
||||
|
||||
|
||||
def test_remote_message(remote_feed): # pylint: disable=redefined-outer-name
|
||||
def test_remote_message(remote_feed: Feed) -> None: # pylint: disable=redefined-outer-name
|
||||
"""Test a remote message"""
|
||||
|
||||
signature = "lPsQ9P10OgeyH6u0unFgiI2wV/RQ7Q2x2ebxnXYCzsJ055TBMXphRADTKhOMS2EkUxXQ9k3amj5fnWPudGxwBQ==.sig.ed25519"
|
||||
@@ -136,7 +136,7 @@ def test_remote_message(remote_feed): # pylint: disable=redefined-outer-name
|
||||
assert m2.key == "%nx13uks5GUwuKJC49PfYGMS/1pgGTtwwdWT7kbVaroM=.sha256"
|
||||
|
||||
|
||||
def test_remote_no_signature(remote_feed): # pylint: disable=redefined-outer-name
|
||||
def test_remote_no_signature(remote_feed: Feed) -> None: # pylint: disable=redefined-outer-name
|
||||
"""Test remote feed without a signature"""
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
@@ -150,7 +150,7 @@ def test_remote_no_signature(remote_feed): # pylint: disable=redefined-outer-na
|
||||
)
|
||||
|
||||
|
||||
def test_serialize(local_feed): # pylint: disable=redefined-outer-name
|
||||
def test_serialize(local_feed: LocalFeed) -> None: # pylint: disable=redefined-outer-name
|
||||
"""Test feed serialization"""
|
||||
|
||||
m1 = LocalMessage(
|
||||
@@ -162,7 +162,7 @@ def test_serialize(local_feed): # pylint: disable=redefined-outer-name
|
||||
assert m1.serialize() == SERIALIZED_M1
|
||||
|
||||
|
||||
def test_parse(local_feed): # pylint: disable=redefined-outer-name
|
||||
def test_parse(local_feed: LocalFeed) -> None: # pylint: disable=redefined-outer-name
|
||||
"""Test feed parsing"""
|
||||
|
||||
m1 = LocalMessage.parse(SERIALIZED_M1, local_feed)
|
||||
|
@@ -1,18 +1,23 @@
|
||||
"""Tests for the packet stream"""
|
||||
|
||||
from asyncio import Event, ensure_future, gather
|
||||
from asyncio.events import AbstractEventLoop
|
||||
import json
|
||||
from typing import AsyncGenerator, Awaitable, Callable, Generator, List
|
||||
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
from secret_handshake.network import SHSDuplexStream
|
||||
|
||||
from ssb.packet_stream import PacketStream, PSMessageType
|
||||
from ssb.packet_stream import PacketStream, PSMessage, PSMessageType
|
||||
|
||||
|
||||
async def _collect_messages(generator):
|
||||
async def _collect_messages(generator: AsyncGenerator[PSMessage, None]) -> List[PSMessage]:
|
||||
results = []
|
||||
|
||||
async for msg in generator:
|
||||
results.append(msg)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
@@ -39,45 +44,47 @@ MSG_BODY_2 = (
|
||||
class MockSHSSocket(SHSDuplexStream):
|
||||
"""A mocked SHS socket"""
|
||||
|
||||
def __init__(self, *args, **kwargs): # pylint: disable=unused-argument
|
||||
def __init__(self): # pylint: disable=unused-argument
|
||||
super().__init__()
|
||||
|
||||
self.input = []
|
||||
self.output = []
|
||||
self.input: List[bytes] = []
|
||||
self.output: List[bytes] = []
|
||||
self.is_connected = False
|
||||
self._on_connect = []
|
||||
self._on_connect: List[Callable[[SHSDuplexStream], Awaitable[None]]] = []
|
||||
|
||||
def on_connect(self, cb):
|
||||
def on_connect(self, cb: Callable[[SHSDuplexStream], Awaitable[None]]) -> None:
|
||||
"""Set the on_connect callback"""
|
||||
|
||||
self._on_connect.append(cb)
|
||||
|
||||
async def read(self):
|
||||
async def read(self) -> bytes:
|
||||
"""Read data from the socket"""
|
||||
|
||||
if not self.input:
|
||||
raise StopAsyncIteration
|
||||
|
||||
return self.input.pop(0)
|
||||
|
||||
def write(self, data):
|
||||
def write(self, data: bytes) -> None:
|
||||
"""Write data to the socket"""
|
||||
|
||||
self.output.append(data)
|
||||
|
||||
def feed(self, input_):
|
||||
"""Get the connection’s feed"""
|
||||
def feed(self, input_: List[bytes]) -> None:
|
||||
"""Feed data into the connection"""
|
||||
|
||||
self.input += input_
|
||||
|
||||
def get_output(self):
|
||||
def get_output(self) -> Generator[bytes, None, None]:
|
||||
"""Get the output of a call"""
|
||||
|
||||
while True:
|
||||
if not self.output:
|
||||
break
|
||||
|
||||
yield self.output.pop(0)
|
||||
|
||||
def disconnect(self):
|
||||
def disconnect(self) -> None:
|
||||
"""Disconnect from the remote party"""
|
||||
|
||||
self.is_connected = False
|
||||
@@ -86,48 +93,48 @@ class MockSHSSocket(SHSDuplexStream):
|
||||
class MockSHSClient(MockSHSSocket):
|
||||
"""A mocked SHS client"""
|
||||
|
||||
async def connect(self):
|
||||
async def connect(self) -> None:
|
||||
"""Connect to a SHS server"""
|
||||
|
||||
self.is_connected = True
|
||||
|
||||
for cb in self._on_connect:
|
||||
await cb()
|
||||
await cb(self)
|
||||
|
||||
|
||||
class MockSHSServer(MockSHSSocket):
|
||||
"""A mocked SHS server"""
|
||||
|
||||
def listen(self):
|
||||
def listen(self) -> None:
|
||||
"""Listen for new connections"""
|
||||
|
||||
self.is_connected = True
|
||||
|
||||
for cb in self._on_connect:
|
||||
ensure_future(cb())
|
||||
ensure_future(cb(self))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ps_client(event_loop): # pylint: disable=unused-argument
|
||||
def ps_client(event_loop: AbstractEventLoop) -> MockSHSClient: # pylint: disable=unused-argument
|
||||
"""Fixture to provide a mocked SHS client"""
|
||||
|
||||
return MockSHSClient()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ps_server(event_loop): # pylint: disable=unused-argument
|
||||
def ps_server(event_loop: AbstractEventLoop) -> MockSHSServer: # pylint: disable=unused-argument
|
||||
"""Fixture to provide a mocked SHS server"""
|
||||
|
||||
return MockSHSServer()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_on_connect(ps_server): # pylint: disable=redefined-outer-name
|
||||
async def test_on_connect(ps_server: MockSHSServer) -> None: # pylint: disable=redefined-outer-name
|
||||
"""Test the on_connect callback functionality"""
|
||||
|
||||
called = Event()
|
||||
|
||||
async def _on_connect():
|
||||
async def _on_connect(_: SHSDuplexStream) -> None:
|
||||
called.set()
|
||||
|
||||
ps_server.on_connect(_on_connect)
|
||||
@@ -137,7 +144,7 @@ async def test_on_connect(ps_server): # pylint: disable=redefined-outer-name
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_decoding(ps_client): # pylint: disable=redefined-outer-name
|
||||
async def test_message_decoding(ps_client: MockSHSClient) -> None: # pylint: disable=redefined-outer-name
|
||||
"""Test message decoding"""
|
||||
|
||||
await ps_client.connect()
|
||||
@@ -167,7 +174,7 @@ async def test_message_decoding(ps_client): # pylint: disable=redefined-outer-n
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_encoding(ps_client): # pylint: disable=redefined-outer-name
|
||||
async def test_message_encoding(ps_client: MockSHSClient) -> None: # pylint: disable=redefined-outer-name
|
||||
"""Test message encoding"""
|
||||
|
||||
await ps_client.connect()
|
||||
@@ -200,7 +207,7 @@ async def test_message_encoding(ps_client): # pylint: disable=redefined-outer-n
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_stream(ps_client, mocker): # pylint: disable=redefined-outer-name
|
||||
async def test_message_stream(ps_client: MockSHSClient, mocker: MockerFixture): # pylint: disable=redefined-outer-name
|
||||
"""Test requesting a history stream"""
|
||||
|
||||
await ps_client.connect()
|
||||
@@ -272,7 +279,9 @@ async def test_message_stream(ps_client, mocker): # pylint: disable=redefined-o
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_request(ps_server, mocker): # pylint: disable=redefined-outer-name
|
||||
async def test_message_request(
|
||||
ps_server: MockSHSServer, mocker: MockerFixture # pylint: disable=redefined-outer-name
|
||||
) -> None:
|
||||
"""Test message sending"""
|
||||
|
||||
ps_server.listen()
|
||||
|
@@ -20,7 +20,7 @@ CONFIG_FILE = """
|
||||
CONFIG_FILE_INVALID = CONFIG_FILE.replace("ed25519", "foo")
|
||||
|
||||
|
||||
def test_load_secret():
|
||||
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):
|
||||
@@ -33,8 +33,9 @@ def test_load_secret():
|
||||
assert bytes(secret["keypair"].verify_key) == b64decode("rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=")
|
||||
|
||||
|
||||
def test_load_exception():
|
||||
def test_load_exception() -> None:
|
||||
"""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