"""Tests for the EST interface endpoints."""importbase64fromunittest.mockimportMagicMock,patchimportpytestfromcryptographyimportx509fromdevices.modelsimportIssuedCredentialModelfromdjango.testimportRequestFactoryfrompki.util.idevidimportIDevIDAuthenticatorfrompki.util.x509importClientCertificateAuthenticationErrorfromest.viewsimport(EstCACertsView,EstSimpleEnrollmentView,LoggedHttpResponse,UsernamePasswordAuthenticationError,)@pytest.fixture
[docs]defget_mock_truststore(certificates:list[x509.Certificate])->MagicMock:"""Get a mock truststore for testing."""ts=MagicMock()ts.unique_name='test_mock_truststore'ts.get_certificate_collection_serializer.return_value.as_crypto.return_value=certificatesreturnts
[docs]deftest_get_credential_for_certificate(mock_get,mock_filter)->None:"""Test the get_credential_for_certificate method."""cert_mock=MagicMock()cert_mock.fingerprint.return_value=b'sample_fingerprint'mock_credential=MagicMock()mock_filter.return_value.first.return_value=mock_credentialmock_issued_credential=MagicMock()mock_issued_credential.credential=mock_credentialmock_issued_credential.device=MagicMock()mock_get.return_value=mock_issued_credentialissued_credential=IssuedCredentialModel.get_credential_for_certificate(cert_mock)assertissued_credential==mock_issued_credentialassertissued_credential.device==mock_issued_credential.deviceassertissued_credential.credential==mock_credential
[docs]deftest_tls_client_cert_verification_no_cert(est_simple_enrollment_view)->None:"""Tests the TLS client certificate verification if no valid PEM is passed."""est_simple_enrollment_view.request.META={'SSL_CLIENT_CERT':'41foobar',}withpytest.raises(ClientCertificateAuthenticationError):IDevIDAuthenticator.authenticate_idevid(est_simple_enrollment_view.request,est_simple_enrollment_view.requested_domain)
[docs]deftest_tls_client_cert_domain_credential_enrollment()->None:"""Tests that an issued credential can be enrolled via EST simpleenroll using an IDevID."""
[docs]deftest_tls_client_cert_enrollment_twice()->None:"""Tests that the same domain credential cannot be enrolled twice via EST simpleenroll using an IDevID."""
[docs]deftest_tls_client_cert_application_credential_enrollment()->None:"""Tests that an application credential cannot directly be enrolled via EST simpleenroll using an IDevID."""
[docs]deftest_tls_reenrollment_valid()->None:"""Tests that an issued credential can be re-enrolled via EST simplereenroll."""
[docs]deftest_tls_reenrollment_mismatched()->None:"""Tests that an issued credential cannot be re-enrolled if the TLS client cert does not match the issued cred. For this example, it is attempted to re-enroll a domain credential with an application credential. """