Unit tests for notify.py and get_sans_from_cert

This commit is contained in:
Seth Schoen 2015-05-13 22:35:00 -07:00
parent 0a62bd6ebe
commit af767f917b
3 changed files with 65 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -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