Projet

Général

Profil

0004-implement-URL-signatures-in-the-file-validation-web-.patch

Benjamin Dauvergne, 31 mars 2016 16:37

Télécharger (7,54 ko)

Voir les différences:

Subject: [PATCH 4/4] implement URL signatures in the file validation
 web-service calls (#10444)

 tests/test_backoffice_pages.py | 45 ++++++++++++++++++++++++------------------
 wcs/file_validation.py         | 40 +++++++++++++++++++++++++------------
 2 files changed, 53 insertions(+), 32 deletions(-)
tests/test_backoffice_pages.py
1615 1615
    assert not '0_structured' in formdata.data
1616 1616
    resp = app.get('/backoffice/management/form-title/%s/' % form_id)
1617 1617
    assert not 'Validate' in resp.body
1618
    with mock.patch('wcs.file_validation.http_post_request') as http_post_request:
1618
    with mock.patch('wcs.file_validation.fargo_post_json') as fargo_post_json:
1619 1619
        resp = app.get('/backoffice/management/form-title/%s/validate?field_id=0' % form_id)
1620
        assert http_post_request.call_count == 0
1620
        assert fargo_post_json.call_count == 0
1621 1621
    resp = resp.follow()
1622 1622
    assert not 'Valid ' in resp.body
1623 1623
    assert not 'Validate' in resp.body
......
1692 1692

  
1693 1693
        fragment = '%s : %s' % (metadata_field['label'], metadata[metadata_field['name']])
1694 1694
        assert fragment in resp.body
1695
    with mock.patch('wcs.file_validation.http_post_request') as http_post_request:
1695
    with mock.patch('wcs.file_validation.fargo_post_json') as fargo_post_json:
1696 1696
        payload = {
1697 1697
            'user_nameid': '12345',
1698 1698
            'origin': 'example.net',
......
1711 1711
            'end': '1978-01-01',
1712 1712
            'display': 'John Doe, 169 rue du château, 75014 PARIS'
1713 1713
        })
1714
        http_post_request.return_value = None, 201, json.dumps(result), None
1714
        fargo_post_json.return_value = 201, result
1715 1715
        resp = app.get('/backoffice/management/form-title/%s/validate?field_id=0' % form_id)
1716
        assert http_post_request.call_count == 1
1717
        assert http_post_request.call_args[0][0] == 'http://fargo.example.net/api/validation/justificatif-de-domicile/'
1718
        assert json_loads(http_post_request.call_args[0][1]) == payload
1719
        assert http_post_request.call_args[1] == {'headers': {'Content-Type': 'application/json'}}
1716
        assert fargo_post_json.call_count == 1
1717
        assert fargo_post_json.call_args[0][0] == '/api/validation/justificatif-de-domicile/'
1718
        assert fargo_post_json.call_args[0][1] == payload
1720 1719
    resp = resp.follow()
1721 1720

  
1722 1721
    assert 'Valid from 1970-01-01 to 1978-01-01' in resp.body
......
1777 1776
    resp.forms[0]['f0$validation_url'] = 'zob'
1778 1777
    for key in metadata:
1779 1778
        resp.forms[0]['f0$%s' % key] = metadata[key]
1780
    with mock.patch('wcs.file_validation.fargo_get') as fargo_get, \
1781
            mock.patch('wcs.file_validation.http_get_page') as http_get_page:
1782
        fargo_get.return_value = return_value
1783
        return_value = {
1784
            'result': 1,
1785
            'data': validation,
1786
        }
1787
        http_get_page.return_value = None, 200, json.dumps(return_value), None
1779
    with mock.patch('wcs.file_validation.fargo_get') as fargo_get:
1780
        side_effect = [return_value]
1781

  
1782
        def side_effect(url):
1783
            if url == 'zob':
1784
                return {
1785
                    'result': 1,
1786
                    'data': validation,
1787
                }
1788
            else:
1789
                return return_value
1790
        fargo_get.side_effect = side_effect
1791
        # http_get_page.return_value = None, 200, json.dumps(return_value), None
