33# pylint: disable=unused-variable
44# pylint: disable=too-many-arguments
55
6+ from enum import Enum
67from typing import Any
78
89import pytest
910from 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
1112from pytest_simcore .helpers .typing_env import EnvVarsDict
1213from 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