2015-08-29 13:29:09 +00:00
|
|
|
# Copyright (c) 2015 Nicolas JOUANIN
|
|
|
|
#
|
|
|
|
# See the file license.txt for copying permission.
|
|
|
|
|
2024-12-21 10:52:26 +00:00
|
|
|
import asyncio
|
2015-08-29 13:29:09 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2024-12-21 10:52:26 +00:00
|
|
|
import unittest
|
|
|
|
|
2021-03-27 12:16:42 +00:00
|
|
|
from amqtt.plugins.authentication import AnonymousAuthPlugin, FileAuthPlugin
|
2024-12-21 10:52:26 +00:00
|
|
|
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
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
class TestAnonymousAuthPlugin(unittest.TestCase):
|
2015-08-29 19:25:59 +00:00
|
|
|
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__)
|
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
|
|
|
|
|
|
|
def test_disallow_anonymous(self):
|
|
|
|
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
|
|
|
|
|
|
|
def test_allow_nonanonymous(self):
|
|
|
|
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):
|
2015-08-29 19:25:59 +00:00
|
|
|
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 = {
|
2021-03-14 20:44:41 +00:00
|
|
|
"auth": {
|
|
|
|
"password-file": os.path.join(
|
2024-12-21 10:52:26 +00:00
|
|
|
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)
|
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
|
|
|
|
|
|
|
def test_wrong_password(self):
|
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
|
|
|
context.config = {
|
2021-03-14 20:44:41 +00:00
|
|
|
"auth": {
|
|
|
|
"password-file": os.path.join(
|
2024-12-21 10:52:26 +00:00
|
|
|
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)
|
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
|
|
|
|
|
|
|
def test_unknown_password(self):
|
|
|
|
context = BaseContext()
|
|
|
|
context.logger = logging.getLogger(__name__)
|
|
|
|
context.config = {
|
2021-03-14 20:44:41 +00:00
|
|
|
"auth": {
|
|
|
|
"password-file": os.path.join(
|
2024-12-21 10:52:26 +00:00
|
|
|
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)
|
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
|