From 6fd7b8f7c8db98dfc2fcb2b551ebc5def01b1b0e Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Fri, 11 Jan 2019 13:53:56 +0100
Subject: [PATCH] planitech: add some parameters to createreservation endpoint
 (#29659)

---
 functests/planitech/test_planitech.py  | 13 +++-
 passerelle/contrib/planitech/models.py | 30 +++++++--
 tests/test_planitech.py                | 85 ++++++++------------------
 3 files changed, 62 insertions(+), 66 deletions(-)

diff --git a/functests/planitech/test_planitech.py b/functests/planitech/test_planitech.py
index 10dc867..d6b42c7 100644
--- a/functests/planitech/test_planitech.py
+++ b/functests/planitech/test_planitech.py
@@ -47,13 +47,19 @@ def test_main(conn):
     data = res['data']
     assert data
 
+    activities = call_generic(conn, 'getactivities')
+    activity_id = activities[random.randint(0, len(activities) - 1)]['identifier']
+
+    resa_types = call_generic(conn, 'getreservationtypes')
+    resa_type_id = resa_types[random.randint(0, len(resa_types) - 1)]['identifier']
+
     chosen_date = data[0]['id']
     # create reservation
     params = {
         'date': chosen_date, 'start_time': '10:00', 'end_time': '11:00',
-        'place_id': place, 'price': 200, 'name_id': 'john-doe',
-        'first_name': 'jon', 'last_name': 'doe',
-        'email': 'jon.doe@localhost'
+        'place_id': place, 'price': 200, 'name_id': 'john-doe', 'type_id': resa_type_id,
+        'first_name': 'jon', 'last_name': 'doe', 'activity_id': activity_id,
+        'email': 'jon.doe@localhost', 'object': 'reservation object', 'vat_rate': 200
     }
     print('Create reservation parameters \n')
     pprint.pprint(params)
@@ -107,6 +113,7 @@ def call_generic(conn, endpoint):
     print('%s \n' % endpoint)
     pprint.pprint(data)
     print('\n')
+    return data
 
 
 def test_getusers(conn):
diff --git a/passerelle/contrib/planitech/models.py b/passerelle/contrib/planitech/models.py
index ed58319..a652d59 100644
--- a/passerelle/contrib/planitech/models.py
+++ b/passerelle/contrib/planitech/models.py
@@ -85,6 +85,26 @@ CREATE_RESERVATION_SCHEMA = {
             "description": "Email",
             "type": "string",
             "required": True
+        },
+        "activity_id": {
+            "description": "Activity identifier",
+            "type": "number",
+            "required": True
+        },
+        "object": {
+            "description": "Object",
+            "type": "string",
+            "required": True
+        },
+        "type_id": {
+            "description": "Rerservation type identifier",
+            "type": "number",
+            "required": True
+        },
+        "vat_rate": {
+            "description": "VAT rate",
+            "type": "number",
+            "required": True
         }
     }
 }
@@ -315,17 +335,17 @@ class PlanitechConnector(BaseResource):
                     raise APIError("Person creation failed: %s" % data.get('creationStatus'))
 
         params = {
-            "activityID": mste.Uint32(6),  # relaxation FIXME
+            "activityID": mste.Uint32(post_data['activity_id']),
             "contractorExternalIdentifier": pairing.external_id,
             "end": end_datetime,
             "isWeekly": False,
-            "object": "reservation  test entouvert",  # FIXME
+            "object": post_data['object'],
             "places": [float(post_data['place_id'])],
             "price": mste.Uint32(post_data['price']),
             "requestDate": request_date,
             "start": start_datetime,
-            "typeID": mste.Uint32(2),  # payant FIXME
-            "vatRate": mste.Uint32(4000)  # FIXME
+            "typeID": mste.Uint32(post_data['type_id']),
+            "vatRate": mste.Uint32(post_data['vat_rate'])
         }
         data = self._call_planitech(self.requests.post, 'createReservation', params)
         if data.get('creationStatus') != 'OK':
@@ -541,7 +561,7 @@ class PlanitechConnector(BaseResource):
     def getplacesreferential(self, request):
         return {'data': self._get_places_referential()}
 
-    @endpoint(description_get=_('Get activity types'), methods=['get'], perm='can_access')
+    @endpoint(description_get=_('Get reservation types'), methods=['get'], perm='can_access')
     def getreservationtypes(self, request):
         return {'data': self.generic_call('getReservationTypes', 'types')}
 
diff --git a/tests/test_planitech.py b/tests/test_planitech.py
index 2a09b07..c169380 100644
--- a/tests/test_planitech.py
+++ b/tests/test_planitech.py
@@ -156,20 +156,23 @@ def test_create_reservation(app, connector, monkeypatch):
         }
     ]
     mock_call_planitech = mock_planitech(monkeypatch, side_effect=side_effect)
