Skip to content

Commit 638c2bc

Browse files
committed
email settings
1 parent 83c388a commit 638c2bc

File tree

3 files changed

+115
-61
lines changed

3 files changed

+115
-61
lines changed

packages/settings-library/src/settings_library/efs.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
from pathlib import Path
2+
from typing import Annotated
23

34
from pydantic import Field
45

56
from .base import BaseCustomSettings
67

78

89
class AwsEfsSettings(BaseCustomSettings):
9-
EFS_DNS_NAME: str = Field(
10-
description="AWS Elastic File System DNS name",
11-
examples=["fs-xxx.efs.us-east-1.amazonaws.com"],
12-
)
10+
EFS_DNS_NAME: Annotated[
11+
str,
12+
Field(
13+
description="AWS Elastic File System DNS name",
14+
examples=["fs-xxx.efs.us-east-1.amazonaws.com"],
15+
),
16+
]
1317
EFS_PROJECT_SPECIFIC_DATA_DIRECTORY: str
14-
EFS_MOUNTED_PATH: Path = Field(
15-
description="This is the path where EFS is mounted to the EC2 machine",
16-
)
18+
EFS_MOUNTED_PATH: Annotated[
19+
Path,
20+
Field(
21+
description="This is the path where EFS is mounted to the EC2 machine",
22+
),
23+
]
1724

1825

1926
NFS_PROTOCOL = "4.1"

packages/settings-library/src/settings_library/email.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import Enum
2-
from typing import Self
2+
from typing import Annotated, Self
33

44
from pydantic import model_validator
55
from pydantic.fields import Field
@@ -25,12 +25,15 @@ class SMTPSettings(BaseCustomSettings):
2525

2626
SMTP_HOST: str
2727
SMTP_PORT: PortInt
28-
SMTP_PROTOCOL: EmailProtocol = Field(
29-
EmailProtocol.UNENCRYPTED,
30-
description="Select between TLS, STARTTLS Secure Mode or unencrypted communication",
31-
)
32-
SMTP_USERNAME: str | None = Field(None, min_length=1)
33-
SMTP_PASSWORD: SecretStr | None = Field(None, min_length=1)
28+
SMTP_PROTOCOL: Annotated[
29+
EmailProtocol,
30+
Field(
31+
description="Select between TLS, STARTTLS Secure Mode or unencrypted communication",
32+
),
33+
] = EmailProtocol.UNENCRYPTED
34+
35+
SMTP_USERNAME: Annotated[str | None, Field(min_length=1)] = None
36+
SMTP_PASSWORD: Annotated[SecretStr | None, Field(min_length=1)] = None
3437

3538
@model_validator(mode="after")
3639
def _both_credentials_must_be_set(self) -> Self:

packages/settings-library/tests/test_email.py

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
# pylint: disable=unused-variable
44
# pylint: disable=too-many-arguments
55

6+
from enum import Enum
67
from typing import Any
78

89
import pytest
910
from pydantic import ValidationError
10-
from pytest_simcore.helpers.monkeypatch_envs import delenvs_from_dict
11+
from pytest_simcore.helpers.monkeypatch_envs import delenvs_from_dict, setenvs_from_dict
1112
from pytest_simcore.helpers.typing_env import EnvVarsDict
1213
from settings_library.email import EmailProtocol, SMTPSettings
1314

@@ -35,7 +36,7 @@ def all_env_devel_undefined(
3536
{
3637
"SMTP_HOST": "test",
3738
"SMTP_PORT": 113,
38-
"SMTP_PROTOCOL": EmailProtocol.UNENCRYPTED,
39+
"SMTP_PROTOCOL": EmailProtocol.UNENCRYPTED.value,
3940
},
4041
{
4142
"SMTP_HOST": "test",
@@ -48,71 +49,114 @@ def all_env_devel_undefined(
4849
"SMTP_PORT": 113,
4950
"SMTP_USERNAME": "test",
5051
"SMTP_PASSWORD": "test",
51-
"SMTP_PROTOCOL": EmailProtocol.UNENCRYPTED,
52+
"SMTP_PROTOCOL": EmailProtocol.UNENCRYPTED.value,
5253
},
5354
{
5455
"SMTP_HOST": "test",
5556
"SMTP_PORT": 113,
5657
"SMTP_USERNAME": "test",
5758
"SMTP_PASSWORD": "test",
58-
"SMTP_PROTOCOL": EmailProtocol.TLS,
59+
"SMTP_PROTOCOL": EmailProtocol.TLS.value,
5960
},
6061
{
6162
"SMTP_HOST": "test",
6263
"SMTP_PORT": 113,
6364
"SMTP_USERNAME": "test",
6465
"SMTP_PASSWORD": "test",
65-
"SMTP_PROTOCOL": EmailProtocol.STARTTLS,
66+
"SMTP_PROTOCOL": EmailProtocol.STARTTLS.value,
6667
},
6768
],
6869
)
69-
def test_smtp_configuration_ok(cfg: dict[str, Any], all_env_devel_undefined: None):
70+
def test_smtp_configuration_ok(
71+
all_env_devel_undefined: None,
72+
monkeypatch: pytest.MonkeyPatch,
73+
cfg: dict[str, Any],
74+
):
7075
assert SMTPSettings.model_validate(cfg)
7176

