Projet

Général

Profil

0001-data-sources-check-identifiers-as-strings-47974.patch

Frédéric Péters, 22 octobre 2020 18:11

Télécharger (4,22 ko)

Voir les différences:

Subject: [PATCH] data sources: check identifiers as strings (#47974)

 tests/test_form_pages.py | 52 ++++++++++++++++++++++++++++++++++++++++
 wcs/data_sources.py      |  2 +-
 2 files changed, 53 insertions(+), 1 deletion(-)
tests/test_form_pages.py
6126 6126
        assert formdef.data_class().select()[0].data['0_display'] == 'hello'
6127 6127
        assert formdef.data_class().select()[0].data['0_structured'] == data['data'][0]
6128 6128

  
6129
    # same thing with numeric identifiers
6130
    formdef.data_class().wipe()
6131
    data_source.data_source = {'type': 'json', 'value': 'http://remote.example.net/json-numeric-id'}
6132
    data_source.store()
6133

  
6134
    app = get_app(pub)
6135
    with mock.patch('wcs.qommon.misc.urlopen') as urlopen:
6136
        data = {'data': [{'id': 1, 'text': 'hello', 'extra': 'foo'},
6137
                         {'id': 2, 'text': 'world', 'extra': 'bar'}]}
6138
        urlopen.side_effect = lambda *args: StringIO(json.dumps(data))
6139
        resp = app.get('/test/')
6140
        assert urlopen.call_count == 0
6141
        pq = resp.pyquery.remove_namespaces()
6142
        select2_url = pq('select').attr['data-select2-url']
6143

  
6144
    with mock.patch('wcs.qommon.misc.urlopen') as urlopen:
6145
        data = {'data': [{'id': 1, 'text': 'hello', 'extra': 'foo'}]}
6146
        urlopen.side_effect = lambda *args: StringIO(json.dumps(data))
6147
        resp2 = app.get(select2_url + '?q=hell')
6148
        assert urlopen.call_count == 1
6149
        assert urlopen.call_args[0][0] == 'http://remote.example.net/json-numeric-id?q=hell'
6150
        assert resp2.json == data
6151

  
6152
        # check unauthorized access
6153
        resp2 = get_app(pub).get(select2_url + '?q=hell', status=403)
6154

  
6155
    # simulate select2 mode, with qommon.forms.js adding an extra hidden widget
6156
    resp.form.fields['f0_display'] = Hidden(form=resp.form, tag='input', name='f0_display', pos=10)
6157
    resp.form['f0'].force_value('1')
6158
    resp.form.fields['f0_display'].force_value('hello')
6159

  
6160
    with mock.patch('wcs.qommon.misc.urlopen') as urlopen:
6161
        data = {'data': [{'id': 1, 'text': 'hello', 'extra': 'foo'}]}
6162
        urlopen.side_effect = lambda *args: StringIO(json.dumps(data))
6163
        resp = resp.form.submit('submit') # -> validation page
6164
        assert urlopen.call_count == 1
6165
        assert urlopen.call_args[0][0] == 'http://remote.example.net/json-numeric-id?id=1'
6166
        assert resp.form['f0'].value == '1'
6167
        assert resp.form['f0_label'].value == 'hello'
6168

  
6169
    with mock.patch('wcs.qommon.misc.urlopen') as urlopen:
6170
        data = {'data': [{'id': 1, 'text': 'hello', 'extra': 'foo'}]}
6171
        urlopen.side_effect = lambda *args: StringIO(json.dumps(data))
6172
        resp = resp.form.submit('submit') # -> submit
6173
        assert urlopen.call_count == 1
6174
        assert urlopen.call_args[0][0] == 'http://remote.example.net/json-numeric-id?id=1'
6175
        assert formdef.data_class().select()[0].data['0'] == '1'
6176
        assert formdef.data_class().select()[0].data['0_display'] == 'hello'
6177
        assert formdef.data_class().select()[0].data['0_structured'] == data['data'][0]
6178

  
6129 6179
    # same thing with signed URLs
6180
    data_source.data_source = {'type': 'json', 'value': 'http://remote.example.net/json'}
6181
    data_source.store()
6130 6182
    formdef.data_class().wipe()
6131 6183
    open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w').write('''\
6132 6184
[wscall-secrets]
wcs/data_sources.py
439 439
        def find_item(items, name, value):
440 440
            from wcs.logged_errors import LoggedError
441 441
            for item in items:
442
                if item.get(name) == value:
442
                if str(item.get(name)) == str(value):
443 443
                    return item
444 444
            # not found
445 445
            LoggedError.record(_('Could not find element by id "%s"') % value)
446
-