Skip to content

Commit a37d89a

Browse files
authored
Fix Sentry url parsing (#725)
* Use `AnyUrl` directly, rather than subclassing to provide an alias Also: * Use built-in StrEnum * Move environment tests into environment module
1 parent ac064a0 commit a37d89a

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

jbi/environment.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
Module dedicated to interacting with the environment (variables, version.json)
33
"""
44
import json
5-
from enum import Enum
5+
6+
# https://github.com/python/mypy/issues/12841
7+
from enum import StrEnum, auto # type: ignore
68
from functools import lru_cache
79
from pathlib import Path
810
from typing import Optional
@@ -11,20 +13,12 @@
1113
from pydantic_settings import BaseSettings, SettingsConfigDict
1214

1315

14-
class Environment(str, Enum):
16+
class Environment(StrEnum):
1517
"""Production environment choices"""
1618

17-
LOCAL = "local"
18-
NONPROD = "nonprod"
19-
PROD = "prod"
20-
21-
def __str__(self):
22-
# Force enum string representation to be the value instead 'Environment.NAME'
23-
return str(self._value_) # # pylint: disable=no-member
24-
25-
26-
class SentryDsn(AnyUrl):
27-
"""Url type to validate Sentry DSN"""
19+
LOCAL = auto()
20+
NONPROD = auto()
21+
PROD = auto()
2822

2923

3024
class Settings(BaseSettings):
@@ -35,7 +29,8 @@ class Settings(BaseSettings):
3529
app_reload: bool = False
3630
app_debug: bool = False
3731
max_retries: int = 3
38-
env: Environment = Environment.NONPROD
32+
# https://github.com/python/mypy/issues/12841
33+
env: Environment = Environment.NONPROD # type: ignore
3934

4035
# Jira
4136
jira_base_url: str = "https://mozit-test.atlassian.net/"
@@ -51,7 +46,7 @@ class Settings(BaseSettings):
5146
log_format: str = "json" # set to "text" for human-readable logs
5247

5348
# Sentry
54-
sentry_dsn: Optional[SentryDsn] = None
49+
sentry_dsn: Optional[AnyUrl] = None
5550
sentry_traces_sample_rate: float = 1.0
5651

5752
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8")

tests/unit/test_configuration.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,3 @@ def test_filename_uses_env():
2727
with mock.patch("jbi.configuration.get_actions_from_file") as mocked:
2828
configuration.get_actions()
2929
mocked.assert_called_with("config/config.local.yaml")
30-
31-
32-
def test_settings_env_is_enum_string():
33-
settings = environment.Settings()
34-
settings.env = environment.Environment.PROD
35-
36-
assert settings.env == "prod"
37-
assert str(settings.env) == "prod"

tests/unit/test_environment.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Tests for our application environment config parsing
2+
3+
Pytest overwrites your local environment with the values set by options in
4+
`tool.pytest.ini_options`
5+
"""
6+
7+
import pydantic
8+
import pytest
9+
10+
from jbi.environment import Environment, Settings
11+
12+
13+
def test_settings_env_is_enum_string():
14+
settings = Settings(env=Environment.PROD)
15+
16+
assert settings.env == "prod"
17+
assert str(settings.env) == "prod"
18+
19+
20+
def test_sentry_dsn():
21+
settings = Settings(sentry_dsn="http://www.example.com/")
22+
23+
24+
def test_sentry_dsn_no_url_string_raises():
25+
with pytest.raises(pydantic.ValidationError):
26+
settings = Settings(sentry_dsn="foobar")

0 commit comments

Comments
 (0)