plugins.topic_checking tests: Replace DummyLogger with `logdog` plug-in.

pull/69/head
Stuart Longland 2021-07-04 15:56:34 +10:00 zatwierdzone przez Florian Ludwig
rodzic 5ad48d9129
commit 0760bd7613
1 zmienionych plików z 263 dodań i 254 usunięć

Wyświetl plik

@ -1,4 +1,5 @@
import pytest import pytest
import logging
from amqtt.plugins.manager import BaseContext from amqtt.plugins.manager import BaseContext
from amqtt.plugins.topic_checking import ( from amqtt.plugins.topic_checking import (
@ -10,24 +11,17 @@ from amqtt.plugins.topic_checking import (
from amqtt.session import Session from amqtt.session import Session
class DummyLogger(object):
def __init__(self):
self.messages = []
def warning(self, *args, **kwargs):
self.messages.append((args, kwargs))
# Base plug-in object # Base plug-in object
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_base_no_config(): async def test_base_no_config(logdog):
""" """
Check BaseTopicPlugin returns false if no topic-check is present. Check BaseTopicPlugin returns false if no topic-check is present.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {} context.config = {}
plugin = BaseTopicPlugin(context) plugin = BaseTopicPlugin(context)
@ -35,24 +29,24 @@ async def test_base_no_config():
assert authorised is False assert authorised is False
# Should have printed a couple of warnings # Should have printed a couple of warnings
assert len(context.logger.messages) == 2 log_records = list(pile.drain(name='testlog'))
assert context.logger.messages[0] == ( assert len(log_records) == 2
("'topic-check' section not found in context configuration",), assert log_records[0].levelno == logging.WARN
{}, assert log_records[0].message == "'topic-check' section not found in context configuration"
)
assert context.logger.messages[1] == ( assert log_records[1].levelno == logging.WARN
("'auth' section not found in context configuration",), assert log_records[1].message == "'auth' section not found in context configuration"
{}, assert pile.is_empty()
)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_base_empty_config(): async def test_base_empty_config(logdog):
""" """
Check BaseTopicPlugin returns false if topic-check is empty. Check BaseTopicPlugin returns false if topic-check is empty.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {}} context.config = {"topic-check": {}}
plugin = BaseTopicPlugin(context) plugin = BaseTopicPlugin(context)
@ -60,20 +54,20 @@ async def test_base_empty_config():
assert authorised is False assert authorised is False
# Should have printed just one warning # Should have printed just one warning
assert len(context.logger.messages) == 1 log_records = list(pile.drain(name='testlog'))
assert context.logger.messages[0] == ( assert len(log_records) == 1
("'auth' section not found in context configuration",), assert log_records[0].levelno == logging.WARN
{}, assert log_records[0].message == "'auth' section not found in context configuration"
)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_base_disabled_config(): async def test_base_disabled_config(logdog):
""" """
Check BaseTopicPlugin returns true if disabled. (it doesn't actually check) Check BaseTopicPlugin returns true if disabled. (it doesn't actually check)
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {"enabled": False}} context.config = {"topic-check": {"enabled": False}}
plugin = BaseTopicPlugin(context) plugin = BaseTopicPlugin(context)
@ -81,16 +75,18 @@ async def test_base_disabled_config():
assert authorised is True assert authorised is True
# Should NOT have printed warnings # Should NOT have printed warnings
assert len(context.logger.messages) == 0 log_records = list(pile.drain(name='testlog'))
assert len(log_records) == 0
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_base_enabled_config(): async def test_base_enabled_config(logdog):
""" """
Check BaseTopicPlugin returns true if enabled. Check BaseTopicPlugin returns true if enabled.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {"enabled": True}} context.config = {"topic-check": {"enabled": True}}
plugin = BaseTopicPlugin(context) plugin = BaseTopicPlugin(context)
@ -98,43 +94,43 @@ async def test_base_enabled_config():
assert authorised is True assert authorised is True
# Should NOT have printed warnings # Should NOT have printed warnings
assert len(context.logger.messages) == 0 log_records = list(pile.drain(name='testlog'))
assert len(log_records) == 0
# Taboo plug-in # Taboo plug-in
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taboo_empty_config(): async def test_taboo_empty_config(logdog):
""" """
Check TopicTabooPlugin returns false if topic-check absent. Check TopicTabooPlugin returns false if topic-check absent.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {} context.config = {}
plugin = TopicTabooPlugin(context) plugin = TopicTabooPlugin(context)
assert (await plugin.topic_filtering()) is False assert (await plugin.topic_filtering()) is False
# Should have printed a couple of warnings # Should have printed a couple of warnings
assert len(context.logger.messages) == 2 log_records = list(pile.drain(name='testlog'))
assert context.logger.messages[0] == ( assert len(log_records) == 2
("'topic-check' section not found in context configuration",), assert log_records[0].levelno == logging.WARN
{}, assert log_records[0].message == "'topic-check' section not found in context configuration"
) assert log_records[1].levelno == logging.WARN
assert context.logger.messages[1] == ( assert log_records[1].message == "'auth' section not found in context configuration"
("'auth' section not found in context configuration",),
{},
)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taboo_not_taboo_topic(): async def test_taboo_not_taboo_topic(logdog):
""" """
Check TopicTabooPlugin returns true if checking disabled. Check TopicTabooPlugin returns true if checking disabled.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {"enabled": False}} context.config = {"topic-check": {"enabled": False}}
session = Session() session = Session()
@ -146,16 +142,18 @@ async def test_taboo_not_taboo_topic():
) is True ) is True
# Should NOT have printed warnings # Should NOT have printed warnings
assert len(context.logger.messages) == 0 log_records = list(pile.drain(name='testlog'))
assert len(log_records) == 0
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taboo_not_taboo_topic(): async def test_taboo_not_taboo_topic(logdog):
""" """
Check TopicTabooPlugin returns true if topic not taboo Check TopicTabooPlugin returns true if topic not taboo
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {"enabled": True}} context.config = {"topic-check": {"enabled": True}}
session = Session() session = Session()
@ -167,16 +165,18 @@ async def test_taboo_not_taboo_topic():
) is True ) is True
# Should NOT have printed warnings # Should NOT have printed warnings
assert len(context.logger.messages) == 0 log_records = list(pile.drain(name='testlog'))
assert len(log_records) == 0
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taboo_anon_taboo_topic(): async def test_taboo_anon_taboo_topic(logdog):
""" """
Check TopicTabooPlugin returns false if topic is taboo and session is anonymous. Check TopicTabooPlugin returns false if topic is taboo and session is anonymous.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {"enabled": True}} context.config = {"topic-check": {"enabled": True}}
session = Session() session = Session()
@ -186,16 +186,18 @@ async def test_taboo_anon_taboo_topic():
assert (await plugin.topic_filtering(session=session, topic="prohibited")) is False assert (await plugin.topic_filtering(session=session, topic="prohibited")) is False
# Should NOT have printed warnings # Should NOT have printed warnings
assert len(context.logger.messages) == 0 log_records = list(pile.drain(name='testlog'))
assert len(log_records) == 0
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taboo_notadmin_taboo_topic(): async def test_taboo_notadmin_taboo_topic(logdog):
""" """
Check TopicTabooPlugin returns false if topic is taboo and user is not "admin". Check TopicTabooPlugin returns false if topic is taboo and user is not "admin".
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {"enabled": True}} context.config = {"topic-check": {"enabled": True}}
session = Session() session = Session()
@ -205,16 +207,18 @@ async def test_taboo_notadmin_taboo_topic():
assert (await plugin.topic_filtering(session=session, topic="prohibited")) is False assert (await plugin.topic_filtering(session=session, topic="prohibited")) is False
# Should NOT have printed warnings # Should NOT have printed warnings
assert len(context.logger.messages) == 0 log_records = list(pile.drain(name='testlog'))
assert len(log_records) == 0
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taboo_admin_taboo_topic(): async def test_taboo_admin_taboo_topic(logdog):
""" """
Check TopicTabooPlugin returns true if topic is taboo and user is "admin". Check TopicTabooPlugin returns true if topic is taboo and user is "admin".
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {"enabled": True}} context.config = {"topic-check": {"enabled": True}}
session = Session() session = Session()
@ -224,7 +228,8 @@ async def test_taboo_admin_taboo_topic():
assert (await plugin.topic_filtering(session=session, topic="prohibited")) is True assert (await plugin.topic_filtering(session=session, topic="prohibited")) is True
# Should NOT have printed warnings # Should NOT have printed warnings
assert len(context.logger.messages) == 0 log_records = list(pile.drain(name='testlog'))
assert len(log_records) == 0
# TopicAccessControlListPlugin tests # TopicAccessControlListPlugin tests
@ -286,36 +291,33 @@ def test_topic_ac_match_hash():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taclp_empty_config(): async def test_taclp_empty_config(logdog):
""" """
Check TopicAccessControlListPlugin returns false if topic-check absent. Check TopicAccessControlListPlugin returns false if topic-check absent.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {} context.config = {}
plugin = TopicAccessControlListPlugin(context) plugin = TopicAccessControlListPlugin(context)
assert (await plugin.topic_filtering()) is False assert (await plugin.topic_filtering()) is False
# Should have printed a couple of warnings # Should have printed a couple of warnings
assert len(context.logger.messages) == 2 log_records = list(pile.drain(name='testlog'))
assert context.logger.messages[0] == ( assert len(log_records) == 2
("'topic-check' section not found in context configuration",), assert log_records[0].message == "'topic-check' section not found in context configuration"
{}, assert log_records[1].message == "'auth' section not found in context configuration"
)
assert context.logger.messages[1] == (
("'auth' section not found in context configuration",),
{},
)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taclp_true_disabled(): async def test_taclp_true_disabled(logdog):
""" """
Check TopicAccessControlListPlugin returns true if topic checking is disabled. Check TopicAccessControlListPlugin returns true if topic checking is disabled.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {"enabled": False}} context.config = {"topic-check": {"enabled": False}}
session = Session() session = Session()
@ -329,13 +331,14 @@ async def test_taclp_true_disabled():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taclp_true_no_pub_acl(): async def test_taclp_true_no_pub_acl(logdog):
""" """
Check TopicAccessControlListPlugin returns true if action=publish and no publish-acl given. Check TopicAccessControlListPlugin returns true if action=publish and no publish-acl given.
(This is for backward-compatibility with existing installations.) (This is for backward-compatibility with existing installations.)
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = {"topic-check": {"enabled": True}} context.config = {"topic-check": {"enabled": True}}
session = Session() session = Session()
@ -349,12 +352,13 @@ async def test_taclp_true_no_pub_acl():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taclp_false_sub_no_topic(): async def test_taclp_false_sub_no_topic(logdog):
""" """
Check TopicAccessControlListPlugin returns false user there is no topic. Check TopicAccessControlListPlugin returns false user there is no topic.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = { context.config = {
"topic-check": { "topic-check": {
"enabled": True, "enabled": True,
@ -373,12 +377,13 @@ async def test_taclp_false_sub_no_topic():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taclp_false_sub_unknown_user(): async def test_taclp_false_sub_unknown_user(logdog):
""" """
Check TopicAccessControlListPlugin returns false user is not listed in ACL. Check TopicAccessControlListPlugin returns false user is not listed in ACL.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = { context.config = {
"topic-check": { "topic-check": {
"enabled": True, "enabled": True,
@ -397,12 +402,13 @@ async def test_taclp_false_sub_unknown_user():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taclp_false_sub_no_permission(): async def test_taclp_false_sub_no_permission(logdog):
""" """
Check TopicAccessControlListPlugin returns false if "acl" does not list allowed topic. Check TopicAccessControlListPlugin returns false if "acl" does not list allowed topic.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = { context.config = {
"topic-check": { "topic-check": {
"enabled": True, "enabled": True,
@ -421,12 +427,13 @@ async def test_taclp_false_sub_no_permission():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taclp_true_sub_permission(): async def test_taclp_true_sub_permission(logdog):
""" """
Check TopicAccessControlListPlugin returns true if "acl" lists allowed topic. Check TopicAccessControlListPlugin returns true if "acl" lists allowed topic.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = { context.config = {
"topic-check": { "topic-check": {
"enabled": True, "enabled": True,
@ -445,12 +452,13 @@ async def test_taclp_true_sub_permission():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taclp_true_pub_permission(): async def test_taclp_true_pub_permission(logdog):
""" """
Check TopicAccessControlListPlugin returns true if "publish-acl" lists allowed topic for publish action. Check TopicAccessControlListPlugin returns true if "publish-acl" lists allowed topic for publish action.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = { context.config = {
"topic-check": { "topic-check": {
"enabled": True, "enabled": True,
@ -469,12 +477,13 @@ async def test_taclp_true_pub_permission():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_taclp_true_anon_sub_permission(): async def test_taclp_true_anon_sub_permission(logdog):
""" """
Check TopicAccessControlListPlugin handles anonymous users. Check TopicAccessControlListPlugin handles anonymous users.
""" """
with logdog() as pile:
context = BaseContext() context = BaseContext()
context.logger = DummyLogger() context.logger = logging.getLogger('testlog')
context.config = { context.config = {
"topic-check": { "topic-check": {
"enabled": True, "enabled": True,