From e546766e81ac5655e46f4c43b83388737acb51b0 Mon Sep 17 00:00:00 2001 From: yazansalti Date: Tue, 14 Nov 2023 14:05:19 +0400 Subject: [PATCH] Checks if certificate is already requested Adds unit tests cases --- src/charm.py | 4 +++- tests/unit/test_charm.py | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/charm.py b/src/charm.py index ed0d2c4..ce2c592 100755 --- a/src/charm.py +++ b/src/charm.py @@ -197,7 +197,9 @@ def _on_certificates_relation_joined(self, event: EventBase) -> None: if not self._private_key_is_stored(): event.defer() return - self._request_new_certificate() + if not self._certificate_is_stored(): + self._request_new_certificate() + return def _on_certificate_available(self, event: CertificateAvailableEvent) -> None: """Pushes certificate to workload and configures workload.""" diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index d27b926..08dfac8 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -434,7 +434,7 @@ def test_given_private_key_exists_when_on_certificates_relation_joined_then_csr_ csr = b"whatever csr content" patch_generate_csr.return_value = csr patch_pull.return_value = StringIO("private key content") - patch_exists.return_value = True + patch_exists.side_effect = [True, False] self.harness.set_can_connect(container="nrf", val=True) self.harness.charm._on_certificates_relation_joined(event=Mock) @@ -448,7 +448,7 @@ def test_given_private_key_exists_when_on_certificates_relation_joined_then_csr_ @patch("charm.generate_csr") @patch("ops.model.Container.pull") @patch("ops.model.Container.exists") - def test_given_private_key_exists_when_on_certificates_relation_joined_then_cert_is_requested( + def test_given_private_key_exists_and_cert_not_yet_requested_when_on_certificates_relation_joined_then_cert_is_requested( # noqa: E501 self, patch_exists, patch_pull, @@ -458,13 +458,33 @@ def test_given_private_key_exists_when_on_certificates_relation_joined_then_cert csr = b"whatever csr content" patch_generate_csr.return_value = csr patch_pull.return_value = StringIO("private key content") - patch_exists.return_value = True + patch_exists.side_effect = [True, False] self.harness.set_can_connect(container="nrf", val=True) self.harness.charm._on_certificates_relation_joined(event=Mock) patch_request_certificate_creation.assert_called_with(certificate_signing_request=csr) + @patch( + "charms.tls_certificates_interface.v2.tls_certificates.TLSCertificatesRequiresV2.request_certificate_creation", # noqa: E501 + ) + @patch("ops.model.Container.push", new=Mock) + @patch("ops.model.Container.pull") + @patch("ops.model.Container.exists") + def test_given_cert_already_stored_when_on_certificates_relation_joined_then_cert_is_not_requested( # noqa: E501 + self, + patch_exists, + patch_pull, + patch_request_certificate_creation, + ): + patch_pull.return_value = StringIO("private key content") + patch_exists.return_value = True + self.harness.set_can_connect(container="nrf", val=True) + + self.harness.charm._on_certificates_relation_joined(event=Mock) + + patch_request_certificate_creation.assert_not_called + @patch("ops.model.Container.pull") @patch("ops.model.Container.exists") @patch("ops.model.Container.push")