From af767f917bdd577942e24cb3970045ed054ffe0c Mon Sep 17 00:00:00 2001 From: Seth Schoen Date: Wed, 13 May 2015 22:35:00 -0700 Subject: [PATCH] Unit tests for notify.py and get_sans_from_cert --- letsencrypt/notify.py | 3 +- letsencrypt/tests/crypto_util_test.py | 13 +++++++ letsencrypt/tests/notify_test.py | 51 +++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 letsencrypt/tests/notify_test.py diff --git a/letsencrypt/notify.py b/letsencrypt/notify.py index 954862e3b..6efb42d21 100644 --- a/letsencrypt/notify.py +++ b/letsencrypt/notify.py @@ -24,7 +24,6 @@ def notify(subject, whom, what): proc = subprocess.Popen(["/usr/sbin/sendmail", "-t"], stdin=subprocess.PIPE) proc.communicate(msg) - except OSError, err: - print err + except OSError: return False return True diff --git a/letsencrypt/tests/crypto_util_test.py b/letsencrypt/tests/crypto_util_test.py index bdd67da6a..3e75ea220 100644 --- a/letsencrypt/tests/crypto_util_test.py +++ b/letsencrypt/tests/crypto_util_test.py @@ -14,6 +14,10 @@ RSA256_KEY = pkg_resources.resource_string( 'acme.jose', os.path.join('testdata', 'rsa256_key.pem')) RSA512_KEY = pkg_resources.resource_string( 'acme.jose', os.path.join('testdata', 'rsa512_key.pem')) +CERT = pkg_resources.resource_string( + 'letsencrypt.tests', os.path.join('testdata', 'cert.pem')) +SAN_CERT = pkg_resources.resource_string( + 'letsencrypt.tests', os.path.join('testdata', 'cert-san.pem')) class InitSaveKeyTest(unittest.TestCase): @@ -150,5 +154,14 @@ class MakeSSCertTest(unittest.TestCase): make_ss_cert(RSA512_KEY, ['example.com', 'www.example.com']) +class GetSansFromCertTest(unittest.TestCase): + # pylint: disable=too-few-public-methods + """Tests for letsencrypt.crypto_util.get_sans_from_cert.""" + def test_it(self): + from letsencrypt.crypto_util import get_sans_from_cert + self.assertEqual(get_sans_from_cert(CERT), []) + self.assertEqual(get_sans_from_cert(SAN_CERT), + ['example.com', 'www.example.com']) + if __name__ == '__main__': unittest.main() # pragma: no cover diff --git a/letsencrypt/tests/notify_test.py b/letsencrypt/tests/notify_test.py new file mode 100644 index 000000000..c6f76c42e --- /dev/null +++ b/letsencrypt/tests/notify_test.py @@ -0,0 +1,51 @@ +"""Tests for letsencrypt/notify.py""" + +import mock +import socket +import unittest + +class NotifyTests(unittest.TestCase): + """Tests for the notifier.""" + + @mock.patch("letsencrypt.notify.smtplib.LMTP") + def test_smtp_success(self, mock_lmtp): + from letsencrypt.notify import notify + lmtp_obj = mock.MagicMock() + mock_lmtp.return_value = lmtp_obj + self.assertTrue(notify("Goose", "auntrhody@example.com", + "The old grey goose is dead.")) + self.assertEqual(lmtp_obj.connect.call_count, 1) + self.assertEqual(lmtp_obj.sendmail.call_count, 1) + + @mock.patch("letsencrypt.notify.smtplib.LMTP") + @mock.patch("letsencrypt.notify.subprocess.Popen") + def test_smtp_failure(self, mock_popen, mock_lmtp): + from letsencrypt.notify import notify + lmtp_obj = mock.MagicMock() + mock_lmtp.return_value = lmtp_obj + lmtp_obj.sendmail.side_effect = socket.error(17) + proc = mock.MagicMock() + mock_popen.return_value = proc + self.assertTrue(notify("Goose", "auntrhody@example.com", + "The old grey goose is dead.")) + self.assertEqual(lmtp_obj.sendmail.call_count, 1) + self.assertEqual(proc.communicate.call_count, 1) + + @mock.patch("letsencrypt.notify.smtplib.LMTP") + @mock.patch("letsencrypt.notify.subprocess.Popen") + def test_everything_fails(self, mock_popen, mock_lmtp): + from letsencrypt.notify import notify + lmtp_obj = mock.MagicMock() + mock_lmtp.return_value = lmtp_obj + lmtp_obj.sendmail.side_effect = socket.error(17) + proc = mock.MagicMock() + mock_popen.return_value = proc + proc.communicate.side_effect = OSError("What we have here is a " + "failure to communicate.") + self.assertFalse(notify("Goose", "auntrhody@example.com", + "The old grey goose is dead.")) + self.assertEqual(lmtp_obj.sendmail.call_count, 1) + self.assertEqual(proc.communicate.call_count, 1) + +if __name__ == "__main__": + unittest.main() # pragma: no cover