Projet

Général

Profil

0001-test-make-a-fixture-of-HttpRequestMocking-16509.patch

Thomas Noël, 08 décembre 2017 14:54

Télécharger (12 ko)

Voir les différences:

Subject: [PATCH] test: make a fixture of HttpRequestMocking (#16509)

 tests/conftest.py              |  9 ++++++++-
 tests/test_admin_pages.py      |  7 ++++---
 tests/test_backoffice_pages.py |  6 +++---
 tests/test_datasource.py       |  2 +-
 tests/test_form_pages.py       |  8 ++++----
 tests/test_hobo.py             |  2 +-
 tests/test_workflows.py        | 13 +++++++------
 tests/test_wscall.py           | 13 ++++++++-----
 tests/utilities.py             | 19 +++++++++++++++++--
 9 files changed, 53 insertions(+), 26 deletions(-)
tests/conftest.py
3 3

  
4 4
import pytest
5 5

  
6
from utilities import EmailsMocking, SMSMocking
6
from utilities import EmailsMocking, SMSMocking, HttpRequestsMocking
7 7

  
8 8
def pytest_addoption(parser):
9 9
    parser.addoption('--without-postgresql-tests', action='store_true',
......
57 57
    with EmailsMocking() as mock:
58 58
        yield mock
59 59

  
60

  
60 61
@pytest.fixture
61 62
def sms_mocking():
62 63
    with SMSMocking() as sms:
63 64
        yield sms
65

  
66

  
67
@pytest.fixture
68
def http_requests():
69
    with HttpRequestsMocking() as http_requests:
70
        yield http_requests
tests/test_admin_pages.py
42 42
from wcs.formdef import FormDef
43 43
from wcs import fields
44 44

  
45
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub
45
from utilities import get_app, login, create_temporary_pub, clean_temporary_pub, HttpRequestsMocking
46 46

  
47 47
def pytest_generate_tests(metafunc):
48 48
    if 'pub' in metafunc.fixturenames:
......
1507 1507
    assert FormDef.get(formdef_id).url_name == 'form-test'
1508 1508
    assert FormDef.get(formdef_id).table_name == 'xxx'
1509 1509

  
1510
def test_form_comment_with_error_in_wscall(pub):
1510
def test_form_comment_with_error_in_wscall(http_requests, pub):
1511 1511
    create_superuser(pub)
1512 1512
    NamedWsCall.wipe()
1513 1513

  
......
3923 3923

  
3924 3924
    data_source.data_source = {'type': 'json', 'value': 'file://%s' % json_file_path}
3925 3925
    data_source.store()
3926
    resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id)
3926
    with HttpRequestsMocking() as http_requests:
3927
        resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id)
3927 3928
    assert 'Preview' in resp.body
3928 3929
    assert 'foo' in resp.body
3929 3930

  
tests/test_backoffice_pages.py
2105 2105
    resp = app.get('/backoffice/submission/%s/' % formdef.url_name)
2106 2106
    assert 'submission_channel' not in resp.form.fields
2107 2107

  
2108
def test_backoffice_wscall_failure_display(pub):
2108
def test_backoffice_wscall_failure_display(http_requests, pub):
2109 2109
    create_user(pub)
2110 2110
    create_environment(pub)
2111 2111
    formdef = FormDef.get_by_urlname('form-title')
......
2153 2153
    assert (' with the number %s.' % number31.get_display_id()) in resp.body
2154 2154
    assert not 'Error during webservice call' in resp.body
2155 2155

  
2156
def test_backoffice_wscall_attachment(pub):
2156
def test_backoffice_wscall_attachment(http_requests, pub):
2157 2157
    create_user(pub)
2158 2158
    create_environment(pub)
2159 2159
    formdef = FormDef.get_by_urlname('form-title')
......
3782 3782
    assert 'HELLO WORLD' in resp.body
3783 3783
    assert 'id="evolution-log"' in resp.body
3784 3784

  
3785
def test_backoffice_formdata_named_wscall(pub):
3785
def test_backoffice_formdata_named_wscall(http_requests, pub):
3786 3786
    user = create_user(pub)
3787 3787
    create_environment(pub)
