Ejemplo n.º 1
0
    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):
        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_PATH_TO_PRIVATE_KEY_FILE':
            self.path_to_private_key_file,
            'GOOGLE_ADS_DELEGATED_ACCOUNT': self.delegated_account
        }

        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,
                    'path_to_private_key_file': self.path_to_private_key_file,
                    'delegated_account': self.delegated_account
                })
Ejemplo n.º 3
0
    def test_load_from_env(self):
        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_PATH_TO_PRIVATE_KEY_FILE":
            self.path_to_private_key_file,
            "GOOGLE_ADS_DELEGATED_ACCOUNT": self.delegated_account,
        }

        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,
                    "path_to_private_key_file": self.path_to_private_key_file,
                    "delegated_account": self.delegated_account,
                },
            )
Ejemplo n.º 4
0
    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_JSON_KEY_FILE_PATH": self.path_to_private_key_file,
            "GOOGLE_ADS_IMPERSONATED_EMAIL": self.delegated_account,
        }

        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.path_to_private_key_file,
                    "impersonated_email": self.delegated_account,
                },
            )
            config_spy.assert_called_once()
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
def load_credentials():
    """Loads credentials with the following ordering: from env, Google's yaml file, a custom .json file in .config/gaql"""
    from google.ads.google_ads import config
    try:
        return config.load_from_env()
    except:
        try:
            return config.load_from_yaml_file()
        except:
            if not CREDENTIAL_FILE.exists():
                print(f"Couldn't load credentials from the environment, or from ~/google-ads.yaml. Trying {CREDENTIAL_FILE}")
            return config.load_from_dict(read_credentials_from_file())
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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)