class CF: cfenv = attr.ib( type=CFenv, factory=CFenv, validator=attr.validators.instance_of(CFenv), ) oauth2 = attr.ib(type=OAuth2, default=None) client = attr.ib(type=ConfigClient, default=None) def __attrs_post_init__(self) -> None: if not self.oauth2: self.oauth2 = OAuth2( access_token_uri=self.cfenv.configserver_access_token_uri(), client_id=self.cfenv.configserver_client_id(), client_secret=self.cfenv.configserver_client_secret(), ) if not self.client: self.client = ConfigClient( address=self.cfenv.configserver_uri(), app_name=self.cfenv.application_name, profile=self.cfenv.space_name.lower(), oauth2=self.oauth2, ) @property def vcap_services(self): return self.cfenv.vcap_services @property def vcap_application(self): return self.cfenv.vcap_application def get_config(self, **kwargs) -> None: self.client.get_config(**kwargs) @property def config(self) -> Dict: return self.client.config def get_attribute(self, value: str) -> Any: return self.client.get_attribute(value) def get(self, key, default: Any = ""): return self.client.get(key, default) def get_keys(self) -> KeysView: return self.client.get_keys() def keys(self) -> KeysView: return self.client.keys()
class CF: cfenv = attr.ib( type=CFenv, factory=CFenv, validator=attr.validators.instance_of(CFenv), ) oauth2 = attr.ib(type=OAuth2, default=None) client = attr.ib(type=ConfigClient, default=None) def __attrs_post_init__(self) -> None: if not self.oauth2: self.oauth2 = OAuth2( access_token_uri=self.cfenv.configserver_access_token_uri(), client_id=self.cfenv.configserver_client_id(), client_secret=self.cfenv.configserver_client_secret(), ) if not self.client: self.client = ConfigClient( address=self.cfenv.configserver_uri(), app_name=self.cfenv.application_name, profile=self.cfenv.space_name.lower(), ) self.oauth2.configure() @property def vcap_services(self): return self.cfenv.vcap_services @property def vcap_application(self): return self.cfenv.vcap_application def get_config(self) -> None: header = {"Authorization": f"Bearer {self.oauth2.token}"} self.client.get_config(headers=header) @property def config(self) -> Dict: return self.client.config def get_attribute(self, value: str) -> Any: return self.client.get_attribute(value) def get_keys(self) -> KeysView: return self.client.get_keys()
class TestConfigClient(unittest.TestCase): """Unit tests to spring module.""" def setUp(self): """Mock of responses generated by Spring Cloud Config.""" self.config_example = { "health": { "config": { "enabled": False } }, "spring": { "cloud": { "consul": { "discovery": { "health-check-interval": "10s", "health-check-path": "/manage/health", "instance-id": "pecas-textos:${random.value}", "prefer-ip-address": True, "register-health-check": True }, "host": "discovery", "port": 8500 } } } } self.obj = ConfigClient( app_name='test-app', url='{address}/{branch}/{app_name}-{profile}.yaml') def test_get_config_failed(self): """Test failed to connect on ConfigClient.""" with self.assertRaises(SystemExit): self.obj.get_config() @patch('config.spring.requests.get', return_value=ResponseMock()) def test_get_config(self, RequestMock): self.obj.get_config() self.assertDictEqual(self.obj.config, self.config_example) @patch('config.spring.requests.get', return_value=ResponseMock(code=402, ok=False)) def test_get_config_response_failed(self, RequestMock): with self.assertRaises(SystemExit): self.obj.get_config() def test_config_property(self): self.assertIsInstance(self.obj.config, dict) def test_default_url_property(self): self.assertIsInstance(self.obj.url, str) self.assertEqual( self.obj.url, "http://*****:*****@patch('config.spring.requests.get', return_value=ResponseMock()) def test_decorator(self, RequestMock): @config_client(app_name='myapp') def inner_method(c=None): self.assertIsInstance(c, ConfigClient) return c response = inner_method() self.assertIsNotNone(response) def test_decorator_failed(self): @config_client() def inner_method(c=None): self.assertEqual(ConfigClient(), c) with self.assertRaises(SystemExit): inner_method() def test_fix_valid_url_extension(self): self.assertEqual( self.obj.url, "http://*****:*****@patch('config.spring.requests.get', return_value=ResponseMock()) def test_create_config_client_with_singleton(self, RequestMock): client1 = create_config_client(app_name='simpleweb000') client2 = create_config_client(app_name='simpleweb999') self.assertEqual(client1, client2)
class TestConfigClient(unittest.TestCase): """Unit tests to spring module.""" def setUp(self): """Mock of responses generated by Spring Cloud Config.""" self.config_example = { "health": { "config": { "enabled": False } }, "spring": { "cloud": { "consul": { "discovery": { "health-check-interval": "10s", "health-check-path": "/manage/health", "instance-id": "pecas-textos:${random.value}", "prefer-ip-address": True, "register-health-check": True }, "host": "discovery", "port": 8500 } } } } self.obj = ConfigClient( app_name='test-app', url='{address}/{branch}/{app_name}-{profile}.yaml') def test_get_config_failed(self): """Test failed to connect on ConfigClient.""" with self.assertRaises(SystemExit): self.obj.get_config() @patch('urllib.request.urlopen', return_value=ResponseMock()) def test_get_config(self, RequestMock): self.obj.get_config() self.assertDictEqual(self.obj.config, self.config_example) @patch('urllib.request.urlopen', return_value=ResponseMock(code=402)) def test_get_config_response_failed(self, RequestMock): with self.assertRaises(Exception): self.obj.get_config() def test_config_property(self): self.assertIsInstance(self.obj.config, dict) def test_default_url_property(self): self.assertIsInstance(self.obj.url, str) self.assertEqual( self.obj.url, "http://*****:*****@unittest.skip('skipping due singleton use.') def test_custom_url_property(self): obj = ConfigClient(app_name='test-app', branch='development', url="{address}/{branch}/{profile}-{app_name}.json") self.assertIsInstance(obj.url, str) self.assertEqual(obj.branch, 'development') self.assertEqual( obj.url, "http://*****:*****@patch('urllib.request.urlopen', return_value=ResponseMock()) def test_decorator(self, RequestMock): @config_client(app_name='myapp') def inner_method(c=None): self.assertEqual(ConfigClient(), c) return c response = inner_method() self.assertIsNotNone(response) def test_decorator_failed(self): @config_client() def inner_method(c=None): self.assertEqual(ConfigClient(), c) with self.assertRaises(SystemExit): inner_method() def test_fix_valid_url_extension(self): self.assertTrue(self.obj.url.endswith('json'))