def test_get_api_with_proxy_file(self): secrets = { "api": { config_keys[key]["config"]: value for key, value in source_config_details.items() if value is not None and "proxy" not in key }, "proxy": { config_keys[key]["config"]: value for key, value in source_config_details.items() if value is not None and "proxy" in key } } secrets["api"].pop("clientCertificate", None) if secrets["proxy"].get("address", None) is None: self.skipTest(f"missing proxy configuration") secrets_file = TempFileManager.create_temp_file(secrets) # Load the config factory = ApiClientFactory(api_secrets_filename=secrets_file.name) # Close and thus delete the temporary file TempFileManager.delete_temp_file(secrets_file) api = factory.build(InstrumentsApi) self.validate_api(api)
def test_get_api_with_configuration(self): factory = ApiClientFactory( api_secrets_filename=CredentialsSource.secrets_path() ) api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api)
def test_get_api_with_proxy_config(self): secrets = { "api": { config_keys[key]["config"]: value for key, value in source_config_details.items() if value is not None and "proxy" not in key } } secrets["api"].pop("clientCertificate", None) if source_config_details.get("proxy_address", None) is None: self.skipTest(f"missing proxy configuration") secrets_file = TempFileManager.create_temp_file(secrets) # Load the config with patch.dict('os.environ', {}, clear=True): factory = ApiClientFactory( api_secrets_filename=secrets_file.name, proxy_url=source_config_details["proxy_address"], proxy_username=source_config_details["proxy_username"], proxy_password=source_config_details["proxy_password"]) # Close and thus delete the temporary file TempFileManager.delete_temp_file(secrets_file) api = factory.build(InstrumentsApi) self.validate_api(api)
def test_no_pat_but_good_secrets_file_as_param(self): with patch.dict(self.os_environ_dict_str, self.get_env_vars_without_pat(), clear=True): factory = ApiClientFactory(api_secrets_filename=self.secrets) api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api)
def test_get_api_with_token(self): token, refresh_token = tu.get_okta_tokens( CredentialsSource.secrets_path()) factory = ApiClientFactory(token=token, api_url=source_config_details["api_url"], app_name=source_config_details["app_name"]) api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api)
def test_good_env_pat_but_no_param_pat(self): with patch.dict(self.os_environ_dict_str, self.get_pat_env_var(), clear=True): factory = ApiClientFactory() api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api)
def test_none_str_param_pat_but_good_secrets_envs(self): with patch.dict(self.os_environ_dict_str, self.get_env_vars_without_pat(), clear=True): factory = ApiClientFactory(token="None") api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api)
def test_get_api_with_token_url_as_env_var(self): token, refresh_token = tu.get_okta_tokens(CredentialsSource.secrets_path()) with patch.dict('os.environ', {"FBN_LUSID_API_URL": source_config_details["api_url"]}, clear=True): factory = ApiClientFactory( token=token, app_name=source_config_details["app_name"]) api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api)
def test_get_api_with_info(self): factory = ApiClientFactory( api_secrets_filename=CredentialsSource.secrets_path()) api = factory.build(ScopesApi) self.assertIsInstance(api, ScopesApi) result = api.list_scopes(call_info=lambda r: print(r)) self.assertIsNotNone(result)
def test_wrapped_method(self): factory = ApiClientFactory( api_secrets_filename=CredentialsSource.secrets_path()) wrapped_scopes_api = factory.build(InstrumentsApi) portfolio = InstrumentsApi(wrapped_scopes_api.api_client) self.assertEqual(portfolio.__doc__, wrapped_scopes_api.__doc__) self.assertEqual(portfolio.__module__, wrapped_scopes_api.__module__) self.assertDictEqual(portfolio.__dict__, wrapped_scopes_api.__dict__)
def test_no_env_pat_but_good_param_pat(self): with patch.dict( self.os_environ_dict_str, {"FBN_LUSID_API_URL": self.source_config_details["api_url"]}, clear=True): factory = ApiClientFactory(token=self.pat_token) api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api)
def test_get_api_with_str_none_token(self): factory = ApiClientFactory( token=RefreshingToken(), api_url=source_config_details["api_url"], app_name=source_config_details["app_name"], api_secrets_filename=CredentialsSource.secrets_path(), ) api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api)
def test_get_info_with_invalid_param_throws_error(self): factory = ApiClientFactory( api_secrets_filename=CredentialsSource.secrets_path() ) api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) with self.assertRaises(ValueError) as error: api.get_instrument_identifier_types(call_info="invalid param") self.assertEqual(error.exception.args[0], "call_info value must be a lambda")
def test_api(self): # create an ApiFactory configured with the api credentials secrets_file = Path(__file__).parent.parent.joinpath('secrets.json') api_factory = ApiClientFactory(api_secrets_filename=secrets_file) # create an api and call a function on it scopes_api = api_factory.build(ScopesApi) scopes = scopes_api.list_scopes() # verify the call was successful self.assertGreater(len(scopes.values), 0)
def test_get_api_with_correlation_id_from_env_var(self): env_vars = {config_keys[key]["env"]: value for key, value in source_config_details.items() if value is not None} env_vars["FBN_CORRELATION_ID"] = "env-correlation-id" with patch.dict('os.environ', env_vars, clear=True): factory = ApiClientFactory() api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api) self.assertTrue("CorrelationId" in api.api_client.default_headers, msg="CorrelationId not found in headers") self.assertEquals(api.api_client.default_headers["CorrelationId"], "env-correlation-id")
def main(argv): # create an ApiFactory configured with the api credentials secrets_file = Path(__file__).parent.parent.joinpath('secrets.json') api_factory = ApiClientFactory(api_secrets_filename=secrets_file) # create an api and call a function on it scopes_api = api_factory.build(ScopesApi) scopes = scopes_api.list_scopes() for scope in scopes.values: print(scope)
def test_get_api_with_correlation_id_from_param(self): env_vars = {config_keys[key]["env"]: value for key, value in source_config_details.items() if value is not None} with patch.dict('os.environ', env_vars, clear=True): factory = ApiClientFactory( api_secrets_filename=CredentialsSource.secrets_path(), correlation_id="param-correlation-id" ) api = factory.build(InstrumentsApi) self.assertIsInstance(api, InstrumentsApi) self.validate_api(api) self.assertTrue("CorrelationId" in api.api_client.default_headers, msg="CorrelationId not found in headers") self.assertEquals(api.api_client.default_headers["CorrelationId"], "param-correlation-id")
def test_bad_pat_in_param_but_good_pat_in_env_vars(self): with patch.dict(self.os_environ_dict_str, self.get_pat_env_var(), clear=True): try: factory = ApiClientFactory(token="INVALID_TOKEN") api = factory.build(InstrumentsApi) api.get_instrument_identifier_types() except ApiException as e: self.assertEquals(401, e.status)
def test_use_apifactory_with_id_provider_response_handler(self): """ Ensures that an id_provider_response handler that is passed to the ApiClientFactory can be used during communication with the id provider (if appropriate). """ responses = [] def record_response(id_provider_response): nonlocal responses responses.append(id_provider_response.status_code) api_factory = ApiClientFactory( api_secrets_filename=CredentialsSource.secrets_path(), id_provider_response_handler=record_response) api = api_factory.build(InstrumentsApi) self.validate_api(api) self.assertGreater(len(responses), 0)