run black
This commit is contained in:
parent
a2157f7bc8
commit
80f778c031
33 changed files with 4040 additions and 3885 deletions
|
|
@ -14,6 +14,7 @@ import time
|
|||
|
||||
from tests.const import HOST, MQTT_PORT
|
||||
|
||||
|
||||
async def test_helperbot_message():
|
||||
mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd")
|
||||
await mqtt_server.broker_coro()
|
||||
|
|
@ -51,13 +52,11 @@ async def test_helperbot_message():
|
|||
mqtt_helperbot.Client._connected_state._value == True
|
||||
) # Check helperbot is connected
|
||||
msg_payload = "{}"
|
||||
msg_topic_name = (
|
||||
"iot/p2p/GetWKVer/helperbot/bumper/helperbot/bot_serial/ls1ok3/wC3g/q/iCmuqp/j"
|
||||
)
|
||||
msg_topic_name = "iot/p2p/GetWKVer/helperbot/bumper/helperbot/bot_serial/ls1ok3/wC3g/q/iCmuqp/j"
|
||||
await mqtt_helperbot.Client.publish(
|
||||
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||
)
|
||||
|
||||
|
||||
await asyncio.wait_for(mqtt_helperbot.Client.deliver_message(), timeout=0.1)
|
||||
|
||||
l.check_present(
|
||||
|
|
@ -77,9 +76,7 @@ async def test_helperbot_message():
|
|||
mqtt_helperbot.Client._connected_state._value == True
|
||||
) # Check helperbot is connected
|
||||
msg_payload = '{"ret":"ok","ver":"0.13.5"}'
|
||||
msg_topic_name = (
|
||||
"iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/iCmuqp/j"
|
||||
)
|
||||
msg_topic_name = "iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/iCmuqp/j"
|
||||
await mqtt_helperbot.Client.publish(
|
||||
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||
)
|
||||
|
|
@ -103,16 +100,13 @@ async def test_helperbot_message():
|
|||
mqtt_helperbot.Client._connected_state._value == True
|
||||
) # Check helperbot is connected
|
||||
msg_payload = "test"
|
||||
msg_topic_name = (
|
||||
"iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/TESTBAD/bumper/helperbot/p/iCmuqp/j"
|
||||
)
|
||||
msg_topic_name = "iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/TESTBAD/bumper/helperbot/p/iCmuqp/j"
|
||||
await mqtt_helperbot.Client.publish(
|
||||
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||
)
|
||||
|
||||
await asyncio.wait_for(mqtt_helperbot.Client.deliver_message(), timeout=0.1)
|
||||
|
||||
|
||||
l.check_present(
|
||||
(
|
||||
"helperbot",
|
||||
|
|
@ -134,7 +128,7 @@ async def test_helperbot_message():
|
|||
await mqtt_helperbot.Client.publish(
|
||||
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||
)
|
||||
|
||||
|
||||
await asyncio.wait_for(mqtt_helperbot.Client.deliver_message(), timeout=0.1)
|
||||
|
||||
l.check_present(
|
||||
|
|
@ -156,7 +150,7 @@ async def test_helperbot_expire_message():
|
|||
|
||||
timeout = 0.1
|
||||
# Test broadcast message
|
||||
mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT,timeout)
|
||||
mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT, timeout)
|
||||
bumper.mqtt_helperbot = mqtt_helperbot
|
||||
await mqtt_helperbot.start_helper_bot()
|
||||
assert (
|
||||
|
|
@ -167,14 +161,13 @@ async def test_helperbot_expire_message():
|
|||
expire_msg_topic_name = "iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/testgood/j"
|
||||
currenttime = time.time()
|
||||
request_id = "ABC"
|
||||
data = {
|
||||
"time": currenttime,
|
||||
"topic": expire_msg_topic_name,
|
||||
"payload": expire_msg_payload,
|
||||
}
|
||||
data = {
|
||||
"time": currenttime,
|
||||
"topic": expire_msg_topic_name,
|
||||
"payload": expire_msg_payload,
|
||||
}
|
||||
|
||||
|
||||
mqtt_helperbot.commands[request_id]= data
|
||||
mqtt_helperbot.commands[request_id] = data
|
||||
|
||||
assert mqtt_helperbot.commands[request_id] == data
|
||||
|
||||
|
|
@ -185,13 +178,12 @@ async def test_helperbot_expire_message():
|
|||
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||
) # Send another message to force get_msg
|
||||
|
||||
await asyncio.sleep(timeout*2)
|
||||
await asyncio.sleep(timeout * 2)
|
||||
|
||||
assert mqtt_helperbot.commands.get(request_id, None) == None
|
||||
|
||||
await mqtt_helperbot.Client.disconnect()
|
||||
await mqtt_server.broker.shutdown()
|
||||
|
||||
|
||||
|
||||
async def test_helperbot_sendcommand():
|
||||
|
|
@ -233,9 +225,7 @@ async def test_helperbot_sendcommand():
|
|||
|
||||
# Send response beforehand
|
||||
msg_payload = '{"ret":"ok","ver":"0.13.5"}'
|
||||
msg_topic_name = (
|
||||
"iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/testgood/j"
|
||||
)
|
||||
msg_topic_name = "iot/p2p/GetWKVer/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/testgood/j"
|
||||
await mqtt_helperbot.Client.publish(
|
||||
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||
)
|
||||
|
|
@ -247,7 +237,7 @@ async def test_helperbot_sendcommand():
|
|||
"ret": "ok",
|
||||
}
|
||||
|
||||
#mqtt_helperbot.Client.disconnect()
|
||||
# mqtt_helperbot.Client.disconnect()
|
||||
|
||||
# Test GetLifeSpan (xml command)
|
||||
cmdjson = {
|
||||
|
|
@ -269,9 +259,7 @@ async def test_helperbot_sendcommand():
|
|||
|
||||
# Send response beforehand
|
||||
msg_payload = "<ctl ret='ok' type='Brush' left='4142' total='18000'/>"
|
||||
msg_topic_name = (
|
||||
"iot/p2p/GetLifeSpan/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/testx/q"
|
||||
)
|
||||
msg_topic_name = "iot/p2p/GetLifeSpan/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/testx/q"
|
||||
await mqtt_helperbot.Client.publish(
|
||||
msg_topic_name, msg_payload.encode(), hbmqtt.client.QOS_0
|
||||
)
|
||||
|
|
@ -289,12 +277,7 @@ async def test_helperbot_sendcommand():
|
|||
"payloadType": "j",
|
||||
"toRes": "wC3g",
|
||||
"payload": {
|
||||
"header": {
|
||||
"pri": 1,
|
||||
"ts": "1569380075887",
|
||||
"tzm": -240,
|
||||
"ver": "0.0.50"
|
||||
}
|
||||
"header": {"pri": 1, "ts": "1569380075887", "tzm": -240, "ver": "0.0.50"}
|
||||
},
|
||||
"td": "q",
|
||||
"toId": "bot_serial",
|
||||
|
|
@ -310,7 +293,7 @@ async def test_helperbot_sendcommand():
|
|||
|
||||
# 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_topic_name = (
|
||||
"iot/p2p/getStats/bot_serial/ls1ok3/wC3g/helperbot/bumper/helperbot/p/testj/j"
|
||||
)
|
||||
|
|
@ -322,14 +305,33 @@ async def test_helperbot_sendcommand():
|
|||
|
||||
assert commandresult == {
|
||||
"id": "testj",
|
||||
"resp": {'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'}},
|
||||
"resp": {
|
||||
"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",
|
||||
},
|
||||
},
|
||||
"ret": "ok",
|
||||
}
|
||||
|
||||
mqtt_helperbot.Client.disconnect()
|
||||
|
||||
await mqtt_server.broker.shutdown()
|
||||
|
||||
|
||||
|
||||
async def test_mqttserver():
|
||||
|
|
@ -338,9 +340,10 @@ async def test_mqttserver():
|
|||
|
||||
bumper.db = "tests/tmp.db" # Set db location for testing
|
||||
|
||||
mqtt_server = bumper.MQTTServer(
|
||||
HOST, MQTT_PORT, 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()
|
||||
|
||||
# Test helperbot connect
|
||||
|
|
@ -387,13 +390,19 @@ async def test_mqttserver():
|
|||
test_client.client_id = "test-file-auth"
|
||||
# await test_client.start_helper_bot()
|
||||
test_client.Client = hbmqtt.client.MQTTClient(
|
||||
client_id=test_client.client_id, config={"check_hostname": False, "auto_reconnect": False, "reconnect_retries": 1}
|
||||
client_id=test_client.client_id,
|
||||
config={
|
||||
"check_hostname": False,
|
||||
"auto_reconnect": False,
|
||||
"reconnect_retries": 1,
|
||||
},
|
||||
)
|
||||
|
||||
# good user/pass
|
||||
await test_client.Client.connect(
|
||||
f"mqtts://test-client:abc123!@{HOST}:{MQTT_PORT}/",
|
||||
cafile=bumper.ca_cert, cleansession=True
|
||||
cafile=bumper.ca_cert,
|
||||
cleansession=True,
|
||||
)
|
||||
|
||||
assert (
|
||||
|
|
@ -403,42 +412,57 @@ async def test_mqttserver():
|
|||
assert (
|
||||
test_client.Client._connected_state._value == False
|
||||
) # Check client is disconnected
|
||||
|
||||
|
||||
# bad password
|
||||
with LogCapture() as l:
|
||||
|
||||
|
||||
await test_client.Client.connect(
|
||||
f"mqtts://test-client:notvalid!@{HOST}:{MQTT_PORT}/",
|
||||
cafile=bumper.ca_cert, cleansession=True
|
||||
cafile=bumper.ca_cert,
|
||||
cleansession=True,
|
||||
)
|
||||
|
||||
l.check_present(
|
||||
("mqttserver", "INFO", "File Authentication Failed - Username: test-client - ClientID: test-file-auth"),
|
||||
order_matters=False
|
||||
)
|
||||
# no username in file
|
||||
(
|
||||
"mqttserver",
|
||||
"INFO",
|
||||
"File Authentication Failed - Username: test-client - ClientID: test-file-auth",
|
||||
),
|
||||
order_matters=False,
|
||||
)
|
||||
# no username in file
|
||||
await test_client.Client.connect(
|
||||
f"mqtts://test-client-noexist:notvalid!@{HOST}:{MQTT_PORT}/",
|
||||
cafile=bumper.ca_cert, cleansession=True
|
||||
cafile=bumper.ca_cert,
|
||||
cleansession=True,
|
||||
)
|
||||
|
||||
|
||||
l.check_present(
|
||||
("mqttserver", "INFO", 'File Authentication Failed - No Entry for Username: test-client-noexist - ClientID: test-file-auth'),
|
||||
order_matters=False
|
||||
(
|
||||
"mqttserver",
|
||||
"INFO",
|
||||
"File Authentication Failed - No Entry for Username: test-client-noexist - ClientID: test-file-auth",
|
||||
),
|
||||
order_matters=False,
|
||||
)
|
||||
|
||||
|
||||
await mqtt_server.broker.shutdown()
|
||||
|
||||
|
||||
|
||||
async def test_nofileauth_mqttserver():
|
||||
with LogCapture() as l:
|
||||
|
||||
mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd-notfound")
|
||||
mqtt_server = bumper.MQTTServer(
|
||||
HOST, MQTT_PORT, password_file="tests/passwd-notfound"
|
||||
)
|
||||
await mqtt_server.broker_coro()
|
||||
await mqtt_server.broker.shutdown()
|
||||
await mqtt_server.broker.shutdown()
|
||||
|
||||
l.check_present(
|
||||
("hbmqtt.broker.plugins.bumper", "WARNING", 'Password file tests/passwd-notfound not found'),
|
||||
order_matters=False
|
||||
(
|
||||
"hbmqtt.broker.plugins.bumper",
|
||||
"WARNING",
|
||||
"Password file tests/passwd-notfound not found",
|
||||
),
|
||||
order_matters=False,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue