ci: Add and configure PyLint, and make it happy

This commit is contained in:
2023-11-01 06:03:06 +01:00
parent e0cd456e77
commit d51f27d883
13 changed files with 379 additions and 81 deletions

View File

@@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Tests for the feed functionality"""
from base64 import b64decode
from collections import OrderedDict
@@ -46,17 +48,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
@@ -65,6 +73,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
@@ -88,7 +98,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(
@@ -133,7 +145,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,
@@ -177,7 +191,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,
@@ -194,7 +210,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(
@@ -211,7 +229,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",

View File

@@ -20,10 +20,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Tests for the packet stream"""
from asyncio import Event, ensure_future, gather
import json
from nacl.signing import SigningKey
import pytest
from secret_handshake.network import SHSDuplexStream
@@ -58,63 +59,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 connections 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():
@@ -127,7 +159,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)
@@ -160,7 +194,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)
@@ -201,7 +237,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)
@@ -226,8 +264,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.AsyncMock()
mocker.patch.object(handler, "process", mock_process)
@@ -259,8 +297,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)
ps_client.feed(
@@ -286,7 +324,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)
@@ -300,8 +340,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.AsyncMock()
mocker.patch.object(handler, "process", mock_process)

View File

@@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Tests for the utility functions"""
from base64 import b64decode
from unittest.mock import mock_open, patch
@@ -41,6 +43,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()
@@ -52,6 +56,8 @@ 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()