3788 3788

  
tests/test_datasource.py
105 105
            ('foo', 'Foo', 'foo', {'id': 'foo', 'text': 'Foo'}),
106 106
            ('bar', 'Bar', 'bar', {'id': 'bar', 'text': 'Bar', 'disabled': True})]
107 107

  
108
def test_json_datasource():
108
def test_json_datasource(http_requests):
109 109
    datasource = {'type': 'json', 'value': ''}
110 110
    assert data_sources.get_items(datasource) == []
111 111

  
tests/test_form_pages.py
3007 3007
    data = formdef.data_class().get(data_id)
3008 3008
    assert data.data['0'] is None
3009 3009

  
3010
def test_form_jsonp_item_field(pub):
3010
def test_form_jsonp_item_field(http_requests, pub):
3011 3011
    formdef = create_formdef()
3012 3012
    formdef.fields = [
3013 3013
            fields.ItemField(id='1', label='string', type='item',
......
3577 3577
    assert not 'Check values then click submit.' in resp.body
3578 3578
    assert resp.form['f0'].value == 'foo@localhost'
3579 3579

  
3580
def test_item_field_with_disabled_items(pub):
3580
def test_item_field_with_disabled_items(http_requests, pub):
3581 3581
    user = create_user(pub)
3582 3582
    formdef = create_formdef()
3583 3583
    formdef.data_class().wipe()
......
3669 3669
        resp = resp.form.submit('submit') # -> validation page
3670 3670
        assert 'There were errors processing the form' in resp.body
3671 3671

  
3672
def test_items_field_with_disabled_items(pub):
3672
def test_items_field_with_disabled_items(http_requests, pub):
3673 3673
    user = create_user(pub)
3674 3674
    formdef = create_formdef()
3675 3675
    formdef.data_class().wipe()
......
3780 3780
    assert len(LoggedError.get_ids_with_indexed_value('workflow_id', '12')) == 1
3781 3781
    assert len(LoggedError.get_ids_with_indexed_value('workflow_id', 'X')) == 0
3782 3782

  
3783
def test_formdata_named_wscall(pub):
3783
def test_formdata_named_wscall(http_requests, pub):
3784 3784
    create_user(pub)
3785 3785
    NamedWsCall.wipe()
3786 3786

  
tests/test_hobo.py
340 340
        else:
341 341
            assert attribute_mapping[attribute_name] == field_id
342 342

  
343
def test_configure_authentication_methods():
343
def test_configure_authentication_methods(http_requests):
344 344
    pub.cfg['idp'] = {}
345 345
    service = [x for x in HOBO_JSON.get('services', []) if x.get('service-id') == 'wcs'][0]
346 346

  
tests/test_workflows.py
43 43
from wcs.wf.redirect_to_url import RedirectToUrlWorkflowStatusItem
44 44

  
45 45
from utilities import (create_temporary_pub, MockSubstitutionVariables,
46
        http_requests, clean_temporary_pub)
46
        clean_temporary_pub)
47 47

  
48 48
def setup_module(module):
49 49
    cleanup()
......
946 946
    assert len(emails.emails['foobar']['msg'].get_payload()) == 3
947 947

  
948 948

  
949
def test_webservice_call(pub):
949

  
950
def test_webservice_call(http_requests, pub):
950 951
    pub.substitutions.feed(MockSubstitutionVariables())
951 952

  
952 953
    FormDef.wipe()
......
1233 1234
    item.action_on_network_errors = ':stop'
1234 1235
    assert item.waitpoint
1235 1236

  
1236
def test_webservice_call_error_handling(pub):
1237
def test_webservice_call_error_handling(http_requests, pub):
1237 1238
    pub.substitutions.feed(MockSubstitutionVariables())
1238 1239

  
1239 1240
    FormDef.wipe()
......
1468 1469
    item.perform(formdata)
1469 1470
    assert formdata.evolution[-1].parts[-1].summary == 'ConnectionError: error\n'
1470 1471

  
1471
def test_webservice_call_store_in_backoffice_filefield(pub):
1472
def test_webservice_call_store_in_backoffice_filefield(http_requests, pub):
1472 1473
    wf = Workflow(name='wscall to backoffice file field')
1473 1474
    wf.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(wf)
1474 1475
    wf.backoffice_fields_formdef.fields = [
......
2569 2570
    item.perform(formdata)
2570 2571
    assert pub.user_class.get(user.id).form_data == {'3': 'Plop'}
2571 2572

  
2572
def test_set_backoffice_field(two_pubs):
2573
def test_set_backoffice_field(http_requests, two_pubs):
2573 2574
    Workflow.wipe()
2574 2575
    FormDef.wipe()
2575 2576
    wf = Workflow(name='xxx')
......
2617 2618
    formdata = formdef.data_class().get(formdata.id)
2618 2619
    assert formdata.data['bo1'] == 'HELLO GOODBYE'
2619 2620

  
2620
def test_set_backoffice_field_file(two_pubs):
2621
def test_set_backoffice_field_file(http_requests, two_pubs):
2621 2622
    Workflow.wipe()
2622 2623
    FormDef.wipe()
2623 2624
    wf = Workflow(name='xxx')
tests/test_wscall.py
7 7
from wcs.qommon.template import Template
8 8
from wcs.wscalls import NamedWsCall
9 9

  
10
from utilities import create_temporary_pub, clean_temporary_pub, http_requests
10
from utilities import create_temporary_pub, clean_temporary_pub
11 11

  
12 12

  
13 13
@pytest.fixture
......
50 50
    assert 'bye' in NamedWsCall.keys()
51 51
    assert not 'hello_1' in NamedWsCall.keys()
52 52

  
53
def test_webservice_substitution_variable(pub):
53

  
54
def test_webservice_substitution_variable(http_requests, pub):
54 55
    NamedWsCall.wipe()
55 56

  
56 57
    wscall = NamedWsCall()
......
63 64
    variables = pub.substitutions.get_context_variables()
64 65
    assert variables['webservice'].hello_world == {'foo': 'bar'}
65 66

  
66
def test_webservice_auto_sign(pub):
67

  
68
def test_webservice_auto_sign(http_requests, pub):
67 69
    NamedWsCall.wipe()
68 70

  
69 71
    wscall = NamedWsCall()
......
100 102
    assert not 'orig=example.net' in http_requests.get_last('url')
101 103
    assert 'signature=' in http_requests.get_last('url')
102 104

  
103
def test_webservice_post_with_no_payload(pub):
105

  
106
def test_webservice_post_with_no_payload(http_requests, pub):
104 107
    NamedWsCall.wipe()
105 108

  
106 109
    wscall = NamedWsCall()
......
109 112
    wscall.call()
110 113
    assert http_requests.get_last('body') is None
111 114

  
112
def test_wscall_ezt(pub):
115
def test_wscall_ezt(http_requests, pub):
113 116
    NamedWsCall.wipe()
114 117

  
115 118
    wscall = NamedWsCall()
tests/utilities.py
223 223
class HttpRequestsMocking(object):
224 224
    def __init__(self):
225 225
        self.requests = []
226

  
227
    def __enter__(self):
226 228
        import wcs.qommon.misc
227 229
        import qommon.misc
230

  
231
        self.wcs_qommon_misc_http_request = wcs.qommon.misc._http_request
232
        self.qommon_misc_http_request = qommon.misc._http_request
233

  
228 234
        wcs.qommon.misc._http_request = self.http_request
229 235
        qommon.misc._http_request = self.http_request
236
        return self
237

  
238
    def __exit__(self, exc_type, exc_value, tb):
239
        import wcs.qommon.misc
240
        import qommon.misc
241

  
242
        wcs.qommon.misc._http_request = self.wcs_qommon_misc_http_request
243
        qommon.misc._http_request = self.qommon_misc_http_request
244

  
245
        del self.wcs_qommon_misc_http_request
246
        del self.qommon_misc_http_request
230 247

  
231 248
    def http_request(self, url, method='GET', body=None, headers={},
232 249
            cert_file=None, timeout=None, raise_on_http_errors=False):
......
293 310
    def empty(self):
294 311
        self.requests = []
295 312

  
296
http_requests = HttpRequestsMocking()
297

  
298 313

  
299 314
class SMSMocking(wcs.qommon.sms.MobytSMS):
300 315
    def get_sms_class(self, mode):
301
-