Exemplo n.º 1
0
    def test_init_empty_instance(self):
        """
        Tests the class constructor with an empty instance
        """

        error_msg = 'either xml_element or state are required'

        if sys.version_info[0] >= 3 and sys.version_info[1] >= 2:
            with self.assertRaisesRegex(ValueError, error_msg):
                Status()
        else:
            with self.assertRaisesRegexp(ValueError, error_msg):
                Status()
Exemplo n.º 2
0
    def test_method_update_xml_element_empty_instance(self):
        """
        Tests the update_xml_element method with an empty instance
        """

        xccdf_status = Status(state=STATUS_VALUE_CHOICES[0])

        self.assertFalse(hasattr(xccdf_status, 'xml_element'),
                         'XML element is defined')

        xccdf_status.update_xml_element()

        self.assertTrue(hasattr(xccdf_status, 'xml_element'),
                        'XML element is not defined')
Exemplo n.º 3
0
    def test_init_wrong_state(self):
        """
        Tests the class constructor with a wrong state
        """
        xml_element = self.load_example_element('ko')

        error_msg = 'is not valid. Must be one of this'

        if sys.version_info[0] >= 3 and sys.version_info[1] >= 2:
            with self.assertRaisesRegex(InvalidValueException, error_msg):
                Status(xml_element)
        else:
            with self.assertRaisesRegexp(InvalidValueException, error_msg):
                Status(xml_element)
Exemplo n.º 4
0
    def test_method_update_xml_element_empty_instance(self):
        """
        Tests the update_xml_element method with an empty instance
        """

        xccdf_status = Status(state=STATUS_VALUE_CHOICES[0])

        self.assertFalse(hasattr(xccdf_status, 'xml_element'),
                         'XML element is defined')

        xccdf_status.update_xml_element()

        self.assertTrue(hasattr(xccdf_status, 'xml_element'),
                        'XML element is not defined')
Exemplo n.º 5
0
    def load_children(self):
        """
        Load the subelements from the xml_element in its correspondent classes.

        :returns: List of child objects.
        :rtype: list
        :raises CardinalityException: If there is more than one Version child.
        :raises CardinalityException: If there is no Group and no Rule child.
        """

        # Containers
        children = list()
        statuses = list()
        version = None
        titles = list()
        descriptions = list()
        platforms = list()
        groups = list()
        rules = list()

        # Element load
        for element in self.xml_element:
            uri, tag = Element.get_namespace_and_tag(element.tag)
            if tag == 'version':
                if version is None:
                    version = Version(element)
                else:
                    error_msg = 'version element found more than once'
                    raise CardinalityException(error_msg)
            elif tag == 'status':
                statuses.append(Status(element))
            elif tag == 'title':
                titles.append(Title(element))
            elif tag == 'description':
                descriptions.append(Description(element))
            elif tag == 'platform':
                platforms.append(Platform(element))
            elif tag == 'Group':
                groups.append(Group(element))
            elif tag == 'Rule':
                rules.append(Rule(element))

        # Element validation
        if len(groups) <= 0 and len(rules) <= 0:
            error_msg = 'a group must contain at least a group or a rule'
            raise CardinalityException(error_msg)

        # List construction
        children.extend(statuses)
        if version is not None:
            children.append(version)
        children.extend(titles)
        children.extend(descriptions)
        children.extend(platforms)
        children.extend(groups)
        children.extend(rules)

        return children
Exemplo n.º 6
0
    def create_status_object(self, object_type='ok'):
        """
        Helper method to create the Status object

        :returns: Status object
        :rtype: xccdf.models.status.Status
        """

        xml_element = self.load_example_element(object_type)

        return Status(xml_element)
Exemplo n.º 7
0
    def test_print_object_empty_instance(self):
        """
        Tests the string representation of an Status object
        from an empty instance
        """

        xccdf_status = Status(state=STATUS_VALUE_CHOICES[0])

        string_value = 'status {state}'.format(state=STATUS_VALUE_CHOICES[0])
        self.assertEqual(str(xccdf_status), string_value,
                         'String representation does not match')
Exemplo n.º 8
0
    def test_init_no_xml_element(self):
        """
        Tests the class constructor with an empty instance
        """

        xccdf_status = Status(state=STATUS_VALUE_CHOICES[0])

        self.assertEqual(xccdf_status.name, 'status',
                         'Status tag name does not match')

        self.assertEqual(xccdf_status.text, STATUS_VALUE_CHOICES[0],
                         'State does not match')
Exemplo n.º 9
0
    def test_method_to_xml_string(self):
        """
        Tests the to_xml_string method
        """

        xccdf_status = self.create_status_object('ok')

        xml_content = xccdf_status.to_xml_string()

        new_xccdf_select = Status(etree.fromstring(
            xml_content.encode('utf-8')))

        self.assertEqual(xccdf_status.text, new_xccdf_select.text,
                         'Status state does not match')
Exemplo n.º 10
0
    def load_children(self):
        """
        Load the subelements from the xml_element in its correspondent classes.

        :returns: List of child objects.
        :rtype: list
        :raises CardinalityException: If there is more than one Version child.
        :raises CardinalityException: If there is no Version child.
        :raises CardinalityException: If there is no Profile element.
        """
        # Containers
        children = list()
        statuses = list()
        version = None
        profiles = list()

        # Element load
        for element in self.xml_element:
            uri, tag = Element.get_namespace_and_tag(element.tag)
            if tag == 'version':
                if version is None:
                    version = TailoringVersion(element)
                else:
                    error_msg = 'version element found more than once'
                    raise CardinalityException(error_msg)
            elif tag == 'status':
                statuses.append(Status(element))
            elif tag == 'Profile':
                profiles.append(Profile(element))

        # Element validation
        if version is None:
            error_msg = 'version element is required'
            raise CardinalityException(error_msg)
        if len(profiles) <= 0:
            error_msg = 'Profile element is required at least once'
            raise CardinalityException(error_msg)

        # List construction
        children.extend(statuses)
        if version is not None:
            children.append(version)
        children.extend(profiles)

        return children