Skip to content

Commit

Permalink
plugins.topic_checking tests: Test BaseTopicPlugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlongland committed Jun 6, 2021
1 parent 6ab32a1 commit 60f3bee
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions tests/plugins/test_topic_checking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright (c) 2015 Nicolas JOUANIN
#
# See the file license.txt for copying permission.

import pytest

from amqtt.plugins.manager import BaseContext
from amqtt.plugins.topic_checking import BaseTopicPlugin, TopicTabooPlugin, TopicAccessControlListPlugin


class DummyLogger(object):
def __init__(self):
self.messages = []

def warning(self, *args, **kwargs):
self.messages.append((args, kwargs))


@pytest.mark.asyncio
async def test_base_no_config():
"""
Check BaseTopicPlugin returns false if no topic-check is present.
"""
context = BaseContext()
context.logger = DummyLogger()
context.config = {}

plugin = BaseTopicPlugin(context)
assert plugin.topic_filtering() is False

# Should have printed a couple of warnings
assert len(context.logger.messages) == 2
assert context.logger.messages[0] == (
("'topic-check' section not found in context configuration",),
{}
)
assert context.logger.messages[1] == (
("'auth' section not found in context configuration",),
{}
)


@pytest.mark.asyncio
async def test_base_empty_config():
"""
Check BaseTopicPlugin returns false if topic-check is empty.
"""
context = BaseContext()
context.logger = DummyLogger()
context.config = {
'topic-check': {}
}

plugin = BaseTopicPlugin(context)
assert plugin.topic_filtering() is False

# Should NOT have printed warnings
assert len(context.logger.messages) == 1
assert context.logger.messages[0] == (
("'auth' section not found in context configuration",),
{}
)


@pytest.mark.asyncio
async def test_base_enabled_config():
"""
Check BaseTopicPlugin returns true if enabled.
"""
context = BaseContext()
context.logger = DummyLogger()
context.config = {
'topic-check': {
'enabled': True
}
}

plugin = BaseTopicPlugin(context)
assert plugin.topic_filtering() is True

# Should NOT have printed warnings
assert len(context.logger.messages) == 0

0 comments on commit 60f3bee

Please sign in to comment.