Compare commits
No commits in common. "101dbdc96b029bdfb86e9aa3f4ea6d2c20858fd9" and "f4d8d69cb27d63f98346cdd3322e4da50a8b555a" have entirely different histories.
101dbdc96b
...
f4d8d69cb2
4 changed files with 86 additions and 93 deletions
|
|
@ -26,7 +26,6 @@ classifiers = [
|
||||||
]
|
]
|
||||||
requires-python = ">= 3.8"
|
requires-python = ">= 3.8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bcrypt",
|
|
||||||
"humanize",
|
"humanize",
|
||||||
'importlib-metadata; python_version < "3.10"',
|
'importlib-metadata; python_version < "3.10"',
|
||||||
"importlib_resources ; python_version < '3.9'",
|
"importlib_resources ; python_version < '3.9'",
|
||||||
|
|
@ -40,7 +39,7 @@ dependencies = [
|
||||||
|
|
||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
db = ["SQLAlchemy", "alembic", "alembic-postgresql-enum"]
|
db = ["SQLAlchemy", "alembic", "alembic-postgresql-enum", "passlib"]
|
||||||
docs = ["Sphinx", "sphinxcontrib-programoutput", "enum-tools[sphinx]", "furo"]
|
docs = ["Sphinx", "sphinxcontrib-programoutput", "enum-tools[sphinx]", "furo"]
|
||||||
tests = ["pylint", "pytest", "pytest-cov", "tox"]
|
tests = ["pylint", "pytest", "pytest-cov", "tox"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,11 +29,18 @@ This defines the default :term:`auth handler`.
|
||||||
import secrets
|
import secrets
|
||||||
import uuid as _uuid
|
import uuid as _uuid
|
||||||
|
|
||||||
import bcrypt
|
|
||||||
|
|
||||||
from wuttjamaican.app import GenericHandler
|
from wuttjamaican.app import GenericHandler
|
||||||
|
|
||||||
|
|
||||||
|
# nb. this only works if passlib is installed (part of 'db' extra)
|
||||||
|
try:
|
||||||
|
from passlib.context import CryptContext
|
||||||
|
except ImportError: # pragma: no cover
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
password_context = CryptContext(schemes=["bcrypt"])
|
||||||
|
|
||||||
|
|
||||||
class AuthHandler(GenericHandler): # pylint: disable=too-many-public-methods
|
class AuthHandler(GenericHandler): # pylint: disable=too-many-public-methods
|
||||||
"""
|
"""
|
||||||
Base class and default implementation for the :term:`auth
|
Base class and default implementation for the :term:`auth
|
||||||
|
|
@ -136,7 +143,7 @@ class AuthHandler(GenericHandler): # pylint: disable=too-many-public-methods
|
||||||
|
|
||||||
:returns: ``True`` if password matches; else ``False``.
|
:returns: ``True`` if password matches; else ``False``.
|
||||||
"""
|
"""
|
||||||
return bcrypt.checkpw(password.encode("utf-8"), user.password.encode("utf-8"))
|
return password_context.verify(password, user.password)
|
||||||
|
|
||||||
def get_role(self, session, key):
|
def get_role(self, session, key):
|
||||||
"""
|
"""
|
||||||
|
|
@ -412,9 +419,7 @@ class AuthHandler(GenericHandler): # pylint: disable=too-many-public-methods
|
||||||
|
|
||||||
:param password: New password in plain text.
|
:param password: New password in plain text.
|
||||||
"""
|
"""
|
||||||
user.password = bcrypt.hashpw(
|
user.password = password_context.hash(password)
|
||||||
password.encode("utf-8"), bcrypt.gensalt()
|
|
||||||
).decode("utf-8")
|
|
||||||
|
|
||||||
def get_role_administrator(self, session):
|
def get_role_administrator(self, session):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -415,8 +415,8 @@ app_title = WuttaTest
|
||||||
self.assertEqual(ver, version("SQLAlchemy"))
|
self.assertEqual(ver, version("SQLAlchemy"))
|
||||||
|
|
||||||
# can also specify the dist
|
# can also specify the dist
|
||||||
ver = self.app.get_version(dist="progress")
|
ver = self.app.get_version(dist="passlib")
|
||||||
self.assertEqual(ver, version("progress"))
|
self.assertEqual(ver, version("passlib"))
|
||||||
|
|
||||||
def test_make_title(self):
|
def test_make_title(self):
|
||||||
text = self.app.make_title("foo_bar")
|
text = self.app.make_title("foo_bar")
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,9 @@ from unittest.mock import patch, MagicMock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from wuttjamaican import conf as mod
|
from wuttjamaican import conf as mod
|
||||||
|
|
||||||
|
# TODO: get rid of this eventually
|
||||||
|
from wuttjamaican import conf
|
||||||
from wuttjamaican.exc import ConfigurationError
|
from wuttjamaican.exc import ConfigurationError
|
||||||
from wuttjamaican.app import AppHandler
|
from wuttjamaican.app import AppHandler
|
||||||
from wuttjamaican.testing import FileTestCase, ConfigTestCase
|
from wuttjamaican.testing import FileTestCase, ConfigTestCase
|
||||||
|
|
@ -18,19 +21,19 @@ class TestWuttaConfig(FileTestCase):
|
||||||
return mod.WuttaConfig(**kwargs)
|
return mod.WuttaConfig(**kwargs)
|
||||||
|
|
||||||
def test_contstructor_basic(self):
|
def test_contstructor_basic(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertEqual(config.appname, "wutta")
|
self.assertEqual(config.appname, "wutta")
|
||||||
self.assertEqual(config.files_read, [])
|
self.assertEqual(config.files_read, [])
|
||||||
|
|
||||||
def test_constructor_valid_files(self):
|
def test_constructor_valid_files(self):
|
||||||
myfile = self.write_file("my.conf", "")
|
myfile = self.write_file("my.conf", "")
|
||||||
config = mod.WuttaConfig(files=[myfile])
|
config = conf.WuttaConfig(files=[myfile])
|
||||||
self.assertEqual(len(config.files_read), 1)
|
self.assertEqual(len(config.files_read), 1)
|
||||||
self.assertEqual(config.files_read[0], myfile)
|
self.assertEqual(config.files_read[0], myfile)
|
||||||
|
|
||||||
def test_constructor_missing_files(self):
|
def test_constructor_missing_files(self):
|
||||||
invalid = os.path.join(self.tempdir, "invalid.conf")
|
invalid = os.path.join(self.tempdir, "invalid.conf")
|
||||||
self.assertRaises(FileNotFoundError, mod.WuttaConfig, files=[invalid])
|
self.assertRaises(FileNotFoundError, conf.WuttaConfig, files=[invalid])
|
||||||
|
|
||||||
def test_constructor_required_files_are_present(self):
|
def test_constructor_required_files_are_present(self):
|
||||||
first = self.write_file(
|
first = self.write_file(
|
||||||
|
|
@ -53,7 +56,7 @@ baz = B
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
config = mod.WuttaConfig(files=[second])
|
config = conf.WuttaConfig(files=[second])
|
||||||
self.assertEqual(len(config.files_read), 2)
|
self.assertEqual(len(config.files_read), 2)
|
||||||
# nb. files_read listing is in order of "priority" which is
|
# nb. files_read listing is in order of "priority" which is
|
||||||
# same the as order in which files were initially read
|
# same the as order in which files were initially read
|
||||||
|
|
@ -74,7 +77,7 @@ baz = B
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertRaises(FileNotFoundError, mod.WuttaConfig, files=[second])
|
self.assertRaises(FileNotFoundError, conf.WuttaConfig, files=[second])
|
||||||
|
|
||||||
def test_constructor_included_files_are_present(self):
|
def test_constructor_included_files_are_present(self):
|
||||||
first = self.write_file(
|
first = self.write_file(
|
||||||
|
|
@ -97,7 +100,7 @@ baz = B
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
config = mod.WuttaConfig(files=[second])
|
config = conf.WuttaConfig(files=[second])
|
||||||
self.assertEqual(len(config.files_read), 2)
|
self.assertEqual(len(config.files_read), 2)
|
||||||
# nb. files_read listing is in order of "priority" which is
|
# nb. files_read listing is in order of "priority" which is
|
||||||
# same the as order in which files were initially read
|
# same the as order in which files were initially read
|
||||||
|
|
@ -118,7 +121,7 @@ baz = B
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
config = mod.WuttaConfig(files=[second])
|
config = conf.WuttaConfig(files=[second])
|
||||||
self.assertEqual(len(config.files_read), 1)
|
self.assertEqual(len(config.files_read), 1)
|
||||||
self.assertEqual(config.files_read[0], second)
|
self.assertEqual(config.files_read[0], second)
|
||||||
self.assertIsNone(config.get("foo.bar"))
|
self.assertIsNone(config.get("foo.bar"))
|
||||||
|
|
@ -156,7 +159,7 @@ baz = C
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
config = mod.WuttaConfig(files=[top, middle, base])
|
config = conf.WuttaConfig(files=[top, middle, base])
|
||||||
self.assertEqual(len(config.files_read), 3)
|
self.assertEqual(len(config.files_read), 3)
|
||||||
# nb. files_read listing is in order of "priority" which is
|
# nb. files_read listing is in order of "priority" which is
|
||||||
# same the as order in which files were initially read
|
# same the as order in which files were initially read
|
||||||
|
|
@ -183,7 +186,7 @@ require = %(here)s/first.conf
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
config = mod.WuttaConfig(files=[second])
|
config = conf.WuttaConfig(files=[second])
|
||||||
files = config.get_prioritized_files()
|
files = config.get_prioritized_files()
|
||||||
self.assertEqual(len(files), 2)
|
self.assertEqual(len(files), 2)
|
||||||
self.assertEqual(files[0], second)
|
self.assertEqual(files[0], second)
|
||||||
|
|
@ -199,16 +202,16 @@ baz = %(__file__)s
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
config = mod.WuttaConfig(files=[myconf])
|
config = conf.WuttaConfig(files=[myconf])
|
||||||
self.assertEqual(config.get("foo.bar"), f"{self.tempdir}/bar.txt")
|
self.assertEqual(config.get("foo.bar"), f"{self.tempdir}/bar.txt")
|
||||||
self.assertEqual(config.get("foo.baz"), myconf)
|
self.assertEqual(config.get("foo.baz"), myconf)
|
||||||
|
|
||||||
def test_constructor_defaults(self):
|
def test_constructor_defaults(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertEqual(config.defaults, {})
|
self.assertEqual(config.defaults, {})
|
||||||
self.assertIsNone(config.get("foo"))
|
self.assertIsNone(config.get("foo"))
|
||||||
|
|
||||||
config = mod.WuttaConfig(defaults={"foo": "bar"})
|
config = conf.WuttaConfig(defaults={"foo": "bar"})
|
||||||
self.assertEqual(config.defaults, {"foo": "bar"})
|
self.assertEqual(config.defaults, {"foo": "bar"})
|
||||||
self.assertEqual(config.get("foo"), "bar")
|
self.assertEqual(config.get("foo"), "bar")
|
||||||
|
|
||||||
|
|
@ -230,17 +233,17 @@ preferdb = true
|
||||||
)
|
)
|
||||||
|
|
||||||
# flags are off by default
|
# flags are off by default
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertFalse(config.usedb)
|
self.assertFalse(config.usedb)
|
||||||
self.assertFalse(config.preferdb)
|
self.assertFalse(config.preferdb)
|
||||||
|
|
||||||
# but may override via constructor
|
# but may override via constructor
|
||||||
config = mod.WuttaConfig(usedb=True, preferdb=True)
|
config = conf.WuttaConfig(usedb=True, preferdb=True)
|
||||||
self.assertTrue(config.usedb)
|
self.assertTrue(config.usedb)
|
||||||
self.assertTrue(config.preferdb)
|
self.assertTrue(config.preferdb)
|
||||||
|
|
||||||
# and also may override via config file
|
# and also may override via config file
|
||||||
config = mod.WuttaConfig(files=[myfile])
|
config = conf.WuttaConfig(files=[myfile])
|
||||||
self.assertTrue(config.usedb)
|
self.assertTrue(config.usedb)
|
||||||
self.assertTrue(config.preferdb)
|
self.assertTrue(config.preferdb)
|
||||||
|
|
||||||
|
|
@ -253,12 +256,12 @@ preferdb = true
|
||||||
pytest.skip("test is not relevant without sqlalchemy")
|
pytest.skip("test is not relevant without sqlalchemy")
|
||||||
|
|
||||||
# flags are off by default
|
# flags are off by default
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertFalse(config.usedb)
|
self.assertFalse(config.usedb)
|
||||||
self.assertFalse(config.preferdb)
|
self.assertFalse(config.preferdb)
|
||||||
|
|
||||||
# but caller may enable the flags (if sqlalchemy available)
|
# but caller may enable the flags (if sqlalchemy available)
|
||||||
config = mod.WuttaConfig(usedb=True, preferdb=True)
|
config = conf.WuttaConfig(usedb=True, preferdb=True)
|
||||||
self.assertTrue(config.usedb)
|
self.assertTrue(config.usedb)
|
||||||
self.assertTrue(config.preferdb)
|
self.assertTrue(config.preferdb)
|
||||||
|
|
||||||
|
|
@ -273,7 +276,7 @@ preferdb = true
|
||||||
return orig_import(name, *args, **kwargs)
|
return orig_import(name, *args, **kwargs)
|
||||||
|
|
||||||
with patch("builtins.__import__", side_effect=mock_import):
|
with patch("builtins.__import__", side_effect=mock_import):
|
||||||
config = mod.WuttaConfig(usedb=True, preferdb=True)
|
config = conf.WuttaConfig(usedb=True, preferdb=True)
|
||||||
self.assertFalse(config.usedb)
|
self.assertFalse(config.usedb)
|
||||||
self.assertFalse(config.preferdb)
|
self.assertFalse(config.preferdb)
|
||||||
|
|
||||||
|
|
@ -286,19 +289,19 @@ configure_logging = true
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch.object(mod.WuttaConfig, "_configure_logging") as method:
|
with patch.object(conf.WuttaConfig, "_configure_logging") as method:
|
||||||
# no logging config by default
|
# no logging config by default
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
method.assert_not_called()
|
method.assert_not_called()
|
||||||
|
|
||||||
# but may override via constructor
|
# but may override via constructor
|
||||||
method.reset_mock()
|
method.reset_mock()
|
||||||
config = mod.WuttaConfig(configure_logging=True)
|
config = conf.WuttaConfig(configure_logging=True)
|
||||||
method.assert_called_once()
|
method.assert_called_once()
|
||||||
|
|
||||||
# and also may override via config file
|
# and also may override via config file
|
||||||
method.reset_mock()
|
method.reset_mock()
|
||||||
config = mod.WuttaConfig(files=[myfile])
|
config = conf.WuttaConfig(files=[myfile])
|
||||||
method.assert_called_once()
|
method.assert_called_once()
|
||||||
|
|
||||||
def test_constructor_configures_logging(self):
|
def test_constructor_configures_logging(self):
|
||||||
|
|
@ -315,7 +318,7 @@ configure_logging = true
|
||||||
|
|
||||||
with patch("wuttjamaican.conf.logging") as logging:
|
with patch("wuttjamaican.conf.logging") as logging:
|
||||||
# basic constructor attempts logging config
|
# basic constructor attempts logging config
|
||||||
config = mod.WuttaConfig(configure_logging=True)
|
config = conf.WuttaConfig(configure_logging=True)
|
||||||
logging.config.fileConfig.assert_called_once()
|
logging.config.fileConfig.assert_called_once()
|
||||||
|
|
||||||
# if logging config fails, error is *not* raised
|
# if logging config fails, error is *not* raised
|
||||||
|
|
@ -323,18 +326,18 @@ configure_logging = true
|
||||||
logging.config.fileConfig.side_effect = configparser.NoSectionError(
|
logging.config.fileConfig.side_effect = configparser.NoSectionError(
|
||||||
"logging"
|
"logging"
|
||||||
)
|
)
|
||||||
config = mod.WuttaConfig(configure_logging=True)
|
config = conf.WuttaConfig(configure_logging=True)
|
||||||
logging.config.fileConfig.assert_called_once()
|
logging.config.fileConfig.assert_called_once()
|
||||||
|
|
||||||
# and it works if we specify config file
|
# and it works if we specify config file
|
||||||
logging.config.fileConfig.reset_mock()
|
logging.config.fileConfig.reset_mock()
|
||||||
config = mod.WuttaConfig(files=[myfile])
|
config = conf.WuttaConfig(files=[myfile])
|
||||||
logging.config.fileConfig.assert_called_once()
|
logging.config.fileConfig.assert_called_once()
|
||||||
|
|
||||||
def test_config_has_no_app_after_init(self):
|
def test_config_has_no_app_after_init(self):
|
||||||
# initial config should *not* have an app yet, otherwise
|
# initial config should *not* have an app yet, otherwise
|
||||||
# extensions cannot specify a default app handler
|
# extensions cannot specify a default app handler
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertIsNone(config._app)
|
self.assertIsNone(config._app)
|
||||||
|
|
||||||
# but after that we can get an app okay
|
# but after that we can get an app okay
|
||||||
|
|
@ -343,7 +346,7 @@ configure_logging = true
|
||||||
self.assertIs(app, config._app)
|
self.assertIs(app, config._app)
|
||||||
|
|
||||||
def test_setdefault(self):
|
def test_setdefault(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
|
|
||||||
# value is empty by default
|
# value is empty by default
|
||||||
self.assertIsNone(config.get("foo"))
|
self.assertIsNone(config.get("foo"))
|
||||||
|
|
@ -357,15 +360,15 @@ configure_logging = true
|
||||||
self.assertEqual(config.setdefault("baz", "blarg"), "blarg")
|
self.assertEqual(config.setdefault("baz", "blarg"), "blarg")
|
||||||
|
|
||||||
def test_get_require_with_default(self):
|
def test_get_require_with_default(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertRaises(ValueError, config.get, "foo", require=True, default="bar")
|
self.assertRaises(ValueError, config.get, "foo", require=True, default="bar")
|
||||||
|
|
||||||
def test_get_require_missing(self):
|
def test_get_require_missing(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertRaises(ConfigurationError, config.get, "foo", require=True)
|
self.assertRaises(ConfigurationError, config.get, "foo", require=True)
|
||||||
|
|
||||||
def test_get_with_default(self):
|
def test_get_with_default(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
# nb. returns None if no default specified
|
# nb. returns None if no default specified
|
||||||
self.assertIsNone(config.get("foo"))
|
self.assertIsNone(config.get("foo"))
|
||||||
self.assertEqual(config.get("foo", default="bar"), "bar")
|
self.assertEqual(config.get("foo", default="bar"), "bar")
|
||||||
|
|
@ -378,7 +381,7 @@ configure_logging = true
|
||||||
pytest.skip("test is not relevant without sqlalchemy")
|
pytest.skip("test is not relevant without sqlalchemy")
|
||||||
|
|
||||||
# minimal config, but at least it needs db cxn info
|
# minimal config, but at least it needs db cxn info
|
||||||
config = mod.WuttaConfig(defaults={"wutta.db.default.url": "sqlite://"})
|
config = conf.WuttaConfig(defaults={"wutta.db.default.url": "sqlite://"})
|
||||||
|
|
||||||
session = Session()
|
session = Session()
|
||||||
|
|
||||||
|
|
@ -411,17 +414,17 @@ configure_logging = true
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
def test_get_default(self):
|
def test_get_default(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertIsNone(config.get("foo"))
|
self.assertIsNone(config.get("foo"))
|
||||||
self.assertEqual(config.get("foo", default="bar"), "bar")
|
self.assertEqual(config.get("foo", default="bar"), "bar")
|
||||||
|
|
||||||
def test_get_require(self):
|
def test_get_require(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertIsNone(config.get("foo"))
|
self.assertIsNone(config.get("foo"))
|
||||||
self.assertRaises(ConfigurationError, config.get, "foo", require=True)
|
self.assertRaises(ConfigurationError, config.get, "foo", require=True)
|
||||||
|
|
||||||
def test_get_require_message(self):
|
def test_get_require_message(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertIsNone(config.get("foo"))
|
self.assertIsNone(config.get("foo"))
|
||||||
try:
|
try:
|
||||||
config.get("foo", require=True, message="makin stuff up")
|
config.get("foo", require=True, message="makin stuff up")
|
||||||
|
|
@ -436,7 +439,7 @@ configure_logging = true
|
||||||
pytest.skip("test is not relevant without sqlalchemy")
|
pytest.skip("test is not relevant without sqlalchemy")
|
||||||
|
|
||||||
# start out with a default value
|
# start out with a default value
|
||||||
config = mod.WuttaConfig(
|
config = conf.WuttaConfig(
|
||||||
defaults={"wutta.db.default.url": "sqlite://", "foo": "bar"}
|
defaults={"wutta.db.default.url": "sqlite://", "foo": "bar"}
|
||||||
)
|
)
|
||||||
self.assertEqual(config.get("foo"), "bar")
|
self.assertEqual(config.get("foo"), "bar")
|
||||||
|
|
@ -476,7 +479,7 @@ configure_logging = true
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
def test_get_ambiguous(self):
|
def test_get_ambiguous(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
|
|
||||||
# value is returned if key is not ambiguous
|
# value is returned if key is not ambiguous
|
||||||
config.setdefault("foo", "bar")
|
config.setdefault("foo", "bar")
|
||||||
|
|
@ -487,23 +490,23 @@ configure_logging = true
|
||||||
self.assertIsNone(config.get("foo"))
|
self.assertIsNone(config.get("foo"))
|
||||||
|
|
||||||
def test_require(self):
|
def test_require(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertRaises(ConfigurationError, config.require, "foo")
|
self.assertRaises(ConfigurationError, config.require, "foo")
|
||||||
|
|
||||||
def test_get_bool(self):
|
def test_get_bool(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertFalse(config.get_bool("foo.bar"))
|
self.assertFalse(config.get_bool("foo.bar"))
|
||||||
config.setdefault("foo.bar", "true")
|
config.setdefault("foo.bar", "true")
|
||||||
self.assertTrue(config.get_bool("foo.bar"))
|
self.assertTrue(config.get_bool("foo.bar"))
|
||||||
|
|
||||||
def test_get_int(self):
|
def test_get_int(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertIsNone(config.get_int("foo.bar"))
|
self.assertIsNone(config.get_int("foo.bar"))
|
||||||
config.setdefault("foo.bar", "42")
|
config.setdefault("foo.bar", "42")
|
||||||
self.assertEqual(config.get_int("foo.bar"), 42)
|
self.assertEqual(config.get_int("foo.bar"), 42)
|
||||||
|
|
||||||
def test_get_list(self):
|
def test_get_list(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertIsNone(config.get_list("foo.bar"))
|
self.assertIsNone(config.get_list("foo.bar"))
|
||||||
config.setdefault("foo.bar", "hello world")
|
config.setdefault("foo.bar", "hello world")
|
||||||
self.assertEqual(config.get_list("foo.bar"), ["hello", "world"])
|
self.assertEqual(config.get_list("foo.bar"), ["hello", "world"])
|
||||||
|
|
@ -623,7 +626,7 @@ configure_logging = true
|
||||||
|
|
||||||
def test_get_app(self):
|
def test_get_app(self):
|
||||||
# default handler
|
# default handler
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertEqual(config.default_app_handler_spec, "wuttjamaican.app:AppHandler")
|
self.assertEqual(config.default_app_handler_spec, "wuttjamaican.app:AppHandler")
|
||||||
app = config.get_app()
|
app = config.get_app()
|
||||||
self.assertIsInstance(app, AppHandler)
|
self.assertIsInstance(app, AppHandler)
|
||||||
|
|
@ -631,7 +634,7 @@ configure_logging = true
|
||||||
self.assertIs(type(app), AppHandler)
|
self.assertIs(type(app), AppHandler)
|
||||||
|
|
||||||
# custom default handler
|
# custom default handler
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
config.default_app_handler_spec = "tests.test_conf:CustomAppHandler"
|
config.default_app_handler_spec = "tests.test_conf:CustomAppHandler"
|
||||||
app = config.get_app()
|
app = config.get_app()
|
||||||
self.assertIsInstance(app, CustomAppHandler)
|
self.assertIsInstance(app, CustomAppHandler)
|
||||||
|
|
@ -643,7 +646,7 @@ configure_logging = true
|
||||||
pytest.skip("test is not relevant without sqlalchemy")
|
pytest.skip("test is not relevant without sqlalchemy")
|
||||||
|
|
||||||
# default func
|
# default func
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
config.default_engine_maker_spec,
|
config.default_engine_maker_spec,
|
||||||
"wuttjamaican.db.conf:make_engine_from_config",
|
"wuttjamaican.db.conf:make_engine_from_config",
|
||||||
|
|
@ -652,7 +655,7 @@ configure_logging = true
|
||||||
self.assertIs(make_engine, make_engine_from_config)
|
self.assertIs(make_engine, make_engine_from_config)
|
||||||
|
|
||||||
# custom default func
|
# custom default func
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
config.default_engine_maker_spec = (
|
config.default_engine_maker_spec = (
|
||||||
"tests.test_conf:custom_make_engine_from_config"
|
"tests.test_conf:custom_make_engine_from_config"
|
||||||
)
|
)
|
||||||
|
|
@ -660,7 +663,7 @@ configure_logging = true
|
||||||
self.assertIs(make_engine, custom_make_engine_from_config)
|
self.assertIs(make_engine, custom_make_engine_from_config)
|
||||||
|
|
||||||
def test_production(self):
|
def test_production(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
|
|
||||||
# false if not defined
|
# false if not defined
|
||||||
self.assertFalse(config.production())
|
self.assertFalse(config.production())
|
||||||
|
|
@ -680,15 +683,15 @@ def custom_make_engine_from_config():
|
||||||
|
|
||||||
class TestWuttaConfigExtension(TestCase):
|
class TestWuttaConfigExtension(TestCase):
|
||||||
def test_basic(self):
|
def test_basic(self):
|
||||||
config = mod.WuttaConfig()
|
config = conf.WuttaConfig()
|
||||||
ext = mod.WuttaConfigExtension()
|
ext = conf.WuttaConfigExtension()
|
||||||
self.assertIsNone(ext.key)
|
self.assertIsNone(ext.key)
|
||||||
self.assertEqual(repr(ext), "WuttaConfigExtension(key=None)")
|
self.assertEqual(repr(ext), "WuttaConfigExtension(key=None)")
|
||||||
|
|
||||||
|
|
||||||
class TestGenericDefaultFiles(TestCase):
|
class TestGenericDefaultFiles(TestCase):
|
||||||
def test_linux(self):
|
def test_linux(self):
|
||||||
files = mod.generic_default_files("wuttatest")
|
files = conf.generic_default_files("wuttatest")
|
||||||
self.assertIsInstance(files, list)
|
self.assertIsInstance(files, list)
|
||||||
self.assertTrue(len(files) > 1)
|
self.assertTrue(len(files) > 1)
|
||||||
self.assertIn("/etc/wuttatest.conf", files)
|
self.assertIn("/etc/wuttatest.conf", files)
|
||||||
|
|
@ -698,7 +701,7 @@ class TestGenericDefaultFiles(TestCase):
|
||||||
win32com.shell.SHGetSpecialFolderPath.return_value = r"C:" + os.sep
|
win32com.shell.SHGetSpecialFolderPath.return_value = r"C:" + os.sep
|
||||||
with patch.dict("sys.modules", **{"win32com.shell": win32com}):
|
with patch.dict("sys.modules", **{"win32com.shell": win32com}):
|
||||||
with patch("wuttjamaican.conf.sys", platform="win32"):
|
with patch("wuttjamaican.conf.sys", platform="win32"):
|
||||||
files = mod.generic_default_files("wuttatest")
|
files = conf.generic_default_files("wuttatest")
|
||||||
self.assertIsInstance(files, list)
|
self.assertIsInstance(files, list)
|
||||||
self.assertTrue(len(files) > 1)
|
self.assertTrue(len(files) > 1)
|
||||||
self.assertIn(os.path.join("C:", "wuttatest.conf"), files)
|
self.assertIn(os.path.join("C:", "wuttatest.conf"), files)
|
||||||
|
|
@ -713,7 +716,7 @@ class TestGenericDefaultFiles(TestCase):
|
||||||
|
|
||||||
with patch("builtins.__import__", side_effect=mock_import):
|
with patch("builtins.__import__", side_effect=mock_import):
|
||||||
with patch("wuttjamaican.conf.sys", platform="win32"):
|
with patch("wuttjamaican.conf.sys", platform="win32"):
|
||||||
files = mod.generic_default_files("wuttatest")
|
files = conf.generic_default_files("wuttatest")
|
||||||
self.assertIsInstance(files, list)
|
self.assertIsInstance(files, list)
|
||||||
self.assertEqual(len(files), 0)
|
self.assertEqual(len(files), 0)
|
||||||
|
|
||||||
|
|
@ -728,11 +731,11 @@ winsvc.RattailFileMonitor = /path/to/other/file
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
files = mod.get_config_paths(files=[myconf], winsvc="RattailFileMonitor")
|
files = conf.get_config_paths(files=[myconf], winsvc="RattailFileMonitor")
|
||||||
self.assertEqual(files, ["/path/to/other/file"])
|
self.assertEqual(files, ["/path/to/other/file"])
|
||||||
|
|
||||||
def test_nonexistent_default_files(self):
|
def test_nonexistent_default_files(self):
|
||||||
files = mod.get_config_paths(
|
files = conf.get_config_paths(
|
||||||
files=None,
|
files=None,
|
||||||
env_files_name="IGNORE_THIS",
|
env_files_name="IGNORE_THIS",
|
||||||
default_files=["/this/does/not/exist"],
|
default_files=["/this/does/not/exist"],
|
||||||
|
|
@ -752,7 +755,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
with patch("wuttjamaican.conf.WuttaConfig") as WuttaConfig:
|
with patch("wuttjamaican.conf.WuttaConfig") as WuttaConfig:
|
||||||
# generic files are used if nothing is specified
|
# generic files are used if nothing is specified
|
||||||
generic_default_files.return_value = [generic]
|
generic_default_files.return_value = [generic]
|
||||||
config = mod.make_config(appname="wuttatest", extend=False)
|
config = conf.make_config(appname="wuttatest")
|
||||||
generic_default_files.assert_called_once_with("wuttatest")
|
generic_default_files.assert_called_once_with("wuttatest")
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[generic], appname="wuttatest", usedb=None, preferdb=None
|
[generic], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -762,7 +765,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
generic_default_files.return_value = []
|
generic_default_files.return_value = []
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config(appname="wuttatest", extend=False)
|
config = conf.make_config(appname="wuttatest")
|
||||||
generic_default_files.assert_called_once_with("wuttatest")
|
generic_default_files.assert_called_once_with("wuttatest")
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[], appname="wuttatest", usedb=None, preferdb=None
|
[], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -776,7 +779,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
with patch("wuttjamaican.conf.WuttaConfig") as WuttaConfig:
|
with patch("wuttjamaican.conf.WuttaConfig") as WuttaConfig:
|
||||||
# generic defaults are used if nothing specified
|
# generic defaults are used if nothing specified
|
||||||
generic_default_files.return_value = [generic]
|
generic_default_files.return_value = [generic]
|
||||||
config = mod.make_config(appname="wuttatest", extend=False)
|
config = conf.make_config(appname="wuttatest")
|
||||||
generic_default_files.assert_called_once_with("wuttatest")
|
generic_default_files.assert_called_once_with("wuttatest")
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[generic], appname="wuttatest", usedb=None, preferdb=None
|
[generic], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -785,9 +788,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
# can specify single default file
|
# can specify single default file
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config(
|
config = conf.make_config(appname="wuttatest", default_files=myfile)
|
||||||
appname="wuttatest", default_files=myfile, extend=False
|
|
||||||
)
|
|
||||||
generic_default_files.assert_not_called()
|
generic_default_files.assert_not_called()
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -796,9 +797,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
# can specify default files as list
|
# can specify default files as list
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config(
|
config = conf.make_config(appname="wuttatest", default_files=[myfile])
|
||||||
appname="wuttatest", default_files=[myfile], extend=False
|
|
||||||
)
|
|
||||||
generic_default_files.assert_not_called()
|
generic_default_files.assert_not_called()
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -807,10 +806,8 @@ class TestMakeConfig(FileTestCase):
|
||||||
# can specify default files as callable
|
# can specify default files as callable
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config(
|
config = conf.make_config(
|
||||||
appname="wuttatest",
|
appname="wuttatest", default_files=lambda appname: [myfile]
|
||||||
default_files=lambda appname: [myfile],
|
|
||||||
extend=False,
|
|
||||||
)
|
)
|
||||||
generic_default_files.assert_not_called()
|
generic_default_files.assert_not_called()
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
|
|
@ -826,7 +823,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
generic_default_files.return_value = [generic]
|
generic_default_files.return_value = [generic]
|
||||||
|
|
||||||
# no plus files by default
|
# no plus files by default
|
||||||
config = mod.make_config(appname="wuttatest", extend=False)
|
config = conf.make_config(appname="wuttatest")
|
||||||
generic_default_files.assert_called_once_with("wuttatest")
|
generic_default_files.assert_called_once_with("wuttatest")
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[generic], appname="wuttatest", usedb=None, preferdb=None
|
[generic], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -835,9 +832,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
# can specify single plus file
|
# can specify single plus file
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config(
|
config = conf.make_config(appname="wuttatest", plus_files=myfile)
|
||||||
appname="wuttatest", plus_files=myfile, extend=False
|
|
||||||
)
|
|
||||||
generic_default_files.assert_called_once_with("wuttatest")
|
generic_default_files.assert_called_once_with("wuttatest")
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[generic, myfile], appname="wuttatest", usedb=None, preferdb=None
|
[generic, myfile], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -846,9 +841,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
# can specify plus files as list
|
# can specify plus files as list
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config(
|
config = conf.make_config(appname="wuttatest", plus_files=[myfile])
|
||||||
appname="wuttatest", plus_files=[myfile], extend=False
|
|
||||||
)
|
|
||||||
generic_default_files.assert_called_once_with("wuttatest")
|
generic_default_files.assert_called_once_with("wuttatest")
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[generic, myfile], appname="wuttatest", usedb=None, preferdb=None
|
[generic, myfile], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -857,10 +850,8 @@ class TestMakeConfig(FileTestCase):
|
||||||
# can specify plus files via env
|
# can specify plus files via env
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config(
|
config = conf.make_config(
|
||||||
appname="wuttatest",
|
appname="wuttatest", env={"WUTTATEST_CONFIG_PLUS_FILES": myfile}
|
||||||
env={"WUTTATEST_CONFIG_PLUS_FILES": myfile},
|
|
||||||
extend=False,
|
|
||||||
)
|
)
|
||||||
generic_default_files.assert_called_once_with("wuttatest")
|
generic_default_files.assert_called_once_with("wuttatest")
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
|
|
@ -876,7 +867,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
generic_default_files.return_value = [generic]
|
generic_default_files.return_value = [generic]
|
||||||
|
|
||||||
# generic files by default
|
# generic files by default
|
||||||
config = mod.make_config(appname="wuttatest", extend=False)
|
config = conf.make_config(appname="wuttatest")
|
||||||
generic_default_files.assert_called_once_with("wuttatest")
|
generic_default_files.assert_called_once_with("wuttatest")
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[generic], appname="wuttatest", usedb=None, preferdb=None
|
[generic], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -885,7 +876,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
# can specify single primary file (nb. no default files)
|
# can specify single primary file (nb. no default files)
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config(myfile, appname="wuttatest", extend=False)
|
config = conf.make_config(myfile, appname="wuttatest")
|
||||||
generic_default_files.assert_not_called()
|
generic_default_files.assert_not_called()
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -894,7 +885,7 @@ class TestMakeConfig(FileTestCase):
|
||||||
# can specify primary files as list
|
# can specify primary files as list
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config([myfile], appname="wuttatest", extend=False)
|
config = conf.make_config([myfile], appname="wuttatest")
|
||||||
generic_default_files.assert_not_called()
|
generic_default_files.assert_not_called()
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
[myfile], appname="wuttatest", usedb=None, preferdb=None
|
||||||
|
|
@ -903,10 +894,8 @@ class TestMakeConfig(FileTestCase):
|
||||||
# can specify primary files via env
|
# can specify primary files via env
|
||||||
generic_default_files.reset_mock()
|
generic_default_files.reset_mock()
|
||||||
WuttaConfig.reset_mock()
|
WuttaConfig.reset_mock()
|
||||||
config = mod.make_config(
|
config = conf.make_config(
|
||||||
appname="wuttatest",
|
appname="wuttatest", env={"WUTTATEST_CONFIG_FILES": myfile}
|
||||||
env={"WUTTATEST_CONFIG_FILES": myfile},
|
|
||||||
extend=False,
|
|
||||||
)
|
)
|
||||||
generic_default_files.assert_not_called()
|
generic_default_files.assert_not_called()
|
||||||
WuttaConfig.assert_called_once_with(
|
WuttaConfig.assert_called_once_with(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue