Projet

Général

Profil

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

Benjamin Dauvergne, 25 mai 2017 15:02

Télécharger (6,79 ko)

Voir les différences:

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

 tests/conftest.py              |  9 ++++++++-
 tests/test_admin_pages.py      |  2 +-
 tests/test_backoffice_pages.py |  2 +-
 tests/test_workflows.py        | 11 ++++++-----
 tests/test_wscall.py           | 11 +++++++----
 tests/utilities.py             | 19 +++++++++++++++++--
 6 files changed, 40 insertions(+), 14 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
1409 1409
    assert FormDef.get(formdef_id).url_name == 'form-test'
1410 1410
    assert FormDef.get(formdef_id).table_name == 'xxx'
1411 1411

  
1412
def test_form_comment_with_error_in_wscall(pub):
1412
def test_form_comment_with_error_in_wscall(http_requests, pub):
1413 1413
    create_superuser(pub)
1414 1414
    NamedWsCall.wipe()
1415 1415

  
tests/test_backoffice_pages.py
1963 1963
    assert (' with the number %s.' % number31.get_display_id()) in resp.body
1964 1964
    assert not 'Error during webservice call' in resp.body
1965 1965

  
1966
def test_backoffice_wscall_attachment(pub):
1966
def test_backoffice_wscall_attachment(http_requests, pub):
1967 1967
    create_user(pub)
1968 1968
    create_environment(pub)
1969 1969
    formdef = FormDef.get_by_urlname('form-title')
tests/test_workflows.py
40 40
from wcs.wf.redirect_to_url import RedirectToUrlWorkflowStatusItem
41 41

  
42 42
from utilities import (create_temporary_pub, MockSubstitutionVariables,
43
        http_requests, clean_temporary_pub)
43
        clean_temporary_pub)
44 44

  
45 45
def setup_module(module):
46 46
    cleanup()
......
691 691
    assert emails.count() == 1
692 692
    assert emails.get('foobar').get('from') == 'foobar@localhost'
693 693

  
694
def test_webservice_call(pub):
694

  
695
def test_webservice_call(http_requests, pub):
695 696
    pub.substitutions.feed(MockSubstitutionVariables())
696 697

  
697 698
    FormDef.wipe()
......
951 952
    item.action_on_network_errors = ':stop'
952 953
    assert item.waitpoint
953 954

  
954
def test_webservice_call_error_handling(pub):
955
def test_webservice_call_error_handling(http_requests, pub):
955 956
    pub.substitutions.feed(MockSubstitutionVariables())
956 957

  
957 958
    FormDef.wipe()
......
2033 2034
    item.perform(formdata)
2034 2035
    assert pub.user_class.get(user.id).form_data == {'3': 'Plop'}
2035 2036

  
2036
def test_set_backoffice_field(two_pubs):
2037
def test_set_backoffice_field(http_requests, two_pubs):
2037 2038
    Workflow.wipe()
2038 2039
    FormDef.wipe()
2039 2040
    wf = Workflow(name='xxx')
......
2071 2072
    formdata = formdef.data_class().get(formdata.id)
2072 2073
    assert formdata.data['bo1'] == 'HELLO'
2073 2074

  
2074
def test_set_backoffice_field_file(two_pubs):
2075
def test_set_backoffice_field_file(http_requests, two_pubs):
2075 2076
    Workflow.wipe()
2076 2077
    FormDef.wipe()
2077 2078
    wf = Workflow(name='xxx')
tests/test_wscall.py
5 5
from wcs.qommon.http_request import HTTPRequest
6 6
from wcs.wscalls import NamedWsCall
7 7

  
8
from utilities import create_temporary_pub, clean_temporary_pub, http_requests
8
from utilities import create_temporary_pub, clean_temporary_pub
9 9

  
10 10

  
11 11
@pytest.fixture
......
48 48
    assert 'bye' in NamedWsCall.keys()
49 49
    assert not 'hello_1' in NamedWsCall.keys()
50 50

  
51
def test_webservice_substitution_variable(pub):
51

  
52
def test_webservice_substitution_variable(http_requests, pub):
52 53
    NamedWsCall.wipe()
53 54

  
54 55
    wscall = NamedWsCall()
......
61 62
    variables = pub.substitutions.get_context_variables()
62 63
    assert variables['webservice'].hello_world == {'foo': 'bar'}
63 64

  
64
def test_webservice_auto_sign(pub):
65

  
66
def test_webservice_auto_sign(http_requests, pub):
65 67
    NamedWsCall.wipe()
66 68

  
67 69
    wscall = NamedWsCall()
......
98 100
    assert not 'orig=example.net' in http_requests.get_last('url')
99 101
    assert 'signature=' in http_requests.get_last('url')
100 102

  
101
def test_webservice_post_with_no_payload(pub):
103

  
104
def test_webservice_post_with_no_payload(http_requests, pub):
102 105
    NamedWsCall.wipe()
103 106

  
104 107
    wscall = NamedWsCall()
tests/utilities.py
237 237
class HttpRequestsMocking(object):
238 238
    def __init__(self):
239 239
        self.requests = []
240

  
241
    def __enter__(self):
240 242
        import wcs.qommon.misc
241 243
        import qommon.misc
244

  
245
        self.wcs_qommon_misc_http_request = wcs.qommon.misc._http_request
246
        self.qommon_misc_http_request = qommon.misc._http_request
247

  
242 248
        wcs.qommon.misc._http_request = self.http_request
243 249
        qommon.misc._http_request = self.http_request
250
        return self
251

  
252
    def __exit__(self, exc_type, exc_value, tb):
253
        import wcs.qommon.misc
254
        import qommon.misc
255

  
256
        wcs.qommon.misc._http_request = self.wcs_qommon_misc_http_request
257
        qommon.misc._http_request = self.qommon_misc_http_request
258

  
259
        del self.wcs_qommon_misc_http_request
260
        del self.qommon_misc_http_request
244 261

  
245 262
    def http_request(self, url, method='GET', body=None, headers={}, timeout=None):
246 263
        self.requests.append(
......
297 314
    def empty(self):
298 315
        self.requests = []
299 316

  
300
http_requests = HttpRequestsMocking()
301

  
302 317

  
303 318
class SMSMocking(wcs.qommon.sms.MobytSMS):
304 319
    def get_sms_class(self, mode):
305
-