def test_to_statement_must_fail_for_invalid_parameter_values(self, intrinsics_resolver_mock):
        parameter_values = None

        template = Template("name", {}, {})

        with self.assertRaises(InvalidParameterValues):
            template.to_statement(parameter_values)
Beispiel #2
0
    def test_to_statement_must_fail_for_invalid_parameter_values(
            self, intrinsics_resolver_mock):
        parameter_values = None

        template = Template("name", {}, {})

        with self.assertRaises(InvalidParameterValues):
            template.to_statement(parameter_values)
    def test_to_statement_must_raise_with_missing_parameters(self, intrinsics_resolver_mock):
        parameter_values = {"key1": "value1", "key2": "value2"}
        template_parameters = {"param1": {"Description": "something"}}
        template_definition = {"Statement": {"key": "value"}}

        template = Template("name", template_parameters, template_definition)
        with self.assertRaises(InsufficientParameterValues):
            template.to_statement(parameter_values)
Beispiel #4
0
    def test_to_statement_must_raise_with_missing_parameters(
            self, intrinsics_resolver_mock):
        parameter_values = {"key1": "value1", "key2": "value2"}
        template_parameters = {"param1": {"Description": "something"}}
        template_definition = {"Statement": {"key": "value"}}

        template = Template("name", template_parameters, template_definition)
        with self.assertRaises(InsufficientParameterValues):
            template.to_statement(parameter_values)
    def test_missing_parameter_values_must_raise_on_invalid_input(self):
        template_parameters = {
            "param1": {"Description": "foo"},
            "param2": {"Description": "bar"}
        }
        parameter_values = [1,2,3]

        template = Template("name", template_parameters, {})

        with self.assertRaises(InvalidParameterValues):
            template.missing_parameter_values(parameter_values)
    def test_missing_parameter_values_must_work_when_input_has_less_keys(self):
        template_parameters = {
            "param1": {"Description": "foo"},
            "param2": {"Description": "bar"}
        }
        parameter_values = {
            "param1": "value1"
        }
        expected = ["param2"]

        template = Template("name", template_parameters, {})
        result = template.missing_parameter_values(parameter_values)

        self.assertEqual(expected, result)
    def test_to_statement_must_exclude_extra_parameter_values(self, intrinsics_resolver_mock):
        parameter_values = {"param1": "b", "key1": "value1", "key2": "value2"}
        template_parameters = {"param1": {"Description": "something"}}
        template_definition = {"Statement": {"key": "value"}}

        resolver_instance_mock = Mock()
        intrinsics_resolver_mock.return_value = resolver_instance_mock
        resolver_instance_mock.resolve_parameter_refs.return_value = "some result"

        template = Template("name", template_parameters, template_definition)
        template.to_statement(parameter_values)

        # Intrinsics resolver must be called only with the parameters declared in the template
        expected_parameter_values = {"param1": "b"}
        intrinsics_resolver_mock.assert_called_once_with(expected_parameter_values, ANY)
Beispiel #8
0
    def test_missing_parameter_values_must_raise_on_invalid_input(self):
        template_parameters = {
            "param1": {
                "Description": "foo"
            },
            "param2": {
                "Description": "bar"
            }
        }
        parameter_values = [1, 2, 3]

        template = Template("name", template_parameters, {})

        with self.assertRaises(InvalidParameterValues):
            template.missing_parameter_values(parameter_values)
    def test_to_statement_must_work_with_valid_inputs(self, intrinsics_resolver_mock):
        parameter_values = {"param1": "b"}
        template_parameters = {"param1": {"Description": "something"}}
        template_definition = {"Statement": {"key": "value"}}
        expected = "some result"

        resolver_instance_mock = Mock()
        intrinsics_resolver_mock.return_value = resolver_instance_mock
        resolver_instance_mock.resolve_parameter_refs.return_value = expected

        template = Template("name", template_parameters, template_definition)
        result = template.to_statement(parameter_values)

        self.assertEqual(expected, result)
        intrinsics_resolver_mock.assert_called_once_with(parameter_values, {"Ref": ANY})
        resolver_instance_mock.resolve_parameter_refs.assert_called_once_with({"Statement": {"key": "value"}})
