staging_test_acme_account_rollover.py 2.06 KB
Newer Older
1 2 3 4
"""Test acme_account_rollover script with real ACME server"""
import unittest
import os
import configparser
5
import acme_dns_tiny
6
from tests.config_factory import generate_acme_account_rollover_config
7
from tools.acme_account_deactivate import account_deactivate
8 9
import tools.acme_account_rollover

10 11
ACME_DIRECTORY = os.getenv("GITLABCI_ACMEDIRECTORY_V2",
                           "https://acme-staging-v02.api.letsencrypt.org/directory")
12 13

class TestACMEAccountRollover(unittest.TestCase):
14
    "Tests for acme_account_rollover"
15 16

    @classmethod
17 18 19 20
    def setUpClass(cls):
        cls.configs = generate_acme_account_rollover_config()
        acme_dns_tiny.main([cls.configs['config']])
        super(TestACMEAccountRollover, cls).setUpClass()
21 22 23

    # To clean ACME staging server and close correctly temporary files
    @classmethod
24
    def tearDownClass(cls):
25
        # deactivate account key registration at end of tests
26
        # (we assume the key has been roll oved)
27
        account_deactivate(cls.configs["newaccountkey"], ACME_DIRECTORY)
28 29
        # Remove temporary files
        parser = configparser.ConfigParser()
30
        parser.read(cls.configs['config'])
31 32 33
        try:
            os.remove(parser["acmednstiny"]["AccountKeyFile"])
            os.remove(parser["acmednstiny"]["CSRFile"])
34 35 36
            os.remove(cls.configs["newaccountkey"])
            os.remove(cls.configs['config'])
        except: #pylint: disable=bare-except
37
            pass
38
        super(TestACMEAccountRollover, cls).tearDownClass()
39 40 41

    def test_success_account_rollover(self):
        """ Test success account key rollover """
42
        with self.assertLogs(level='INFO') as accountrolloverlog:
43
            tools.acme_account_rollover.main(["--current", self.configs['oldaccountkey'],
44 45
                                              "--new", self.configs['newaccountkey'],
                                              "--acme-directory", ACME_DIRECTORY])
46
        self.assertIn("INFO:acme_account_rollover:Account keys rolled over !",
47
                      accountrolloverlog.output)
48 49 50

if __name__ == "__main__":
    unittest.main()