fix(config): return None if env var is empty or non-existent #315

Merged
luketainton merged 4 commits from 278-bug-handle-empty-or-undefined-environment-variables-for-approved-usersroomsdomains into main 2024-11-21 23:26:07 +01:00
2 changed files with 73 additions and 21 deletions
Showing only changes of commit ff6dd431e1 - Show all commits

View File

@ -6,30 +6,30 @@
import os import os
config_vars: dict = {
"APP_VERSION": "dev",
"BOT_NAME": "TestBot",
"WEBEX_API_KEY": "testing",
"ADMIN_FIRST_NAME": "Test",
"ADMIN_EMAIL": "test@test.com",
"N8N_WEBHOOK_URL": "https://n8n.test.com/webhook/abcdefg",
"SENTRY_ENABLED": "false",
"SENTRY_DSN": "http://localhost",
"APPROVED_USERS": "test@test.com",
"APPROVED_DOMAINS": "test.com",
"APPROVED_ROOMS": "test",
}
for config_var, value in config_vars.items():
os.environ[config_var] = value
# needs to be imported AFTER environment variables are set
from app.utils.config import config # pragma: no cover
def test_config() -> None: def test_config() -> None:
"""Test config module.""" """Test config module."""
config_vars: dict = {
"APP_VERSION": "dev",
"BOT_NAME": "TestBot",
"WEBEX_API_KEY": "testing",
"ADMIN_FIRST_NAME": "Test",
"ADMIN_EMAIL": "test@test.com",
"N8N_WEBHOOK_URL": "https://n8n.test.com/webhook/abcdefg",
"SENTRY_ENABLED": "false",
"SENTRY_DSN": "http://localhost",
"APPROVED_USERS": "test@test.com",
"APPROVED_DOMAINS": "test.com",
"APPROVED_ROOMS": "test",
}
for config_var, value in config_vars.items():
os.environ[config_var] = value
# needs to be imported AFTER environment variables are set
from app.utils.config import config # pragma: no cover
assert config.admin_emails == config_vars["ADMIN_EMAIL"].split(",") assert config.admin_emails == config_vars["ADMIN_EMAIL"].split(",")
assert config.admin_first_name == config_vars["ADMIN_FIRST_NAME"] assert config.admin_first_name == config_vars["ADMIN_FIRST_NAME"]
assert config.approved_domains == config_vars["APPROVED_DOMAINS"].split(",") assert config.approved_domains == config_vars["APPROVED_DOMAINS"].split(",")
@ -47,3 +47,6 @@ def test_config() -> None:
assert config.sentry_dsn == config_vars["SENTRY_DSN"] assert config.sentry_dsn == config_vars["SENTRY_DSN"]
else: else:
assert config.sentry_dsn == "" assert config.sentry_dsn == ""
for config_var in config_vars:
os.environ.pop(config_var, None)

49
tests/test_config_2.py Normal file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env python3
# ruff: noqa: E402 pylint: disable=wrong-import-position
"""Provides test cases for app/utils/config.py."""
import os
def test_config_no_admin_vars() -> None:
"""Test config module."""
config_vars: dict = {
"APP_VERSION": "dev",
"BOT_NAME": "TestBot",
"WEBEX_API_KEY": "testing",
"ADMIN_FIRST_NAME": "Test",
"ADMIN_EMAIL": "test@test.com",
"N8N_WEBHOOK_URL": "https://n8n.test.com/webhook/abcdefg",
"SENTRY_ENABLED": "false",
"SENTRY_DSN": "http://localhost",
}
for config_var, value in config_vars.items():
os.environ[config_var] = value
# needs to be imported AFTER environment variables are set
from app.utils.config import config # pragma: no cover
assert config.approved_domains is None
assert config.approved_rooms is None
assert config.approved_users is None
assert config.admin_emails == config_vars["ADMIN_EMAIL"].split(",")
assert config.admin_first_name == config_vars["ADMIN_FIRST_NAME"]
assert config.bot_name == config_vars["BOT_NAME"]
assert config.n8n_webhook_url == config_vars["N8N_WEBHOOK_URL"]
assert config.sentry_enabled == bool(
config_vars["SENTRY_ENABLED"].upper() == "TRUE"
)
assert config.version == config_vars["APP_VERSION"]
assert config.webex_token == config_vars["WEBEX_API_KEY"]
if config.sentry_enabled:
assert config.sentry_dsn == config_vars["SENTRY_DSN"]
else:
assert config.sentry_dsn == ""
for config_var in config_vars:
os.environ.pop(config_var, None)