From 1a5d9a9d2dfc28b51d040a958d9d2b6ca7a6b081 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20NO=C3=8BL?= <tnoel@entrouvert.com>
Date: Tue, 3 May 2022 11:42:09 +0200
Subject: [PATCH] isere_ens: use empty POST for booking cancel (#64787)

---
 passerelle/contrib/isere_ens/models.py |  8 ++++----
 tests/test_isere_ens.py                | 25 +++++++------------------
 2 files changed, 11 insertions(+), 22 deletions(-)

diff --git a/passerelle/contrib/isere_ens/models.py b/passerelle/contrib/isere_ens/models.py
index 3d7a7003..bace05c4 100644
--- a/passerelle/contrib/isere_ens/models.py
+++ b/passerelle/contrib/isere_ens/models.py
@@ -183,10 +183,10 @@ class IsereENS(BaseResource, HTTPResource):
     class Meta:
         verbose_name = _("Espaces naturels sensibles de l'Isère")
 
-    def request(self, endpoint, params=None, json=None):
+    def request(self, endpoint, params=None, json=None, method='get'):
         url = urlparse.urljoin(self.base_url, endpoint)
         headers = {"token": self.token}
-        if json is not None:
+        if method == 'post' or json is not None:
             response = self.requests.post(url, params=params, json=json, headers=headers)
         else:
             response = self.requests.get(url, params=params, headers=headers)
@@ -504,9 +504,9 @@ class IsereENS(BaseResource, HTTPResource):
     @endpoint(
         name="cancel-site-booking",
         description=_("Cancel a booking"),
+        methods=["post"],
         display_order=6,
         perm="can_access",
-        methods=["get", "post", "delete"],
         parameters={
             "code": {"description": _('External ID')},
         },
@@ -514,7 +514,7 @@ class IsereENS(BaseResource, HTTPResource):
     def cancel_booking(self, request, code):
         if self.api_version == '1.0.0':
             raise APIError('not available on API v1.0.0', data=None)
-        status = self.request('api/' + self.api_version + '/site/booking/school/cancel/' + code, json={})
+        status = self.request('api/' + self.api_version + '/site/booking/school/cancel/' + code, method='post')
         if not isinstance(status, dict):
             raise APIError('response is not a dict', data=status)
         if 'status' not in status:
diff --git a/tests/test_isere_ens.py b/tests/test_isere_ens.py
index 671b4398..5f7c2734 100644
--- a/tests/test_isere_ens.py
+++ b/tests/test_isere_ens.py
@@ -591,7 +591,7 @@ def test_cancel_booking(mocked_post, app, setup, api_version):
         },
     )
     mocked_post.return_value = tests.utils.FakedResponse(content=CANCEL_RESPONSE, status_code=200)
-    response = app.delete(endpoint + "?code=123")
+    response = app.post(endpoint + "?code=123")
     if api_version == '1.0.0':
         assert response.json["err"] == 1
         assert response.json["data"] is None
@@ -601,32 +601,21 @@ def test_cancel_booking(mocked_post, app, setup, api_version):
     assert mocked_post.call_count == 1
     assert response.json["err"] == 0
     assert response.json["data"]["status"] == "CANCELED"
-    # post and get methods
-    response = app.post(endpoint + "?code=123")
-    assert mocked_post.call_args[0][0].endswith("api/" + api_version + "/site/booking/school/cancel/123")
-    assert mocked_post.call_count == 2
-    assert response.json["err"] == 0
-    assert response.json["data"]["status"] == "CANCELED"
-    response = app.get(endpoint + "?code=123")
-    assert mocked_post.call_args[0][0].endswith("api/" + api_version + "/site/booking/school/cancel/123")
-    assert mocked_post.call_count == 3
-    assert response.json["err"] == 0
-    assert response.json["data"]["status"] == "CANCELED"
 
-    response = app.get(endpoint, status=400)  # no code specified
-    assert mocked_post.call_count == 3  # same as before
+    response = app.post(endpoint, status=400)  # no code specified
+    assert mocked_post.call_count == 1  # same as before
 
     mocked_post.return_value = tests.utils.FakedResponse(content="""["not", "a", "dict"]""", status_code=200)
-    response = app.get(endpoint + "?code=123")
-    assert mocked_post.call_count == 4
+    response = app.post(endpoint + "?code=123")
+    assert mocked_post.call_count == 2
     assert response.json["err"] == 1
     assert response.json["err_class"].endswith("APIError")
     assert response.json["err_desc"] == "response is not a dict"
     assert response.json["data"] == ["not", "a", "dict"]
 
     mocked_post.return_value = tests.utils.FakedResponse(content="""{"foo": "bar"}""", status_code=200)
-    response = app.get(endpoint + "?code=123")
-    assert mocked_post.call_count == 5
+    response = app.post(endpoint + "?code=123")
+    assert mocked_post.call_count == 3
     assert response.json["err"] == 1
     assert response.json["err_class"].endswith("APIError")
     assert response.json["err_desc"] == "no status in response"
-- 
2.35.1

