From 62a9556bd20d4791c69f10225c567ae10107c3e1 Mon Sep 17 00:00:00 2001 From: Jakub Warmuz Date: Thu, 10 Sep 2015 21:20:48 +0000 Subject: [PATCH] Add unittest for save_certificate --- letsencrypt/tests/client_test.py | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/letsencrypt/tests/client_test.py b/letsencrypt/tests/client_test.py index b992089cc..0760b78d3 100644 --- a/letsencrypt/tests/client_test.py +++ b/letsencrypt/tests/client_test.py @@ -1,4 +1,7 @@ """Tests for letsencrypt.client.""" +import os +import shutil +import tempfile import unittest import configobj @@ -145,6 +148,36 @@ class ClientTest(unittest.TestCase): self.assertTrue("renewal but not automatic deployment" in msg) self.assertTrue(cert.cli_config.renewal_configs_dir in msg) + def test_save_certificate(self): + certs = ["matching_cert.pem", "cert.pem", "cert-san.pem"] + tmp_path = tempfile.mkdtemp() + os.chmod(tmp_path, 0o755) # TODO: really?? + + certr = mock.MagicMock(body=test_util.load_cert(certs[0])) + cert1 = test_util.load_cert(certs[1]) + cert2 = test_util.load_cert(certs[2]) + candidate_cert_path = os.path.join(tmp_path, "certs", "cert.pem") + candidate_chain_path = os.path.join(tmp_path, "chains", "chain.pem") + + cert_path, chain_path = self.client.save_certificate( + certr, [cert1, cert2], candidate_cert_path, candidate_chain_path) + + self.assertEqual(os.path.dirname(cert_path), + os.path.dirname(candidate_cert_path)) + self.assertEqual(os.path.dirname(chain_path), + os.path.dirname(candidate_chain_path)) + + with open(cert_path, "r") as cert_file: + cert_contents = cert_file.read() + self.assertEqual(cert_contents, test_util.load_vector(certs[0])) + + with open(chain_path, "r") as chain_file: + chain_contents = chain_file.read() + self.assertEqual(chain_contents, test_util.load_vector(certs[1]) + + test_util.load_vector(certs[2])) + + shutil.rmtree(tmp_path) + class RollbackTest(unittest.TestCase): """Tests for letsencrypt.client.rollback."""