def test__request_handler_Should_CallFunctionWithKwargs_When_Kwargs( self, get_headers, *patches): get_headers.return_value = {'h1': 'v1'} mock_function = Mock(__name__='mocked method') client = RESTclient('hostname1.company.com') decorated_function = RESTclient.request_handler(mock_function) object1 = b'' decorated_function(client, '/rest/endpoint', kwarg1='kwarg1', kwarg2='kwarg2', kwarg3=object1, verify=False) expected_kwargs = { 'headers': { 'h1': 'v1' }, 'verify': False, 'address': 'https://hostname1.company.com/rest/endpoint', 'kwarg1': 'kwarg1', 'kwarg2': 'kwarg2', 'kwarg3': object1 } _, kwargs = mock_function.call_args_list[0] self.assertEqual(kwargs, expected_kwargs)
def test__request_handler_Should_NotCallFunctionAndReturnNone_When_FunctionSetsNoop( self, *patches): mock_function = Mock(__name__='mocked method') client = RESTclient('hostname1.company.com') decorated_function = RESTclient.request_handler(mock_function) result = decorated_function(client, '/rest/endpoint', noop=True) self.assertIsNone(result) self.assertFalse(mock_function.called)
def test__request_handler_Should_CallFunctionAndReturnResult_When_FunctionDoesNotSetNoop( self, *patches): mock_function = Mock(__name__='mocked method') client = RESTclient('hostname1.company.com') decorated_function = RESTclient.request_handler(mock_function) result = decorated_function(client, '/rest/endpoint') self.assertTrue(mock_function.called) self.assertEqual(result, 'result')
def test__request_handler_Should_CallFunctionWithArgs_When_Args( self, *patches): mock_function = Mock(__name__='mocked method') client = RESTclient('hostname1.company.com') decorated_function = RESTclient.request_handler(mock_function) decorated_function(client, '/rest/endpoint', k1='arg1', k2='arg2') expected_args = (client, '/rest/endpoint') args, _ = mock_function.call_args_list[0] self.assertEqual(args, expected_args)
def test__request_handler_Should_CallFunctionAndReturnResult_When_SslAdapter( self, session_patch, ssl_adapter_patch, *patches): session_mock = Mock() session_patch.return_value.__enter__.return_value = session_mock mock_function = Mock(__name__='mocked method') client = RESTclient('hostname1.company.com', certfile='-certfile-', certpass='******') decorated_function = RESTclient.request_handler(mock_function) result = decorated_function(client, '/rest/endpoint', key='value') session_mock.mount.assert_called_once_with( 'https://hostname1.company.com', ssl_adapter_patch.return_value) session_mock.request.assert_called_once_with( 'mocked method', 'https://hostname1.company.com/rest/endpoint', key='value', headers={'Content-Type': 'application/json'}, verify='/etc/ssl/certs/ca-certificates.crt') self.assertEqual(result, 'result')