Beispiel #10
0
    def test_missing_parameter_values_must_work_when_input_has_all_keys(self):
        template_parameters = {
            "param1": {
                "Description": "foo"
            },
            "param2": {
                "Description": "bar"
            }
        }
        parameter_values = {"param1": "value1", "param2": "value3"}
        expected = []

        template = Template("name", template_parameters, {})
        result = template.missing_parameter_values(parameter_values)

        self.assertEquals(expected, result)
    def test_missing_parameter_values_must_work_when_input_has_more_keys(self):
        template_parameters = {
            "param1": {"Description": "foo"},
            "param2": {"Description": "bar"}
        }
        parameter_values = {
            "param1": "value1",
            "param2": "value2",
            "newparam": "new value"
        }
        expected = [] # We do a set-difference. So new keys won't make it here

        template = Template("name", template_parameters, {})
        result = template.missing_parameter_values(parameter_values)

        self.assertEqual(expected, result)
Beispiel #12
0
    def test_to_statement_must_exclude_extra_parameter_values(
            self, intrinsics_resolver_mock):
        parameter_values = {"param1": "b", "key1": "value1", "key2": "value2"}
        template_parameters = {"param1": {"Description": "something"}}
        template_definition = {"Statement": {"key": "value"}}

        resolver_instance_mock = Mock()
        intrinsics_resolver_mock.return_value = resolver_instance_mock
        resolver_instance_mock.resolve_parameter_refs.return_value = "some result"

        template = Template("name", template_parameters, template_definition)
        template.to_statement(parameter_values)

        # Intrinsics resolver must be called only with the parameters declared in the template
        expected_parameter_values = {"param1": "b"}
        intrinsics_resolver_mock.assert_called_once_with(
            expected_parameter_values, ANY)
Beispiel #13
0
    def test_from_dict_must_work_when_parameters_is_absent(
            self, check_parameters_exist_mock):
        template_name = "template_name"
        template_definition = {"key": "value"}

        template_dict = {"Definition": template_definition}

        template = Template.from_dict(template_name, template_dict)

        self.assertEqual(template.parameters, {})  # Defaults to {}
        self.assertEqual(template.definition, template_definition)
Beispiel #14
0
    def test_from_dict_must_work_when_template_definition_is_absent(
            self, check_parameters_exist_mock):
        template_name = "template_name"
        parameters = {"key": "value"}

        template_dict = {"Parameters": parameters}

        template = Template.from_dict(template_name, template_dict)

        self.assertEquals(template.parameters, parameters)
        self.assertEquals(template.definition, {})  # Defaults to {}
Beispiel #15
0
    def test_missing_parameter_values_must_work_when_input_has_more_keys(self):
        template_parameters = {
            "param1": {
                "Description": "foo"
            },
            "param2": {
                "Description": "bar"
            }
        }
        parameter_values = {
            "param1": "value1",
            "param2": "value2",
            "newparam": "new value"
        }
        expected = []  # We do a set-difference. So new keys won't make it here

        template = Template("name", template_parameters, {})
        result = template.missing_parameter_values(parameter_values)

        self.assertEquals(expected, result)
    def test_from_dict_must_work_when_parameters_is_absent(self, check_parameters_exist_mock):
        template_name = "template_name"
        template_definition = {"key": "value"}

        template_dict = {
            "Definition": template_definition
        }

        template = Template.from_dict(template_name, template_dict)

        self.assertEquals(template.parameters, {}) # Defaults to {}
        self.assertEquals(template.definition, template_definition)
