Completed
Push — master ( b114e7...97ac5e )
by
unknown
24s queued 10s
created

GmpModifyTargetTestCase.test_modify_target_with_hosts()   A

Complexity

Conditions 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nop 1
dl 0
loc 19
rs 9.85
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import unittest
20
21
from gvm.errors import RequiredArgument, InvalidArgument
22
from gvm.protocols.gmpv7 import Gmp
23
24
from .. import MockConnection
25
26
27
class GmpModifyTargetTestCase(unittest.TestCase):
28
29
    def setUp(self):
30
        self.connection = MockConnection()
31
        self.gmp = Gmp(self.connection)
32
33
    def test_modify_target(self):
34
        self.gmp.modify_target(
35
            target_id='t1',
36
        )
37
38
        self.connection.send.has_been_called_with(
39
            '<modify_target target_id="t1"/>'
40
        )
41
42
    def test_modify_target_missing_target_id(self):
43
        with self.assertRaises(RequiredArgument):
44
            self.gmp.modify_target(
45
                target_id=None,
46
            )
47
48
        with self.assertRaises(RequiredArgument):
49
            self.gmp.modify_target(
50
                target_id='',
51
            )
52
53
    def test_modify_target_with_comment(self):
54
        self.gmp.modify_target(
55
            target_id='t1',
56
            comment='foo',
57
        )
58
59
        self.connection.send.has_been_called_with(
60
            '<modify_target target_id="t1">'
61
            '<comment>foo</comment>'
62
            '</modify_target>'
63
        )
64
65
    def test_modify_target_with_hosts(self):
66
        self.gmp.modify_target(
67
            target_id='t1',
68
            hosts=['foo'],
69
        )
70
71
        self.connection.send.has_been_called_with(
72
            '<modify_target target_id="t1">'
73
            '<hosts>foo</hosts>'
74
            '</modify_target>'
75
        )
76
77
        self.gmp.modify_target(
78
            target_id='t1',
79
            hosts=['foo', 'bar'],
80
        )
81
82
        self.connection.send.has_been_called_with(
83
            '<modify_target target_id="t1">'
84
            '<hosts>foo,bar</hosts>'
85
            '</modify_target>'
86
        )
87
88
    def test_modify_target_with_name(self):
89
        self.gmp.modify_target(
90
            target_id='t1',
91
            name='foo',
92
        )
93
94
        self.connection.send.has_been_called_with(
95
            '<modify_target target_id="t1">'
96
            '<name>foo</name>'
97
            '</modify_target>'
98
        )
99
100
    def test_modify_target_with_exclude_hosts(self):
101
        self.gmp.modify_target(
102
            target_id='t1',
103
            exclude_hosts=['foo'],
104
        )
105
106
        self.connection.send.has_been_called_with(
107
            '<modify_target target_id="t1">'
108
            '<exclude_hosts>foo</exclude_hosts>'
109
            '</modify_target>'
110
        )
111
112
        self.gmp.modify_target(
113
            target_id='t1',
114
            exclude_hosts=['foo', 'bar'],
115
        )
116
117
        self.connection.send.has_been_called_with(
118
            '<modify_target target_id="t1">'
119
            '<exclude_hosts>foo,bar</exclude_hosts>'
120
            '</modify_target>'
121
        )
122
123
    def test_modify_target_with_ssh_credential(self):
124
        self.gmp.modify_target(
125
            target_id='t1',
126
            ssh_credential_id='c1',
127
        )
128
129
        self.connection.send.has_been_called_with(
130
            '<modify_target target_id="t1">'
131
            '<ssh_credential id="c1"/>'
132
            '</modify_target>'
133
        )
134
135
    def test_modify_target_with_ssh_credential_port(self):
136
        self.gmp.modify_target(
137
            target_id='t1',
138
            ssh_credential_id='c1',
139
            ssh_credential_port=123,
140
        )
