fix tests

This commit is contained in:
Robert Resch 2022-01-30 11:41:56 +01:00
parent 04bd6b3ef0
commit 57f7490c92
8 changed files with 111 additions and 138 deletions

5
.gitignore vendored
View file

@ -7,10 +7,11 @@ __pycache__
!.dockerignore !.dockerignore
# Ignore items in test (report, cache, etc), except files starting with test # Ignore items in test (report, cache, etc), except files starting with test
tests/* tests/logs
tests/tmp.db
!tests/test* !tests/test*
!tests/test_certs !tests/test_certs
!tests/pytest.ini !pytest.ini
!tests/passwd !tests/passwd
!tests/passwd_bad !tests/passwd_bad

View file

@ -42,19 +42,23 @@ class CommandDto:
class MQTTHelperBot: class MQTTHelperBot:
Client = None Client = None
wait_resp_timeout_seconds = 60
def __init__(self, host: str, port: int): def __init__(self, host: str, port: int, timeout: float = 60):
self._commands: MutableMapping[str, CommandDto] = TTLCache(maxsize=self.wait_resp_timeout_seconds * 60, self._commands: MutableMapping[str, CommandDto] = TTLCache(maxsize=timeout * 60,
ttl=self.wait_resp_timeout_seconds + 10) ttl=timeout*1.1)
self._host = host self._host = host
self._port = port self._port = port
self.client_id = "helperbot@bumper/helperbot" self.client_id = "helperbot@bumper/helperbot"
self._timeout = timeout
@property @property
def commands(self) -> MutableMapping[str, CommandDto]: def commands(self) -> MutableMapping[str, CommandDto]:
return self._commands return self._commands
@property
def timeout(self)->float:
return self._timeout
async def start_helper_bot(self): async def start_helper_bot(self):
try: try:
if self.Client is None: if self.Client is None:
@ -74,7 +78,7 @@ class MQTTHelperBot:
async def _wait_for_resp(self, command_dto: CommandDto, request_id: str): async def _wait_for_resp(self, command_dto: CommandDto, request_id: str):
try: try:
payload = await asyncio.wait_for(command_dto.wait_for_response(), timeout=self.wait_resp_timeout_seconds) payload = await asyncio.wait_for(command_dto.wait_for_response(), timeout=self.timeout)
return { return {
"id": request_id, "id": request_id,
"ret": "ok", "ret": "ok",

View file

@ -3,3 +3,6 @@ env =
D:BUMPER_CA=tests/test_certs/ca.crt D:BUMPER_CA=tests/test_certs/ca.crt
D:BUMPER_CERT=tests/test_certs/bumper.crt D:BUMPER_CERT=tests/test_certs/bumper.crt
D:BUMPER_KEY=tests/test_certs/bumper.key D:BUMPER_KEY=tests/test_certs/bumper.key
#log_cli=true
#log_level=DEBUG

0
tests/__init__.py Normal file
View file

2
tests/const.py Normal file
View file

@ -0,0 +1,2 @@
HOST= "127.0.0.1"
MQTT_PORT = 8883

View file

@ -13,6 +13,8 @@ import logging
from testfixtures import LogCapture from testfixtures import LogCapture
from unittest.mock import MagicMock from unittest.mock import MagicMock
from tests.const import HOST, MQTT_PORT
def create_confserver(): def create_confserver():
return bumper.ConfServer("127.0.0.1:11111", False) return bumper.ConfServer("127.0.0.1:11111", False)
@ -36,14 +38,14 @@ def remove_existing_db():
async def test_confserver_ssl(): async def test_confserver_ssl():
conf_server = bumper.ConfServer(("127.0.0.1", 111111), usessl=True) conf_server = bumper.ConfServer((HOST, 111111), usessl=True)
conf_server.confserver_app() conf_server.confserver_app()
asyncio.create_task(conf_server.start_server()) asyncio.create_task(conf_server.start_server())
async def test_confserver_exceptions(): async def test_confserver_exceptions():
with LogCapture() as l: with LogCapture() as l:
conf_server = bumper.ConfServer(("127.0.0.1", 8007), usessl=True) conf_server = bumper.ConfServer((HOST, 8007), usessl=True)
conf_server.confserver_app() conf_server.confserver_app()
conf_server.site = web.TCPSite conf_server.site = web.TCPSite
@ -67,7 +69,7 @@ async def test_confserver_exceptions():
async def test_confserver_no_ssl(): async def test_confserver_no_ssl():
conf_server = bumper.ConfServer(("127.0.0.1", 111111), usessl=False) conf_server = bumper.ConfServer((HOST, 111111), usessl=False)
conf_server.confserver_app() conf_server.confserver_app()
asyncio.create_task(conf_server.start_server()) asyncio.create_task(conf_server.start_server())
@ -89,19 +91,18 @@ async def test_base(aiohttp_client):
bumper.db = "tests/tmp.db" # Set db location for testing bumper.db = "tests/tmp.db" # Set db location for testing
# Start MQTT # Start MQTT
mqtt_address = ("127.0.0.1", 8883) mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd")
mqtt_server = bumper.MQTTServer(mqtt_address, password_file="tests/passwd")
bumper.mqtt_server = mqtt_server bumper.mqtt_server = mqtt_server
await mqtt_server.broker_coro() await mqtt_server.broker_coro()
# Start XMPP # Start XMPP
xmpp_address = ("127.0.0.1", 5223) xmpp_address = (HOST, 5223)
xmpp_server = bumper.XMPPServer(xmpp_address) xmpp_server = bumper.XMPPServer(xmpp_address)
bumper.xmpp_server = xmpp_server bumper.xmpp_server = xmpp_server
await xmpp_server.start_async_server() await xmpp_server.start_async_server()
# Start Helperbot # Start Helperbot
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
bumper.mqtt_helperbot = mqtt_helperbot bumper.mqtt_helperbot = mqtt_helperbot
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
@ -121,19 +122,18 @@ async def test_restartService(aiohttp_client):
bumper.db = "tests/tmp.db" # Set db location for testing bumper.db = "tests/tmp.db" # Set db location for testing
# Start MQTT # Start MQTT
mqtt_address = ("127.0.0.1", 8883) mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd")
mqtt_server = bumper.MQTTServer(mqtt_address, password_file="tests/passwd")
bumper.mqtt_server = mqtt_server bumper.mqtt_server = mqtt_server
await mqtt_server.broker_coro() await mqtt_server.broker_coro()
# Start XMPP # Start XMPP
xmpp_address = ("127.0.0.1", 5223) xmpp_address = (HOST, 5223)
xmpp_server = bumper.XMPPServer(xmpp_address) xmpp_server = bumper.XMPPServer(xmpp_address)
bumper.xmpp_server = xmpp_server bumper.xmpp_server = xmpp_server
await xmpp_server.start_async_server() await xmpp_server.start_async_server()
# Start Helperbot # Start Helperbot
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
bumper.mqtt_helperbot = mqtt_helperbot bumper.mqtt_helperbot = mqtt_helperbot
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
@ -830,7 +830,7 @@ async def test_lg_logs(aiohttp_client):
bumper.bot_set_mqtt("did_1234", True) bumper.bot_set_mqtt("did_1234", True)
confserver = create_confserver() confserver = create_confserver()
client = await aiohttp_client(create_app) client = await aiohttp_client(create_app)
bumper.mqtt_helperbot = bumper.mqttserver.MQTTHelperBot("127.0.0.1") bumper.mqtt_helperbot = bumper.mqttserver.MQTTHelperBot(HOST, MQTT_PORT)
# Test return get status # Test return get status
command_getstatus_resp = { command_getstatus_resp = {
@ -889,7 +889,7 @@ async def test_devmgr(aiohttp_client):
bumper.db = "tests/tmp.db" # Set db location for testing bumper.db = "tests/tmp.db" # Set db location for testing
confserver = create_confserver() confserver = create_confserver()
client = await aiohttp_client(create_app) client = await aiohttp_client(create_app)
bumper.mqtt_helperbot = bumper.mqttserver.MQTTHelperBot("127.0.0.1") bumper.mqtt_helperbot = bumper.mqttserver.MQTTHelperBot(HOST, MQTT_PORT)
# Test PollSCResult # Test PollSCResult
postbody = {"td": "PollSCResult"} postbody = {"td": "PollSCResult"}
@ -945,7 +945,7 @@ async def test_dim_devmanager(aiohttp_client):
bumper.db = "tests/tmp.db" # Set db location for testing bumper.db = "tests/tmp.db" # Set db location for testing
confserver = create_confserver() confserver = create_confserver()
client = await aiohttp_client(create_app) client = await aiohttp_client(create_app)
bumper.mqtt_helperbot = bumper.mqttserver.MQTTHelperBot("127.0.0.1") bumper.mqtt_helperbot = bumper.mqttserver.MQTTHelperBot(HOST, MQTT_PORT)
# Test PollSCResult # Test PollSCResult
postbody = {"td": "PollSCResult"} postbody = {"td": "PollSCResult"}

View file

@ -12,16 +12,16 @@ import logging
from testfixtures import LogCapture from testfixtures import LogCapture
import time import time
from tests.const import HOST, MQTT_PORT
async def test_helperbot_message(): async def test_helperbot_message():
mqtt_address = ("127.0.0.1", 8883) mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd")
mqtt_server = bumper.MQTTServer(mqtt_address, password_file="tests/passwd")
await mqtt_server.broker_coro() await mqtt_server.broker_coro()
with LogCapture() as l: with LogCapture() as l:
# Test broadcast message # Test broadcast message
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
assert ( assert (
mqtt_helperbot.Client._connected_state._value == True mqtt_helperbot.Client._connected_state._value == True
@ -45,7 +45,7 @@ async def test_helperbot_message():
mqtt_helperbot.Client.disconnect() mqtt_helperbot.Client.disconnect()
# Send command to bot # Send command to bot
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
assert ( assert (
mqtt_helperbot.Client._connected_state._value == True mqtt_helperbot.Client._connected_state._value == True
@ -71,7 +71,7 @@ async def test_helperbot_message():
mqtt_helperbot.Client.disconnect() mqtt_helperbot.Client.disconnect()
# Received response to command # Received response to command
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
assert ( assert (
mqtt_helperbot.Client._connected_state._value == True mqtt_helperbot.Client._connected_state._value == True
@ -97,7 +97,7 @@ async def test_helperbot_message():
mqtt_helperbot.Client.disconnect() mqtt_helperbot.Client.disconnect()
# Received unknown message # Received unknown message
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
assert ( assert (
mqtt_helperbot.Client._connected_state._value == True mqtt_helperbot.Client._connected_state._value == True
@ -124,7 +124,7 @@ async def test_helperbot_message():
mqtt_helperbot.Client.disconnect() mqtt_helperbot.Client.disconnect()
# Received error message # Received error message
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
assert ( assert (
mqtt_helperbot.Client._connected_state._value == True mqtt_helperbot.Client._connected_state._value == True
@ -151,14 +151,12 @@ async def test_helperbot_message():
async def test_helperbot_expire_message(): async def test_helperbot_expire_message():
mqtt_address = ("127.0.0.1", 8883) mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd")
mqtt_server = bumper.MQTTServer(mqtt_address, password_file="tests/passwd")
await mqtt_server.broker_coro() await mqtt_server.broker_coro()
with LogCapture("helperbot") as l: timeout = 0.1
# Test broadcast message # Test broadcast message
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT,timeout)
bumper.mqtt_helperbot = mqtt_helperbot bumper.mqtt_helperbot = mqtt_helperbot
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
assert ( assert (
@ -168,61 +166,40 @@ async def test_helperbot_expire_message():
expire_msg_payload = '{"ret":"ok","ver":"0.13.5"}' expire_msg_payload = '{"ret":"ok","ver":"0.13.5"}'
expire_msg_topic_name = "iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/testgood/j" expire_msg_topic_name = "iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/testgood/j"
currenttime = time.time() currenttime = time.time()
mqtt_helperbot.command_responses.append( request_id = "ABC"
{ data = {
"time": currenttime, "time": currenttime,
"topic": expire_msg_topic_name, "topic": expire_msg_topic_name,
"payload": expire_msg_payload, "payload": expire_msg_payload,
} }
)
assert {
"time": currenttime, mqtt_helperbot.commands[request_id]= data
"topic": expire_msg_topic_name,
"payload": expire_msg_payload, assert mqtt_helperbot.commands[request_id] == data
} in mqtt_helperbot.command_responses # check message is in command_responses
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
mqtt_helperbot.expire_msg_seconds = (
0.1
) # Set expire message seconds to 0.1 so we don't wait 10 seconds
msg_payload = "<ctl ts='1547822804960' td='DustCaseST' st='0'/>" msg_payload = "<ctl ts='1547822804960' td='DustCaseST' st='0'/>"
msg_topic_name = "iot/atr/DustCaseST/bot_serial/ls1ok3/wC3g/x" msg_topic_name = "iot/atr/DustCaseST/bot_serial/ls1ok3/wC3g/x"
await mqtt_helperbot.Client.publish( await mqtt_helperbot.Client.publish(
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0 msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
) # Send another message to force get_msg ) # Send another message to force get_msg
await asyncio.sleep(timeout*2)
await asyncio.wait_for(mqtt_helperbot.Client.deliver_message(), timeout=0.1) assert mqtt_helperbot.commands.get(request_id, None) == None
assert {
"time": currenttime,
"topic": expire_msg_topic_name,
"payload": expire_msg_payload,
} not in mqtt_helperbot.command_responses # check message was expired and removed from command_responses
l.check_present(
(
"helperbot",
"DEBUG",
"Pruning Message Due To Expiration - Message Topic: {}".format(
expire_msg_topic_name
),
)
) # Check received message was logged
mqtt_helperbot.Client.disconnect()
await mqtt_helperbot.Client.disconnect()
await mqtt_server.broker.shutdown() await mqtt_server.broker.shutdown()
async def test_helperbot_sendcommand(): async def test_helperbot_sendcommand():
mqtt_address = ("127.0.0.1", 8883) mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd")
mqtt_server = bumper.MQTTServer(mqtt_address, password_file="tests/passwd")
await mqtt_server.broker_coro() await mqtt_server.broker_coro()
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) timeout = 0.1
mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT, timeout)
bumper.mqtt_helperbot = mqtt_helperbot bumper.mqtt_helperbot = mqtt_helperbot
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
assert ( assert (
@ -245,9 +222,6 @@ async def test_helperbot_sendcommand():
"realm": "ecouser.net", "realm": "ecouser.net",
}, },
} }
mqtt_helperbot.wait_resp_timeout_seconds = (
0.1
) # Override wait_resp_timeout (so we don't wait 10 seconds for timeout)
commandresult = await mqtt_helperbot.send_command(cmdjson, "testfail") commandresult = await mqtt_helperbot.send_command(cmdjson, "testfail")
# Don't send a response, ensure timeout # Don't send a response, ensure timeout
assert commandresult == { assert commandresult == {
@ -257,9 +231,6 @@ async def test_helperbot_sendcommand():
"ret": "fail", "ret": "fail",
} # Check timeout } # Check timeout
mqtt_helperbot.wait_resp_timeout_seconds = (
0.2
) # Override wait_resp_timeout (so we don't wait 10 seconds for timeout)
# Send response beforehand # Send response beforehand
msg_payload = '{"ret":"ok","ver":"0.13.5"}' msg_payload = '{"ret":"ok","ver":"0.13.5"}'
msg_topic_name = ( msg_topic_name = (
@ -296,9 +267,6 @@ async def test_helperbot_sendcommand():
}, },
} }
mqtt_helperbot.wait_resp_timeout_seconds = (
0.2
) # Override wait_resp_timeout (so we don't wait 10 seconds for timeout)
# Send response beforehand # Send response beforehand
msg_payload = "<ctl ret='ok' type='Brush' left='4142' total='18000'/>" msg_payload = "<ctl ret='ok' type='Brush' left='4142' total='18000'/>"
msg_topic_name = ( msg_topic_name = (
@ -340,9 +308,6 @@ async def test_helperbot_sendcommand():
}, },
} }
mqtt_helperbot.wait_resp_timeout_seconds = (
0.2
) # Override wait_resp_timeout (so we don't wait 10 seconds for timeout)
# Send response beforehand # Send response beforehand
msg_payload = '{"body":{"code":0,"data":{"area":0,"cid":"111","start":"1569378657","time":6,"type":"auto"},"msg":"ok"},"header":{"fwVer":"1.6.4","hwVer":"0.1.1","pri":1,"ts":"1569380074036","tzm":480,"ver":"0.0.1"}}' msg_payload = '{"body":{"code":0,"data":{"area":0,"cid":"111","start":"1569378657","time":6,"type":"auto"},"msg":"ok"},"header":{"fwVer":"1.6.4","hwVer":"0.1.1","pri":1,"ts":"1569380074036","tzm":480,"ver":"0.0.1"}}'
@ -373,14 +338,13 @@ async def test_mqttserver():
bumper.db = "tests/tmp.db" # Set db location for testing bumper.db = "tests/tmp.db" # Set db location for testing
mqtt_address = ("127.0.0.1", 8883)
mqtt_server = bumper.MQTTServer(mqtt_address, password_file="tests/passwd", allow_anonymous=True) mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd", allow_anonymous=True)
await mqtt_server.broker_coro() await mqtt_server.broker_coro()
# Test helperbot connect # Test helperbot connect
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address) mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
await mqtt_helperbot.start_helper_bot() await mqtt_helperbot.start_helper_bot()
assert ( assert (
mqtt_helperbot.Client._connected_state._value == True mqtt_helperbot.Client._connected_state._value == True
@ -390,7 +354,7 @@ async def test_mqttserver():
# Test client connect # Test client connect
bumper.user_add("user_123") # Add user to db bumper.user_add("user_123") # Add user to db
bumper.client_add("user_123", "ecouser.net", "resource_123") # Add client to db bumper.client_add("user_123", "ecouser.net", "resource_123") # Add client to db
test_client = bumper.MQTTHelperBot(mqtt_address) test_client = bumper.MQTTHelperBot(HOST, MQTT_PORT)
test_client.client_id = "user_123@ecouser.net/resource_123" test_client.client_id = "user_123@ecouser.net/resource_123"
# await test_client.start_helper_bot() # await test_client.start_helper_bot()
test_client.Client = hbmqtt.client.MQTTClient( test_client.Client = hbmqtt.client.MQTTClient(
@ -398,7 +362,7 @@ async def test_mqttserver():
) )
await test_client.Client.connect( await test_client.Client.connect(
f"mqtts://{test_client.address[0]}:{test_client.address[1]}/", f"mqtts://{HOST}:{MQTT_PORT}/",
cafile=bumper.ca_cert, cafile=bumper.ca_cert,
) )
assert ( assert (
@ -410,7 +374,7 @@ async def test_mqttserver():
) # Check client is disconnected ) # Check client is disconnected
# Test fake_bot connect # Test fake_bot connect
fake_bot = bumper.MQTTHelperBot(mqtt_address) fake_bot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
fake_bot.client_id = "bot_serial@ls1ok3/wC3g" fake_bot.client_id = "bot_serial@ls1ok3/wC3g"
await fake_bot.start_helper_bot() await fake_bot.start_helper_bot()
assert ( assert (
@ -419,7 +383,7 @@ async def test_mqttserver():
await fake_bot.Client.disconnect() await fake_bot.Client.disconnect()
# Test file auth client connect # Test file auth client connect
test_client = bumper.MQTTHelperBot(mqtt_address) test_client = bumper.MQTTHelperBot(HOST, MQTT_PORT)
test_client.client_id = "test-file-auth" test_client.client_id = "test-file-auth"
# await test_client.start_helper_bot() # await test_client.start_helper_bot()
test_client.Client = hbmqtt.client.MQTTClient( test_client.Client = hbmqtt.client.MQTTClient(
@ -428,7 +392,7 @@ async def test_mqttserver():
# good user/pass # good user/pass
await test_client.Client.connect( await test_client.Client.connect(
f"mqtts://test-client:abc123!@{test_client.address[0]}:{test_client.address[1]}/", f"mqtts://test-client:abc123!@{HOST}:{MQTT_PORT}/",
cafile=bumper.ca_cert, cleansession=True cafile=bumper.ca_cert, cleansession=True
) )
@ -444,7 +408,7 @@ async def test_mqttserver():
with LogCapture() as l: with LogCapture() as l:
await test_client.Client.connect( await test_client.Client.connect(
f"mqtts://test-client:notvalid!@{test_client.address[0]}:{test_client.address[1]}/", f"mqtts://test-client:notvalid!@{HOST}:{MQTT_PORT}/",
cafile=bumper.ca_cert, cleansession=True cafile=bumper.ca_cert, cleansession=True
) )
@ -454,7 +418,7 @@ async def test_mqttserver():
) )
# no username in file # no username in file
await test_client.Client.connect( await test_client.Client.connect(
f"mqtts://test-client-noexist:notvalid!@{test_client.address[0]}:{test_client.address[1]}/", f"mqtts://test-client-noexist:notvalid!@{HOST}:{MQTT_PORT}/",
cafile=bumper.ca_cert, cleansession=True cafile=bumper.ca_cert, cleansession=True
) )
@ -470,8 +434,7 @@ async def test_mqttserver():
async def test_nofileauth_mqttserver(): async def test_nofileauth_mqttserver():
with LogCapture() as l: with LogCapture() as l:
mqtt_address = ("127.0.0.1", 8883) mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd-notfound")
mqtt_server = bumper.MQTTServer(mqtt_address, password_file="tests/passwd-notfound")
await mqtt_server.broker_coro() await mqtt_server.broker_coro()
await mqtt_server.broker.shutdown() await mqtt_server.broker.shutdown()

View file

@ -73,12 +73,12 @@ async def test_client_connect_no_starttls(*args, **kwargs):
assert mock_send.call_count == 2 assert mock_send.call_count == 2
# Server opens stream # Server opens stream
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">' == '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">'
) )
# Server tells client available features # Server tells client available features
assert ( assert (
mock_send.mock_calls[1].args[0] mock_send.mock_calls[1][1][0]
== '<stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms></stream:features>' == '<stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms></stream:features>'
) )
@ -90,7 +90,7 @@ async def test_client_connect_no_starttls(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>' == '<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>'
) # Client successfully authenticated ) # Client successfully authenticated
assert xmppclient.state == xmppclient.INIT # Client moved to INIT state assert xmppclient.state == xmppclient.INIT # Client moved to INIT state
@ -111,7 +111,7 @@ async def test_client_end_stream(*args, **kwargs):
# Expect 2 calls to send # Expect 2 calls to send
assert mock_send.call_count == 1 assert mock_send.call_count == 1
# Server opens stream # Server opens stream
assert mock_send.mock_calls[0].args[0] == "</stream:stream>" assert mock_send.mock_calls[0][1][0] == "</stream:stream>"
# Reset mock calls # Reset mock calls
mock_send.reset_mock() mock_send.reset_mock()
@ -144,12 +144,12 @@ async def test_client_connect_starttls_called(*args, **kwargs):
assert mock_send.call_count == 2 assert mock_send.call_count == 2
# Server opens stream # Server opens stream
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">' == '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">'
) )
# Server tells client available features # Server tells client available features
assert ( assert (
mock_send.mock_calls[1].args[0] mock_send.mock_calls[1][1][0]
== '<stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms></stream:features>' == '<stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms></stream:features>'
) )
@ -175,12 +175,12 @@ async def test_client_connect_starttls_called(*args, **kwargs):
assert mock_send.call_count == 2 assert mock_send.call_count == 2
# Server opens stream # Server opens stream
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">' == '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">'
) )
# Server tells client available features (without STARTTLS) # Server tells client available features (without STARTTLS)
assert ( assert (
mock_send.mock_calls[1].args[0] mock_send.mock_calls[1][1][0]
== '<stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms></stream:features>' == '<stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms></stream:features>'
) )
# Reset mock calls # Reset mock calls
@ -191,7 +191,7 @@ async def test_client_connect_starttls_called(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>' == '<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>'
) # Client successfully authenticated ) # Client successfully authenticated
assert xmppclient.state == xmppclient.INIT # Client moved to INIT state assert xmppclient.state == xmppclient.INIT # Client moved to INIT state
@ -274,12 +274,12 @@ async def test_client_init(*args, **kwargs):
assert mock_send.call_count == 2 assert mock_send.call_count == 2
# Server opens stream # Server opens stream
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">' == '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">'
) )
# Server tells client binds # Server tells client binds
assert ( assert (
mock_send.mock_calls[1].args[0] mock_send.mock_calls[1][1][0]
== '<stream:features><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features>' == '<stream:features><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features>'
) )
@ -291,7 +291,7 @@ async def test_client_init(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq type="result" id="5E9872D5-547E-49AF-AE51-9EFAA282F952"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>fuid_tmpuser@ecouser.net/IOSF53D07BA</jid></bind></iq>' == '<iq type="result" id="5E9872D5-547E-49AF-AE51-9EFAA282F952"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>fuid_tmpuser@ecouser.net/IOSF53D07BA</jid></bind></iq>'
) # client successfully binded ) # client successfully binded
assert xmppclient.state == xmppclient.BIND # client moved to BIND state assert xmppclient.state == xmppclient.BIND # client moved to BIND state
@ -305,7 +305,7 @@ async def test_client_init(*args, **kwargs):
assert xmppclient.state == xmppclient.READY # client moved to READY state assert xmppclient.state == xmppclient.READY # client moved to READY state
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq type="result" id="FA1041E7-AA27-43DD-BAA3-64DE2DE56AA3" />' == '<iq type="result" id="FA1041E7-AA27-43DD-BAA3-64DE2DE56AA3" />'
) # client ready ) # client ready
@ -317,7 +317,7 @@ async def test_client_init(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<presence to="fuid_tmpuser@ecouser.net/IOSF53D07BA"> dummy </presence>' == '<presence to="fuid_tmpuser@ecouser.net/IOSF53D07BA"> dummy </presence>'
) # client presence - dummy response ) # client presence - dummy response
@ -338,12 +338,12 @@ async def test_bot_connect(*args, **kwargs):
assert mock_send.call_count == 2 assert mock_send.call_count == 2
# Server opens stream # Server opens stream
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">' == '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">'
) )
# Server tells client available features # Server tells client available features
assert ( assert (
mock_send.mock_calls[1].args[0] mock_send.mock_calls[1][1][0]
== '<stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms></stream:features>' == '<stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>PLAIN</mechanism></mechanisms></stream:features>'
) )
@ -355,7 +355,7 @@ async def test_bot_connect(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>' == '<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>'
) # Bot successfully authenticated ) # Bot successfully authenticated
assert xmppclient.state == xmppclient.INIT # Bot moved to INIT state assert xmppclient.state == xmppclient.INIT # Bot moved to INIT state
@ -381,12 +381,12 @@ async def test_bot_init(*args, **kwargs):
assert mock_send.call_count == 2 assert mock_send.call_count == 2
# Server opens stream # Server opens stream
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">' == '<stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0" id="1" from="ecouser.net">'
) )
# Server tells client binds # Server tells client binds
assert ( assert (
mock_send.mock_calls[1].args[0] mock_send.mock_calls[1][1][0]
== '<stream:features><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features>' == '<stream:features><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features>'
) )
@ -398,7 +398,7 @@ async def test_bot_init(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq type="result" id="2521"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>E0000000000000001234@159.ecorobot.net/atom</jid></bind></iq>' == '<iq type="result" id="2521"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>E0000000000000001234@159.ecorobot.net/atom</jid></bind></iq>'
) # Bot successfully binded ) # Bot successfully binded
assert xmppclient.state == xmppclient.BIND # Bot moved to BIND state assert xmppclient.state == xmppclient.BIND # Bot moved to BIND state
@ -412,7 +412,7 @@ async def test_bot_init(*args, **kwargs):
assert xmppclient.state == xmppclient.READY # Bot moved to READY state assert xmppclient.state == xmppclient.READY # Bot moved to READY state
assert ( assert (
mock_send.mock_calls[0].args[0] == '<iq type="result" id="2522" />' mock_send.mock_calls[0][1][0] == '<iq type="result" id="2522" />'
) # Bot ready ) # Bot ready
# Reset mock calls # Reset mock calls
@ -423,7 +423,7 @@ async def test_bot_init(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<presence to="E0000000000000001234@159.ecorobot.net/atom"> dummy </presence>' == '<presence to="E0000000000000001234@159.ecorobot.net/atom"> dummy </presence>'
) # bot presence - dummy response ) # bot presence - dummy response
@ -443,7 +443,7 @@ async def test_ping_server(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq type="result" id="2542" from="159.ecorobot.net" />' == '<iq type="result" id="2542" from="159.ecorobot.net" />'
) # ping response ) # ping response
@ -475,7 +475,7 @@ async def test_ping_client_to_client(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send2.mock_calls[0].args[0] mock_send2.mock_calls[0][1][0]
== '<iq from="E0000000000000001234@159.ecorobot.net/atom" id="104934615" to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="get"><ping xmlns="urn:xmpp:ping" /></iq>' == '<iq from="E0000000000000001234@159.ecorobot.net/atom" id="104934615" to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="get"><ping xmlns="urn:xmpp:ping" /></iq>'
) # ping response ) # ping response
@ -484,7 +484,7 @@ async def test_ping_client_to_client(*args, **kwargs):
xmppclient2._parse_data(test_data) xmppclient2._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq from="fuid_tmpuser@ecouser.net/IOSF53D07BA" id="104934615" to="E0000000000000001234@159.ecorobot.net/atom" type="result" />' == '<iq from="fuid_tmpuser@ecouser.net/IOSF53D07BA" id="104934615" to="E0000000000000001234@159.ecorobot.net/atom" type="result" />'
) # ping response ) # ping response
@ -517,7 +517,7 @@ async def test_client_send_iq(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq type="error" id="EE0XQ-2"><error type="cancel" code="501"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>' == '<iq type="error" id="EE0XQ-2"><error type="cancel" code="501"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>'
) # feature not implemented response ) # feature not implemented response
@ -529,7 +529,7 @@ async def test_client_send_iq(*args, **kwargs):
xmppclient._parse_data(test_data) xmppclient._parse_data(test_data)
assert ( assert (
mock_send2.mock_calls[0].args[0] mock_send2.mock_calls[0][1][0]
== '<iq from="fuid_tmpuser@ecouser.net/IOSF53D07BA" id="7" to="E0000000000000001234@159.ecorobot.net/atom" type="set"><query xmlns="com:ctl"><ctl id="72107787" td="GetCleanState" /></query></iq>' == '<iq from="fuid_tmpuser@ecouser.net/IOSF53D07BA" id="7" to="E0000000000000001234@159.ecorobot.net/atom" type="set"><query xmlns="com:ctl"><ctl id="72107787" td="GetCleanState" /></query></iq>'
) # command was sent to bot ) # command was sent to bot
@ -541,7 +541,7 @@ async def test_client_send_iq(*args, **kwargs):
xmppclient2._parse_data(test_data) xmppclient2._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq from="E0000000000000001234@159.ecorobot.net/atom" id="2679" to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="set"><query xmlns="com:ctl"><ctl td="ChargeState"><charge h="0" r="a" type="Going" /></ctl></query></iq>' == '<iq from="E0000000000000001234@159.ecorobot.net/atom" id="2679" to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="set"><query xmlns="com:ctl"><ctl td="ChargeState"><charge h="0" r="a" type="Going" /></ctl></query></iq>'
) # result sent to client ) # result sent to client
@ -553,7 +553,7 @@ async def test_client_send_iq(*args, **kwargs):
xmppclient2._parse_data(test_data) xmppclient2._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq from="E0000000000000001234@159.ecorobot.net/atom" id="s2c1" to="ecouser.net" type="result" />' == '<iq from="E0000000000000001234@159.ecorobot.net/atom" id="s2c1" to="ecouser.net" type="result" />'
) # result sent to ecouser.net ) # result sent to ecouser.net
@ -565,7 +565,7 @@ async def test_client_send_iq(*args, **kwargs):
xmppclient2._parse_data(test_data) xmppclient2._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq from="E0000000000000001234@159.ecorobot.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 from="E0000000000000001234@159.ecorobot.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>'
) # result sent to ecouser.net ) # result sent to ecouser.net
@ -577,7 +577,7 @@ async def test_client_send_iq(*args, **kwargs):
xmppclient2._parse_data(test_data) xmppclient2._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] mock_send.mock_calls[0][1][0]
== '<iq from="E0000000000000001234@159.ecorobot.net/atom" id="631" to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="set"><query xmlns="com:ctl"><ctl errs="102" td="error" /></query></iq>' == '<iq from="E0000000000000001234@159.ecorobot.net/atom" id="631" to="fuid_tmpuser@ecouser.net/IOSF53D07BA" type="set"><query xmlns="com:ctl"><ctl errs="102" td="error" /></query></iq>'
) # result sent to ecouser.net ) # result sent to ecouser.net
@ -588,7 +588,7 @@ async def test_client_send_iq(*args, **kwargs):
test_data = b"<iq to='rl.ecorobot.net' type='set' id='1234'><query xmlns='com:sf'><sf td='pub' t='log' ts='1559893796000' tp='p' k='DeviceAlert' v='DorpError' f='E0000000000000001234@159.ecorobot.net' g='fuid_tmpuser@ecouser.net'/></query></iq>" test_data = b"<iq to='rl.ecorobot.net' type='set' id='1234'><query xmlns='com:sf'><sf td='pub' t='log' ts='1559893796000' tp='p' k='DeviceAlert' v='DorpError' f='E0000000000000001234@159.ecorobot.net' g='fuid_tmpuser@ecouser.net'/></query></iq>"
xmppclient2._parse_data(test_data) xmppclient2._parse_data(test_data)
assert ( assert (
mock_send.mock_calls[0].args[0] 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="E0000000000000001234@159.ecorobot.net" g="fuid_tmpuser@ecouser.net" k="DeviceAlert" t="log" td="pub" tp="p" ts="1559893796000" v="DorpError" /></query></iq>' == '<iq xmlns="com:sf" from="E0000000000000001234@159.ecorobot.net/atom" id="1234" to="rl.ecorobot.net" type="set"><query xmlns="com:ctl"><sf f="E0000000000000001234@159.ecorobot.net" g="fuid_tmpuser@ecouser.net" k="DeviceAlert" t="log" td="pub" tp="p" ts="1559893796000" v="DorpError" /></query></iq>'
) # result sent to ecouser.net ) # result sent to ecouser.net