def test_request_success_response(): responses.add( responses.GET, 'https://gateway.watsonplatform.net/test/api', status=200, body=json.dumps({ 'foo': 'bar' }), content_type='application/json') service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) prepped = service.prepare_request('GET', url='') detailed_response = service.send(prepped) assert detailed_response.get_result() == {"foo": "bar"}
def test_disable_ssl_verification(): service1 = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator(), disable_ssl_verification=True) assert service1.disable_ssl_verification is True service1.set_disable_ssl_verification(False) assert service1.disable_ssl_verification is False cp4d_authenticator = CloudPakForDataAuthenticator('my_username', 'my_password', 'my_url') service2 = AnyServiceV1('2017-07-07', authenticator=cp4d_authenticator) assert service2.disable_ssl_verification is False cp4d_authenticator.set_disable_ssl_verification(True) assert service2.authenticator.token_manager.disable_ssl_verification is True
def test_gzip_compression(): # Should return uncompressed data when gzip is off service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) assert not service.get_enable_gzip_compression() prepped = service.prepare_request( 'GET', url='', data=json.dumps({"foo": "bar"})) assert prepped['data'] == b'{"foo": "bar"}' assert prepped['headers'].get('content-encoding') != 'gzip' # Should return compressed data when gzip is on service.set_enable_gzip_compression(True) assert service.get_enable_gzip_compression() prepped = service.prepare_request( 'GET', url='', data=json.dumps({"foo": "bar"})) assert prepped['data'] == gzip.compress(b'{"foo": "bar"}') assert prepped['headers'].get('content-encoding') == 'gzip' # Should return compressed data when gzip is on for non-json data assert service.get_enable_gzip_compression() prepped = service.prepare_request('GET', url='', data=b'rawdata') assert prepped['data'] == gzip.compress(b'rawdata') assert prepped['headers'].get('content-encoding') == 'gzip' # Should return compressed data when gzip is on for gzip file data assert service.get_enable_gzip_compression() with tempfile.TemporaryFile(mode='w+b') as t_f: with gzip.GzipFile(mode='wb', fileobj=t_f) as gz_f: gz_f.write(json.dumps({"foo": "bar"}).encode()) with gzip.GzipFile(mode='rb', fileobj=t_f) as gz_f: gzip_data = gz_f.read() prepped = service.prepare_request('GET', url='', data=gzip_data) assert prepped['data'] == gzip.compress(t_f.read()) assert prepped['headers'].get('content-encoding') == 'gzip' # Should return compressed json data when gzip is on for gzip file json data assert service.get_enable_gzip_compression() with tempfile.TemporaryFile(mode='w+b') as t_f: with gzip.GzipFile(mode='wb', fileobj=t_f) as gz_f: gz_f.write("rawdata".encode()) with gzip.GzipFile(mode='rb', fileobj=t_f) as gz_f: gzip_data = gz_f.read() prepped = service.prepare_request('GET', url='', data=gzip_data) assert prepped['data'] == gzip.compress(t_f.read()) assert prepped['headers'].get('content-encoding') == 'gzip' # Should return uncompressed data when content-encoding is set assert service.get_enable_gzip_compression() prepped = service.prepare_request('GET', url='', headers={"content-encoding": "gzip"}, data=json.dumps({"foo": "bar"})) assert prepped['data'] == b'{"foo": "bar"}' assert prepped['headers'].get('content-encoding') == 'gzip'
def test_gzip_compression_external(): # Should set gzip compression from external config file_path = os.path.join(os.path.dirname(__file__), '../resources/ibm-credentials-gzip.env') os.environ['IBM_CREDENTIALS_FILE'] = file_path service = IncludeExternalConfigService('v1', authenticator=NoAuthAuthenticator()) assert service.service_url == 'https://mockurl' assert service.get_enable_gzip_compression() is True prepped = service.prepare_request('GET', url='', data=json.dumps({"foo": "bar"})) assert prepped['data'] == gzip.compress(b'{"foo": "bar"}') assert prepped['headers'].get('content-encoding') == 'gzip'
def test_http_head(): service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) expected_headers = {'Test-Header1': 'value1', 'Test-Header2': 'value2'} responses.add(responses.HEAD, service.default_url, status=200, headers=expected_headers, content_type=None) response = service.head_request() assert response is not None assert len(responses.calls) == 1 assert response.headers is not None assert response.headers == expected_headers
def test_http_config(): service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator()) responses.add( responses.GET, service.default_url, status=200, body=json.dumps({ "foobar": "baz" }), content_type='application/json') response = service.with_http_config({'timeout': 100}) assert response is not None assert len(responses.calls) == 1
def test_retry_config_non_default(): service = BaseService(service_url='https://mockurl/', authenticator=NoAuthAuthenticator()) service.enable_retries(2, 0.3) assert service.retry_config.total == 2 assert service.retry_config.backoff_factor == 0.3 # Ensure retries fail after 2 retries error = ConnectTimeoutError() retry = service.http_client.get_adapter('https://').max_retries retry = retry.increment(error=error) retry = retry.increment(error=error) with pytest.raises(MaxRetryError) as retry_err: retry.increment(error=error) assert retry_err.value.reason == error
def test_retry_config_disable(): # Test disabling retries service = BaseService(service_url='https://mockurl/', authenticator=NoAuthAuthenticator()) service.enable_retries() service.disable_retries() assert service.retry_config is None assert service.http_client.get_adapter('https://').max_retries.total == 0 # Ensure retries are not started after one connection attempt error = ConnectTimeoutError() retry = service.http_client.get_adapter('https://').max_retries with pytest.raises(MaxRetryError) as retry_err: retry.increment(error=error) assert retry_err.value.reason == error
def test_request_server_error(): responses.add( responses.GET, 'https://gateway.watsonplatform.net/test/api', status=500, body=json.dumps({ 'error': 'internal server error' }), content_type='application/json') service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) try: prepped = service.prepare_request('GET', url='') service.send(prepped) except ApiException as err: assert err.message == 'internal server error'
def test_request_fail_401(): responses.add( responses.GET, 'https://gateway.watsonplatform.net/test/api', status=401, body=json.dumps({ 'foo': 'bar' }), content_type='application/json') service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) try: prepped = service.prepare_request('GET', url='') service.send(prepped) except ApiException as err: assert err.message == 'Unauthorized: Access is denied due to invalid credentials'
def test_no_auth_authenticator(): authenticator = NoAuthAuthenticator() assert authenticator is not None assert authenticator.authentication_type == 'noAuth' authenticator.validate() request = {'headers': {}} authenticator.authenticate(request) assert request['headers'] == {}
def test_files_list(): service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) form_data = [] file = open( os.path.join( os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r') form_data.append(('file1', (None, file, 'application/octet-stream'))) form_data.append(('string1', (None, 'hello', 'text/plain'))) request = service.prepare_request('GET', url='', headers={'X-opt-out': True}, files=form_data) files = request['files'] assert isinstance(files, list) assert len(files) == 2 files_dict = dict(files) file1 = files_dict['file1'] assert file1[0] == 'ibm-credentials-iam.env' string1 = files_dict['string1'] assert string1[0] is None
def test_retry_config_external(): file_path = os.path.join( os.path.dirname(__file__), '../resources/ibm-credentials-retry.env') os.environ['IBM_CREDENTIALS_FILE'] = file_path service = IncludeExternalConfigService( 'v1', authenticator=NoAuthAuthenticator()) assert service.retry_config.total == 3 assert service.retry_config.backoff_factor == 0.2 # Ensure retries fail after 3 retries error = ConnectTimeoutError() retry = service.http_client.get_adapter('https://').max_retries retry = retry.increment(error=error) retry = retry.increment(error=error) retry = retry.increment(error=error) with pytest.raises(MaxRetryError) as retry_err: retry.increment(error=error) assert retry_err.value.reason == error
def test_request_success_json(): responses.add(responses.GET, 'https://gateway.watsonplatform.net/test/api', status=200, body=json.dumps({'foo': 'bar'}), content_type='application/json') service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) prepped = service.prepare_request('GET', url='') detailed_response = service.send(prepped) assert detailed_response.get_result() == {'foo': 'bar'} service = AnyServiceV1('2018-11-20', authenticator=BasicAuthenticator( 'my_username', 'my_password')) service.set_default_headers({'test': 'header'}) service.set_disable_ssl_verification(True) prepped = service.prepare_request('GET', url='') detailed_response = service.send(prepped) assert detailed_response.get_result() == {'foo': 'bar'}
def test_no_auth(): class MadeUp: def __init__(self): self.lazy = 'made up' with pytest.raises(ValueError) as err: service = AnyServiceV1('2017-07-07', authenticator=MadeUp()) service.prepare_request( responses.GET, url='https://gateway.watsonplatform.net/test/api', ) assert str(err.value) == 'authenticator should be of type Authenticator' service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator()) service.prepare_request( responses.GET, url='https://gateway.watsonplatform.net/test/api', ) assert service.authenticator is not None assert isinstance(service.authenticator, Authenticator)
def test_user_agent_header(): service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) user_agent_header = service.user_agent_header assert user_agent_header is not None assert user_agent_header['User-Agent'] is not None responses.add(responses.GET, 'https://gateway.watsonplatform.net/test/api', status=200, body='some text') prepped = service.prepare_request('GET', url='', headers={'user-agent': 'my_user_agent'}) response = service.send(prepped) assert response.get_result().request.headers.__getitem__( 'user-agent') == 'my_user_agent' prepped = service.prepare_request('GET', url='', headers=None) response = service.send(prepped) assert response.get_result().request.headers.__getitem__( 'user-agent') == user_agent_header['User-Agent']
def test_stream_json_response(): service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator()) path = '/v1/streamjson' test_url = service.default_url + path expected_response = json.dumps({ "id": 1, "rev": "v1", "content": "this is a document" }) # print("Expected response: ", expected_response) # Simulate a JSON response responses.add(responses.GET, test_url, status=200, body=expected_response, content_type='application/json') # Invoke the operation and receive an "iterable" as the response response = service.get_document_as_stream() assert response is not None assert len(responses.calls) == 1 # retrieve the requests.Response object from the DetailedResponse resp = response.get_result() assert isinstance(resp, requests.Response) assert hasattr(resp, "iter_content") # Retrieve the response body, one chunk at a time. actual_response = '' for chunk in resp.iter_content(chunk_size=3): actual_response += chunk.decode("utf-8") # print("Actual response: ", actual_response) assert actual_response == expected_response
def test_files_duplicate_parts(): service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) form_data = [] file = open( os.path.join( os.path.dirname(__file__), '../resources/ibm-credentials-iam.env'), 'r') form_data.append(('creds_file', (None, file, 'application/octet-stream'))) file = open( os.path.join( os.path.dirname(__file__), '../resources/ibm-credentials-basic.env'), 'r') form_data.append(('creds_file', (None, file, 'application/octet-stream'))) file = open( os.path.join( os.path.dirname(__file__), '../resources/ibm-credentials-bearer.env'), 'r') form_data.append(('creds_file', (None, file, 'application/octet-stream'))) request = service.prepare_request('GET', url='', headers={'X-opt-out': True}, files=form_data) files = request['files'] assert isinstance(files, list) assert len(files) == 3 for part_name, file_tuple in files: assert part_name == 'creds_file' assert file_tuple[0] is not None
def test_reserved_keys(caplog): service = AnyServiceV1('2021-07-02', authenticator=NoAuthAuthenticator()) responses.add( responses.GET, 'https://gateway.watsonplatform.net/test/api', status=200, body='some text') prepped = service.prepare_request('GET', url='', headers={'key': 'OK'}) response = service.send( prepped, headers={'key': 'bad'}, method='POST', url='localhost', cookies=None, hooks={'response': []}) assert response.get_result().request.headers.__getitem__('key') == 'OK' assert response.get_result().request.url == 'https://gateway.watsonplatform.net/test/api' assert response.get_result().request.method == 'GET' assert response.get_result().request.hooks == {'response': []} assert caplog.record_tuples[0][2] == '"method" has been removed from the request' assert caplog.record_tuples[1][2] == '"url" has been removed from the request' assert caplog.record_tuples[2][2] == '"headers" has been removed from the request' assert caplog.record_tuples[3][2] == '"cookies" has been removed from the request'
def retrieve_workspace( iam_apikey=None, workspace_id=None, url=DEFAULT_PROD_URL, api_version=DEFAULT_API_VERSION, username=DEFAULT_USERNAME, password=None, export_flag=True, ): """ Retrieve workspace from Assistant instance :param iam_apikey: :param workspace_id: :param url: :param api_version: :param username: :param password: :param export_flag: :return workspace: workspace json """ if iam_apikey: authenticator = IAMAuthenticator(apikey=iam_apikey) elif username and password: authenticator = BasicAuthenticator(username=username, password=password) else: authenticator = NoAuthAuthenticator() conversation = ibm_watson.AssistantV1( authenticator=authenticator, version=api_version ) conversation.set_service_url(url) if export_flag: ws_json = conversation.get_workspace(workspace_id, export=export_flag) return conversation, ws_json.get_result() return conversation
def test_misc_methods(): class MockModel: def __init__(self, xyz=None): self.xyz = xyz def _to_dict(self): _dict = {} if hasattr(self, 'xyz') and self.xyz is not None: _dict['xyz'] = self.xyz return _dict @classmethod def _from_dict(cls, _dict): args = {} if 'xyz' in _dict: args['xyz'] = _dict.get('xyz') return cls(**args) mock = MockModel('foo') service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) model1 = service._convert_model(mock) assert model1 == {'xyz': 'foo'} model2 = service._convert_model("{\"xyz\": \"foo\"}") assert model2 is not None assert model2['xyz'] == 'foo' temp = ['default', '123'] res_str = service._convert_list(temp) assert res_str == 'default,123' temp2 = 'default123' res_str2 = service._convert_list(temp2) assert res_str2 == temp2
def test_configure_service_error(): service = BaseService(service_url='v1', authenticator=NoAuthAuthenticator()) with pytest.raises(ValueError) as err: service.configure_service(None) assert str(err.value) == 'Service_name must be of type string.'
def test_service_url_not_set(): service = BaseService(service_url='', authenticator=NoAuthAuthenticator()) with pytest.raises(ValueError) as err: service.prepare_request('POST', url='') assert str(err.value) == 'The service_url is required'
def test_default_headers(): service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) service.set_default_headers({'xxx': 'yyy'}) assert service.default_headers == {'xxx': 'yyy'} with pytest.raises(TypeError): service.set_default_headers('xxx')
def test_fail_http_config(): service = AnyServiceV1('2017-07-07', authenticator=NoAuthAuthenticator()) with pytest.raises(TypeError): service.with_http_config(None)
def test_json(): service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) req = service.prepare_request('POST', url='', headers={'X-opt-out': True}, data={'hello': 'world'}) assert req.get('data') == "{\"hello\": \"world\"}"
def test_json(): service = AnyServiceV1('2018-11-20', authenticator=NoAuthAuthenticator()) req = service.prepare_request('POST', url='', headers={ 'X-opt-out': True}, data={'hello': 'world', 'fóó': 'bår'}) assert req.get( 'data') == b'{"hello": "world", "f\\u00f3\\u00f3": "b\\u00e5r"}'