From 58baedfc5e0cba0d41f7c93d6e714f21f5f6c5bd Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 25 May 2017 01:25:17 +0200 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(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2fb5cb5..783847a 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 258b9a2..3da925d 100644 --- a/tests/test_admin_pages.py +++ b/tests/test_admin_pages.py @@ -1409,7 +1409,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() diff --git a/tests/test_backoffice_pages.py b/tests/test_backoffice_pages.py index 93b7bb2..79bef25 100644 --- a/tests/test_backoffice_pages.py +++ b/tests/test_backoffice_pages.py @@ -1963,7 +1963,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') diff --git a/tests/test_workflows.py b/tests/test_workflows.py index ef8c293..272d414 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -40,7 +40,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() @@ -691,7 +691,8 @@ def test_email(pub, emails): assert emails.count() == 1 assert emails.get('foobar').get('from') == 'foobar@localhost' -def test_webservice_call(pub): + +def test_webservice_call(http_requests, pub): pub.substitutions.feed(MockSubstitutionVariables()) FormDef.wipe() @@ -951,7 +952,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() @@ -2033,7 +2034,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') @@ -2071,7 +2072,7 @@ def test_set_backoffice_field(two_pubs): formdata = formdef.data_class().get(formdata.id) assert formdata.data['bo1'] == 'HELLO' -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 b2bf0ba..6036f70 100644 --- a/tests/test_wscall.py +++ b/tests/test_wscall.py @@ -5,7 +5,7 @@ import pytest from wcs.qommon.http_request import HTTPRequest 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 @@ -48,7 +48,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() @@ -61,7 +62,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() @@ -98,7 +100,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() diff --git a/tests/utilities.py b/tests/utilities.py index df04a77..c346919 100644 --- a/tests/utilities.py +++ b/tests/utilities.py @@ -237,10 +237,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={}, timeout=None): self.requests.append( @@ -297,8 +314,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.1.4