Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Checks if certificate is already requested #26

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,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."""
Expand Down
26 changes: 23 additions & 3 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,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="udr", val=True)

self.harness.charm._on_certificates_relation_joined(event=Mock)
Expand All @@ -514,7 +514,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,
Expand All @@ -524,13 +524,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="udr", 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="udr", 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")
Expand Down
Loading