141
142
        self.connection.send.has_been_called_with(
143
            '<modify_target target_id="t1">'
144
            '<ssh_credential id="c1">'
145
            '<port>123</port>'
146
            '</ssh_credential>'
147
            '</modify_target>'
148
        )
149
150
    def test_modify_target_with_smb_credential_id(self):
151
        self.gmp.modify_target(
152
            target_id='t1',
153
            smb_credential_id='c1',
154
        )
155
156
        self.connection.send.has_been_called_with(
157
            '<modify_target target_id="t1">'
158
            '<smb_credential id="c1"/>'
159
            '</modify_target>'
160
        )
161
162
    def test_modify_target_with_esxi_credential_id(self):
163
        self.gmp.modify_target(
164
            target_id='t1',
165
            esxi_credential_id='c1',
166
        )
167
168
        self.connection.send.has_been_called_with(
169
            '<modify_target target_id="t1">'
170
            '<esxi_credential id="c1"/>'
171
            '</modify_target>'
172
        )
173
174
    def test_modify_target_with_snmp_credential_id(self):
175
        self.gmp.modify_target(
176
            target_id='t1',
177
            snmp_credential_id='c1',
178
        )
179
180
        self.connection.send.has_been_called_with(
181
            '<modify_target target_id="t1">'
182
            '<snmp_credential id="c1"/>'
183
            '</modify_target>'
184
        )
185
186
    def test_modify_target_with_alive_tests(self):
187
        self.gmp.modify_target(
188
            target_id='t1',
189
            alive_tests='ICMP Ping',
190
        )
191
192
        self.connection.send.has_been_called_with(
193
            '<modify_target target_id="t1">'
194
            '<alive_tests>ICMP Ping</alive_tests>'
195
            '</modify_target>'
196
        )
197
198
    def test_modify_target_invalid_alive_tests(self):
199
        with self.assertRaises(InvalidArgument):
200
            self.gmp.modify_target(
201
                target_id='t1',
202
                alive_tests='foo',
203
            )
204
205
    def test_modify_target_with_reverse_lookup_only(self):
206
        self.gmp.modify_target(
207
            target_id='t1',
208
            reverse_lookup_only=True,
209
        )
210
211
        self.connection.send.has_been_called_with(
212
            '<modify_target target_id="t1">'
213
            '<reverse_lookup_only>1</reverse_lookup_only>'
214
            '</modify_target>'
215
        )
216
217
        self.gmp.modify_target(
218
            target_id='t1',
219
            reverse_lookup_only=False,
220
        )
221
222
        self.connection.send.has_been_called_with(
223
            '<modify_target target_id="t1">'
224
            '<reverse_lookup_only>0</reverse_lookup_only>'
225
            '</modify_target>'
226
        )
227
228
    def test_modify_target_with_reverse_lookup_unify(self):
229
        self.gmp.modify_target(
230
            target_id='t1',
231
            reverse_lookup_unify=True,
232
        )
233
234
        self.connection.send.has_been_called_with(
235
            '<modify_target target_id="t1">'
236
            '<reverse_lookup_unify>1</reverse_lookup_unify>'
237
            '</modify_target>'
238
        )
239
240
        self.gmp.modify_target(
241
            target_id='t1',
242
            reverse_lookup_unify=False,
243
        )
244
245
        self.connection.send.has_been_called_with(
246
            '<modify_target target_id="t1">'
247
            '<reverse_lookup_unify>0</reverse_lookup_unify>'
248
            '</modify_target>'
249
        )
250
251
    def test_modify_target_with_port_list_id(self):
252
        self.gmp.modify_target(
253
            target_id='t1',
254
            port_list_id='pl1',
255
        )
256
257
        self.connection.send.has_been_called_with(
258
            '<modify_target target_id="t1">'
259
            '<port_list id="pl1"/>'
260
            '</modify_target>'
261
        )
262
263
264
if __name__ == '__main__':
265
    unittest.main()
266