Skip to content

Commit

Permalink
update to python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus committed Feb 11, 2022
1 parent 1e58f8b commit f8045c3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- dev

env:
DEFAULT_PYTHON: 3.7
DEFAULT_PYTHON: 3.9

jobs:
code-quality:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ARG ARCH=amd64

FROM $ARCH/python:3.7-alpine
FROM $ARCH/python:3.9-alpine

EXPOSE 443
EXPOSE 5223
Expand Down
10 changes: 1 addition & 9 deletions bumper/confserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,9 @@ async def start_server(self):
)

await self.site.start()

except PermissionError as e:
confserverlog.error(e.strerror)
asyncio.create_task(bumper.shutdown())

except asyncio.CancelledError:
pass

except Exception as e:
confserverlog.exception(f"{e}")
asyncio.create_task(bumper.shutdown())
raise e

async def stop_server(self):
try:
Expand Down
10 changes: 1 addition & 9 deletions bumper/xmppserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,9 @@ async def start_async_server(self):

self.server_coro = loop.create_task(self.server.serve_forever())

except PermissionError as e:
xmppserverlog.error(e.strerror)
asyncio.create_task(bumper.shutdown())
pass

except asyncio.CancelledError:
pass

except Exception as e:
xmppserverlog.exception(f"{e}")
asyncio.create_task(bumper.shutdown())
raise e

def disconnect(self):

Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.7
python_version = 3.9
show_error_codes = true
follow_imports = silent
ignore_missing_imports = true
Expand Down
41 changes: 3 additions & 38 deletions tests/test_confserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,48 +28,13 @@ def remove_existing_db():


async def test_confserver_ssl():
conf_server = bumper.ConfServer((HOST, 111111), usessl=True)
conf_server = bumper.ConfServer((HOST, 11111), usessl=True)
conf_server.confserver_app()
asyncio.create_task(conf_server.start_server())


async def test_confserver_exceptions():
with LogCapture() as l:

conf_server = bumper.ConfServer((HOST, 8007), usessl=True)
conf_server.confserver_app()
conf_server.site = web.TCPSite

# bind permission
conf_server.site.start = mock.Mock(
side_effect=OSError(
1,
"error while attempting to bind on address ('127.0.0.1', 8007): permission denied",
)
)
await conf_server.start_server()

# asyncio Cancel
conf_server.site = web.TCPSite
conf_server.site.start = mock.Mock(side_effect=asyncio.CancelledError)
await conf_server.start_server()

# general exception
conf_server.site = web.TCPSite
conf_server.site.start = mock.Mock(side_effect=Exception(1, "general"))
await conf_server.start_server()

l.check_present(
(
"confserver",
"ERROR",
"error while attempting to bind on address ('127.0.0.1', 8007): permission denied",
)
)
await conf_server.start_server()


async def test_confserver_no_ssl():
conf_server = bumper.ConfServer((HOST, 111111), usessl=False)
conf_server = bumper.ConfServer((HOST, 11112), usessl=False)
conf_server.confserver_app()
await conf_server.start_server()

Expand Down
113 changes: 28 additions & 85 deletions tests/test_xmppserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import bumper


def return_send_data(data, *args, **kwargs):
def return_send_data(data):
return data


def mock_transport_extra_info(*args, **kwargs):
def mock_transport_extra_info():
return ("127.0.0.1", 5223)


Expand Down Expand Up @@ -59,8 +59,8 @@ async def test_xmpp_server():
xmpp_server.disconnect()


async def test_client_connect_no_starttls(*args, **kwargs):
test_transport = asyncio.Transport()
async def test_client_connect_no_starttls():
test_transport = mock.Mock()
test_transport.get_extra_info = mock.Mock(return_value=mock_transport_extra_info())
test_transport.write = mock.Mock(return_value=return_send_data)
xmppclient = bumper.xmppserver.XMPPAsyncClient(test_transport)
Expand Down Expand Up @@ -98,8 +98,8 @@ async def test_client_connect_no_starttls(*args, **kwargs):
assert xmppclient.state == xmppclient.INIT # Client moved to INIT state


async def test_client_end_stream(*args, **kwargs):
test_transport = asyncio.Transport()
async def test_client_end_stream():
test_transport = mock.Mock()
test_transport.get_extra_info = mock.Mock(return_value=mock_transport_extra_info())
test_transport.write = mock.Mock(return_value=return_send_data)
xmppclient = bumper.xmppserver.XMPPAsyncClient(test_transport)
Expand Down Expand Up @@ -130,8 +130,8 @@ async def test_client_end_stream(*args, **kwargs):
xmppclient._parse_data(test_data)


