staging_test_acme_account_rollover.py 2.26 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

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

    def test_success_account_rollover(self):
        """ Test success account key rollover """
55
        with self.assertLogs(level='INFO') as accountrolloverlog:
56 57
            tools.acme_account_rollover.main(["--current", self.configs['old_account_key'],
                                              "--new", self.configs['new_account_key'],
58
                                              "--acme-directory", ACME_DIRECTORY])
59
        self.assertIn("INFO:acme_account_rollover:Keys rolled over.",
60
                      accountrolloverlog.output)
61 62 63

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