run black

This commit is contained in:
Robert Resch 2022-01-30 11:53:54 +01:00
parent a2157f7bc8
commit 80f778c031
33 changed files with 4040 additions and 3885 deletions

View file

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

View file

@ -42,29 +42,39 @@ async def test_confserver_ssl():
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
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()
# 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()
# 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()
#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")
(
"confserver",
"ERROR",
"error while attempting to bind on address ('127.0.0.1', 8007): permission denied",
)
)
@ -89,7 +99,7 @@ def test_get_milli_time():
async def test_base(aiohttp_client):
remove_existing_db()
bumper.db = "tests/tmp.db" # Set db location for testing
# Start MQTT
mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd")
bumper.mqtt_server = mqtt_server
@ -100,7 +110,7 @@ async def test_base(aiohttp_client):
xmpp_server = bumper.XMPPServer(xmpp_address)
bumper.xmpp_server = xmpp_server
await xmpp_server.start_async_server()
# Start Helperbot
mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
bumper.mqtt_helperbot = mqtt_helperbot
@ -108,7 +118,7 @@ async def test_base(aiohttp_client):
client = await aiohttp_client(create_app)
resp = await client.get("/")
assert resp.status == 200
assert resp.status == 200
mqtt_helperbot.Client.disconnect()
@ -120,7 +130,7 @@ async def test_base(aiohttp_client):
async def test_restartService(aiohttp_client):
remove_existing_db()
bumper.db = "tests/tmp.db" # Set db location for testing
# Start MQTT
mqtt_server = bumper.MQTTServer(HOST, MQTT_PORT, password_file="tests/passwd")
bumper.mqtt_server = mqtt_server
@ -131,37 +141,39 @@ async def test_restartService(aiohttp_client):
xmpp_server = bumper.XMPPServer(xmpp_address)
bumper.xmpp_server = xmpp_server
await xmpp_server.start_async_server()
# Start Helperbot
mqtt_helperbot = bumper.MQTTHelperBot(HOST, MQTT_PORT)
bumper.mqtt_helperbot = mqtt_helperbot
await mqtt_helperbot.start_helper_bot()
client = await aiohttp_client(create_app)
resp = await client.get("/restart_Helperbot")
assert resp.status == 200
assert resp.status == 200
resp = await client.get("/restart_MQTTServer")
assert resp.status == 200
assert resp.status == 200
resp = await client.get("/restart_XMPPServer")
assert resp.status == 200
assert resp.status == 200
mqtt_helperbot.Client.disconnect()
await mqtt_server.broker.shutdown()
xmpp_server.disconnect()
async def test_RemoveBot(aiohttp_client):
client = await aiohttp_client(create_app)
resp = await client.get("/bot/remove/test_did")
assert resp.status == 200
assert resp.status == 200
async def test_RemoveClient(aiohttp_client):
client = await aiohttp_client(create_app)
resp = await client.get("/client/remove/test_resource")
assert resp.status == 200
assert resp.status == 200
async def test_login(aiohttp_client):
@ -180,7 +192,7 @@ async def test_login(aiohttp_client):
assert "username" in jsonresp["data"]
remove_existing_db()
bumper.db = "tests/tmp.db" # Set db location for testing
bumper.db = "tests/tmp.db" # Set db location for testing
# Test global_e without user
resp = await client.get("/v1/private/us/en/dev_1234/global_e/1/0/0/user/login")
@ -216,11 +228,11 @@ async def test_login(aiohttp_client):
# Add a bot to db that doesn't have a did
newbot = {
"class": "dev_1234",
"company": "com_123",
#"did": self.did,
"name": "sn_1234",
"resource": "res_1234",
"class": "dev_1234",
"company": "com_123",
# "did": self.did,
"name": "sn_1234",
"resource": "res_1234",
}
bumper.bot_full_upsert(newbot)
@ -592,11 +604,10 @@ async def test_getProductIotMap(aiohttp_client):
jsonresp = json.loads(text)
assert jsonresp["code"] == bumper.RETURN_API_SUCCESS
# Test getPimFile
resp = await client.get("/api/pim/file/get/123")
assert resp.status == 200
async def test_getUsersAPI(aiohttp_client):
remove_existing_db()
@ -1006,6 +1017,3 @@ async def test_dim_devmanager(aiohttp_client):
text = await resp.text()
test_resp = json.loads(text)
assert test_resp["ret"] == "fail"

View file

@ -59,4 +59,3 @@ async def test_start_stop_debug():
("bumper", "INFO", "Shutting down"), ("bumper", "INFO", "Shutdown complete")
)
assert b.shutting_down == True

View file

@ -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,
)

View file

@ -513,7 +513,9 @@ async def test_client_send_iq(*args, **kwargs):
bumper.xmppserver.XMPPServer.clients.append(xmppclient2)
# Roster IQ - Only seen from Android app so far
test_data = b'<iq id="EE0XQ-2" type="get"><query xmlns="jabber:iq:roster" ></query></iq>'
test_data = (
b'<iq id="EE0XQ-2" type="get"><query xmlns="jabber:iq:roster" ></query></iq>'
)
xmppclient._parse_data(test_data)
assert (
@ -594,4 +596,3 @@ async def test_client_send_iq(*args, **kwargs):
# Reset mock calls
mock_send.reset_mock()