-    response = app.post_json(
-        '/planitech/slug-planitech/createreservation',
-        params={
-            'date': '2018-11-11',
-            'start_time': '10:00',
-            'end_time': '11:00',
-            'place_id': 1,
-            'price': 10,
-            'name_id': 'john-doe',
-            'first_name': 'jon',
-            'last_name': 'doe',
-            'email': 'jon.doe@localhost'
-        }
-    )
+    params = {
+        'date': '2018-11-11',
+        'start_time': '10:00',
+        'end_time': '11:00',
+        'place_id': 1,
+        'price': 10,
+        'name_id': 'john-doe',
+        'first_name': 'jon',
+        'last_name': 'doe',
+        'email': 'jon.doe@localhost',
+        'activity_id': 1,
+        'object': 'reservation object',
+        'type_id': 1,
+        'vat_rate': 1,
+    }
+
+    response = app.post_json('/planitech/slug-planitech/createreservation', params=params)
     json_resp = response.json
     assert json_resp['err'] == 0
     assert json_resp['data']['reservation_id'] == 1
@@ -181,7 +184,8 @@ def test_create_reservation(app, connector, monkeypatch):
     assert person_args['firstName'] == 'jon'
     assert person_args['name'] == 'doe'
     external_id = person_args['externalUserIdentifier']
-    assert Pairing.objects.get(resource=connector, name_id='john-doe').external_id == external_id
+    pairing = Pairing.objects.get(resource=connector, name_id='john-doe')
+    assert pairing.external_id == external_id
 
     reservation_args = mock_call_planitech.call_args_list[1][0]
     assert reservation_args[1] == 'createReservation'
@@ -190,23 +194,15 @@ def test_create_reservation(app, connector, monkeypatch):
     assert reservation_args['end'] == datetime(2018, 11, 11, 11, 0)
     assert reservation_args['places'] == [1]
     assert reservation_args['price'] == 10
+    assert reservation_args['contractorExternalIdentifier'] == pairing.external_id
+    assert reservation_args['activityID'] == 1
+    assert reservation_args['object'] == 'reservation object'
+    assert reservation_args['typeID'] == 1
+    assert reservation_args['vatRate'] == 1
 
     # Second reservation for same user : no planitech person created
     mock_call_planitech = mock_planitech(monkeypatch, return_value=side_effect[1])
-    response = app.post_json(
-        '/planitech/slug-planitech/createreservation',
-        params={
-            'date': '2018-11-11',
-            'start_time': '10:00',
-            'end_time': '11:00',
-            'place_id': 1,
-            'price': 10,
-            'name_id': 'john-doe',
-            'first_name': 'jon',
-            'last_name': 'doe',
-            'email': 'jon.doe@localhost'
-        }
-    )
+    response = app.post_json('/planitech/slug-planitech/createreservation', params=params)
     json_resp = response.json
     assert json_resp['err'] == 0
     assert json_resp['data']['reservation_id'] == 1
@@ -221,21 +217,7 @@ def test_create_reservation(app, connector, monkeypatch):
             'creationStatus': 'NOTOK',
             'reservationIdentifier': 1
         })
-    response = app.post_json(
-        '/planitech/slug-planitech/createreservation',
-        params={
-            'date': '2018-11-11',
-            'start_time': '10:00',
-            'end_time': '11:00',
-            'place_id': 1,
-            'price': 10,
-            'name_id': 'john-doe',
-            'first_name': 'jon',
-            'last_name': 'doe',
-            'email': 'jon.doe@localhost'
-
-        }
-    )
+    response = app.post_json('/planitech/slug-planitech/createreservation', params=params)
     json_resp = response.json
     assert json_resp['err'] == 1
     assert json_resp['err_desc'] == 'Reservation creation failed: NOTOK'
@@ -245,20 +227,7 @@ def test_create_reservation(app, connector, monkeypatch):
         monkeypatch, return_value={
             'creationStatus': 'OK'
         })
-    response = app.post_json(
-        '/planitech/slug-planitech/createreservation',
-        params={
-            'date': '2018-11-11',
-            'start_time': '10:00',
-            'end_time': '11:00',
-            'place_id': 1,
-            'price': 10,
-            'name_id': 'john-doe',
-            'first_name': 'jon',
-            'last_name': 'doe',
-            'email': 'jon.doe@localhost'
-        }
-    )
+    response = app.post_json('/planitech/slug-planitech/createreservation', params=params)
     json_resp = response.json
     assert json_resp['err'] == 1
     assert json_resp['err_desc'] == 'Reservation creation failed: no reservation ID'
-- 
2.20.1

