Skip to content

Commit

Permalink
config
Browse files Browse the repository at this point in the history
  • Loading branch information
srh-sloan committed Sep 19, 2024
1 parent 48ed534 commit 06db131
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
16 changes: 12 additions & 4 deletions fsd_utils/config/commonconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@


class CommonConfig:
def _resolve_secret_key(flask_env: str = None) -> str:
secret_key = os.getenv("SECRET_KEY", None)
if not secret_key:
if flask_env in ["dev", "test", "uat", "production"]:
raise KeyError("SECRET_KEY is not present in environment")
secret_key = "dev-secret" # pragma: allowlist secret
return secret_key

FSD_LOG_LEVELS = {
"development": logging.DEBUG,
Expand All @@ -15,12 +22,13 @@ class CommonConfig:
# ---------------
# General App Config
# ---------------
FLASK_ENV = os.getenv("FLASK_ENV")

FLASK_ENV = os.getenv("FLASK_ENV", None)
if not FLASK_ENV:
raise KeyError("FLASK_ENV is not present in environment")
SECRET_KEY = os.getenv("SECRET_KEY")
if not SECRET_KEY and FLASK_ENV not in ["development", "unit_test"]:
raise KeyError("SECRET_KEY is not present in environment")

SECRET_KEY = _resolve_secret_key(FLASK_ENV)

SESSION_COOKIE_NAME = os.getenv("SESSION_COOKIE_NAME", "session_cookie")
try:
FSD_LOG_LEVEL = FSD_LOG_LEVELS["FLASK_ENV"]
Expand Down
48 changes: 48 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
from unittest import mock

import pytest
from fsd_utils.config.commonconfig import CommonConfig


@pytest.mark.parametrize(
"flask_env, env_secret_key, exp_secret_key",
[
("dev", "dev_key", "dev_key"),
("development", "dev_key", "dev_key"),
("uat", "abc123", "abc123"),
("development", "", "dev-secret"),
("db_migrations", "", "dev-secret"),
],
)
def test_config(flask_env, env_secret_key, exp_secret_key):
with mock.patch.dict(os.environ, {"SECRET_KEY": env_secret_key}):
result = CommonConfig._resolve_secret_key(flask_env)
assert result == exp_secret_key


@pytest.mark.parametrize(
"flask_env, env_secret_key",
[
(
"dev",
"",
),
(
"test",
"",
),
(
"uat",
"",
),
(
"production",
"",
),
],
)
def test_config_error(flask_env, env_secret_key):
with mock.patch.dict(os.environ, {"SECRET_KEY": env_secret_key}):
with pytest.raises(KeyError):
CommonConfig._resolve_secret_key(flask_env)

0 comments on commit 06db131

Please sign in to comment.