async def test_client_connect_starttls_called(*args, **kwargs):
test_transport = asyncio.Transport()
async def test_client_connect_starttls_called():
test_transport = mock.Mock()
test_transport.get_extra_info = mock.Mock(return_value=mock_transport_extra_info())
test_transport.write = mock.Mock(return_value=return_send_data)
xmppclient = bumper.xmppserver.XMPPAsyncClient(test_transport)
Expand Down Expand Up @@ -199,65 +199,9 @@ async def test_client_connect_starttls_called(*args, **kwargs):
assert xmppclient.state == xmppclient.INIT # Client moved to INIT state


async def test_xmpp_server_client_tls():

xmpp_address = ("127.0.0.1", 5223)
xmpp_server = bumper.XMPPServer(xmpp_address)
await xmpp_server.start_async_server()

with LogCapture("xmppserver") as l:

async def do_stuff_after_start_tls(
ssl_reader, ssl_writer
): # Used after starttls

writer.write(
b"<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' to='ecouser.net'>"
) # New Stream

await writer.drain()

writer.write(
b'<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AGZ1aWRfdG1wdXNlcgAwL0lPU0Y1M0QwN0JBL3VzXzg5ODgwMmZkYmM0NDQxYjBiYzgxNWIxZDFjNjgzMDJl</auth>'
) # Send Auth

await writer.drain()

reader, writer = await asyncio.open_connection("127.0.0.1", 5223)

writer.write(
b"<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' to='ecouser.net'>"
) # Start stream
await writer.drain()

await asyncio.sleep(0.1)

writer.write(
b"<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"
) # Send StartTLS
await writer.drain()

await asyncio.sleep(0.1)

# Below will upgrade connection to TLS then callback to "do_stuff_after_start_tls"
ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ssl_context.check_hostname = False
ssl_context.load_verify_locations(cafile=bumper.ca_cert)
loop = asyncio.get_event_loop()
transport = writer.transport
protocol = writer.transport.get_protocol()
new_transport = await loop.start_tls(
transport, protocol, ssl_context, server_side=False
)
protocol._stream_reader = asyncio.StreamReader(loop=loop)
protocol._client_connected_cb = do_stuff_after_start_tls
protocol.connection_made(new_transport)

print(l)


async def test_client_init(*args, **kwargs):
test_transport = asyncio.Transport()
async def test_client_init():
test_transport = mock.Mock()
test_transport.get_extra_info = mock.Mock(return_value=mock_transport_extra_info())
test_transport.write = mock.Mock(return_value=return_send_data)
xmppclient = bumper.xmppserver.XMPPAsyncClient(test_transport)
Expand Down Expand Up @@ -324,8 +268,8 @@ async def test_client_init(*args, **kwargs):
) # client presence - dummy response


async def test_bot_connect(*args, **kwargs):
test_transport = asyncio.Transport()
async def test_bot_connect():
test_transport = mock.Mock()
test_transport.get_extra_info = mock.Mock(return_value=mock_transport_extra_info())
test_transport.write = mock.Mock(return_value=return_send_data)
xmppclient = bumper.xmppserver.XMPPAsyncClient(test_transport)
Expand Down Expand Up @@ -364,8 +308,8 @@ async def test_bot_connect(*args, **kwargs):
assert xmppclient.type == xmppclient.BOT # Client type is now bot


async def test_bot_init(*args, **kwargs):
test_transport = asyncio.Transport()
async def test_bot_init():
test_transport = mock.Mock()
test_transport.get_extra_info = mock.Mock(return_value=mock_transport_extra_info())
test_transport.write = mock.Mock(return_value=return_send_data)
xmppclient = bumper.xmppserver.XMPPAsyncClient(test_transport)
Expand Down Expand Up @@ -430,8 +374,8 @@ async def test_bot_init(*args, **kwargs):
) # bot presence - dummy response


async def test_ping_server(*args, **kwargs):
test_transport = asyncio.Transport()
async def test_ping_server():
test_transport = mock.Mock()
test_transport.get_extra_info = mock.Mock(return_value=mock_transport_extra_info())
test_transport.write = mock.Mock(return_value=return_send_data)
xmppclient = bumper.xmppserver.XMPPAsyncClient(test_transport)
Expand All @@ -450,9 +394,8 @@ async def test_ping_server(*args, **kwargs):
) # ping response


async def test_ping_client_to_client(*args, **kwargs):

test_transport = asyncio.Transport()
async def test_ping_client_to_client():
test_transport = mock.Mock()
test_transport.get_extra_info = mock.Mock(return_value=mock_transport_extra_info())
test_transport.write = mock.Mock(return_value=return_send_data)
xmppclient = bumper.xmppserver.XMPPAsyncClient(test_transport)
Expand All @@ -478,7 +421,7 @@ async def test_ping_client_to_client(*args, **kwargs):

assert (
mock_send2.mock_calls[0][1][0]
== '<iq from="[email protected]/atom" id="104934615" to="[email protected]/IOSF53D07BA" type="get"><ping xmlns="urn:xmpp:ping" /></iq>'
== '<iq id="104934615" to="[email protected]/IOSF53D07BA" type="get" from="[email protected]/atom"><ping xmlns="urn:xmpp:ping" /></iq>'
) # ping response

