"""Tests for the auto-generated PKI."""importpytestfromdevices.issuerimportLocalDomainCredentialIssuerfromdevices.modelsimportDeviceModelfrompki.auto_gen_pkiimportAutoGenPkifrompki.modelsimportCertificateModel,DomainModel,IssuingCaModelfrompki.util.keysimportAutoGenPkiKeyAlgorithm@pytest.mark.parametrize('key_alg',[AutoGenPkiKeyAlgorithm.RSA2048,AutoGenPkiKeyAlgorithm.SECP256R1])
[docs]deftest_auto_gen_pki(key_alg:AutoGenPkiKeyAlgorithm)->None:"""Test that the auto-generated PKI can be correctly enabled, used and disabled."""# Check that the auto-generated PKI is disabled by defaultassertAutoGenPki.get_auto_gen_pki()isNone# Enable the auto-generated PKIAutoGenPki.enable_auto_gen_pki(key_alg=key_alg)# Check that the auto-generated PKI is enabledissuing_ca=AutoGenPki.get_auto_gen_pki()assertissuing_caisnotNone# Use the auto-generated PKI domain to issue a domain credential to a new devicetry:domain=DomainModel.objects.get(unique_name='AutoGenPKI')exceptDomainModel.DoesNotExist:pytest.fail('Auto-generated PKI domain was not created')test_device=DeviceModel(common_name='test_device',serial_number='1234567890',domain=domain,)test_device.save()credential_issuer=LocalDomainCredentialIssuer(device=test_device,domain=domain)issued_credential=credential_issuer.issue_domain_credential()# Disable the auto-generated PKIAutoGenPki.disable_auto_gen_pki()# Check that the issued credential has been revokedassertissued_credential.credential.certificate.certificate_status==CertificateModel.CertificateStatus.REVOKED# Check that the issuing CA has been revoked and set as inactiveissuing_ca=IssuingCaModel.objects.get(pk=issuing_ca.pk)# reload from DBassertissuing_ca.credential.certificate.certificate_status==CertificateModel.CertificateStatus.REVOKEDassertnotissuing_ca.is_active# Check that the auto-generated PKI is disabled (this checks that the Issuing CA has been renamed)assertAutoGenPki.get_auto_gen_pki()isNone# Check that the domain has been set as inactivedomain=DomainModel.objects.get(unique_name='AutoGenPKI')assertnotdomain.is_active