Beispiel #17
0
    def test_to_statement_must_work_with_valid_inputs(
            self, intrinsics_resolver_mock):
        parameter_values = {"param1": "b"}
        template_parameters = {"param1": {"Description": "something"}}
        template_definition = {"Statement": {"key": "value"}}
        expected = "some result"

        resolver_instance_mock = Mock()
        intrinsics_resolver_mock.return_value = resolver_instance_mock
        resolver_instance_mock.resolve_parameter_refs.return_value = expected

        template = Template("name", template_parameters, template_definition)
        result = template.to_statement(parameter_values)

        self.assertEquals(expected, result)
        intrinsics_resolver_mock.assert_called_once_with(
            parameter_values, {"Ref": ANY})
        resolver_instance_mock.resolve_parameter_refs.assert_called_once_with(
            {"Statement": {
                "key": "value"
            }})
    def test_from_dict_must_work_when_template_definition_is_absent(self, check_parameters_exist_mock):
        template_name = "template_name"
        parameters = {"key": "value"}

        template_dict = {
            "Parameters": parameters
        }

        template = Template.from_dict(template_name, template_dict)

        self.assertEqual(template.parameters, parameters)
        self.assertEqual(template.definition, {}) # Defaults to {}
    def __init__(self, policy_templates_dict, schema=None):
        """
        Initialize the class

        :param policy_templates_dict: Dictionary containing the policy templates definition
        :param dict schema: Dictionary containing the JSON Schema of policy templates
        :raises ValueError: If policy templates does not match up with the schema
        """
        PolicyTemplatesProcessor._is_valid_templates_dict(policy_templates_dict, schema)

        self.policy_templates = {}
        for template_name, template_value_dict in policy_templates_dict["Templates"].items():
            self.policy_templates[template_name] = Template.from_dict(template_name, template_value_dict)
Beispiel #20
0
    def test_init_must_check_for_existence_of_all_parameters(
            self, check_parameters_exist_mock):

        template_name = "template_name"
        parameters = {}
        template_definition = {"key": "value"}

        template = Template(template_name, parameters, template_definition)

        self.assertEquals(template.name, template_name)
        self.assertEquals(template.parameters, parameters)
        self.assertEquals(template.definition, template_definition)
        check_parameters_exist_mock.assert_called_once_with(
            parameters, template_definition)
Beispiel #21
0
    def __init__(self, policy_templates_dict, schema=None):
        """
        Initialize the class

        :param policy_templates_dict: Dictionary containing the policy templates definition
        :param dict schema: Dictionary containing the JSON Schema of policy templates
        :raises ValueError: If policy templates does not match up with the schema
        """
        PolicyTemplatesProcessor._is_valid_templates_dict(
            policy_templates_dict, schema)

        self.policy_templates = {}
        for template_name, template_value_dict in policy_templates_dict[
                "Templates"].iteritems():
            self.policy_templates[template_name] = Template.from_dict(
                template_name, template_value_dict)
Beispiel #22
0
    def test_from_dict_must_return_object(self, check_parameters_exist_mock):
        template_name = "template_name"
        parameters = {"A": "B"}
        template_definition = {"key": "value"}

        template_dict = {
            "Parameters": parameters,
            "Definition": template_definition
        }

        template = Template.from_dict(template_name, template_dict)

        self.assertTrue(isinstance(template, Template))
        self.assertEquals(template.name, template_name)
        self.assertEquals(template.parameters, parameters)
        self.assertEquals(template.definition, template_definition)
    def test_from_dict_must_return_object(self, check_parameters_exist_mock):
        template_name = "template_name"
        parameters = {"A": "B"}
        template_definition = {"key": "value"}

        template_dict = {
            "Parameters": parameters,
            "Definition": template_definition
        }

        template = Template.from_dict(template_name, template_dict)

        self.assertTrue(isinstance(template, Template))
        self.assertEqual(template.name, template_name)
        self.assertEqual(template.parameters, parameters)
        self.assertEqual(template.definition, template_definition)
    def test_is_valid_parameter_values_must_work(self):

        parameter_values = {"a": "b"}
        self.assertTrue(Template._is_valid_parameter_values(parameter_values))
    def test_is_valid_parameter_values_must_fail_for_none_value(self):

        parameter_values = None
        self.assertFalse(Template._is_valid_parameter_values(parameter_values))
Beispiel #26
0
    def test_is_valid_parameter_values_must_fail_for_non_dict(self):

        parameter_values = [1, 2, 3]
        self.assertFalse(Template._is_valid_parameter_values(parameter_values))
Beispiel #27
0
    def test_is_valid_parameter_values_must_fail_for_none_value(self):

        parameter_values = None
        self.assertFalse(Template._is_valid_parameter_values(parameter_values))
Beispiel #28
0
    def test_is_valid_parameter_values_must_work(self):

        parameter_values = {"a": "b"}
        self.assertTrue(Template._is_valid_parameter_values(parameter_values))
    def test_is_valid_parameter_values_must_fail_for_non_dict(self):

        parameter_values = [1,2,3]
        self.assertFalse(Template._is_valid_parameter_values(parameter_values))