From 5ed35370b4dcbfd0e106817e7c50163b63d69857 Mon Sep 17 00:00:00 2001 From: Sharat Sharma Date: Wed, 31 May 2017 12:18:02 +0530 Subject: [PATCH] Replace request mocking in test_httpclient with requests-mock Similar to the client functions replace the requests mocking in test_httpclient file with calls to requests-mock. This means we test the actual data on the wire instead of the exact calls to requests so they can change in future. Change-Id: Id65374e14e3b18dfed46f0c78a95ff10502ce785 --- mistralclient/tests/unit/test_httpclient.py | 213 +++++++------------- 1 file changed, 76 insertions(+), 137 deletions(-) diff --git a/mistralclient/tests/unit/test_httpclient.py b/mistralclient/tests/unit/test_httpclient.py index aaa18073..dba27680 100644 --- a/mistralclient/tests/unit/test_httpclient.py +++ b/mistralclient/tests/unit/test_httpclient.py @@ -16,14 +16,14 @@ import base64 import copy import mock -from oslotest import base -import requests +from six.moves.urllib import parse as urlparse from oslo_utils import uuidutils from osprofiler import _utils as osprofiler_utils import osprofiler.profiler from mistralclient.api import httpclient +from mistralclient.tests.unit import base API_BASE_URL = 'http://localhost:8989/v2' API_URL = '/executions' @@ -55,21 +55,7 @@ EXPECTED_BODY = { } -class FakeRequest(object): - - def __init__(self, method): - self.method = method - - -class FakeResponse(object): - - def __init__(self, method, url, status_code): - self.request = FakeRequest(method) - self.url = url - self.status_code = status_code - - -class HTTPClientTest(base.BaseTestCase): +class HTTPClientTest(base.BaseClientTest): def setUp(self): super(HTTPClientTest, self).setUp() @@ -82,18 +68,33 @@ class HTTPClientTest(base.BaseTestCase): region_name=REGION_NAME ) - @mock.patch.object( - requests, - 'get', - mock.MagicMock(return_value=FakeResponse('get', EXPECTED_URL, 200)) - ) + def assertExpectedAuthHeaders(self): + headers = self.requests_mock.last_request.headers + + self.assertEqual(AUTH_TOKEN, headers['X-Auth-Token']) + self.assertEqual(PROJECT_ID, headers['X-Project-Id']) + self.assertEqual(USER_ID, headers['X-User-Id']) + + return headers + + def assertExpectedBody(self): + text = self.requests_mock.last_request.text + form = urlparse.parse_qs(text, strict_parsing=True) + + self.assertEqual(len(EXPECTED_BODY), len(form)) + + for k, v in EXPECTED_BODY.items(): + self.assertEqual([str(v)], form[k]) + + return form + def test_get_request_options(self): + m = self.requests_mock.get(EXPECTED_URL, text='text') + self.client.get(API_URL) - requests.get.assert_called_with( - EXPECTED_URL, - **EXPECTED_REQ_OPTIONS - ) + self.assertTrue(m.called_once) + self.assertExpectedAuthHeaders() @mock.patch.object( osprofiler.profiler._Profiler, @@ -105,12 +106,8 @@ class HTTPClientTest(base.BaseTestCase): 'get_id', mock.MagicMock(return_value=PROFILER_TRACE_ID) ) - @mock.patch.object( - requests, - 'get', - mock.MagicMock(return_value=FakeResponse('get', EXPECTED_URL, 200)) - ) def test_get_request_options_with_profile_enabled(self): + m = self.requests_mock.get(EXPECTED_URL, text='text') osprofiler.profiler.init(PROFILER_HMAC_KEY) data = {'base_id': PROFILER_TRACE_ID, 'parent_id': PROFILER_TRACE_ID} @@ -123,20 +120,13 @@ class HTTPClientTest(base.BaseTestCase): self.client.get(API_URL) - expected_options = copy.deepcopy(EXPECTED_REQ_OPTIONS) - expected_options['headers'].update(headers) + self.assertTrue(m.called_once) + headers = self.assertExpectedAuthHeaders() + self.assertEqual(signed_data[0], headers['X-Trace-Info']) + self.assertEqual(signed_data[1], headers['X-Trace-HMAC']) - requests.get.assert_called_with( - EXPECTED_URL, - **expected_options - ) - - @mock.patch.object( - requests, - 'get', - mock.MagicMock(return_value=FakeResponse('get', EXPECTED_URL, 200)) - ) def test_get_request_options_with_headers_for_get(self): + m = self.requests_mock.get(EXPECTED_URL, text='text') target_auth_url = uuidutils.generate_uuid() target_auth_token = uuidutils.generate_uuid() target_user_id = 'target_user' @@ -166,116 +156,79 @@ class HTTPClientTest(base.BaseTestCase): target_client.get(API_URL) - expected_options = copy.deepcopy(EXPECTED_REQ_OPTIONS) - - headers = expected_options["headers"] - - headers["X-Target-Auth-Uri"] = target_auth_url - headers["X-Target-Auth-Token"] = target_auth_token - headers["X-Target-Insecure"] = target_insecure - headers["X-Target-User-Id"] = target_user_id - headers["X-Target-Project-Id"] = target_project_id - headers["X-Target-Region-Name"] = target_region - headers["X-Target-User-Domain-Name"] = target_user_domain_name - headers["X-Target-Project-Domain-Name"] = target_project_domain_name + self.assertTrue(m.called_once) + headers = self.assertExpectedAuthHeaders() + self.assertEqual(target_auth_url, headers['X-Target-Auth-Uri']) + self.assertEqual(target_auth_token, headers['X-Target-Auth-Token']) + self.assertEqual(target_user_id, headers['X-Target-User-Id']) + self.assertEqual(target_project_id, headers['X-Target-Project-Id']) + self.assertEqual(target_insecure, headers['X-Target-Insecure']) + self.assertEqual(target_region, headers['X-Target-Region-Name']) + self.assertEqual(target_user_domain_name, + headers['X-Target-User-Domain-Name']) + self.assertEqual(target_project_domain_name, + headers['X-Target-Project-Domain-Name']) catalog = base64.b64encode(target_service_catalog.encode('utf-8')) - headers["X-Target-Service-Catalog"] = catalog + self.assertEqual(catalog, headers['X-Target-Service-Catalog']) - requests.get.assert_called_with(EXPECTED_URL, **expected_options) - - @mock.patch.object( - requests, - 'post', - mock.MagicMock(return_value=FakeResponse('post', EXPECTED_URL, 201)) - ) def test_get_request_options_with_headers_for_post(self): + m = self.requests_mock.post(EXPECTED_URL, text='text') headers = {'foo': 'bar'} self.client.post(API_URL, EXPECTED_BODY, headers=headers) - expected_options = copy.deepcopy(EXPECTED_REQ_OPTIONS) - expected_options['headers'].update(headers) - expected_options['headers']['content-type'] = 'application/json' + self.assertTrue(m.called_once) + headers = self.assertExpectedAuthHeaders() + self.assertEqual('application/json', headers['Content-Type']) + self.assertEqual('bar', headers['foo']) + self.assertExpectedBody() - requests.post.assert_called_with( - EXPECTED_URL, - data=EXPECTED_BODY, - **expected_options - ) - - @mock.patch.object( - requests, - 'put', - mock.MagicMock(return_value=FakeResponse('put', EXPECTED_URL, 200)) - ) def test_get_request_options_with_headers_for_put(self): + m = self.requests_mock.put(EXPECTED_URL, text='text') headers = {'foo': 'bar'} self.client.put(API_URL, EXPECTED_BODY, headers=headers) - expected_options = copy.deepcopy(EXPECTED_REQ_OPTIONS) - expected_options['headers'].update(headers) - expected_options['headers']['content-type'] = 'application/json' + self.assertTrue(m.called_once) + headers = self.assertExpectedAuthHeaders() + self.assertEqual('application/json', headers['Content-Type']) + self.assertEqual('bar', headers['foo']) + self.assertExpectedBody() - requests.put.assert_called_with( - EXPECTED_URL, - data=EXPECTED_BODY, - **expected_options - ) - - @mock.patch.object( - requests, - 'delete', - mock.MagicMock(return_value=FakeResponse('delete', EXPECTED_URL, 200)) - ) def test_get_request_options_with_headers_for_delete(self): + m = self.requests_mock.delete(EXPECTED_URL, text='text') headers = {'foo': 'bar'} self.client.delete(API_URL, headers=headers) - expected_options = copy.deepcopy(EXPECTED_REQ_OPTIONS) - expected_options['headers'].update(headers) - - requests.delete.assert_called_with( - EXPECTED_URL, - **expected_options - ) + self.assertTrue(m.called_once) + headers = self.assertExpectedAuthHeaders() + self.assertEqual('bar', headers['foo']) @mock.patch.object( httpclient.HTTPClient, '_get_request_options', mock.MagicMock(return_value=copy.deepcopy(EXPECTED_REQ_OPTIONS)) ) - @mock.patch.object( - requests, - 'get', - mock.MagicMock(return_value=FakeResponse('get', EXPECTED_URL, 200)) - ) def test_http_get(self): + m = self.requests_mock.get(EXPECTED_URL, text='text') self.client.get(API_URL) httpclient.HTTPClient._get_request_options.assert_called_with( 'get', None ) - - requests.get.assert_called_with( - EXPECTED_URL, - **EXPECTED_REQ_OPTIONS - ) + self.assertTrue(m.called_once) + self.assertExpectedAuthHeaders() @mock.patch.object( httpclient.HTTPClient, '_get_request_options', mock.MagicMock(return_value=copy.deepcopy(EXPECTED_REQ_OPTIONS)) ) - @mock.patch.object( - requests, - 'post', - mock.MagicMock(return_value=FakeResponse('post', EXPECTED_URL, 201)) - ) def test_http_post(self): + m = self.requests_mock.post(EXPECTED_URL, status_code=201, text='text') self.client.post(API_URL, EXPECTED_BODY) httpclient.HTTPClient._get_request_options.assert_called_with( @@ -283,23 +236,17 @@ class HTTPClientTest(base.BaseTestCase): None ) - requests.post.assert_called_with( - EXPECTED_URL, - data=EXPECTED_BODY, - **EXPECTED_REQ_OPTIONS - ) + self.assertTrue(m.called_once) + self.assertExpectedAuthHeaders() + self.assertExpectedBody() @mock.patch.object( httpclient.HTTPClient, '_get_request_options', mock.MagicMock(return_value=copy.deepcopy(EXPECTED_REQ_OPTIONS)) ) - @mock.patch.object( - requests, - 'put', - mock.MagicMock(return_value=FakeResponse('put', EXPECTED_URL, 200)) - ) def test_http_put(self): + m = self.requests_mock.put(EXPECTED_URL, json={}) self.client.put(API_URL, EXPECTED_BODY) httpclient.HTTPClient._get_request_options.assert_called_with( @@ -307,23 +254,17 @@ class HTTPClientTest(base.BaseTestCase): None ) - requests.put.assert_called_with( - EXPECTED_URL, - data=EXPECTED_BODY, - **EXPECTED_REQ_OPTIONS - ) + self.assertTrue(m.called_once) + self.assertExpectedAuthHeaders() + self.assertExpectedBody() @mock.patch.object( httpclient.HTTPClient, '_get_request_options', mock.MagicMock(return_value=copy.deepcopy(EXPECTED_REQ_OPTIONS)) ) - @mock.patch.object( - requests, - 'delete', - mock.MagicMock(return_value=FakeResponse('delete', EXPECTED_URL, 200)) - ) def test_http_delete(self): + m = self.requests_mock.delete(EXPECTED_URL, text='text') self.client.delete(API_URL) httpclient.HTTPClient._get_request_options.assert_called_with( @@ -331,7 +272,5 @@ class HTTPClientTest(base.BaseTestCase): None ) - requests.delete.assert_called_with( - EXPECTED_URL, - **EXPECTED_REQ_OPTIONS - ) + self.assertTrue(m.called_once) + self.assertExpectedAuthHeaders()