# Ping response from bot to user
Expand All @@ -487,12 +430,12 @@ async def test_ping_client_to_client(*args, **kwargs):

assert (
mock_send.mock_calls[0][1][0]
== '<iq from="[email protected]/IOSF53D07BA" id="104934615" to="[email protected]/atom" type="result" />'
== '<iq type="result" to="[email protected]/atom" id="104934615" from="[email protected]/IOSF53D07BA" />'
) # ping response


async def test_client_send_iq(*args, **kwargs):
test_transport = asyncio.Transport()
async def test_client_send_iq():
test_transport = mock.Mock()
test_transport.get_extra_info = mock.Mock(return_value=mock_transport_extra_info())
test_transport.write = mock.Mock(return_value=return_send_data)
xmppclient = bumper.xmppserver.XMPPAsyncClient(test_transport)
Expand Down Expand Up @@ -534,7 +477,7 @@ async def test_client_send_iq(*args, **kwargs):

assert (
mock_send2.mock_calls[0][1][0]
== '<iq from="[email protected]/IOSF53D07BA" id="7" to="[email protected]/atom" type="set"><query xmlns="com:ctl"><ctl id="72107787" td="GetCleanState" /></query></iq>'
== '<iq id="7" to="[email protected]/atom" type="set" from="[email protected]/IOSF53D07BA"><query xmlns="com:ctl"><ctl id="72107787" td="GetCleanState" /></query></iq>'
) # command was sent to bot

# Reset mock calls
Expand All @@ -546,7 +489,7 @@ async def test_client_send_iq(*args, **kwargs):

assert (
mock_send.mock_calls[0][1][0]
== '<iq from="[email protected]/atom" id="2679" to="[email protected]/IOSF53D07BA" type="set"><query xmlns="com:ctl"><ctl td="ChargeState"><charge h="0" r="a" type="Going" /></ctl></query></iq>'
== '<iq id="2679" to="[email protected]/IOSF53D07BA" type="set" from="[email protected]/atom"><query xmlns="com:ctl"><ctl td="ChargeState"><charge h="0" r="a" type="Going" /></ctl></query></iq>'
) # result sent to client

# Reset mock calls
Expand All @@ -558,7 +501,7 @@ async def test_client_send_iq(*args, **kwargs):

assert (
mock_send.mock_calls[0][1][0]
== '<iq from="[email protected]/atom" id="s2c1" to="ecouser.net" type="result" />'
== '<iq type="result" from="[email protected]/atom" to="ecouser.net" id="s2c1" />'
) # result sent to ecouser.net

# Reset mock calls
Expand All @@ -570,7 +513,7 @@ async def test_client_send_iq(*args, **kwargs):

assert (
mock_send.mock_calls[0][1][0]
== '<iq from="[email protected].net/atom" id="2700" to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="set"><query xmlns="com:ctl"><ctl td="BatteryInfo"><battery power="100" /></ctl></query></iq>'
== '<iq to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="set" id="2700" from="[email protected].net/atom"><query xmlns="com:ctl"><ctl td="BatteryInfo"><battery power="100" /></ctl></query></iq>'
) # result sent to ecouser.net

# Reset mock calls
Expand All @@ -582,7 +525,7 @@ async def test_client_send_iq(*args, **kwargs):

assert (
mock_send.mock_calls[0][1][0]
== '<iq from="[email protected].net/atom" id="631" to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="set"><query xmlns="com:ctl"><ctl errs="102" td="error" /></query></iq>'
== '<iq to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="set" id="631" from="[email protected].net/atom"><query xmlns="com:ctl"><ctl td="error" errs="102" /></query></iq>'
) # result sent to ecouser.net

# Reset mock calls
Expand All @@ -593,7 +536,7 @@ async def test_client_send_iq(*args, **kwargs):
xmppclient2._parse_data(test_data)
assert (
mock_send.mock_calls[0][1][0]
== '<iq xmlns="com:sf" from="E0000000000000001234@159.ecorobot.net/atom" id="1234" to="rl.ecorobot.net" type="set"><query xmlns="com:ctl"><sf f="[email protected]" g="[email protected]" k="DeviceAlert" t="log" td="pub" tp="p" ts="1559893796000" v="DorpError" /></query></iq>'
== ('<iq xmlns="com:sf" to="rl.ecorobot.net" type="set" id="1234" from="E0000000000000001234@159.ecorobot.net/atom"><query xmlns="com:ctl"><sf td="pub" t="log" ts="1559893796000" tp="p" k="DeviceAlert" v="DorpError" f="[email protected]" g="[email protected]" /></query></iq>')
) # result sent to ecouser.net

# Reset mock calls
Expand Down

0 comments on commit f8045c3

Please sign in to comment.