Skip to content

Commit

Permalink
(PC-34341)[BO] fix: avoid crash at collective offer validation when A…
Browse files Browse the repository at this point in the history
…DAGE notification timeouts
  • Loading branch information
prouzet-pass committed Jan 30, 2025
1 parent 7edaaaa commit 92789ad
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,17 @@ def _batch_validate_or_reject_collective_offers(
except educational_exceptions.AdageInvalidEmailException:
# in the case of an invalid institution email, adage is not notified but we still want to validate of reject the offer
flash(
Markup("Email invalide pour l'offre {offer_id}, Adage n'a pas été notifié").format(
Markup("Email invalide pour l'offre <b>{offer_id}</b>, ADAGE n'a pas été notifié").format(
offer_id=collective_offer.id
)
),
"warning",
)
except educational_exceptions.AdageException as exp:
flash(
Markup("Erreur Adage pour l'offre {offer_id}: {message}").format(
offer_id=collective_offer.id, message=exp.message
)
Markup(
"Erreur lors de la notification à ADAGE pour l'offre <b>{offer_id}</b> : {message}"
).format(offer_id=collective_offer.id, message=exp.message),
"warning",
)

mark_transaction_as_invalid()
Expand All @@ -389,6 +391,16 @@ def _batch_validate_or_reject_collective_offers(
collective_offer_update_succeed_ids.remove(collective_offer.id)

collective_offer_update_failed_ids.append(collective_offer.id)
except Exception as exc: # ConnectionError, ReadTimeout... # pylint: disable=broad-exception-caught
flash(
Markup(
"Erreur lors de la notification à ADAGE pour l'offre <b>{offer_id}</b> : {message}"
).format(
offer_id=collective_offer.id,
message=getattr(exc, "message", None) or str(exc) or type(exc).__name__,
),
"warning",
)

if len(collective_offer_update_succeed_ids) == 1:
flash(
Expand Down
29 changes: 28 additions & 1 deletion api/tests/routes/backoffice/collective_offers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from flask import url_for
import pytest
import requests.exceptions

from pcapi.core.categories import subcategories_v2 as subcategories
from pcapi.core.educational import exceptions as educational_exceptions
Expand Down Expand Up @@ -849,6 +850,32 @@ def test_validate_collective_offer_with_institution_invalid_email(
assert collective_offer_to_validate.validation == OfferValidationStatus.APPROVED
assert collective_offer_to_validate.lastValidationType == OfferValidationType.MANUAL

@pytest.mark.settings(
ADAGE_API_URL="https://adage_base_url",
ADAGE_BACKEND="pcapi.core.educational.adage_backends.adage.AdageHttpClient",
)
def test_validate_collective_offer_adage_timeout(self, legit_user, authenticated_client, requests_mock):
collective_offer = educational_factories.PendingCollectiveOfferFactory()

endpoint = requests_mock.post("https://adage_base_url/v1/offre-assoc", exc=requests.exceptions.ReadTimeout)

response = self.post_to_endpoint(
authenticated_client, collective_offer_id=collective_offer.id, follow_redirects=True
)
assert response.status_code == 200

assert (
html_parser.extract_alert(response.data)
== f"Erreur lors de la notification à ADAGE pour l'offre {collective_offer.id} : ReadTimeout"
)

assert endpoint.called

db.session.refresh(collective_offer)
assert collective_offer.isActive is True
assert collective_offer.validation == OfferValidationStatus.APPROVED
assert collective_offer.lastValidationType == OfferValidationType.MANUAL

def test_cant_validate_non_pending_offer(self, legit_user, authenticated_client):
collective_offer_to_validate = educational_factories.CollectiveOfferFactory(
validation=OfferValidationStatus.REJECTED
Expand Down Expand Up @@ -1042,7 +1069,7 @@ def test_batch_validate_collective_offers_adage_exception(self, legit_user, auth
assert response.status_code == 200
assert (
html_parser.extract_alert(response.data)
== f"Erreur Adage pour l'offre {collective_offer.id}: An error occured on adage side"
== f"Erreur lors de la notification à ADAGE pour l'offre {collective_offer.id} : An error occured on adage side"
)


Expand Down

0 comments on commit 92789ad

Please sign in to comment.