fix tests
- added kwargs for mqttserver init - passwd_file and allow_anonymous - removed unused tests and sleeps
This commit is contained in:
parent
cb693b5cf6
commit
6da98acdad
3 changed files with 58 additions and 171 deletions
|
|
@ -229,10 +229,24 @@ class MQTTServer:
|
||||||
#asyncio.create_task(bumper.shutdown())
|
#asyncio.create_task(bumper.shutdown())
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __init__(self, address):
|
def __init__(self, address, **kwargs):
|
||||||
try:
|
try:
|
||||||
self.address = address
|
self.address = address
|
||||||
|
|
||||||
|
# Default config opts
|
||||||
|
passwd_file = os.path.join(
|
||||||
|
os.path.join(bumper.data_dir, "passwd")
|
||||||
|
) # For file auth, set user:hash in passwd file see (https://hbmqtt.readthedocs.io/en/latest/references/hbmqtt.html#configuration-example)
|
||||||
|
|
||||||
|
allow_anon = False
|
||||||
|
|
||||||
|
for key, value in kwargs.items():
|
||||||
|
if key == "password_file":
|
||||||
|
passwd_file = kwargs["password_file"]
|
||||||
|
|
||||||
|
elif key == "allow_anonymous":
|
||||||
|
allow_anon = kwargs["allow_anonymous"] # Set to True to allow anonymous authentication
|
||||||
|
|
||||||
# The below adds a plugin to the hbmqtt.broker.plugins without having to futz with setup.py
|
# The below adds a plugin to the hbmqtt.broker.plugins without having to futz with setup.py
|
||||||
distribution = pkg_resources.Distribution("hbmqtt.broker.plugins")
|
distribution = pkg_resources.Distribution("hbmqtt.broker.plugins")
|
||||||
bumper_plugin = pkg_resources.EntryPoint.parse(
|
bumper_plugin = pkg_resources.EntryPoint.parse(
|
||||||
|
|
@ -254,10 +268,8 @@ class MQTTServer:
|
||||||
},
|
},
|
||||||
"sys_interval": 0,
|
"sys_interval": 0,
|
||||||
"auth": {
|
"auth": {
|
||||||
"allow-anonymous": False, # Set to True to allow anonymous authentication
|
"allow-anonymous": allow_anon,
|
||||||
"password-file": os.path.join(
|
"password-file": passwd_file,
|
||||||
os.path.join(bumper.data_dir, "passwd")
|
|
||||||
), # For file auth, set user:hash in passwd file see (https://hbmqtt.readthedocs.io/en/latest/references/hbmqtt.html#configuration-example)
|
|
||||||
"plugins": ["bumper"], # Bumper plugin provides auth and handling of bots/clients connecting
|
"plugins": ["bumper"], # Bumper plugin provides auth and handling of bots/clients connecting
|
||||||
},
|
},
|
||||||
"topic-check": {"enabled": False},
|
"topic-check": {"enabled": False},
|
||||||
|
|
@ -385,8 +397,6 @@ class BumperMQTTServer_Plugin:
|
||||||
self.context.logger.debug(f"{(len(self._users))} user(s) read from file {password_file}")
|
self.context.logger.debug(f"{(len(self._users))} user(s) read from file {password_file}")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
self.context.logger.warning(f"Password file {password_file} not found")
|
self.context.logger.warning(f"Password file {password_file} not found")
|
||||||
else:
|
|
||||||
self.context.logger.debug("Configuration parameter 'password_file' not found")
|
|
||||||
|
|
||||||
async def on_broker_client_connected(self, client_id):
|
async def on_broker_client_connected(self, client_id):
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ async def test_base(aiohttp_client):
|
||||||
|
|
||||||
# Start MQTT
|
# Start MQTT
|
||||||
mqtt_address = ("127.0.0.1", 8883)
|
mqtt_address = ("127.0.0.1", 8883)
|
||||||
mqtt_server = bumper.MQTTServer(mqtt_address)
|
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()
|
||||||
|
|
||||||
|
|
@ -84,7 +84,6 @@ async def test_base(aiohttp_client):
|
||||||
mqtt_helperbot.Client.disconnect()
|
mqtt_helperbot.Client.disconnect()
|
||||||
|
|
||||||
await mqtt_server.broker.shutdown()
|
await mqtt_server.broker.shutdown()
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
bumper.xmpp_server.disconnect()
|
bumper.xmpp_server.disconnect()
|
||||||
|
|
||||||
|
|
@ -95,7 +94,7 @@ async def test_restartService(aiohttp_client):
|
||||||
|
|
||||||
# Start MQTT
|
# Start MQTT
|
||||||
mqtt_address = ("127.0.0.1", 8883)
|
mqtt_address = ("127.0.0.1", 8883)
|
||||||
mqtt_server = bumper.MQTTServer(mqtt_address)
|
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()
|
||||||
|
|
||||||
|
|
@ -123,7 +122,6 @@ async def test_restartService(aiohttp_client):
|
||||||
|
|
||||||
mqtt_helperbot.Client.disconnect()
|
mqtt_helperbot.Client.disconnect()
|
||||||
await mqtt_server.broker.shutdown()
|
await mqtt_server.broker.shutdown()
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
xmpp_server.disconnect()
|
xmpp_server.disconnect()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ import time
|
||||||
|
|
||||||
async def test_helperbot_message():
|
async def test_helperbot_message():
|
||||||
mqtt_address = ("127.0.0.1", 8883)
|
mqtt_address = ("127.0.0.1", 8883)
|
||||||
mqtt_server = bumper.MQTTServer(mqtt_address)
|
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:
|
||||||
|
|
@ -154,12 +154,11 @@ async def test_helperbot_message():
|
||||||
mqtt_helperbot.Client.disconnect()
|
mqtt_helperbot.Client.disconnect()
|
||||||
|
|
||||||
await mqtt_server.broker.shutdown()
|
await mqtt_server.broker.shutdown()
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_helperbot_expire_message():
|
async def test_helperbot_expire_message():
|
||||||
mqtt_address = ("127.0.0.1", 8883)
|
mqtt_address = ("127.0.0.1", 8883)
|
||||||
mqtt_server = bumper.MQTTServer(mqtt_address)
|
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:
|
with LogCapture("helperbot") as l:
|
||||||
|
|
@ -188,7 +187,7 @@ async def test_helperbot_expire_message():
|
||||||
"payload": expire_msg_payload,
|
"payload": expire_msg_payload,
|
||||||
} in mqtt_helperbot.command_responses # check message is in command_responses
|
} in mqtt_helperbot.command_responses # check message is in command_responses
|
||||||
|
|
||||||
await asyncio.sleep(0.2)
|
await asyncio.sleep(0.1)
|
||||||
mqtt_helperbot.expire_msg_seconds = (
|
mqtt_helperbot.expire_msg_seconds = (
|
||||||
0.1
|
0.1
|
||||||
) # Set expire message seconds to 0.1 so we don't wait 10 seconds
|
) # Set expire message seconds to 0.1 so we don't wait 10 seconds
|
||||||
|
|
@ -221,12 +220,12 @@ async def test_helperbot_expire_message():
|
||||||
mqtt_helperbot.Client.disconnect()
|
mqtt_helperbot.Client.disconnect()
|
||||||
|
|
||||||
await mqtt_server.broker.shutdown()
|
await mqtt_server.broker.shutdown()
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_helperbot_sendcommand():
|
async def test_helperbot_sendcommand():
|
||||||
mqtt_address = ("127.0.0.1", 8883)
|
mqtt_address = ("127.0.0.1", 8883)
|
||||||
mqtt_server = bumper.MQTTServer(mqtt_address)
|
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)
|
mqtt_helperbot = bumper.MQTTHelperBot(mqtt_address)
|
||||||
|
|
@ -370,7 +369,7 @@ async def test_helperbot_sendcommand():
|
||||||
mqtt_helperbot.Client.disconnect()
|
mqtt_helperbot.Client.disconnect()
|
||||||
|
|
||||||
await mqtt_server.broker.shutdown()
|
await mqtt_server.broker.shutdown()
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_mqttserver():
|
async def test_mqttserver():
|
||||||
|
|
@ -381,26 +380,7 @@ async def test_mqttserver():
|
||||||
|
|
||||||
mqtt_address = ("127.0.0.1", 8883)
|
mqtt_address = ("127.0.0.1", 8883)
|
||||||
|
|
||||||
mqtt_server = bumper.MQTTServer(mqtt_address)
|
mqtt_server = bumper.MQTTServer(mqtt_address, password_file="tests/passwd", allow_anonymous=True)
|
||||||
|
|
||||||
mqtt_server.default_config = {
|
|
||||||
"listeners": {
|
|
||||||
"default": {"type": "tcp"},
|
|
||||||
"tls1": {
|
|
||||||
"bind": "{}:{}".format(mqtt_address[0], mqtt_address[1]),
|
|
||||||
"ssl": "on",
|
|
||||||
"certfile": bumper.server_cert,
|
|
||||||
"keyfile": bumper.server_key,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"sys_interval": 10,
|
|
||||||
"auth": {
|
|
||||||
"allow-anonymous": True, # Set to True to allow anonymous authentication
|
|
||||||
"password-file": "tests/passwd", # For file auth, set user:hash in passwd file see (https://hbmqtt.readthedocs.io/en/latest/references/hbmqtt.html#configuration-example)
|
|
||||||
"plugins": ["bumper"], # Bumper plugin provides auth and handling of bots/clients connecting
|
|
||||||
},
|
|
||||||
"topic-check": {"enabled": False},
|
|
||||||
}
|
|
||||||
|
|
||||||
await mqtt_server.broker_coro()
|
await mqtt_server.broker_coro()
|
||||||
|
|
||||||
|
|
@ -466,148 +446,47 @@ async def test_mqttserver():
|
||||||
) # Check client is disconnected
|
) # Check client is disconnected
|
||||||
|
|
||||||
# bad password
|
# bad password
|
||||||
try:
|
with LogCapture() as l:
|
||||||
await test_client.Client.connect(
|
try:
|
||||||
f"mqtts://test-client:notvalid!@{test_client.address[0]}:{test_client.address[1]}/",
|
await test_client.Client.connect(
|
||||||
cafile=bumper.ca_cert, cleansession=True
|
f"mqtts://test-client:notvalid!@{test_client.address[0]}:{test_client.address[1]}/",
|
||||||
)
|
cafile=bumper.ca_cert, cleansession=True
|
||||||
|
)
|
||||||
|
|
||||||
assert (
|
except Exception as ae:
|
||||||
test_client.Client._connected_state._value == False
|
pass
|
||||||
) # Check client is connected
|
|
||||||
|
|
||||||
except Exception as ae:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
l.check_present(
|
||||||
|
("mqttserver", "INFO", "File Authentication Failed - Username: test-client - ClientID: test-file-auth"),
|
||||||
|
order_matters=False
|
||||||
|
)
|
||||||
# no username in file
|
# no username in file
|
||||||
try:
|
try:
|
||||||
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!@{test_client.address[0]}:{test_client.address[1]}/",
|
||||||
cafile=bumper.ca_cert, cleansession=True
|
cafile=bumper.ca_cert, cleansession=True
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as ae:
|
||||||
|
pass
|
||||||
|
|
||||||
|
l.check_present(
|
||||||
|
("mqttserver", "INFO", 'File Authentication Failed - No Entry for Username: test-client-noexist - ClientID: test-file-auth'),
|
||||||
|
order_matters=False
|
||||||
)
|
)
|
||||||
|
|
||||||
assert (
|
|
||||||
test_client.Client._connected_state._value == False
|
|
||||||
) # Check client is connected
|
|
||||||
|
|
||||||
except Exception as ae:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
await mqtt_server.broker.shutdown()
|
await mqtt_server.broker.shutdown()
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
async def test_passwordfile_badhash_mqttserver():
|
|
||||||
mqtt_address = ("127.0.0.1", 8883)
|
|
||||||
|
|
||||||
mqtt_server = bumper.MQTTServer(mqtt_address)
|
|
||||||
|
|
||||||
mqtt_server.default_config = {
|
|
||||||
"listeners": {
|
|
||||||
"default": {"type": "tcp"},
|
|
||||||
"tls1": {
|
|
||||||
"bind": "{}:{}".format(mqtt_address[0], mqtt_address[1]),
|
|
||||||
"ssl": "on",
|
|
||||||
"certfile": bumper.server_cert,
|
|
||||||
"keyfile": bumper.server_key,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"sys_interval": 10,
|
|
||||||
"auth": {
|
|
||||||
"allow-anonymous": True, # Set to True to allow anonymous authentication
|
|
||||||
"password-file": "tests/passwd_bad", # For file auth, set user:hash in passwd file see (https://hbmqtt.readthedocs.io/en/latest/references/hbmqtt.html#configuration-example)
|
|
||||||
"plugins": ["bumper"], # Bumper plugin provides auth and handling of bots/clients connecting
|
|
||||||
},
|
|
||||||
"topic-check": {"enabled": False},
|
|
||||||
}
|
|
||||||
|
|
||||||
await mqtt_server.broker_coro()
|
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
# bad password
|
|
||||||
try:
|
|
||||||
test_client = bumper.MQTTHelperBot(mqtt_address)
|
|
||||||
|
|
||||||
await test_client.Client.connect(
|
|
||||||
f"mqtts://test-client:notvalid!@{test_client.address[0]}:{test_client.address[1]}/",
|
|
||||||
cafile=bumper.ca_cert, cleansession=True
|
|
||||||
)
|
|
||||||
|
|
||||||
assert (
|
|
||||||
test_client.Client._connected_state._value == False
|
|
||||||
) # Check client is connected
|
|
||||||
|
|
||||||
|
|
||||||
except Exception as ae:
|
|
||||||
pass
|
|
||||||
|
|
||||||
await mqtt_server.broker.shutdown()
|
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
async def test_nofileauth_mqttserver():
|
async def test_nofileauth_mqttserver():
|
||||||
try:
|
with LogCapture() as l:
|
||||||
|
|
||||||
mqtt_address = ("127.0.0.1", 8883)
|
mqtt_address = ("127.0.0.1", 8883)
|
||||||
|
mqtt_server = bumper.MQTTServer(mqtt_address, password_file="tests/passwd-notfound")
|
||||||
mqtt_server = bumper.MQTTServer(mqtt_address)
|
|
||||||
|
|
||||||
mqtt_server.default_config = {
|
|
||||||
"listeners": {
|
|
||||||
"default": {"type": "tcp"},
|
|
||||||
"tls1": {
|
|
||||||
"bind": "{}:{}".format(mqtt_address[0], mqtt_address[1]),
|
|
||||||
"ssl": "on",
|
|
||||||
"certfile": bumper.server_cert,
|
|
||||||
"keyfile": bumper.server_key,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"sys_interval": 10,
|
|
||||||
"auth": {
|
|
||||||
"allow-anonymous": True, # Set to True to allow anonymous authentication
|
|
||||||
"password-file": "tests/passwd-notfound", # For file auth, set user:hash in passwd file see (https://hbmqtt.readthedocs.io/en/latest/references/hbmqtt.html#configuration-example)
|
|
||||||
"plugins": ["bumper"], # Bumper plugin provides auth and handling of bots/clients connecting
|
|
||||||
},
|
|
||||||
"topic-check": {"enabled": False},
|
|
||||||
}
|
|
||||||
|
|
||||||
await mqtt_server.broker_coro()
|
|
||||||
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
async def test_passwordfile_opt_missing_mqttserver():
|
|
||||||
try:
|
|
||||||
|
|
||||||
mqtt_address = ("127.0.0.1", 8883)
|
|
||||||
|
|
||||||
mqtt_server = bumper.MQTTServer(mqtt_address)
|
|
||||||
|
|
||||||
mqtt_server.default_config = {
|
|
||||||
"listeners": {
|
|
||||||
"default": {"type": "tcp"},
|
|
||||||
"tls1": {
|
|
||||||
"bind": "{}:{}".format(mqtt_address[0], mqtt_address[1]),
|
|
||||||
"ssl": "on",
|
|
||||||
"certfile": bumper.server_cert,
|
|
||||||
"keyfile": bumper.server_key,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"sys_interval": 10,
|
|
||||||
"auth": {
|
|
||||||
"allow-anonymous": True, # Set to True to allow anonymous authentication
|
|
||||||
|
|
||||||
"plugins": ["bumper"], # Bumper plugin provides auth and handling of bots/clients connecting
|
|
||||||
},
|
|
||||||
"topic-check": {"enabled": False},
|
|
||||||
}
|
|
||||||
|
|
||||||
await mqtt_server.broker_coro()
|
await mqtt_server.broker_coro()
|
||||||
await mqtt_server.broker.shutdown()
|
await mqtt_server.broker.shutdown()
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
except:
|
l.check_present(
|
||||||
pass
|
("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