def test_get_token_with_proxy_from_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
            },
            "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")

        TempFileManager.create_temp_file(secrets)

        refreshed_token = RefreshingToken(api_configuration=self.config,
                                          expiry_offset=3599)

        self.assertIsNotNone(refreshed_token)
    def test_missing_env_vars_uses_config_file(self):
        """
        This tests loading the configuration details in multiple different ways

        :return: None
        """

        secrets = {
            "api": {
                config_keys["token_url"]["config"]:
                source_config_details["token_url"]
            }
        }

        env_vars = {
            config_keys[key]["env"]: value
            for key, value in source_config_details.items()
            if value is not None and "token_url" not in key
        }

        # Set the environment variables as desired
        with patch.dict(self.OS_ENVIRON, env_vars, clear=True):
            # Create a temporary secrets file as desired
            secrets_file = TempFileManager.create_temp_file(secrets)
            # Load the config
            config = ApiConfigurationLoader.load(secrets_file.name)
            # Close and thus delete the temporary file
            TempFileManager.delete_temp_file(secrets_file)
            # Ensure that the config is populated as expected
            self.assert_config_values(config, source_config_details)
    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_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_build_client_with_token_provided(self, _, config_to_remove,
                                              env_vars, api_configuration,
                                              token):
        """
        This test builds an ApiClient from a provided token.
        """

        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.pop(config) for config in config_to_remove]

        # Use a temporary file and no environment variables to generate the API Client
        with patch.dict('os.environ', env_vars, clear=True):
            secrets_file = TempFileManager.create_temp_file(secrets)
            client = ApiClientBuilder.build(
                api_secrets_filename=secrets_file.name,
                api_configuration=api_configuration,
                token=token)

            TempFileManager.delete_temp_file(secrets_file)

        self.assertEqual(client.configuration.host,
                         source_config_details["api_url"])
        self.assertEqual(client.configuration.access_token, token)
        self.assertIsInstance(client, ApiClient)
    def test_build_client_no_token_provided_file_only(self):
        """
        This test builds an ApiClient from a provided secrets.json file. The call to generate the token is mocked here.
        """

        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
            }
        }

        env_vars = {}

        api_configuration = None

        # Use a temporary file and no environment variables to generate the API Client
        with patch.dict('os.environ', env_vars, clear=True), patch("requests.post") as mock_requests:
            mock_requests.return_value.status_code = 200
            mock_requests.return_value.json.return_value = {
                "access_token": "mock_access_token",
                "refresh_token": "mock_refresh_token",
                "expires_in": 60
            }

            secrets_file = TempFileManager.create_temp_file(secrets)
            client = ApiClientBuilder.build(
                api_secrets_filename=secrets_file.name,
                api_configuration=api_configuration)

            TempFileManager.delete_temp_file(secrets_file)
            self.assertEqual(client.configuration.access_token, "mock_access_token")

        self.assertEqual(client.configuration.host, source_config_details["api_url"])
        self.assertIsInstance(client, ApiClient)
    def test_get_token_with_proxy(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")

        TempFileManager.create_temp_file(secrets)

        proxy_config = ProxyConfig(address=secrets["proxy"]["address"],
                                   username=secrets["proxy"]["username"],
                                   password=secrets["proxy"]["password"])

        proxies = proxy_config.format_proxy_schema()

        with patch.dict('os.environ', {"HTTPS_PROXY": proxies["https"]},
                        clear=True):
            proxy_url = os.getenv("HTTPS_PROXY", None)

        if proxy_url is not None:

            refreshed_token = RefreshingToken(api_configuration=self.config,
                                              expiry_offset=3599)

            self.assertIsNotNone(refreshed_token)
    def test_get_token_with_proxy(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)

        original_token, refresh_token = tu.get_okta_tokens(secrets_file.name)

        proxy_config = ProxyConfig(
            address=secrets["proxy"]["address"],
            username=secrets["proxy"]["username"],
            password=secrets["proxy"]["password"]
        )

        proxies = proxy_config.format_proxy_schema()

        with patch.dict('os.environ', {"HTTPS_PROXY": proxies["https"]}, clear=True):
            proxy_url = os.getenv("HTTPS_PROXY", None)

        if proxy_url is not None:

            refreshed_token = RefreshingToken(token_url=self.config.token_url,
                                              client_id=self.config.client_id,
                                              client_secret=self.config.client_secret,
                                              initial_access_token=original_token,
                                              initial_token_expiry=1,  # 1s expiry
                                              refresh_token=refresh_token,
                                              expiry_offset=3599,     # set to 1s expiry
                                              proxies={})

            self.assertIsNotNone(refreshed_token)
Esempio n. 9
0
    def test_config_file_preferred_over_env_vars(self):
        """
        This tests loading the configuration details in multiple different ways

        :return: None
        """

        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
            }
        }

        env_vars = {
            config_keys[key]["env"]: "DUMMYVALUE"
            for key, value in source_config_details.items()
            if value is not None
        }

        # Set the environment variables as desired
        with patch.dict('os.environ', env_vars, clear=True):
            # Create a temporary secrets file as desired
            secrets_file = TempFileManager.create_temp_file(secrets)
            # Load the config
            config = ApiConfigurationLoader.load(secrets_file.name)
            # Close and thus delete the temporary file
            TempFileManager.delete_temp_file(secrets_file)
            # Ensure that the config is populated as expected
            self.assert_config_values(config, source_config_details)