diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | kerbana/__init__.py | 40 | ||||
-rw-r--r-- | tests/test_app.py | 13 |
3 files changed, 45 insertions, 10 deletions
@@ -1,4 +1,6 @@ *.pyc .coverage +kerbana.toml + .*.sw? diff --git a/kerbana/__init__.py b/kerbana/__init__.py index 920e730..ea7f5cf 100644 --- a/kerbana/__init__.py +++ b/kerbana/__init__.py @@ -1,7 +1,8 @@ +import os from typing import Optional import flask -import tomllib +import toml from . import config @@ -12,15 +13,34 @@ def create_app(test_config: Optional[config.Config] = None): app.config.from_object(config.DefaultConfig) if test_config is None: - app.config.from_file( - "/etc/kerbana/kerbana.toml", - load=tomllib.load, - ) - app.config.from_file( - "kerbana.toml", - load=tomllib.load, - ) - app.config.from_envvar("KERBANA_CONFIG") + try: + app.config.from_file( + "/etc/kerbana/kerbana.toml", + load=toml.load, + ) + except FileNotFoundError: + app.logger.debug("File /etc/kerbana/kerbana.toml not found.") + try: + app.config.from_file( + os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "..", + "kerbana.toml", + ), + load=toml.load, + ) + except FileNotFoundError: + app.logger.debug("File kerbana.toml not found.") + try: + app.config.from_envvar("KERBANA_CONFIG") + except RuntimeError as e: + app.logger.debug(e) + except FileNotFoundError: + app.logger.debug( + "File {} (as found in $KERBANA_CONFIG) not found.".format( + os.environ["KERBANA_CONFIG"] + ) + ) app.config.from_prefixed_env(prefix="KERBANA_") else: app.config.from_object(test_config) diff --git a/tests/test_app.py b/tests/test_app.py index 6d96fbb..6cf29c5 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,4 +1,6 @@ +import os import unittest +import unittest.mock from kerbana import config, create_app @@ -12,3 +14,14 @@ class TestBase(unittest.TestCase): def test_root(self): res = self.client.get("/") self.assertEqual("Hello World!", res.data.decode()) + + +class TestConfig(unittest.TestCase): + def test_default_config(self): + app = create_app() + self.assertEqual(app.config["SECRET_KEY"], "dev") + + @unittest.mock.patch.dict(os.environ, {"KERBANA_CONFIG": "no_such_file"}) + def test_kerbana_config_env_non_existing(self): + app = create_app() + self.assertEqual(app.config["SECRET_KEY"], "dev") |