Skip to content

Commit

Permalink
fix(rdvi): PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-naeka committed Sep 12, 2024
1 parent d2e8cd8 commit c503e84
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
1 change: 0 additions & 1 deletion itou/www/rdv_insertion/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from itou.www.rdv_insertion import views


# https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces-and-included-urlconfs
app_name = "rdv_insertion"

urlpatterns = [
Expand Down
12 changes: 7 additions & 5 deletions itou/www/rdv_insertion/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class InvalidSignature(Exception):
pass


class UnsupportedEvent(Warning):
pass


@require_POST
@csrf_exempt
def webhook(request):
Expand Down Expand Up @@ -146,16 +150,14 @@ def webhook(request):
event.is_processed = True
event.save(update_fields=["is_processed"])
else:
logger.warning("Unhandled event", extra=event.body["meta"])
raise UnsupportedEvent(event.body["meta"]["model"])

return JsonResponse({"success": True})
except Exception as e:
logger.exception("Error while handling RDVI webhook")
logger.exception(f"Error while handling RDVI webhook: {e!r}")
response = JsonResponse({"success": False})
if isinstance(e, SignatureMissing):
if isinstance(e, SignatureMissing | InvalidSignature):
response.status_code = 401
elif isinstance(e, InvalidSignature):
response.status_code = 403
else:
response.status_code = 400
return response
28 changes: 14 additions & 14 deletions tests/www/rdv_insertion/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_webhook_handler_signature_missing(self, client, caplog):
response = client.post(url, json=rdv_insertion_mocks.RDV_INSERTION_WEBHOOK_INVITATION_BODY)
assert response.status_code == 401
assert caplog.messages[0] == "Payload encoding is UTF-8"
assert caplog.messages[1] == "Error while handling RDVI webhook"
assert caplog.messages[1] == "Error while handling RDVI webhook: SignatureMissing()"
assert caplog.records[1].exc_info[0] == SignatureMissing

@override_settings(RDV_INSERTION_WEBHOOK_SECRET="much-much-secret")
Expand All @@ -37,9 +37,9 @@ def test_webhook_handler_signature_invalid(self, client, caplog):
json=rdv_insertion_mocks.RDV_INSERTION_WEBHOOK_INVITATION_BODY,
headers={"x-rdvi-signature": "invalid"},
)
assert response.status_code == 403
assert response.status_code == 401
assert caplog.messages[0] == "Payload encoding is UTF-8"
assert caplog.messages[1] == "Error while handling RDVI webhook"
assert caplog.messages[1] == "Error while handling RDVI webhook: InvalidSignature()"
assert caplog.records[1].exc_info[0] == InvalidSignature

@override_settings(RDV_INSERTION_WEBHOOK_SECRET="much-much-secret")
Expand Down Expand Up @@ -95,8 +95,8 @@ def test_webhook_handler_logs_invalid_events(self, client, caplog):
content_type="application/json",
headers={"x-rdvi-signature": self._make_rdvi_signature(raw_data)},
)
assert response.status_code == 200
assert caplog.messages[0] == "Unhandled event"
assert response.status_code == 400
assert caplog.messages[0] == "Error while handling RDVI webhook: UnsupportedEvent('Invalid')"

@override_settings(RDV_INSERTION_WEBHOOK_SECRET="much-much-secret")
def test_webhook_handler_does_not_update_invitation(self, client, caplog):
Expand Down Expand Up @@ -144,9 +144,10 @@ def test_webhook_handler_updates_invitation(self, client):
)
assert response.status_code == 200

# Event must be persisted as-is, unprocessed
# Event must be persisted as-is, flagged processed
webhook_event = WebhookEvent.objects.get()
assert webhook_event.body == rdv_insertion_mocks.RDV_INSERTION_WEBHOOK_INVITATION_BODY
assert webhook_event.is_processed

# Check for updated objects
assert invitation_request.email_invitation.status == Invitation.Status.OPENED
Expand Down Expand Up @@ -206,13 +207,13 @@ def test_webhook_handler_creates_appointment(self, client):
)
assert response.status_code == 200

# Event must be persisted as-is, unprocessed
# Event must be persisted as-is, flagged processed
webhook_event = WebhookEvent.objects.get()
assert webhook_event.body == rdv_insertion_mocks.RDV_INSERTION_WEBHOOK_APPOINTMENT_BODY
assert webhook_event.is_processed

# Check for created objects
appointment = Appointment.objects.prefetch_related("participants").select_related("location").get()
appointment = Appointment.objects.select_related("location").get()
assert appointment.company == invitation_request.company
assert appointment.status == appointment.Status.UNKNOWN
assert appointment.reason_category == appointment.ReasonCategory.SIAE_INTERVIEW
Expand All @@ -226,12 +227,11 @@ def test_webhook_handler_creates_appointment(self, client):
assert appointment.max_participants == body_data["max_participants_count"]
assert appointment.rdv_insertion_id == body_data["id"]

participations = appointment.rdvi_participations.all()
assert len(participations) == 1
assert participations[0].job_seeker == invitation_request.job_seeker
assert participations[0].status == participations[0].Status.UNKNOWN
assert participations[0].rdv_insertion_user_id == body_data["participations"][0]["user"]["id"]
assert participations[0].rdv_insertion_id == body_data["participations"][0]["id"]
participation = appointment.rdvi_participations.get()
assert participation.job_seeker == invitation_request.job_seeker
assert participation.status == participation.Status.UNKNOWN
assert participation.rdv_insertion_user_id == body_data["participations"][0]["user"]["id"]
assert participation.rdv_insertion_id == body_data["participations"][0]["id"]

assert appointment.location.name == body_data["lieu"]["name"]
assert appointment.location.address == body_data["lieu"]["address"]
Expand Down

0 comments on commit c503e84

Please sign in to comment.