예제 #1
0
    def test_missing_secrets_file_does_not_log_message_at_info(self):

        # using with self.assertRaises(AssertionError) doesn't give
        # anything we can assert on so use a try-except
        try:
            with self.assertLogs():
                non_existent_secrets_file = "Thisfiledefinitelydoesnotexist.json"
                ApiConfigurationLoader.load(non_existent_secrets_file)
        except AssertionError as ex:
            self.assertRegex(ex.args[0],
                             r"^no logs of level INFO or higher triggered.")
    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_specify_config_file_not_exist(self):
        """
        This test checks that an error is raised if a secrets file is specified which can not be found

        :return: None
        """
        non_existent_secrets_file = "Thisfiledefinitelydoesnotexist.json"

        with self.assertRaises(ValueError) as ex:
            ApiConfigurationLoader.load(non_existent_secrets_file)

        self.assertEqual(
            ex.exception.args[0],
            f"Provided secrets file of {non_existent_secrets_file} can not be found, please ensure you "
            f"have correctly specified the full path to the file or don't provide a secrets file to use "
            f"environment variables instead.")
예제 #4
0
    def test_missing_secrets_file_logs_message_at_debug(self):

        with self.assertLogs() as captured:
            import logging
            logger = logging.getLogger()
            logger.setLevel(logging.DEBUG)

            non_existent_secrets_file = "Thisfiledefinitelydoesnotexist.json"
            ApiConfigurationLoader.load(non_existent_secrets_file)

            # 2 loggers are configured by default (see configuration.py)
            self.assertEqual(len(captured.records), 2)

            for log in captured.records:
                self.assertEqual("DEBUG", log.levelname)
                self.assertTrue(
                    log.message.startswith(
                        f"Provided secrets file of {non_existent_secrets_file} can not be found,"
                    ))
    def test_load_from_environment_vars_only(self):
        """
        This tests loading all of the configuration details from environment variables ONLY.

        :return: None
        """
        # Set all the environment variables
        env_vars = {
            config_keys[key]["env"]: value
            for key, value in source_config_details.items()
            if value is not None
        }
        with patch.dict(self.OS_ENVIRON, env_vars, clear=True):
            config = ApiConfigurationLoader.load()

        self.assert_config_values(config, source_config_details)
예제 #6
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)