@@ -240,15 +240,50 @@ def test_send_mime_conn_id(self, mock_hook, mock_smtp):
240
240
241
241
@mock .patch ("smtplib.SMTP_SSL" )
242
242
@mock .patch ("smtplib.SMTP" )
243
- def test_send_mime_ssl (self , mock_smtp , mock_smtp_ssl ):
243
+ def test_send_mime_ssl_none_context (self , mock_smtp , mock_smtp_ssl ):
244
+ mock_smtp_ssl .return_value = mock .Mock ()
245
+ with conf_vars ({("smtp" , "smtp_ssl" ): "True" , ("email" , "ssl_context" ): "none" }):
246
+ email .send_mime_email ("from" , "to" , MIMEMultipart (), dryrun = False )
247
+ assert not mock_smtp .called
248
+ mock_smtp_ssl .assert_called_once_with (
249
+ host = conf .get ("smtp" , "SMTP_HOST" ),
250
+ port = conf .getint ("smtp" , "SMTP_PORT" ),
251
+ timeout = conf .getint ("smtp" , "SMTP_TIMEOUT" ),
252
+ context = None ,
253
+ )
254
+
255
+ @mock .patch ("smtplib.SMTP_SSL" )
256
+ @mock .patch ("smtplib.SMTP" )
257
+ @mock .patch ("ssl.create_default_context" )
258
+ def test_send_mime_ssl_default_context_if_not_set (self , create_default_context , mock_smtp , mock_smtp_ssl ):
244
259
mock_smtp_ssl .return_value = mock .Mock ()
245
260
with conf_vars ({("smtp" , "smtp_ssl" ): "True" }):
246
261
email .send_mime_email ("from" , "to" , MIMEMultipart (), dryrun = False )
247
262
assert not mock_smtp .called
263
+ assert create_default_context .called
248
264
mock_smtp_ssl .assert_called_once_with (
249
265
host = conf .get ("smtp" , "SMTP_HOST" ),
250
266
port = conf .getint ("smtp" , "SMTP_PORT" ),
251
267
timeout = conf .getint ("smtp" , "SMTP_TIMEOUT" ),
268
+ context = create_default_context .return_value ,
269
+ )
270
+
271
+ @mock .patch ("smtplib.SMTP_SSL" )
272
+ @mock .patch ("smtplib.SMTP" )
273
+ @mock .patch ("ssl.create_default_context" )
274
+ def test_send_mime_ssl_default_context_with_value_set_to_default (
275
+ self , create_default_context , mock_smtp , mock_smtp_ssl
276
+ ):
277
+ mock_smtp_ssl .return_value = mock .Mock ()
278
+ with conf_vars ({("smtp" , "smtp_ssl" ): "True" , ("email" , "ssl_context" ): "default" }):
279
+ email .send_mime_email ("from" , "to" , MIMEMultipart (), dryrun = False )
280
+ assert not mock_smtp .called
281
+ assert create_default_context .called
282
+ mock_smtp_ssl .assert_called_once_with (
283
+ host = conf .get ("smtp" , "SMTP_HOST" ),
284
+ port = conf .getint ("smtp" , "SMTP_PORT" ),
285
+ timeout = conf .getint ("smtp" , "SMTP_TIMEOUT" ),
286
+ context = create_default_context .return_value ,
252
287
)
253
288
254
289
@mock .patch ("smtplib.SMTP_SSL" )
@@ -284,7 +319,6 @@ def test_send_mime_complete_failure(self, mock_smtp: mock.Mock, mock_smtp_ssl: m
284
319
msg = MIMEMultipart ()
285
320
with pytest .raises (SMTPServerDisconnected ):
286
321
email .send_mime_email ("from" , "to" , msg , dryrun = False )
287
-
288
322
mock_smtp .assert_any_call (
289
323
host = conf .get ("smtp" , "SMTP_HOST" ),
290
324
port = conf .getint ("smtp" , "SMTP_PORT" ),
@@ -299,7 +333,8 @@ def test_send_mime_complete_failure(self, mock_smtp: mock.Mock, mock_smtp_ssl: m
299
333
300
334
@mock .patch ("smtplib.SMTP_SSL" )
301
335
@mock .patch ("smtplib.SMTP" )
302
- def test_send_mime_ssl_complete_failure (self , mock_smtp , mock_smtp_ssl ):
336
+ @mock .patch ("ssl.create_default_context" )
337
+ def test_send_mime_ssl_complete_failure (self , create_default_context , mock_smtp , mock_smtp_ssl ):
303
338
mock_smtp_ssl .side_effect = SMTPServerDisconnected ()
304
339
msg = MIMEMultipart ()
305
340
with conf_vars ({("smtp" , "smtp_ssl" ): "True" }):
@@ -310,7 +345,9 @@ def test_send_mime_ssl_complete_failure(self, mock_smtp, mock_smtp_ssl):
310
345
host = conf .get ("smtp" , "SMTP_HOST" ),
311
346
port = conf .getint ("smtp" , "SMTP_PORT" ),
312
347
timeout = conf .getint ("smtp" , "SMTP_TIMEOUT" ),
348
+ context = create_default_context .return_value ,
313
349
)
350
+ assert create_default_context .called
314
351
assert mock_smtp_ssl .call_count == conf .getint ("smtp" , "SMTP_RETRY_LIMIT" )
315
352
assert not mock_smtp .called
316
353
assert not mock_smtp_ssl .return_value .starttls .called
0 commit comments