#!/usr/bin/python
# -*- coding: utf-8 -*-
"""unittests for acme2certifier"""

# pylint: disable= C0415, W0212
import unittest
import sys
import os
from unittest.mock import patch, Mock, MagicMock
import base64
import configparser

sys.path.insert(0, ".")
sys.path.insert(1, "..")


class TestACMEHandler(unittest.TestCase):
    """test class for cgi_handler"""

    def setUp(self):
        """setup unittest"""
        import logging

        logging.basicConfig(level=logging.CRITICAL)
        self.logger = logging.getLogger("test_a2c")
        from acme2certifier.cahandlers.mscertsrv_ca_handler import CAhandler

        self.cahandler = CAhandler(False, self.logger)
        self.dir_path = os.path.dirname(os.path.realpath(__file__))

    def test_001_default(self):
        """default test which always passes"""
        self.assertEqual("foo", "foo")

    def test_002__pkcs7_to_pem(self):
        """test pkcs7 to pem default output"""
        with open(self.dir_path + "/ca/certs.p7b", "r") as fso:
            file_content = fso.read()
        with open(self.dir_path + "/ca/certs.pem", "r") as fso:
            result = fso.read()
        self.assertEqual(result, self.cahandler._pkcs7_to_pem(file_content))

    def test_003__pkcs7_to_pem(self):
        """test pkcs7 to pem output string"""
        with open(self.dir_path + "/ca/certs.p7b", "r") as fso:
            file_content = fso.read()
        with open(self.dir_path + "/ca/certs.pem", "r") as fso:
            result = fso.read()
        self.assertEqual(result, self.cahandler._pkcs7_to_pem(file_content, "string"))

    def test_004__pkcs7_to_pem(self):
        """test pkcs7 to pem output list"""
        with open(self.dir_path + "/ca/certs.p7b", "r") as fso:
            file_content = fso.read()
        result = [
            "-----BEGIN CERTIFICATE-----\nMIIFTzCCAzegAwIBAgIIAzHyhSyrXfMwDQYJKoZIhvcNAQELBQAwKzEXMBUGA1UE\nCxMOYWNtZTJjZXJ0aWZpZXIxEDAOBgNVBAMTB3Jvb3QtY2EwHhcNMjAwNTI3MTM1\nNDAwWhcNMzAwNTI2MjM1OTAwWjAqMRcwFQYDVQQLEw5hY21lMmNlcnRpZmllcjEP\nMA0GA1UEAxMGc3ViLWNhMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA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BpFbH2HOPatmu3yZRQ9fj9ta9EW46n33DFRNLinFWa4WJs4yLVP1juge\n2TCOyA1t61iy++RRXSG3e7NFYrEZuCht1EdDAdzIUY89m9NCPwoDYS4CahgnfkkO\n7YQe6f6yqK6isyf8ZFcp1uF58eERDiF/FDqS8nLmCdURuI56DDoNvDpig5J/9RNW\nG8vEvt2p7QrjeZ3EAatx5JuYty/NKTHZwJWk51CgzEgzDwzE2JIiqeldtL5d0Sl6\neVuv0G04BEyuXxEWpgVVzBS4qEFIBSnTJzgu1PXmId3yLvg2Nr8NKvwyZmN5xKFp\n0A9BWo15zW1PXDaD+l39oTYD7agjXkzTAjYIcfNJ7ATIYFD0xAvNAOf70s7aNupF\nfvkG2Q==\n-----END CERTIFICATE-----\n",
        ]
        self.assertEqual(result, self.cahandler._pkcs7_to_pem(file_content, "list"))

    def test_005__pkcs7_to_pem(self):
        """test pkcs7 to pem output list"""
        with open(self.dir_path + "/ca/certs.p7b", "r") as fso:
            file_content = fso.read()
        result = None
        self.assertEqual(result, self.cahandler._pkcs7_to_pem(file_content, "unknown"))

    def test_006__pkcs7_to_pem(self):
        """test pkcs7 to pem output list"""

        file_content = base64.b64decode(
            "MIIK9AYJKoZIhvcNAQcCoIIK5TCCCuECAQExADALBgkqhkiG9w0BBwGgggrHMIIFTzCCAzegAwIBAgIIAzHyhSyrXfMwDQYJKoZIhvcNAQELBQAwKzEXMBUGA1UECxMOYWNtZTJjZXJ0aWZpZXIxEDAOBgNVBAMTB3Jvb3QtY2EwHhcNMjAwNTI3MTM1NDAwWhcNMzAwNTI2MjM1OTAwWjAqMRcwFQYDVQQLEw5hY21lMmNlcnRpZmllcjEPMA0GA1UEAxMGc3ViLWNhMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxXHaGZsolXe+PBdUryngHP9VbBC1mehqeTtYI+hqsqGNH7q9a7bSrxMwFuF1kYL8jqqxkJdtl0L94xcxJg/ZdMx7Nt0vGI+BaAuTpEpUEHeN4tq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AQH/BAQDAgGGMBEG\nCWCGSAGG+EIBAQQEAwIABzAeBglghkgBhvhCAQ0EERYPeGNhIGNlcnRpZmljYXRl\nMA0GCSqGSIb3DQEBCwUAA4ICAQAjko7dX+iCgT+m3Iy1Vg6j7MRevPAzq1lqHRRN\nNdt2ct530pIut7Fv5V2xYk35ka+i/G+XyOvTXa9vAUKiBtiRnUPsXu4UcS7CcrCX\nEzHx4eOtHnp5wDhO0Fx5/OUZTaP+L7Pd1GD/j953ibx5bMa/M9Rj+S486nst57tu\nDRmEAavFDiMd6L3jH4YSckjmIH2uSeDIaRa9k6ag077XmWhvVYQ9tuR7RGbSuuV3\nFc6pqcFbbWpoLhNRcFc+hbUKOsKl2cP+QEKP/H2s3WMllqgAKKZeO+1KOsGo1CDs\n475bIXyCBpFbH2HOPatmu3yZRQ9fj9ta9EW46n33DFRNLinFWa4WJs4yLVP1juge\n2TCOyA1t61iy++RRXSG3e7NFYrEZuCht1EdDAdzIUY89m9NCPwoDYS4CahgnfkkO\n7YQe6f6yqK6isyf8ZFcp1uF58eERDiF/FDqS8nLmCdURuI56DDoNvDpig5J/9RNW\nG8vEvt2p7QrjeZ3EAatx5JuYty/NKTHZwJWk51CgzEgzDwzE2JIiqeldtL5d0Sl6\neVuv0G04BEyuXxEWpgVVzBS4qEFIBSnTJzgu1PXmId3yLvg2Nr8NKvwyZmN5xKFp\n0A9BWo15zW1PXDaD+l39oTYD7agjXkzTAjYIcfNJ7ATIYFD0xAvNAOf70s7aNupF\nfvkG2Q==\n-----END CERTIFICATE-----\n",
        ]
        self.assertEqual(result, self.cahandler._pkcs7_to_pem(file_content, "list"))

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_007_config_load(self, mock_load_cfg):
        """test _config_load no cahandler section"""
        parser = configparser.ConfigParser()
        # parser['CAhandler'] = {'foo': 'bar'}
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_008_config_load(self, mock_load_cfg):
        """test _config_load cahandler section with unknown values"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"foo": "bar"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_009_config_load(self, mock_load_cfg):
        """test _config_load no cahandler section with host value"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"host": "host"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertEqual("host", self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_010_config_load(self, mock_load_cfg):
        """test _config_load cahandler section with user values"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"user": "user"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertEqual("user", self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_011_config_load(self, mock_load_cfg):
        """test _config_load cahandler section with password values"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"password": "password"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertEqual("password", self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_012_config_load(self, mock_load_cfg):
        """test _config_load cahandler section with authmethod basic"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"auth_method": "basic"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_013_config_load(self, mock_load_cfg):
        """test _config_load cahandler section with authmethod ntlm"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"auth_method": "ntlm"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("ntlm", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_013a_config_load_warn_deprecated_basic(self, mock_load_cfg):
        """test warning when auth_method basic is explicitly configured"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"auth_method": "basic"}
        mock_load_cfg.return_value = parser

        with self.assertLogs("test_a2c", level="WARNING") as lcm:
            self.cahandler._config_load()

        self.assertIn(
            "WARNING:test_a2c:Auth method 'basic' is deprecated and will be removed in a future release. Please migrate to 'gssapi' (Kerberos).",
            lcm.output,
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_013b_config_load_warn_deprecated_ntlm(self, mock_load_cfg):
        """test warning when auth_method ntlm is explicitly configured"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"auth_method": "ntlm"}
        mock_load_cfg.return_value = parser

        with self.assertLogs("test_a2c", level="WARNING") as lcm:
            self.cahandler._config_load()

        self.assertIn(
            "WARNING:test_a2c:Auth method 'ntlm' is deprecated and will be removed in a future release. Please migrate to 'gssapi' (Kerberos).",
            lcm.output,
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_014_config_load(self, mock_load_cfg):
        """test _config_load cahandler section with authmethod unknown"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"auth_method": "unknown"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_015_config_load(self, mock_load_cfg):
        """test _config_load cahandler section with ca_bundle value"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"ca_bundle": "ca_bundle"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertEqual("ca_bundle", self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_016_config_load(self, mock_load_cfg):
        """test _config_load cahandler section with template value"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"template": "template"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertEqual("template", self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_017_config_load(self, mock_load_cfg):
        """test _config_load cahandler section with template value"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"krb5_config": "krb5_config"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertEqual("krb5_config", self.cahandler.krb5_config)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_017a_config_load_kerberos_keytab_options(self, mock_load_cfg):
        """test _config_load with kerberos keytab related options"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {
            "auth_method": "gssapi",
            "krb5_principal": "svc-a2c-enroll@EXAMPLE.COM",
            "krb5_keytab": "/tmp/svc.keytab",
            "krb5_cache": "/tmp/krb5cc_svc",
            "krb5_config": "/tmp/krb5.conf",
            "krb5_kinit_path": "/usr/local/bin/kinit",
        }
        mock_load_cfg.return_value = parser

        self.cahandler._config_load()

        self.assertEqual("gssapi", self.cahandler.auth_method)
        self.assertEqual("svc-a2c-enroll@EXAMPLE.COM", self.cahandler.krb5_principal)
        self.assertEqual("/tmp/svc.keytab", self.cahandler.krb5_keytab)
        self.assertEqual("/tmp/krb5cc_svc", self.cahandler.krb5_cache)
        self.assertEqual("/tmp/krb5.conf", self.cahandler.krb5_config)
        self.assertEqual("/usr/local/bin/kinit", self.cahandler.krb5_kinit_path)

    @patch.dict("os.environ", {"host_variable": "host"})
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_018_config_load(self, mock_load_cfg):
        """test _config_load - load with host variable"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"host_variable": "host_variable"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertEqual("host", self.cahandler.host)

    @patch.dict("os.environ", {"host_variable": "host"})
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_019_config_load(self, mock_load_cfg):
        """test _config_load - load with host variable which does not exist"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"host_variable": "doesnotexist"}
        mock_load_cfg.return_value = parser
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertIn(
            "ERROR:test_a2c:Could not load host_variable from environment: 'doesnotexist'",
            lcm.output,
        )

    @patch.dict("os.environ", {"host_variable": "host"})
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_020_config_load(self, mock_load_cfg):
        """test _config_load - load with host variable which gets overwritten"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"host_variable": "host_variable", "host": "host_local"}
        mock_load_cfg.return_value = parser
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.cahandler._config_load()
        self.assertEqual("host_local", self.cahandler.host)
        self.assertIn("INFO:test_a2c:Overwrite host", lcm.output)

    @patch.dict("os.environ", {"user_variable": "user"})
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_021_config_load(self, mock_load_cfg):
        """test _config_load - load with user variable"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"user_variable": "user_variable"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertEqual("user", self.cahandler.user)

    @patch.dict("os.environ", {"user_variable": "user"})
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_022_config_load(self, mock_load_cfg):
        """test _config_load - load with user variable which does not exist"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"user_variable": "doesnotexist"}
        mock_load_cfg.return_value = parser
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.cahandler._config_load()
        self.assertFalse(self.cahandler.user)
        self.assertIn(
            "ERROR:test_a2c:Could not load user_variable from environment: 'doesnotexist'",
            lcm.output,
        )

    @patch.dict("os.environ", {"user_variable": "user"})
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_023_config_load(self, mock_load_cfg):
        """test _config_load - load with user variable which gets overwritten"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"user_variable": "user_variable", "user": "user_local"}
        mock_load_cfg.return_value = parser
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.cahandler._config_load()
        self.assertEqual("user_local", self.cahandler.user)
        self.assertIn("INFO:test_a2c:Overwrite user", lcm.output)

    @patch.dict("os.environ", {"password_variable": "password"})
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_024_config_load(self, mock_load_cfg):
        """test _config_load - load with password variable"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"password_variable": "password_variable"}
        mock_load_cfg.return_value = parser
        self.cahandler._config_load()
        self.assertEqual("password", self.cahandler.password)

    @patch.dict("os.environ", {"password_variable": "password"})
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_025_config_load(self, mock_load_cfg):
        """test _config_load - load with password variable which does not exist"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"password_variable": "doesnotexist"}
        mock_load_cfg.return_value = parser
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.cahandler._config_load()
        self.assertFalse(self.cahandler.password)
        self.assertIn(
            "ERROR:test_a2c:Could not load password_variable from environment: 'doesnotexist'",
            lcm.output,
        )

    @patch.dict("os.environ", {"password_variable": "password"})
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_026_config_load(self, mock_load_cfg):
        """test _config_load - load with password variable which gets overwritten"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {
            "password_variable": "password_variable",
            "password": "password_local",
        }
        mock_load_cfg.return_value = parser
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.cahandler._config_load()
        self.assertEqual("password_local", self.cahandler.password)
        self.assertIn("INFO:test_a2c:Overwrite password", lcm.output)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.proxy_check")
    @patch("json.loads")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_027_config_load(self, mock_load_cfg, mock_json, mock_chk):
        """test _config_load ca_handler configured load proxies"""
        parser = configparser.ConfigParser()
        parser["DEFAULT"] = {"proxy_server_list": "foo"}
        mock_load_cfg.return_value = parser
        mock_json.return_value = "foo.bar.local"
        mock_chk.return_value = "proxy.bar.local"
        self.cahandler._config_load()
        self.assertTrue(mock_json.called)
        self.assertTrue(mock_chk.called)
        self.assertEqual(
            {"http": "proxy.bar.local", "https": "proxy.bar.local"},
            self.cahandler.proxy,
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.proxy_check")
    @patch("json.loads")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_028_config_load(self, mock_load_cfg, mock_json, mock_chk):
        """test _config_load ca_handler configured load proxies failed with exception in json.load"""
        parser = configparser.ConfigParser()
        parser["DEFAULT"] = {"proxy_server_list": "foo"}
        mock_json.side_effect = Exception("exc_load_config")
        mock_load_cfg.return_value = parser
        mock_chk.side = "proxy.bar.local"
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.cahandler._config_load()
        self.assertTrue(mock_json.called)
        self.assertFalse(mock_chk.called)
        self.assertFalse(self.cahandler.proxy)
        self.assertIn(
            "WARNING:test_a2c:Failed to load proxy_server_list from configuration: exc_load_config",
            lcm.output,
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.config_eab_profile_load")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.load_config")
    def test_029_config_load(self, mock_load_cfg, mock_eab):
        """allowd_domain_list"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {
            "foo": "bar",
            "eab_handler": "handler",
            "eab_profiling": "eab",
        }
        mock_load_cfg.return_value = parser
        mock_eab.return_value = ["eab", "handler"]
        self.cahandler._config_load()
        self.assertFalse(self.cahandler.host)
        self.assertFalse(self.cahandler.user)
        self.assertFalse(self.cahandler.password)
        self.assertEqual("basic", self.cahandler.auth_method)
        self.assertFalse(self.cahandler.ca_bundle)
        self.assertFalse(self.cahandler.template)
        self.assertFalse(self.cahandler.krb5_config)
        self.assertEqual("handler", self.cahandler.eab_handler)
        self.assertEqual("eab", self.cahandler.eab_profiling)

    def test_030_revoke(self):
        """test revocation"""
        self.assertEqual(
            (
                500,
                "urn:ietf:params:acme:error:serverInternal",
                "Revocation is not supported.",
            ),
            self.cahandler.revoke("cert", "rev_reason", "rev_date"),
        )

    def test_031_poll(self):
        """test polling"""
        self.assertEqual(
            ("Method not implemented.", None, None, "poll_identifier", False),
            self.cahandler.poll("cert_name", "poll_identifier", "csr"),
        )

    def test_032_trigger(self):
        """test polling"""
        self.assertEqual(
            ("Method not implemented.", None, None), self.cahandler.trigger("payload")
        )

    def test_033_check_credentials(self):
        """test polling"""
        ca_server = Mock()
        ca_server.check_credentials = Mock(return_value=True)
        self.assertTrue(self.cahandler._check_credentials(ca_server))

    def test_034_check_credentials(self):
        """test polling"""
        ca_server = Mock()
        ca_server.check_credentials = Mock(return_value=False)
        self.assertFalse(self.cahandler._check_credentials(ca_server))

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._config_load")
    def test_035__enter__(self, mock_cfg):
        """test enter  called"""
        mock_cfg.return_value = True
        self.cahandler.__enter__()
        self.assertTrue(mock_cfg.called)

    def test_036_enroll(self):
        """enroll without having self.host"""
        self.assertEqual(
            ("Config incomplete", None, None, None), self.cahandler.enroll("csr")
        )

    def test_037_enroll(self):
        """enroll without having self.user"""
        self.cahandler.host = "host"
        self.assertEqual(
            ("Config incomplete", None, None, None), self.cahandler.enroll("csr")
        )

    def test_038_enroll(self):
        """enroll without having self.password"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.assertEqual(
            ("Config incomplete", None, None, None), self.cahandler.enroll("csr")
        )

    def test_039_enroll(self):
        """enroll without having self.template"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.assertEqual(
            ("Config incomplete", None, None, None), self.cahandler.enroll("csr")
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("certsrv.Certsrv")
    def test_040_enroll(self, mock_certserver, mock_credchk):
        """enroll credential check failed"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        mock_certserver.return_value = "foo"
        mock_credchk.return_value = False
        self.assertEqual(
            ("Connection or Credentialcheck failed.", None, None, None),
            self.cahandler.enroll("csr"),
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._template_name_get")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._pkcs7_to_pem")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.convert_byte_to_string")
    @patch("textwrap.fill")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.Certsrv")
    def test_041_enroll(
        self, mock_certserver, mock_credchk, mockwrap, mock_b2s, mock_p2p, mock_tmpl
    ):
        """enroll enroll successful"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        mockresponse = MagicMock()
        mockresponse.get_chain.return_value = "get_chain"
        mockresponse.get_cert.return_value = "get_cert"
        mock_certserver = mockresponse
        mock_credchk.return_value = True
        mockwrap.return_value = "mockwrap"
        mock_b2s.side_effect = ["get_chain", "get_cert"]
        mock_p2p.return_value = "p2p"
        self.assertEqual(
            (None, "get_certp2p", "get_cert", None), self.cahandler.enroll("csr")
        )
        self.assertFalse(mock_tmpl.called)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._template_name_get")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._pkcs7_to_pem")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.convert_byte_to_string")
    @patch("textwrap.fill")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.Certsrv")
    def test_042_enroll(
        self,
        mock_certserver,
        mock_credchk,
        mockwrap,
        mock_b2s,
        mock_p2p,
        mock_tmpl,
    ):
        """enroll enroll successful"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        mockresponse = MagicMock()
        mockresponse.get_chain.return_value = "get_chain"
        mockresponse.get_cert.return_value = "get_cert"
        mock_certserver = mockresponse
        mock_credchk.return_value = True
        mockwrap.return_value = "mockwrap"
        mock_b2s.side_effect = ["get_chain", "get_cert"]
        mock_p2p.return_value = "p2p"
        self.assertEqual(
            (None, "get_certp2p", "get_cert", None), self.cahandler.enroll("csr")
        )
        self.assertFalse(mock_tmpl.called)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._template_name_get")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._pkcs7_to_pem")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.convert_byte_to_string")
    @patch("textwrap.fill")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.Certsrv")
    def test_043_enroll(
        self,
        mock_certserver,
        mock_credchk,
        mockwrap,
        mock_b2s,
        mock_p2p,
        mock_tmpl,
    ):
        """enroll enroll successful"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        mockresponse = MagicMock()
        mockresponse.get_chain.return_value = "get_chain"
        mockresponse.get_cert.return_value = "get_cert"
        mock_certserver = mockresponse
        mock_credchk.return_value = True
        mockwrap.return_value = "mockwrap"
        mock_b2s.side_effect = ["get_chain", "get_cert"]
        mock_p2p.return_value = "p2p"
        self.assertEqual(
            (None, "get_certp2p", "get_cert", None), self.cahandler.enroll("csr")
        )
        self.assertFalse(mock_tmpl.called)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.eab_profile_header_info_check")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._pkcs7_to_pem")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.convert_byte_to_string")
    @patch("textwrap.fill")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.Certsrv")
    def test_044_enroll(
        self, mock_certserver, mock_credchk, mockwrap, mock_b2s, mock_p2p, mock_eab
    ):
        """enroll enroll successful"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        self.cahandler.header_info_field = "header_info"
        mockresponse = MagicMock()
        mockresponse.get_chain.return_value = "get_chain"
        mockresponse.get_cert.return_value = "get_cert"
        mock_eab.return_value = False
        mock_certserver = mockresponse
        mock_credchk.return_value = True
        mockwrap.return_value = "mockwrap"
        mock_b2s.side_effect = ["get_chain", "get_cert"]
        mock_p2p.return_value = "p2p"
        self.assertEqual(
            (None, "get_certp2p", "get_cert", None), self.cahandler.enroll("csr")
        )
        self.assertTrue(mock_eab.called)
        self.assertEqual("template", self.cahandler.template)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.eab_profile_header_info_check")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._pkcs7_to_pem")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.convert_byte_to_string")
    @patch("textwrap.fill")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.Certsrv")
    def test_045_enroll(
        self, mock_certserver, mock_credchk, mockwrap, mock_b2s, mock_p2p, mock_eab
    ):
        """enroll enroll successful"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        self.cahandler.header_info_field = "header_info"
        self.cahandler.krb5_config = "krb5_config"
        mockresponse = MagicMock()
        mockresponse.get_chain.return_value = "get_chain"
        mockresponse.get_cert.return_value = "get_cert"
        mock_eab.return_value = "error"
        mock_certserver = mockresponse
        mock_credchk.return_value = True
        mockwrap.return_value = "mockwrap"
        mock_b2s.side_effect = ["get_chain", "get_cert"]
        mock_p2p.return_value = "p2p"
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.assertEqual(("error", None, None, None), self.cahandler.enroll("csr"))
        self.assertIn(
            "INFO:test_a2c:Load krb5config from krb5_config",
            lcm.output,
        )
        self.assertTrue(mock_eab.called)
        self.assertEqual("template", self.cahandler.template)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.subprocess.run")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.os.path.isfile")
    def test_061a_kerberos_acquire_with_kinit_custom_path(
        self,
        mock_isfile,
        mock_subprocess_run,
    ):
        """test kinit fallback uses custom kinit binary and krb5 config"""
        self.cahandler.krb5_keytab = "/tmp/svc.keytab"
        self.cahandler.krb5_principal = "svc-a2c-enroll@EXAMPLE.COM"
        self.cahandler.krb5_config = "/tmp/krb5.conf"
        self.cahandler.krb5_kinit_path = "/usr/local/bin/kinit"

        mock_isfile.return_value = True
        result = self.cahandler._kerberos_acquire_with_kinit("/tmp/krb5cc_svc")

        self.assertTrue(result)
        run_args, run_kwargs = mock_subprocess_run.call_args
        self.assertEqual("/usr/local/bin/kinit", run_args[0][0])
        self.assertEqual("/tmp/krb5cc_svc", run_kwargs["env"]["KRB5CCNAME"])
        self.assertEqual("/tmp/krb5.conf", run_kwargs["env"]["KRB5_CONFIG"])

    def test_061b_credentials_are_configured_gssapi_keytab(self):
        """test gssapi keytab mode does not require user/password"""
        self.cahandler.auth_method = "gssapi"
        self.cahandler.krb5_principal = "svc-a2c-enroll@EXAMPLE.COM"
        self.cahandler.krb5_keytab = "/tmp/svc.keytab"

        self.assertTrue(self.cahandler._credentials_are_configured())

    def test_061c_enroll_gssapi_keytab_missing_template(self):
        """test enroll in gssapi keytab mode still needs template"""
        self.cahandler.host = "host"
        self.cahandler.auth_method = "gssapi"
        self.cahandler.krb5_principal = "svc-a2c-enroll@EXAMPLE.COM"
        self.cahandler.krb5_keytab = "/tmp/svc.keytab"

        self.assertEqual(
            ("Config incomplete", None, None, None), self.cahandler.enroll("csr")
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.eab_profile_header_info_check")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._pkcs7_to_pem")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.convert_byte_to_string")
    @patch("textwrap.fill")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.Certsrv")
    def test_046_enroll(
        self, mock_certserver, mock_credchk, mockwrap, mock_b2s, mock_p2p, mock_eab
    ):
        """enroll enroll successful"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        self.cahandler.header_info_field = "header_info"
        mockresponse = MagicMock()
        mockresponse.get_chain.return_value = "get_chain"
        mockresponse.get_cert.return_value = "get_cert"
        mock_eab.return_value = None
        mock_certserver = mockresponse
        mock_credchk.return_value = True
        mockwrap.return_value = "mockwrap"
        mock_b2s.side_effect = ["get_chain", "get_cert"]
        mock_p2p.return_value = "p2p"
        self.assertEqual(
            (None, "get_certp2p", "get_cert", None), self.cahandler.enroll("csr")
        )
        self.assertTrue(mock_eab.called)
        self.assertEqual("template", self.cahandler.template)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._pkcs7_to_pem")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.convert_byte_to_string")
    @patch("textwrap.fill")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.Certsrv")
    def test_047_enroll(
        self, mock_certserver, mock_credchk, mockwrap, mock_b2s, mock_p2p
    ):
        """enroll exceütption in get chain"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        mockresponse = MagicMock()
        mockresponse.get_chain.return_value = "get_chain"
        mockresponse.get_cert.return_value = "get_cert"
        mock_certserver = mockresponse
        mock_credchk.return_value = True
        mockwrap.return_value = "mockwrap"
        mock_b2s.side_effect = [Exception("exc_get_chain"), "get_cert"]
        mock_p2p.return_value = "p2p"
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.assertEqual(
                (
                    "Certificate bundling failed: missing CA certificate or issued certificate.",
                    None,
                    "get_cert",
                    None,
                ),
                self.cahandler.enroll("csr"),
            )
        self.assertIn(
            "ERROR:test_a2c:Failed to get CA certificate chain: exc_get_chain",
            lcm.output,
        )
        self.assertIn(
            "ERROR:test_a2c:Failed to bundle certificates: missing ca_pem or cert_raw.",
            lcm.output,
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._pkcs7_to_pem")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.convert_byte_to_string")
    @patch("textwrap.fill")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.Certsrv")
    def test_048_enroll(
        self, mock_certserver, mock_credchk, mockwrap, mock_b2s, mock_p2p
    ):
        """enroll exceütption in get cert"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        mockresponse = MagicMock()
        mockresponse.get_chain.return_value = "get_chain"
        mockresponse.get_cert.return_value = "get_cert"
        mock_certserver = mockresponse
        mock_credchk.return_value = True
        mockwrap.return_value = "mockwrap"
        mock_b2s.side_effect = ["get_chain", Exception("get_cert")]
        mock_p2p.return_value = "p2p"
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.assertEqual(
                ("get_cert", None, None, None), self.cahandler.enroll("csr")
            )
        self.assertIn(
            "ERROR:test_a2c:Failed to enroll certificate from CA: get_cert",
            lcm.output,
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.enrollment_config_log")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("certsrv.Certsrv")
    def test_049_enroll(self, mock_certserver, mock_credchk, mock_ecl):
        """enroll credential check failed"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        mock_certserver.return_value = "foo"
        mock_credchk.return_value = False
        self.assertEqual(
            ("Connection or Credentialcheck failed.", None, None, None),
            self.cahandler.enroll("csr"),
        )
        self.assertFalse(mock_ecl.called)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.enrollment_config_log")
    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.CAhandler._check_credentials")
    @patch("certsrv.Certsrv")
    def test_050_enroll(self, mock_certserver, mock_credchk, mock_ecl):
        """enroll credential check failed"""
        self.cahandler.host = "host"
        self.cahandler.user = "user"
        self.cahandler.password = "password"
        self.cahandler.template = "template"
        self.cahandler.enrollment_config_log = True
        mock_certserver.return_value = "foo"
        mock_credchk.return_value = False
        self.assertEqual(
            ("Connection or Credentialcheck failed.", None, None, None),
            self.cahandler.enroll("csr"),
        )
        self.assertTrue(mock_ecl.called)

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.header_info_get")
    def test_051_template_name_get(self, mock_header):
        """test _template_name_get()"""
        mock_header.return_value = [
            {
                "header_info": '{"header_field": "template=foo lego-cli/4.14.2 xenolf-acme/4.14.2 (release; linux; amd64)"}'
            }
        ]
        self.cahandler.header_info_field = "header_field"
        self.assertEqual("foo", self.cahandler._template_name_get("csr"))

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.header_info_get")
    def test_052_template_name_get(self, mock_header):
        """test _template_name_get()"""
        mock_header.return_value = [
            {
                "header_info": '{"header_field": "Template=foo lego-cli/4.14.2 xenolf-acme/4.14.2 (release; linux; amd64)"}'
            }
        ]
        self.cahandler.header_info_field = "header_field"
        self.assertEqual("foo", self.cahandler._template_name_get("csr"))

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.header_info_get")
    def test_053_template_name_get(self, mock_header):
        """test _template_name_get()"""
        mock_header.return_value = [{"header_info": "header_info"}]
        self.cahandler.header_info_field = "header_field"
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.assertFalse(self.cahandler._template_name_get("csr"))
        self.assertIn(
            "ERROR:test_a2c:Failed to parse template from header_info: Expecting value: line 1 column 1 (char 0)",
            lcm.output,
        )

    def test_054_config_headerinfo_load(self):
        """test config_headerinfo_load()"""
        config_dic = {"Order": {"header_info_list": '["foo", "bar", "foobar"]'}}
        self.cahandler._config_headerinfo_load(config_dic)
        self.assertEqual("foo", self.cahandler.header_info_field)

    def test_055_config_headerinfo_load(self):
        """test config_headerinfo_load()"""
        config_dic = {"Order": {"header_info_list": '["foo"]'}}
        self.cahandler._config_headerinfo_load(config_dic)
        self.assertEqual("foo", self.cahandler.header_info_field)

    def test_056_config_headerinfo_load(self):
        """test config_headerinfo_load()"""
        config_dic = {"Order": {"header_info_list": "foo"}}
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.cahandler._config_headerinfo_load(config_dic)
        self.assertFalse(self.cahandler.header_info_field)
        self.assertIn(
            "WARNING:test_a2c:Failed to parse header_info_list from configuration: Expecting value: line 1 column 1 (char 0)",
            lcm.output,
        )

    def test_057__config_url_load(self):
        """test _config_url_load()"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"url": "foo"}
        self.cahandler._config_url_load(parser)
        self.assertEqual("foo", self.cahandler.url)

    @patch.dict("os.environ", {"url_variable": "foo1"})
    def test_058__config_url_load(self):
        """test _config_url_load()"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"url_variable": "url_variable"}
        self.cahandler._config_url_load(parser)
        self.assertEqual("foo1", self.cahandler.url)

    @patch.dict("os.environ", {"url_variable": "foo1"})
    def test_059__config_url_load(self):
        """test _config_url_load()"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"url_variable": "url_variable", "url": "foo"}
        self.cahandler._config_url_load(parser)
        self.assertEqual("foo", self.cahandler.url)

    @patch.dict("os.environ", {"url_variable": "foo1"})
    def test_060__config_url_load(self):
        """test _config_url_load()"""
        parser = configparser.ConfigParser()
        parser["CAhandler"] = {"url_variable": "doesnotexist"}
        with self.assertLogs("test_a2c", level="INFO") as lcm:
            self.cahandler._config_url_load(parser)
        self.assertFalse(self.cahandler.url)
        self.assertIn(
            "ERROR:test_a2c:Could not load url_variable from environment: 'doesnotexist'",
            lcm.output,
        )

    @patch("acme2certifier.cahandlers.mscertsrv_ca_handler.handler_config_check")
    def test_061_handler_check(self, mock_handler_check):
        """test handler_check"""
        mock_handler_check.return_value = "mock_handler_check"
        self.assertEqual("mock_handler_check", self.cahandler.handler_check())


if __name__ == "__main__":

    unittest.main()
