From 57f80cb01ba94e0893125b3582521431cfafe476 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 8 Dec 2017 14:54:03 +0100 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(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2fb5cb56..783847ac 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,7 @@ import ConfigParser import pytest -from utilities import EmailsMocking, SMSMocking +from utilities import EmailsMocking, SMSMocking, HttpRequestsMocking def pytest_addoption(parser): parser.addoption('--without-postgresql-tests', action='store_true', @@ -57,7 +57,14 @@ def emails(): with EmailsMocking() as mock: yield mock + @pytest.fixture def sms_mocking(): with SMSMocking() as sms: yield sms + + +@pytest.fixture +def http_requests(): + with HttpRequestsMocking() as http_requests: + yield http_requests diff --git a/tests/test_admin_pages.py b/tests/test_admin_pages.py index 5511397f..0ada73f5 100644 --- a/tests/test_admin_pages.py +++ b/tests/test_admin_pages.py @@ -42,7 +42,7 @@ from wcs.wf.wscall import WebserviceCallStatusItem from wcs.formdef import FormDef from wcs import fields -from utilities import get_app, login, create_temporary_pub, clean_temporary_pub +from utilities import get_app, login, create_temporary_pub, clean_temporary_pub, HttpRequestsMocking def pytest_generate_tests(metafunc): if 'pub' in metafunc.fixturenames: @@ -1507,7 +1507,7 @@ def test_form_overwrite(pub): assert FormDef.get(formdef_id).url_name == 'form-test' assert FormDef.get(formdef_id).table_name == 'xxx' -def test_form_comment_with_error_in_wscall(pub): +def test_form_comment_with_error_in_wscall(http_requests, pub): create_superuser(pub) NamedWsCall.wipe() @@ -3923,7 +3923,8 @@ def test_data_sources_view(pub): data_source.data_source = {'type': 'json', 'value': 'file://%s' % json_file_path} data_source.store() - resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) + with HttpRequestsMocking() as http_requests: + resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id) assert 'Preview' in resp.body assert 'foo' in resp.body diff --git a/tests/test_backoffice_pages.py b/tests/test_backoffice_pages.py index 256b3499..5c8d5ab7 100644 --- a/tests/test_backoffice_pages.py +++ b/tests/test_backoffice_pages.py @@ -2105,7 +2105,7 @@ def test_backoffice_submission_no_manual_channel_with_welco(pub, welco_url): resp = app.get('/backoffice/submission/%s/' % formdef.url_name) assert 'submission_channel' not in resp.form.fields -def test_backoffice_wscall_failure_display(pub): +def test_backoffice_wscall_failure_display(http_requests, pub): create_user(pub) create_environment(pub) formdef = FormDef.get_by_urlname('form-title') @@ -2153,7 +2153,7 @@ def test_backoffice_wscall_failure_display(pub): assert (' with the number %s.' % number31.get_display_id()) in resp.body assert not 'Error during webservice call' in resp.body -def test_backoffice_wscall_attachment(pub): +def test_backoffice_wscall_attachment(http_requests, pub): create_user(pub) create_environment(pub) formdef = FormDef.get_by_urlname('form-title') @@ -3782,7 +3782,7 @@ def test_backoffice_private_status_and_history_with_assigned_function(pub): assert 'HELLO WORLD' in resp.body assert 'id="evolution-log"' in resp.body -def test_backoffice_formdata_named_wscall(pub): +def test_backoffice_formdata_named_wscall(http_requests, pub): user = create_user(pub) create_environment(pub) diff --git a/tests/test_datasource.py b/tests/test_datasource.py index 9992671e..2c6382d2 100644 --- a/tests/test_datasource.py +++ b/tests/test_datasource.py @@ -105,7 +105,7 @@ def test_python_datasource(): ('foo', 'Foo', 'foo', {'id': 'foo', 'text': 'Foo'}), ('bar', 'Bar', 'bar', {'id': 'bar', 'text': 'Bar', 'disabled': True})] -def test_json_datasource(): +def test_json_datasource(http_requests): datasource = {'type': 'json', 'value': ''} assert data_sources.get_items(datasource) == [] diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py index e3647c67..52701376 100644 --- a/tests/test_form_pages.py +++ b/tests/test_form_pages.py @@ -3007,7 +3007,7 @@ def test_form_date_field_submit(pub): data = formdef.data_class().get(data_id) assert data.data['0'] is None -def test_form_jsonp_item_field(pub): +def test_form_jsonp_item_field(http_requests, pub): formdef = create_formdef() formdef.fields = [ fields.ItemField(id='1', label='string', type='item', @@ -3577,7 +3577,7 @@ def test_form_page_profile_verified_prefill(pub): assert not 'Check values then click submit.' in resp.body assert resp.form['f0'].value == 'foo@localhost' -def test_item_field_with_disabled_items(pub): +def test_item_field_with_disabled_items(http_requests, pub): user = create_user(pub) formdef = create_formdef() formdef.data_class().wipe() @@ -3669,7 +3669,7 @@ def test_item_field_with_disabled_items(pub): resp = resp.form.submit('submit') # -> validation page assert 'There were errors processing the form' in resp.body -def test_items_field_with_disabled_items(pub): +def test_items_field_with_disabled_items(http_requests, pub): user = create_user(pub) formdef = create_formdef() formdef.data_class().wipe() @@ -3780,7 +3780,7 @@ def test_logged_errors(pub): assert len(LoggedError.get_ids_with_indexed_value('workflow_id', '12')) == 1 assert len(LoggedError.get_ids_with_indexed_value('workflow_id', 'X')) == 0 -def test_formdata_named_wscall(pub): +def test_formdata_named_wscall(http_requests, pub): create_user(pub) NamedWsCall.wipe() diff --git a/tests/test_hobo.py b/tests/test_hobo.py index efefa225..74b1b806 100644 --- a/tests/test_hobo.py +++ b/tests/test_hobo.py @@ -340,7 +340,7 @@ def test_update_profile(): else: assert attribute_mapping[attribute_name] == field_id -def test_configure_authentication_methods(): +def test_configure_authentication_methods(http_requests): pub.cfg['idp'] = {} service = [x for x in HOBO_JSON.get('services', []) if x.get('service-id') == 'wcs'][0] diff --git a/tests/test_workflows.py b/tests/test_workflows.py index bab6530c..bd1cb017 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -43,7 +43,7 @@ from wcs.wf.backoffice_fields import SetBackofficeFieldsWorkflowStatusItem from wcs.wf.redirect_to_url import RedirectToUrlWorkflowStatusItem from utilities import (create_temporary_pub, MockSubstitutionVariables, - http_requests, clean_temporary_pub) + clean_temporary_pub) def setup_module(module): cleanup() @@ -946,7 +946,8 @@ def test_email_attachments(pub, emails): assert len(emails.emails['foobar']['msg'].get_payload()) == 3 -def test_webservice_call(pub): + +def test_webservice_call(http_requests, pub): pub.substitutions.feed(MockSubstitutionVariables()) FormDef.wipe() @@ -1233,7 +1234,7 @@ def test_webservice_waitpoint(pub): item.action_on_network_errors = ':stop' assert item.waitpoint -def test_webservice_call_error_handling(pub): +def test_webservice_call_error_handling(http_requests, pub): pub.substitutions.feed(MockSubstitutionVariables()) FormDef.wipe() @@ -1468,7 +1469,7 @@ def test_webservice_call_error_handling(pub): item.perform(formdata) assert formdata.evolution[-1].parts[-1].summary == 'ConnectionError: error\n' -def test_webservice_call_store_in_backoffice_filefield(pub): +def test_webservice_call_store_in_backoffice_filefield(http_requests, pub): wf = Workflow(name='wscall to backoffice file field') wf.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(wf) wf.backoffice_fields_formdef.fields = [ @@ -2569,7 +2570,7 @@ def test_profile(pub): item.perform(formdata) assert pub.user_class.get(user.id).form_data == {'3': 'Plop'} -def test_set_backoffice_field(two_pubs): +def test_set_backoffice_field(http_requests, two_pubs): Workflow.wipe() FormDef.wipe() wf = Workflow(name='xxx') @@ -2617,7 +2618,7 @@ def test_set_backoffice_field(two_pubs): formdata = formdef.data_class().get(formdata.id) assert formdata.data['bo1'] == 'HELLO GOODBYE' -def test_set_backoffice_field_file(two_pubs): +def test_set_backoffice_field_file(http_requests, two_pubs): Workflow.wipe() FormDef.wipe() wf = Workflow(name='xxx') diff --git a/tests/test_wscall.py b/tests/test_wscall.py index 6f6dac76..feebf24b 100644 --- a/tests/test_wscall.py +++ b/tests/test_wscall.py @@ -7,7 +7,7 @@ from wcs.qommon.http_request import HTTPRequest from wcs.qommon.template import Template from wcs.wscalls import NamedWsCall -from utilities import create_temporary_pub, clean_temporary_pub, http_requests +from utilities import create_temporary_pub, clean_temporary_pub @pytest.fixture @@ -50,7 +50,8 @@ def test_named_wscall(pub): assert 'bye' in NamedWsCall.keys() assert not 'hello_1' in NamedWsCall.keys() -def test_webservice_substitution_variable(pub): + +def test_webservice_substitution_variable(http_requests, pub): NamedWsCall.wipe() wscall = NamedWsCall() @@ -63,7 +64,8 @@ def test_webservice_substitution_variable(pub): variables = pub.substitutions.get_context_variables() assert variables['webservice'].hello_world == {'foo': 'bar'} -def test_webservice_auto_sign(pub): + +def test_webservice_auto_sign(http_requests, pub): NamedWsCall.wipe() wscall = NamedWsCall() @@ -100,7 +102,8 @@ def test_webservice_auto_sign(pub): assert not 'orig=example.net' in http_requests.get_last('url') assert 'signature=' in http_requests.get_last('url') -def test_webservice_post_with_no_payload(pub): + +def test_webservice_post_with_no_payload(http_requests, pub): NamedWsCall.wipe() wscall = NamedWsCall() @@ -109,7 +112,7 @@ def test_webservice_post_with_no_payload(pub): wscall.call() assert http_requests.get_last('body') is None -def test_wscall_ezt(pub): +def test_wscall_ezt(http_requests, pub): NamedWsCall.wipe() wscall = NamedWsCall() diff --git a/tests/utilities.py b/tests/utilities.py index 265464d5..db3e859c 100644 --- a/tests/utilities.py +++ b/tests/utilities.py @@ -223,10 +223,27 @@ class MockSubstitutionVariables(object): class HttpRequestsMocking(object): def __init__(self): self.requests = [] + + def __enter__(self): import wcs.qommon.misc import qommon.misc + + self.wcs_qommon_misc_http_request = wcs.qommon.misc._http_request + self.qommon_misc_http_request = qommon.misc._http_request + wcs.qommon.misc._http_request = self.http_request qommon.misc._http_request = self.http_request + return self + + def __exit__(self, exc_type, exc_value, tb): + import wcs.qommon.misc + import qommon.misc + + wcs.qommon.misc._http_request = self.wcs_qommon_misc_http_request + qommon.misc._http_request = self.qommon_misc_http_request + + del self.wcs_qommon_misc_http_request + del self.qommon_misc_http_request def http_request(self, url, method='GET', body=None, headers={}, cert_file=None, timeout=None, raise_on_http_errors=False): @@ -293,8 +310,6 @@ class HttpRequestsMocking(object): def empty(self): self.requests = [] -http_requests = HttpRequestsMocking() - class SMSMocking(wcs.qommon.sms.MobytSMS): def get_sms_class(self, mode): -- 2.15.0