amqtt/tests/plugins/test_authentication.py

130 wiersze
4.5 KiB
Python
Czysty Zwykły widok Historia

import asyncio
2015-08-29 13:29:09 +00:00
import logging
2024-12-29 18:23:27 +00:00
from pathlib import Path
import unittest
import pytest
2021-03-27 12:16:42 +00:00
from amqtt.plugins.authentication import AnonymousAuthPlugin, FileAuthPlugin
from amqtt.contexts import BaseContext
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
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)
@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()
def test_allow_anonymous_dict_config(self) -> None:
2015-08-29 13:29:09 +00:00
context = BaseContext()
context.logger = logging.getLogger(__name__)
context.config = {"auth": {"allow-anonymous": True}}
2015-08-29 13:29:09 +00:00
s = Session()
s.username = ""
auth_plugin = AnonymousAuthPlugin(context)
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
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__)
context.config = {"auth": {"allow-anonymous": False}}
2015-08-29 13:29:09 +00:00
s = Session()
s.username = ""
auth_plugin = AnonymousAuthPlugin(context)
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
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__)
context.config = {"auth": {"allow-anonymous": False}}
2015-08-29 13:29:09 +00:00
s = Session()
s.username = "test"
auth_plugin = AnonymousAuthPlugin(context)
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()
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 = {
"auth": {
2024-12-29 18:23:27 +00:00
"password-file": Path(__file__).parent / "passwd",
},
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)
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 = {
"auth": {
2024-12-29 18:23:27 +00:00
"password-file": Path(__file__).parent / "passwd",
},
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)
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
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 = {
"auth": {
2024-12-29 18:23:27 +00:00
"password-file": Path(__file__).parent / "passwd",
},
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)
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
assert not ret