amqtt/tests/conftest.py

81 wiersze
1.9 KiB
Python
Czysty Zwykły widok Historia

2021-03-06 17:58:34 +00:00
import unittest.mock
import os.path
2021-03-06 17:58:34 +00:00
import pytest
2021-03-27 12:16:42 +00:00
import amqtt.broker
2021-03-06 17:58:34 +00:00
pytest_plugins = ["pytest_logdog"]
2021-03-06 17:58:34 +00:00
test_config = {
"listeners": {
"default": {"type": "tcp", "bind": "127.0.0.1:1883", "max_connections": 10},
},
"sys_interval": 0,
"auth": {
"allow-anonymous": True,
2021-03-06 17:58:34 +00:00
},
}
test_config_acl = {
"listeners": {
"default": {"type": "tcp", "bind": "127.0.0.1:1884", "max_connections": 10},
},
"sys_interval": 0,
"auth": {
"plugins": ["auth_file"],
"password-file": os.path.join(
2021-07-08 00:23:24 +00:00
os.path.dirname(os.path.realpath(__file__)), "plugins", "passwd"
),
},
"topic-check": {
"enabled": True,
"plugins": ["topic_acl"],
"acl": {
2021-07-08 00:23:24 +00:00
"user1": ["public/#"],
"user2": ["#"],
},
2021-07-08 00:23:24 +00:00
"publish-acl": {"user1": ["public/subtopic/#"]},
},
}
2021-03-06 17:58:34 +00:00
@pytest.fixture(scope="function")
def mock_plugin_manager():
2021-03-27 12:16:42 +00:00
with unittest.mock.patch("amqtt.broker.PluginManager") as plugin_manager:
2021-03-06 17:58:34 +00:00
yield plugin_manager
@pytest.fixture(scope="function")
async def broker(mock_plugin_manager):
# just making sure the mock is in place before we start our broker
assert mock_plugin_manager is not None
2021-03-06 17:58:34 +00:00
2021-03-27 12:16:42 +00:00
broker = amqtt.broker.Broker(test_config, plugin_namespace="amqtt.test.plugins")
2021-03-06 17:58:34 +00:00
await broker.start()
assert broker.transitions.is_started()
assert broker._sessions == {}
assert "default" in broker._servers
2021-03-06 17:58:34 +00:00
yield broker
if not broker.transitions.is_stopped():
await broker.shutdown()
@pytest.fixture(scope="function")
async def acl_broker():
2021-07-08 00:23:24 +00:00
broker = amqtt.broker.Broker(
test_config_acl, plugin_namespace="amqtt.broker.plugins"
)
await broker.start()
assert broker.transitions.is_started()
assert broker._sessions == {}
assert "default" in broker._servers
yield broker
if not broker.transitions.is_stopped():
await broker.shutdown()