Skip to content

Commit

Permalink
test: adds test case to check the cached
Browse files Browse the repository at this point in the history
  • Loading branch information
tkzt committed Nov 30, 2023
1 parent 829b12c commit a7f697a
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import socket
from ssl import SSLError
import tempfile
from unittest import mock
import pytest

from pathlib import Path
Expand Down Expand Up @@ -75,6 +76,16 @@ def setUpClass(cls):
def stop_smtp(cls):
cls.smtp_controller.stop()

def setUp(self):
super().setUp()
_, self.tmpKey = tempfile.mkstemp()
_, self.tmpCert = tempfile.mkstemp()

def tearDown(self):
super().tearDown()
Path(self.tmpKey).unlink()
Path(self.tmpCert).unlink()

def test_console_backend(self):
self.app.extensions['mailman'].backend = 'console'
msg = EmailMessage(
Expand Down Expand Up @@ -344,20 +355,33 @@ def test_email_tls_attempts_starttls(self):

def test_email_ssl_attempts_ssl_connection(self):
with SmtpdContext(self.app.extensions['mailman']):
_, tmpKey = tempfile.mkstemp()
_, tmpCert = tempfile.mkstemp()
self.app.extensions['mailman'].use_ssl = True
self.app.extensions['mailman'].ssl_keyfile = tmpKey
self.app.extensions['mailman'].ssl_certfile = tmpCert
self.app.extensions['mailman'].ssl_keyfile = self.tmpKey
self.app.extensions['mailman'].ssl_certfile = self.tmpCert

backend = smtp.EmailBackend()
self.assertTrue(backend.use_ssl)
with self.assertRaises(SSLError):
with backend:
pass

Path(tmpKey).unlink()
Path(tmpCert).unlink()
@mock.patch("ssl.SSLContext.load_cert_chain", return_value="")
def test_email_ssl_cached_context(self, result_mocked):
with SmtpdContext(self.app.extensions['mailman']):
self.app.extensions['mailman'].use_ssl = True

backend_one = smtp.EmailBackend()
backend_another = smtp.EmailBackend()

self.assertTrue(backend_one.ssl_context, backend_another.ssl_context)

self.app.extensions['mailman'].ssl_keyfile = self.tmpKey
self.app.extensions['mailman'].ssl_certfile = self.tmpCert

backend_one = smtp.EmailBackend()
backend_another = smtp.EmailBackend()

self.assertTrue(backend_one.ssl_context, backend_another.ssl_context)

def test_connection_timeout_default(self):
"""The connection's timeout value is None by default."""
Expand Down

0 comments on commit a7f697a

Please sign in to comment.