77+
setenvs_from_dict(monkeypatch, {k: f"{v}" for k, v in cfg.items()})
78+
assert SMTPSettings.create_from_envs()
79+
7280

7381
@pytest.mark.parametrize(
74-
"cfg",
82+
"cfg,error_type",
7583
[
76-
{
77-
"SMTP_HOST": "test",
78-
"SMTP_PORT": 111,
79-
"SMTP_USERNAME": "test",
80-
# password required if username provided
81-
},
82-
{
83-
"SMTP_HOST": "test",
84-
"SMTP_PORT": 112,
85-
"SMTP_PASSWORD": "test",
86-
# username required if password provided
87-
},
88-
{
89-
"SMTP_HOST": "test",
90-
"SMTP_PORT": 113,
91-
"SMTP_PROTOCOL": EmailProtocol.STARTTLS,
92-
"SMTP_PASSWORD": "test",
93-
},
94-
{
95-
"SMTP_HOST": "test",
96-
"SMTP_PORT": 114,
97-
"SMTP_PROTOCOL": EmailProtocol.STARTTLS,
98-
"SMTP_USERNAME": "test",
99-
},
100-
{
101-
"SMTP_HOST": "test",
102-
"SMTP_PORT": 115,
103-
"SMTP_USERNAME": "",
104-
"SMTP_PASSWORD": "test",
105-
"SMTP_PROTOCOL": EmailProtocol.STARTTLS,
106-
},
107-
{
108-
"SMTP_HOST": "test",
109-
"SMTP_PORT": 116,
110-
"SMTP_USERNAME": "",
111-
"SMTP_PASSWORD": "test",
112-
"SMTP_PROTOCOL": EmailProtocol.TLS,
113-
},
84+
(
85+
{
86+
"SMTP_HOST": "test",
87+
"SMTP_PORT": 111,
88+
"SMTP_USERNAME": "test",
89+
# password required if username provided
90+
},
91+
"value_error",
92+
),
93+
(
94+
{
95+
"SMTP_HOST": "test",
96+
"SMTP_PORT": 112,
97+
"SMTP_PASSWORD": "test",
98+
# username required if password provided
99+
},
100+
"value_error",
101+
),
102+
(
103+
{
104+
"SMTP_HOST": "test",
105+
"SMTP_PORT": 113,
106+
"SMTP_PROTOCOL": EmailProtocol.STARTTLS,
107+
"SMTP_PASSWORD": "test",
108+
},
109+
"value_error",
110+
),
111+
(
112+
{
113+
"SMTP_HOST": "test",
114+
"SMTP_PORT": 114,
115+
"SMTP_PROTOCOL": EmailProtocol.STARTTLS,
116+
"SMTP_USERNAME": "test",
117+
},
118+
"value_error",
119+
),
120+
(
121+
{
122+
"SMTP_HOST": "test",
123+
"SMTP_PORT": 115,
124+
"SMTP_USERNAME": "",
125+
"SMTP_PASSWORD": "test",
126+
"SMTP_PROTOCOL": EmailProtocol.STARTTLS,
127+
},
128+
"string_too_short",
129+
),
130+
(
131+
{
132+
"SMTP_HOST": "test",
133+
"SMTP_PORT": 116,
134+
"SMTP_USERNAME": "",
135+
"SMTP_PASSWORD": "test",
136+
"SMTP_PROTOCOL": EmailProtocol.TLS,
137+
},
138+
"string_too_short",
139+
),
114140
],
115141
)
116-
def test_smtp_configuration_fails(cfg: dict[str, Any], all_env_devel_undefined: None):
117-
with pytest.raises(ValidationError):
142+
def test_smtp_configuration_fails(
143+
all_env_devel_undefined: None,
144+
monkeypatch: pytest.MonkeyPatch,
145+
cfg: dict[str, Any],
146+
error_type: str,
147+
):
148+
with pytest.raises(ValidationError) as err_info:
118149
SMTPSettings(**cfg)
150+
151+
assert err_info.value.error_count() == 1
152+
assert err_info.value.errors()[0]["type"] == error_type
153+
154+
setenvs_from_dict(
155+
monkeypatch,
156+
{k: str(v.value if isinstance(v, Enum) else v) for k, v in cfg.items()},
157+
)
158+
with pytest.raises(ValidationError) as err_info:
159+
SMTPSettings.create_from_envs()
160+
161+
assert err_info.value.error_count() == 1
162+
assert err_info.value.errors()[0]["type"] == error_type

0 commit comments

Comments
 (0)