File: test_xml_command.py

package info (click to toggle)
python-gvm 26.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,132 kB
  • sloc: python: 44,662; makefile: 18
file content (81 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# SPDX-FileCopyrightText: 2018-2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

import unittest
from collections import OrderedDict

from gvm.xml import XmlCommand


class XmlCommandTestCase(unittest.TestCase):
    def test_should_create_command(self):
        cmd = XmlCommand("foo")

        self.assertEqual(cmd.to_string(), "<foo/>")

    def test_should_allow_to_add_element(self):
        cmd = XmlCommand("foo")
        cmd.add_element("bar")

        self.assertEqual(cmd.to_string(), "<foo><bar/></foo>")

    def test_should_allow_to_add_element_with_text(self):
        cmd = XmlCommand("foo")
        cmd.add_element("bar", "1")

        self.assertEqual(cmd.to_string(), "<foo><bar>1</bar></foo>")

    def test_should_allow_to_set_attribute(self):
        cmd = XmlCommand("foo")
        cmd.set_attribute("bar", "1")

        self.assertEqual(cmd.to_string(), '<foo bar="1"/>')

    def test_should_allow_to_set_attributes(self):
        cmd = XmlCommand("foo")
        cmd.set_attributes(OrderedDict([("bar", "1"), ("baz", "2")]))

        self.assertEqual(cmd.to_string(), '<foo bar="1" baz="2"/>')

    def test_should_allow_to_set_text(self):
        cmd = XmlCommand("foo")
        cmd.set_text("bar")
        self.assertEqual(cmd.to_string(), "<foo>bar</foo>")

    def test_should_allow_to_reset_text(self):
        cmd = XmlCommand("foo")
        sub_cmd = cmd.add_element("bar", "baz")
        sub_cmd.set_text("qux")

        self.assertEqual(cmd.to_string(), "<foo><bar>qux</bar></foo>")

    def test_should_convert_to_string(self):
        cmd = XmlCommand("foo")

        self.assertEqual(str(cmd), "<foo/>")

    def test_invalid_xml(self):
        with self.assertRaises(ValueError):
            XmlCommand("Foo & Bar")

    def test_xml_escaping(self):
        cmd = XmlCommand("foo")
        cmd.add_element("bar", "Foo & Bar")

        self.assertEqual(cmd.to_string(), "<foo><bar>Foo &amp; Bar</bar></foo>")

        cmd = XmlCommand("foo")
        cmd.set_attribute("bar", "Foo & Bar")

        self.assertEqual(cmd.to_string(), '<foo bar="Foo &amp; Bar"/>')

        cmd = XmlCommand("foo")
        cmd.set_attribute("bar", 'Foo "Bar"')

        self.assertEqual(cmd.to_string(), '<foo bar="Foo &quot;Bar&quot;"/>')


if __name__ == "__main__":
    unittest.main()