amqtt/tests/plugins/test_authentication.py

110 wiersze
3.5 KiB
Python
Czysty Zwykły widok Historia

2015-08-29 13:29:09 +00:00
# Copyright (c) 2015 Nicolas JOUANIN
#
# See the file license.txt for copying permission.
import asyncio
2015-08-29 13:29:09 +00:00
import logging
import os
import unittest
2021-03-27 12:16:42 +00:00
from amqtt.plugins.authentication import AnonymousAuthPlugin, FileAuthPlugin
from amqtt.plugins.manager import BaseContext
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)
class TestAnonymousAuthPlugin(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
2015-08-29 13:29:09 +00:00
def test_allow_anonymous(self):
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_disallow_anonymous(self):
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
def test_allow_nonanonymous(self):
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):
def setUp(self):
self.loop = asyncio.new_event_loop()
2015-08-29 13:29:09 +00:00
def test_allow(self):
context = BaseContext()
context.logger = logging.getLogger(__name__)
context.config = {
"auth": {
"password-file": os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"passwd",
),
},
2015-08-29 13:29:09 +00:00
}
s = Session()
s.username = "user"
s.password = "test"
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
def test_wrong_password(self):
context = BaseContext()
context.logger = logging.getLogger(__name__)
context.config = {
"auth": {
"password-file": os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"passwd",
),
},
2015-08-29 13:29:09 +00:00
}
s = Session()
s.username = "user"
s.password = "wrong password"
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
def test_unknown_password(self):
context = BaseContext()
context.logger = logging.getLogger(__name__)
context.config = {
"auth": {
"password-file": os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"passwd",
),
},
2015-08-29 13:29:09 +00:00
}
s = Session()
s.username = "some user"
s.password = "some password"
auth_plugin = FileAuthPlugin(context)
ret = self.loop.run_until_complete(auth_plugin.authenticate(session=s))
assert not ret