1788 1792
        resp = resp.forms[0].submit('submit')
1789
        fargo_get.assert_called_once_with(
1790
            'api/validation/justificatif-de-domicile/?user_nameid=12345')
1791
        http_get_page.assert_called_with('zob')
1793
        assert fargo_get.call_count == 3
1794
        assert (fargo_get.call_args_list[0]
1795
                == mock.call('api/validation/justificatif-de-domicile/?user_nameid=12345'))
1796
        assert fargo_get.call_args_list[1] == mock.call('zob')
1797
        assert fargo_get.call_args_list[2] == mock.call('zob')
1798
        # http_get_page.assert_called_with('zob')
1792 1799
        for key in metadata:
1793 1800
            assert 'value="%s"' % metadata[key] in resp.body
1794 1801
        assert 'Check values then click submit.' in resp.body
wcs/file_validation.py
22 22
from qommon.misc import http_get_page, json_loads, http_post_request
23 23
from quixote import get_publisher, get_request
24 24

  
25
from wcs.api_utils import get_secret_and_orig, sign_url
26

  
25 27

  
26 28
def has_file_validation():
27 29
    return get_publisher().get_site_option('fargo_url') is not None
28 30

  
29 31

  
30
def fargo_get(path):
32
def fargo_url(url):
31 33
    fargo_url = get_publisher().get_site_option('fargo_url')
32
    url = urlparse.urljoin(fargo_url, path)
34
    url = urlparse.urljoin(fargo_url, url)
35
    secret, orig = get_secret_and_orig(url)
36
    if '?' in url:
37
        url += '&orig=%s' % orig
38
    else:
39
        url += '?orig=%s' % orig
40
    return sign_url(url, secret)
41

  
42

  
43
def fargo_get(url):
44
    url = fargo_url(url)
33 45
    response, status, data, auth_header = http_get_page(url)
34 46
    if status == 200:
35 47
        return json_loads(data)
36 48
    return None
37 49

  
38 50

  
51
def fargo_post_json(url, payload):
52
    url = fargo_url(url)
53
    headers = {'Content-Type': 'application/json'}
54
    response, status, response_payload, auth_header = http_post_request(
55
        url, json.dumps(payload), headers=headers)
56
    return status, json_loads(response_payload)
57

  
58

  
39 59
def sha256_of_upload(upload):
40 60
    return hashlib.sha256(upload.get_content()).hexdigest()
41 61

  
......
61 81

  
62 82

  
63 83
def get_validation(url):
64
    response, status, data, auth_header = http_get_page(url)
65
    if status == 200:
66
        return json_loads(data)['data']
67
    return None
84
    result = fargo_get(url)
85
    return result['data'] if result else None
68 86

  
69 87

  
70 88
def get_validations(document_type):
......
94 112
def validate(filled, field, upload):
95 113
    '''Compute link to Fargo to validate the given document'''
96 114
    document_type_id = field.document_type['id']
97
    path = 'api/validation/%s/' % urllib.quote(document_type_id)
98
    fargo_url = get_publisher().get_site_option('fargo_url')
99
    url = urlparse.urljoin(fargo_url, path)
115
    url = '/api/validation/%s/' % urllib.quote(document_type_id)
100 116
    payload = {}
101 117
    if filled.user:
102 118
        if filled.user.name_identifiers:
......
108 124
    payload['content_hash'] = sha256_of_upload(upload)
109 125
    for meta_field in field.metadata:
110 126
        payload[meta_field['name']] = upload.metadata.get(meta_field['name'], '')
111
    headers = {'Content-Type': 'application/json'}
112
    response, status, response_payload, auth_header = http_post_request(url, json.dumps(payload),
113
                                                                        headers=headers)
127
    status, response = fargo_post_json(url, payload)
114 128
    if status == 201:
115
        upload.metadata = json_loads(response_payload)['data']
129
        upload.metadata = response['data']
116 130
        filled.data['%s_structured' % field.id] = upload.metadata
117 131
        filled.store()
118
-