File: test_hosts.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 (138 lines) | stat: -rw-r--r-- 4,181 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# SPDX-FileCopyrightText: 2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later

import unittest

from gvm.errors import RequiredArgument
from gvm.protocols.gmp.requests.v224 import Hosts


class HostsTestCase(unittest.TestCase):
    def test_create_host(self):
        request = Hosts.create_host("host_name")
        self.assertEqual(
            bytes(request),
            b"<create_asset><asset>"
            b"<type>host</type>"
            b"<name>host_name</name>"
            b"</asset></create_asset>",
        )

    def test_create_host_with_comment(self):
        request = Hosts.create_host("host_name", comment="comment")
        self.assertEqual(
            bytes(request),
            b"<create_asset><asset>"
            b"<type>host</type>"
            b"<name>host_name</name>"
            b"<comment>comment</comment>"
            b"</asset></create_asset>",
        )

    def test_create_host_missing_name(self):
        with self.assertRaises(RequiredArgument):
            Hosts.create_host(None)

        with self.assertRaises(RequiredArgument):
            Hosts.create_host("")

    def test_delete_host(self):
        request = Hosts.delete_host("host_id")
        self.assertEqual(
            bytes(request),
            b'<delete_asset asset_id="host_id"/>',
        )

    def test_delete_host_missing_host_id(self):
        with self.assertRaises(RequiredArgument):
            Hosts.delete_host(None)

        with self.assertRaises(RequiredArgument):
            Hosts.delete_host("")

    def test_get_hosts(self):
        request = Hosts.get_hosts()
        self.assertEqual(
            bytes(request),
            b'<get_assets type="host"/>',
        )

    def test_get_hosts_with_filter_string(self):
        request = Hosts.get_hosts(filter_string="filter_string")
        self.assertEqual(
            bytes(request),
            b'<get_assets type="host" filter="filter_string"/>',
        )

    def test_get_hosts_with_filter_id(self):
        request = Hosts.get_hosts(filter_id="filter_id")
        self.assertEqual(
            bytes(request),
            b'<get_assets type="host" filt_id="filter_id"/>',
        )

    def test_get_hosts_with_details(self):
        request = Hosts.get_hosts(details=True)
        self.assertEqual(
            bytes(request),
            b'<get_assets type="host" details="1"/>',
        )

        request = Hosts.get_hosts(details=False)
        self.assertEqual(
            bytes(request),
            b'<get_assets type="host" details="0"/>',
        )

    def test_get_host(self):
        request = Hosts.get_host("host_id")
        self.assertEqual(
            bytes(request),
            b'<get_assets asset_id="host_id" type="host"/>',
        )

    def test_get_host_with_details(self):
        request = Hosts.get_host("host_id", details=True)
        self.assertEqual(
            bytes(request),
            b'<get_assets asset_id="host_id" type="host" details="1"/>',
        )

        request = Hosts.get_host("host_id", details=False)
        self.assertEqual(
            bytes(request),
            b'<get_assets asset_id="host_id" type="host" details="0"/>',
        )

    def test_get_host_missing_host_id(self):
        with self.assertRaises(RequiredArgument):
            Hosts.get_host(None)

        with self.assertRaises(RequiredArgument):
            Hosts.get_host("")

    def test_modify_host(self):
        request = Hosts.modify_host("host_id")
        self.assertEqual(
            bytes(request),
            b'<modify_asset asset_id="host_id">'
            b"<comment></comment>"
            b"</modify_asset>",
        )

    def test_modify_host_with_comment(self):
        request = Hosts.modify_host("host_id", comment="comment")
        self.assertEqual(
            bytes(request),
            b'<modify_asset asset_id="host_id">'
            b"<comment>comment</comment>"
            b"</modify_asset>",
        )

    def test_modify_host_missing_host_id(self):
        with self.assertRaises(RequiredArgument):
            Hosts.modify_host(None)

        with self.assertRaises(RequiredArgument):
            Hosts.modify_host("")