Skip to content

Commit

Permalink
fixup! 🐛(backend) submit for signature handle timeout delete signing …
Browse files Browse the repository at this point in the history
…procedure
  • Loading branch information
jonathanreveille committed Sep 18, 2024
1 parent a19f4ae commit d55904b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
23 changes: 14 additions & 9 deletions src/backend/joanie/core/models/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from joanie.core.utils import contract_definition as contract_definition_utility
from joanie.core.utils import issuers, webhooks
from joanie.core.utils.payment_schedule import generate as generate_payment_schedule
from joanie.core.utils.signature import handle_signature_deletion_error
from joanie.signature.backends import get_signature_backend
from joanie.signature.exceptions import DeleteSignatureProcedureFailed

Expand Down Expand Up @@ -971,20 +970,26 @@ def submit_for_signature(self, user: User):
# deletion. If we reach the timeout limit, we should reset the contract submission
# values because the signature provider will delete them.
# We won't be able to use the `contract.signature_backend_reference` again.
# There can be edge case where the signature backend reference was already deleted,
# There is an edge case where the signature backend reference was already deleted,
# causing an error that the reference was not found at the signature provider side.
try:
backend_signature.delete_signing_procedure(
contract.signature_backend_reference
)
except BackendTimeout as exception: # pylint: disable=unused-variable
handle_signature_deletion_error(
order=self, error_message="Timeout on signature reference deletion"
)
except DeleteSignatureProcedureFailed as exception: # pylint: disable=unused-variable
handle_signature_deletion_error(
order=self, error_message="Failed to delete signature procedure"
except (BackendTimeout, DeleteSignatureProcedureFailed) as exception:
logger.error(
exception,
extra={
"context": {
"order": self.to_dict(),
"product": self.product.to_dict(),
"signature_backend_reference": (
contract.signature_backend_reference
),
}
},
)
contract.reset_submission_for_signature()
was_already_submitted = False

# We want to submit or re-submit the contract for signature in three cases:
Expand Down
21 changes: 0 additions & 21 deletions src/backend/joanie/core/utils/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,3 @@ def check_signature(request, settings_name):
)
if not signature_is_valid:
raise exceptions.AuthenticationFailed("Invalid authentication.")


def handle_signature_deletion_error(order, error_message):
"""
Handles the exception raised on the deletion of a signing procedure.
It's responsible for resetting the submission values from the outdated
contract of the order.
"""
logger.error(
error_message,
extra={
"context": {
"order": order.to_dict(),
"product": order.product.to_dict(),
"signature_backend_reference": (
order.contract.signature_backend_reference
),
}
},
)
order.contract.reset_submission_for_signature()
22 changes: 10 additions & 12 deletions src/backend/joanie/tests/core/test_models_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,11 +1338,12 @@ def test_models_order_submit_for_signature_step_delete_signing_procedure_timeout
# Change the contract definition title to trigger the `should_be_resubmitted` condition
order.product.contract_definition.title = "You know nothing John Snow."
order.product.contract_definition.save()
# Prepare the ReadTimeout on the `delete_signing_procedure` method
# Prepare the `BackendTimeout` on the `delete_signing_procedure` method
# when the second call of `submit_for_signature` occurs
responses.add(
responses.DELETE,
f"https://lex_persona.test01.com/api/workflows/{workflow_id}",
body=BackendTimeout(),
body=BackendTimeout("Deletion request is taking longer than expected."),
)
# Prepare the data for the new document to sign on the contract
new_workflow_id = "wfl_id_fake_2"
Expand Down Expand Up @@ -1533,13 +1534,13 @@ def test_models_order_submit_for_signature_step_delete_signing_procedure_timeout
with self.assertLogs("joanie") as logger:
order.submit_for_signature(user=user)

# We should find the in the logger message the reference wfl_id_fake_1 being deleted
# We should find in the logger message the reference wfl_id_fake_1 being deleted
self.assertLogsEquals(
logger.records,
[
(
"ERROR",
"Timeout on signature reference deletion",
"Deletion request is taking longer than expected.",
{
"order": dict,
"product": dict,
Expand Down Expand Up @@ -1824,7 +1825,8 @@ def test_models_order_submit_for_signature_step_delete_signing_procedure_referen
with self.assertLogs("joanie") as logger:
order.submit_for_signature(user=user)

# We should find the in the logger message the reference wfl_id_fake_1 being deleted
# We should find in the logger message the reference wfl_id_fake_1 trying to deleted
# but fails
self.assertLogsEquals(
logger.records,
[
Expand All @@ -1835,17 +1837,13 @@ def test_models_order_submit_for_signature_step_delete_signing_procedure_referen
),
(
"ERROR",
"Failed to delete signature procedure",
{
"order": dict,
"product": dict,
"signature_backend_reference": str,
},
"Lex Persona: Unable to delete the signature procedure the reference "
f"does not exist {workflow_id}",
),
],
)

# Check we have the latest data from db for the contract
# Our contract must have the new values of the document to sign
contract.refresh_from_db()
self.assertIsNotNone(contract.submitted_for_signature_on)
self.assertEqual(contract.signature_backend_reference, new_workflow_id)
Expand Down

0 comments on commit d55904b

Please sign in to comment.