2024-12-21 10:52:26 +00:00
|
|
|
import asyncio
|
2015-08-29 13:29:09 +00:00
|
|
|
import logging
|
2024-12-29 18:23:27 +00:00
|
|
|
from pathlib import Path
|
2024-12-21 10:52:26 +00:00
|
|
|
import unittest
|
|
|
|
|
2025-06-28 03:08:09 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-03-27 12:16:42 +00:00
|
|
|
from amqtt.plugins.authentication import AnonymousAuthPlugin, FileAuthPlugin
|
2025-06-27 17:59:39 +00:00
|
|
|
from amqtt.contexts import BaseContext
|
2025-06-28 03:08:09 +00:00
|
|
|
from amqtt.plugins.base import BaseAuthPlugin
|
2021-03-27 12:16:42 +00:00
|
|
|
from amqtt.session import Session
|
2015-08-29 13:29:09 +00:00
|
|
|
|
2024-12-21 10:52:26 +00:00
|
|
|
formatter = "[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
2015-08-29 13:29:09 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG, format=formatter)
|
|
|
|
|
|
|
|
|
2025-06-28 03:08:09 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_base_no_config(logdog):
|
|
|
|
"""Check BaseTopicPlugin returns false if no topic-check is present."""
|
|
|
|
with logdog() as pile:
|
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger("testlog")
|
|
|
|
context.config = {}
|
|
|
|
|
|
|
|
plugin = BaseAuthPlugin(context)
|
|
|
|
s = Session()
|
|
|
|
authorised = await plugin.authenticate(session=s)
|
|
|
|
assert authorised is False
|
|
|
|
|
|
|
|
# Warning messages are only generated if using deprecated plugin configuration on initial load
|
|
|
|
log_records = list(pile.drain(name="testlog"))
|
|
|
|
assert len(log_records) == 1
|
|
|
|
assert log_records[0].levelno == logging.WARNING
|
|
|
|
assert log_records[0].message == "'auth' section not found in context configuration"
|
|
|
|
|
|
|
|
|
2015-08-29 13:29:09 +00:00
|
|
|
class TestAnonymousAuthPlugin(unittest.TestCase):
|
2024-12-29 18:23:27 +00:00
|
|
|
def setUp(self) -> None:
|
|
|
|
self.loop: asyncio.AbstractEventLoop = asyncio.new_event_loop()
|
2015-08-29 19:25:59 +00:00
|
|
|
|
2025-06-28 03:08:09 +00:00
|
|
|
def test_allow_anonymous_dict_config(self) -> None:
|
2015-08-29 13:29:09 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
2021-03-14 20:44:41 +00:00
|
|
|
context.config = {"auth": {"allow-anonymous": True}}
|
2015-08-29 13:29:09 +00:00
|
|
|
s = Session()
|
|
|
|
s.username = ""
|
|
|
|
auth_plugin = AnonymousAuthPlugin(context)
|
2015-08-29 19:25:59 +00:00
|
|
|
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
|
2021-03-06 17:37:23 +00:00
|
|
|
assert ret
|
2015-08-29 13:29:09 +00:00
|
|
|
|
2025-06-28 03:08:09 +00:00
|
|
|
def test_allow_anonymous_dataclass_config(self) -> None:
|
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
|
|
|
context.config = AnonymousAuthPlugin.Config(allow_anonymous=True)
|
|
|
|
s = Session()
|
|
|
|
s.username = ""
|
|
|
|
auth_plugin = AnonymousAuthPlugin(context)
|
|
|
|
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
|
|
|
|
assert ret
|
|
|
|
|
2024-12-29 18:23:27 +00:00
|
|
|
def test_disallow_anonymous(self) -> None:
|
2015-08-29 13:29:09 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
2021-03-14 20:44:41 +00:00
|
|
|
context.config = {"auth": {"allow-anonymous": False}}
|
2015-08-29 13:29:09 +00:00
|
|
|
s = Session()
|
|
|
|
s.username = ""
|
|
|
|
auth_plugin = AnonymousAuthPlugin(context)
|
2015-08-29 19:25:59 +00:00
|
|
|
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
|
2024-12-21 10:52:26 +00:00
|
|
|
assert not ret
|
2015-08-29 13:29:09 +00:00
|
|
|
|
2024-12-29 18:23:27 +00:00
|
|
|
def test_allow_nonanonymous(self) -> None:
|
2015-08-29 13:29:09 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
2021-03-14 20:44:41 +00:00
|
|
|
context.config = {"auth": {"allow-anonymous": False}}
|
2015-08-29 13:29:09 +00:00
|
|
|
s = Session()
|
|
|
|
s.username = "test"
|
|
|
|
auth_plugin = AnonymousAuthPlugin(context)
|
2015-08-29 19:25:59 +00:00
|
|
|
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
|
2021-03-06 17:37:23 +00:00
|
|
|
assert ret
|
2015-08-29 13:29:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestFileAuthPlugin(unittest.TestCase):
|
2024-12-29 18:23:27 +00:00
|
|
|
def setUp(self) -> None:
|
|
|
|
self.loop: asyncio.AbstractEventLoop = asyncio.new_event_loop()
|
2015-08-29 19:25:59 +00:00
|
|
|
|
2024-12-29 18:23:27 +00:00
|
|
|
def test_allow(self) -> None:
|
2015-08-29 13:29:09 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
|
|
|
context.config = {
|
2021-03-14 20:44:41 +00:00
|
|
|
"auth": {
|
2024-12-29 18:23:27 +00:00
|
|
|
"password-file": Path(__file__).parent / "passwd",
|
2024-12-21 10:52:26 +00:00
|
|
|
},
|
2015-08-29 13:29:09 +00:00
|
|
|
}
|
|
|
|
s = Session()
|
|
|
|
s.username = "user"
|
2025-01-12 19:52:50 +00:00
|
|
|
s.password = "test"
|
2015-08-29 13:29:09 +00:00
|
|
|
auth_plugin = FileAuthPlugin(context)
|
2015-08-29 19:25:59 +00:00
|
|
|
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
|
2021-03-06 17:37:23 +00:00
|
|
|
assert ret
|
2015-08-29 13:29:09 +00:00
|
|
|
|
2024-12-29 18:23:27 +00:00
|
|
|
def test_wrong_password(self) -> None:
|
2015-08-29 13:29:09 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
|
|
|
context.config = {
|
2021-03-14 20:44:41 +00:00
|
|
|
"auth": {
|
2024-12-29 18:23:27 +00:00
|
|
|
"password-file": Path(__file__).parent / "passwd",
|
2024-12-21 10:52:26 +00:00
|
|
|
},
|
2015-08-29 13:29:09 +00:00
|
|
|
}
|
|
|
|
s = Session()
|
|
|
|
s.username = "user"
|
2025-01-12 19:52:50 +00:00
|
|
|
s.password = "wrong password"
|
2015-08-29 13:29:09 +00:00
|
|
|
auth_plugin = FileAuthPlugin(context)
|
2015-08-29 19:25:59 +00:00
|
|
|
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
|
2024-12-21 10:52:26 +00:00
|
|
|
assert not ret
|
2015-08-29 13:29:09 +00:00
|
|
|
|
2024-12-29 18:23:27 +00:00
|
|
|
def test_unknown_password(self) -> None:
|
2015-08-29 13:29:09 +00:00
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
|
|
|
context.config = {
|
2021-03-14 20:44:41 +00:00
|
|
|
"auth": {
|
2024-12-29 18:23:27 +00:00
|
|
|
"password-file": Path(__file__).parent / "passwd",
|
2024-12-21 10:52:26 +00:00
|
|
|
},
|
2015-08-29 13:29:09 +00:00
|
|
|
}
|
|
|
|
s = Session()
|
|
|
|
s.username = "some user"
|
2025-01-12 19:52:50 +00:00
|
|
|
s.password = "some password"
|
2015-08-29 13:29:09 +00:00
|
|
|
auth_plugin = FileAuthPlugin(context)
|
2015-08-29 19:25:59 +00:00
|
|
|
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
|
2024-12-21 10:52:26 +00:00
|
|
|
assert not ret
|