def test_load_from_env_config_file_path(self): """Should delegate to load_from_yaml_file method.""" environ = { "GOOGLE_ADS_CONFIGURATION_FILE_PATH": self.configuration_file_path } with mock.patch("os.environ", environ): with mock.patch.object( config, "load_from_yaml_file", # Return basic config to pass validation. return_value={"developer_token": "1234"}, ) as spy: config.load_from_env() spy.assert_called_once()
def test_load_from_env(self, config_spy): environ = { "GOOGLE_ADS_DEVELOPER_TOKEN": self.developer_token, "GOOGLE_ADS_CLIENT_ID": self.client_id, "GOOGLE_ADS_CLIENT_SECRET": self.client_secret, "GOOGLE_ADS_REFRESH_TOKEN": self.refresh_token, "GOOGLE_ADS_LOGGING": '{"test": true}', "GOOGLE_ADS_ENDPOINT": self.endpoint, "GOOGLE_ADS_LOGIN_CUSTOMER_ID": self.login_customer_id, "GOOGLE_ADS_LINKED_CUSTOMER_ID": self.linked_customer_id, "GOOGLE_ADS_LINKED_CUSTOMER_ID": self.linked_customer_id, "GOOGLE_ADS_JSON_KEY_FILE_PATH": self.json_key_file_path, "GOOGLE_ADS_IMPERSONATED_EMAIL": self.impersonated_email, } with mock.patch("os.environ", environ): result = config.load_from_env() self.assertEqual( result, { "developer_token": self.developer_token, "client_id": self.client_id, "client_secret": self.client_secret, "refresh_token": self.refresh_token, "logging": {"test": True}, "endpoint": self.endpoint, "login_customer_id": self.login_customer_id, "linked_customer_id": self.linked_customer_id, "json_key_file_path": self.json_key_file_path, "impersonated_email": self.impersonated_email, }, ) config_spy.assert_called_once()
def test_load_from_env_config_file_path_added_vars(self): """Should use config from yaml when config file path env var exists. If a configuration file path is defined via an environment variable then the yaml file at that location will be loaded and any other environment variable configuration will be ignored. """ env_dev_token = "123456" environ = { **self.default_env_var_config, **{ "GOOGLE_ADS_CONFIGURATION_FILE_PATH": self.configuration_file_path, "GOOGLE_ADS_DEVELOPER_TOKEN": env_dev_token, }, } with mock.patch("os.environ", environ), mock.patch.object( # Mock load_from_yaml_file return value so it returns # a default dict config that passes validation config, "load_from_yaml_file", return_value=self.default_dict_config, ) as spy: # Assert that the config values were retrieved from the yaml # file and not from environment variables. result = config.load_from_env() self.assertEqual(result["developer_token"], self.developer_token) self.assertEqual(result["use_proto_plus"], self.use_proto_plus)
def test_load_from_env_secondary_delegated_email(self): """IMPERSONATED_EMAIL is used instead of secondary var name.""" environ = { "GOOGLE_ADS_DEVELOPER_TOKEN": self.developer_token, "GOOGLE_ADS_DELEGATED_ACCOUNT": self.delegated_account, } with mock.patch("os.environ", environ): result = config.load_from_env() self.assertEqual( result["impersonated_email"], self.delegated_account )
def test_load_from_env_secondary_file_path(self): """JSON_KEY_FILE_PATH is used instead of secondary var name.""" environ = { "GOOGLE_ADS_DEVELOPER_TOKEN": self.developer_token, "GOOGLE_ADS_PATH_TO_PRIVATE_KEY_FILE": self.path_to_private_key_file, } with mock.patch("os.environ", environ): result = config.load_from_env() self.assertEqual( result["json_key_file_path"], self.path_to_private_key_file )
def test_load_from_env_linked_cid(self): """Should load linked CID from environment when specified""" environ = { "GOOGLE_ADS_DEVELOPER_TOKEN": self.developer_token, "GOOGLE_ADS_LINKED_CUSTOMER_ID": self.linked_customer_id, } with mock.patch("os.environ", environ): results = config.load_from_env() self.assertEqual(results["developer_token"], self.developer_token) self.assertEqual( results["linked_customer_id"], self.linked_customer_id )
def test_load_from_env_linked_cid(self): """Should load linked CID from environment when specified""" environ = { **self.default_env_var_config, **{ "GOOGLE_ADS_LINKED_CUSTOMER_ID": self.linked_customer_id, }, } with mock.patch("os.environ", environ): results = config.load_from_env() self.assertEqual(results["linked_customer_id"], self.linked_customer_id)
def test_load_from_env_redundant_delegated_email(self): """IMPERSONATED_EMAIL takes precedent if both exist.""" environ = { "GOOGLE_ADS_DEVELOPER_TOKEN": self.developer_token, # The two below variables represent the same key, and this test # checks that IMPERSONATED_EMAIL takes precedent and overwrites # the delegate_account key in the returned dict. "GOOGLE_ADS_DELEGATED_ACCOUNT": self.delegated_account, "GOOGLE_ADS_IMPERSONATED_EMAIL": self.impersonated_email, } with mock.patch("os.environ", environ): result = config.load_from_env() self.assertEqual( result["impersonated_email"], self.impersonated_email )
def test_load_from_env_redundant_file_path(self): """JSON_KEY_FILE_PATH takes precedent if both exist.""" environ = { "GOOGLE_ADS_DEVELOPER_TOKEN": self.developer_token, # The two below variables represent the same key, and this test # checks that JSON_KEY_FILE_PATH takes precedent and overwrites # the path_to_private_key_file_path key in the returned dict. "GOOGLE_ADS_PATH_TO_PRIVATE_KEY_FILE": self.path_to_private_key_file, "GOOGLE_ADS_JSON_KEY_FILE_PATH": self.json_key_file_path, } with mock.patch("os.environ", environ): result = config.load_from_env() self.assertEqual( result["json_key_file_path"], self.json_key_file_path )
def test_load_from_env_config_file_path_added_vars(self): """Should use config from yaml when config file path env var exists.""" env_dev_token = "abcdefg" yaml_dev_token = "123456" environ = { "GOOGLE_ADS_CONFIGURATION_FILE_PATH": self.configuration_file_path, "GOOGLE_ADS_DEVELOPER_TOKEN": env_dev_token, } with mock.patch("os.environ", environ), mock.patch.object( config, "load_from_yaml_file", # Return basic config to pass validation. return_value={"developer_token": yaml_dev_token}, ) as spy: result = config.load_from_env() self.assertEqual(result["developer_token"], yaml_dev_token)