Skip to content

Commit 303daf6

Browse files
committed
Ensure Actions are validated before app startup
1 parent 7272117 commit 303daf6

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

jbi/configuration.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Parsing and validating the YAML configuration occurs within this module
33
"""
44
import logging
5-
from functools import lru_cache
65

76
from pydantic import ValidationError
87
from pydantic_yaml import parse_yaml_raw_as
@@ -18,12 +17,6 @@ class ConfigError(Exception):
1817
"""Error when an exception occurs during processing config"""
1918

2019

21-
@lru_cache
22-
def get_actions() -> Actions:
23-
"""Load actions from file determined by ENV name"""
24-
return get_actions_from_file(f"config/config.{settings.env}.yaml")
25-
26-
2720
def get_actions_from_file(jbi_config_file: str) -> Actions:
2821
"""Convert and validate YAML configuration to `Action` objects"""
2922
try:
@@ -34,3 +27,10 @@ def get_actions_from_file(jbi_config_file: str) -> Actions:
3427
except ValidationError as exception:
3528
logger.exception(exception)
3629
raise ConfigError("Errors exist.") from exception
30+
31+
32+
def get_actions(env=settings.env):
33+
return get_actions_from_file(f"config/config.{env}.yaml")
34+
35+
36+
ACTIONS = get_actions()

jbi/router.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
from fastapi.responses import HTMLResponse
1010
from fastapi.templating import Jinja2Templates
1111

12-
from jbi.configuration import get_actions
12+
from jbi.configuration import ACTIONS
1313
from jbi.environment import Settings, get_settings, get_version
1414
from jbi.models import Actions, BugzillaWebhookRequest
1515
from jbi.runner import IgnoreInvalidRequestError, execute_action
1616
from jbi.services import bugzilla, jira
1717

1818
SettingsDep = Annotated[Settings, Depends(get_settings)]
19-
ActionsDep = Annotated[Actions, Depends(get_actions)]
19+
ActionsDep = Annotated[Actions, Depends(lambda: ACTIONS)]
2020
VersionDep = Annotated[dict, Depends(get_version)]
2121
BugzillaServiceDep = Annotated[bugzilla.BugzillaService, Depends(bugzilla.get_service)]
2222
JiraServiceDep = Annotated[jira.JiraService, Depends(jira.get_service)]

tests/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def settings():
8383

8484
@pytest.fixture(autouse=True)
8585
def actions():
86-
get_actions.cache_clear()
87-
return get_actions()
86+
return get_actions
8887

8988

9089
@pytest.fixture(autouse=True)

tests/unit/test_configuration.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from unittest import mock
2-
31
import pytest
42

5-
from jbi import configuration, environment
3+
from jbi import configuration
64

75

86
def test_mock_jbi_files():
@@ -22,8 +20,10 @@ def test_actual_jbi_files():
2220
)
2321

2422

25-
def test_filename_uses_env():
26-
configuration.get_actions.cache_clear()
27-
with mock.patch("jbi.configuration.get_actions_from_file") as mocked:
28-
configuration.get_actions()
29-
mocked.assert_called_with("config/config.local.yaml")
23+
def test_filename_uses_env(mocker, actions, settings):
24+
get_actions_from_file_spy = mocker.spy(configuration, "get_actions_from_file")
25+
assert settings.env == "local"
26+
27+
configuration.get_actions()
28+
29+
get_actions_from_file_spy.assert_called_with("config/config.local.yaml")

0 commit comments

Comments
 (0)