From 74d6a28aefc2aabf9b8a30aaa006664e6cd490a9 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 9 Jul 2021 20:49:33 +0200
Subject: [PATCH 5/7] toulouse_smart: check payload in mock_response (#55230)

---
 tests/test_toulouse_smart.py | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/tests/test_toulouse_smart.py b/tests/test_toulouse_smart.py
index 2f0bbe7e..b3088b3d 100644
--- a/tests/test_toulouse_smart.py
+++ b/tests/test_toulouse_smart.py
@@ -44,19 +44,21 @@ def smart(db):
 
 
 def mock_response(*path_contents):
     def decorator(func):
         @httmock.urlmatch()
         def error(url, request):
             assert False, 'request to %s' % url.geturl()
 
-        def register(path, content, status_code=200):
+        def register(path, payload, content, status_code=200):
             @httmock.urlmatch(path=path)
             def handler(url, request):
+                if payload and json.loads(request.body) != payload:
+                    assert False, 'wrong payload sent to request to %s' % url.geturl()
                 return httmock.response(status_code, content)
 
             return handler
 
         @functools.wraps(func)
         def wrapper(*args, **kwargs):
             handlers = []
             for row in path_contents:
@@ -71,17 +73,17 @@ def mock_response(*path_contents):
     return decorator
 
 
 def get_xml_file(filename):
     with open(os.path.join(TEST_BASE_DIR, "%s.xml" % filename), 'rb') as desc:
         return desc.read()
 
 
-@mock_response(['/v1/type-intervention', b'<List></List>'])
+@mock_response(['/v1/type-intervention', None, b'<List></List>'])
 def test_empty_intervention_types(smart):
     assert smart.get_intervention_types() == []
 
 
 INTERVENTION_TYPES = '''<List>
    <item>
        <id>1234</id>
        <name>coin</name>
@@ -102,17 +104,17 @@ INTERVENTION_TYPES = '''<List>
               <type>int</type>
               <required>true</required>
            </properties>
        </properties>
    </item>
 </List>'''.encode()
 
 
-@mock_response(['/v1/type-intervention', INTERVENTION_TYPES])
+@mock_response(['/v1/type-intervention', None, INTERVENTION_TYPES])
 def test_model_intervention_types(smart):
     assert smart.get_intervention_types() == [
         {
             'id': '1234',
             'name': 'coin',
             'order': 1,
             'properties': [
                 {
@@ -126,29 +128,29 @@ def test_model_intervention_types(smart):
             ],
         },
     ]
 
 
 URL = '/toulouse-smart/test/'
 
 
-@mock_response(['/v1/type-intervention', INTERVENTION_TYPES])
+@mock_response(['/v1/type-intervention', None, INTERVENTION_TYPES])
 def test_endpoint_intervention_types(app, smart):
     resp = app.get(URL + 'type-intervention')
     assert resp.json == {'data': [{'id': 'coin', 'text': 'coin', 'uuid': '1234'}], 'err': 0}
 
 
 @mock_response()
 def test_endpoint_intervention_types_unavailable(app, smart):
     resp = app.get(URL + 'type-intervention')
     assert resp.json == {'data': [{'id': '', 'text': 'Service is unavailable', 'disabled': True}], 'err': 0}
 
 
-@mock_response(['/v1/type-intervention', INTERVENTION_TYPES])
+@mock_response(['/v1/type-intervention', None, INTERVENTION_TYPES])
 def test_manage_intervention_types(app, smart, admin_user):
     login(app)
     resp = app.get('/manage' + URL + 'type-intervention/')
     assert [[td.text for td in tr.cssselect('td,th')] for tr in resp.pyquery('tr')] == [
         ["Nom du type d'intervention", 'Nom', 'Type', 'Requis', 'Valeur par défaut'],
         ['1 - coin'],
         [None, 'FIELD1', 'item («Candélabre», «Mât», «Ne sait pas»)', '✘', 'Ne sait pas'],
         [None, 'FIELD2', 'int', '✔', None],
@@ -196,34 +198,34 @@ def test_manage_intervention_types(app, smart, admin_user):
     </field>
   </fields>
 </block>
 '''
             )
 
 
 @mock_response(
-    ['/v1/intervention', get_xml_file('get_intervention')],
+    ['/v1/intervention', None, get_xml_file('get_intervention')],
 )
 def test_get_intervention(app, smart):
     resp = app.get(URL + 'get-intervention?id=3f0558bd-7d85-49a8-97e4-d07bc7f8dc9b')
     assert not resp.json['err']
     assert resp.json['data']['id'] == '3f0558bd-7d85-49a8-97e4-d07bc7f8dc9b'
     assert resp.json['data']['state'] == ["e844e67f-5382-4c0f-94d8-56f618263485", "Nouveau", "false"]
 
 
 @mock_response(
-    ['/v1/intervention', None, 500],
+    ['/v1/intervention', None, None, 500],
 )
 def test_get_intervention_error_status(app, smart):
     resp = app.get(URL + 'get-intervention?id=3f0558bd-7d85-49a8-97e4-d07bc7f8dc9b')
     assert resp.json['err']
     assert 'failed to get' in resp.json['err_desc']
 
 
 @mock_response(
-    ['/v1/intervention', None, 404],
+    ['/v1/intervention', None, None, 404],
 )
 def test_get_intervention_wrond_id(app, smart):
     resp = app.get(URL + 'get-intervention?id=3f0558bd-7d85-49a8-97e4-d07bc7f8dc9b')
     assert resp.json['err']
     assert 'failed to get' in resp.json['err_desc']
     assert '404' in resp.json['err_desc']
-- 
2.30.2

