Passed
Push — master ( 8c9545...f42925 )
by Jaspar
01:33 queued 10s
created

tests.protocols.gmpv214.entities.targets.test_create_target   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 235
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 90
dl 0
loc 235
rs 10
c 0
b 0
f 0
wmc 22

17 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpCreateTargetTestMixin.test_create_target_missing_hosts() 0 3 2
A GmpCreateTargetTestMixin.test_create_target_with_reverse_lookup_only() 0 15 1
A GmpCreateTargetTestMixin.test_create_target_with_exclude_hosts() 0 7 1
A GmpCreateTargetTestMixin.test_create_target_with_comment() 0 5 1
A GmpCreateTargetTestMixin.test_create_target_with_port_list_id() 0 5 1
A GmpCreateTargetTestMixin.test_create_target_with_asset_hosts_filter() 0 5 1
A GmpCreateTargetTestMixin.test_create_target_with_alive_tests() 0 7 1
A GmpCreateTargetTestMixin.test_create_target_with_snmp_credential_id() 0 5 1
A GmpCreateTargetTestMixin.test_create_target_with_ssh_credential_port() 0 10 1
A GmpCreateTargetTestMixin.test_create_target_with_ssh_credential() 0 5 1
A GmpCreateTargetTestMixin.test_create_target_with_reverse_lookup_unify() 0 15 1
A GmpCreateTargetTestMixin.test_create_target_with_port_range() 0 5 1
A GmpCreateTargetTestMixin.test_create_target_with_allow_simultaneous_ips() 0 19 1
A GmpCreateTargetTestMixin.test_create_target_with_smb_credential_id() 0 5 1
A GmpCreateTargetTestMixin.test_create_target_invalid_alive_tests() 0 3 2
A GmpCreateTargetTestMixin.test_create_target_missing_name() 0 9 4
A GmpCreateTargetTestMixin.test_create_target_with_esxi_credential_id() 0 5 1
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2021 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
from gvm.errors import RequiredArgument, InvalidArgumentType
20
21
from gvm.protocols.gmpv214.entities.targets import AliveTest
22
23
24
class GmpCreateTargetTestMixin:
25
    def test_create_target_missing_name(self):
26
        with self.assertRaises(RequiredArgument):
27
            self.gmp.create_target(None, hosts=['foo'])
28
29
        with self.assertRaises(RequiredArgument):
30
            self.gmp.create_target(name=None, hosts=['foo'])
31
32
        with self.assertRaises(RequiredArgument):
33
            self.gmp.create_target('', hosts=['foo'])
34
35
    def test_create_target_with_asset_hosts_filter(self):
36
        self.gmp.create_target('foo', asset_hosts_filter='name=foo')
37
38
        self.connection.send.has_been_called_with(
39
            '<create_target>'
40
            '<name>foo</name>'
41
            '<asset_hosts filter="name=foo"/>'
42
            '</create_target>'
43
        )
44
45
    def test_create_target_missing_hosts(self):
46
        with self.assertRaises(RequiredArgument):
47
            self.gmp.create_target(name='foo')
48
49
    def test_create_target_with_comment(self):
50
        self.gmp.create_target('foo', hosts=['foo'], comment='bar')
51
52
        self.connection.send.has_been_called_with(
53
            '<create_target>'
54
            '<name>foo</name>'
55
            '<hosts>foo</hosts>'
56
            '<comment>bar</comment>'
57
            '</create_target>'
58
        )
59
60
    def test_create_target_with_exclude_hosts(self):
61
        self.gmp.create_target(
62
            'foo', hosts=['foo', 'bar'], exclude_hosts=['bar', 'ipsum']
63
        )
64
65
        self.connection.send.has_been_called_with(
66
            '<create_target>'
67
            '<name>foo</name>'
68
            '<hosts>foo,bar</hosts>'
69
            '<exclude_hosts>bar,ipsum</exclude_hosts>'
70
            '</create_target>'
71
        )
72
73
    def test_create_target_with_ssh_credential(self):
74
        self.gmp.create_target('foo', hosts=['foo'], ssh_credential_id='c1')
75
76
        self.connection.send.has_been_called_with(
77
            '<create_target>'
78
            '<name>foo</name>'
79
            '<hosts>foo</hosts>'
80
            '<ssh_credential id="c1"/>'
81
            '</create_target>'
82
        )
83
84
    def test_create_target_with_ssh_credential_port(self):
85
        self.gmp.create_target(
86
            'foo',
87
            hosts=['foo'],
88
            ssh_credential_id='c1',
89
            ssh_credential_port=123,
90
        )
91
92
        self.connection.send.has_been_called_with(
93
            '<create_target>'
94
            '<name>foo</name>'
95
            '<hosts>foo</hosts>'
96
            '<ssh_credential id="c1">'
97
            '<port>123</port>'
98
            '</ssh_credential>'
99
            '</create_target>'
100
        )
101
102
    def test_create_target_with_smb_credential_id(self):
103
        self.gmp.create_target('foo', hosts=['foo'], smb_credential_id='c1')
104
105
        self.connection.send.has_been_called_with(
106
            '<create_target>'
107
            '<name>foo</name>'
108
            '<hosts>foo</hosts>'
109
            '<smb_credential id="c1"/>'
110
            '</create_target>'
111
        )
112
113
    def test_create_target_with_esxi_credential_id(self):
114
        self.gmp.create_target('foo', hosts=['foo'], esxi_credential_id='c1')
115
116
        self.connection.send.has_been_called_with(
117
            '<create_target>'
118
            '<name>foo</name>'
119
            '<hosts>foo</hosts>'
120
            '<esxi_credential id="c1"/>'
121
            '</create_target>'
122
        )
123
124
    def test_create_target_with_snmp_credential_id(self):
125
        self.gmp.create_target('foo', hosts=['foo'], snmp_credential_id='c1')
126
127
        self.connection.send.has_been_called_with(
128
            '<create_target>'
129
            '<name>foo</name>'
130
            '<hosts>foo</hosts>'
131
            '<snmp_credential id="c1"/>'
132
            '</create_target>'
133
        )
134
135
    def test_create_target_with_alive_tests(self):
136
        self.gmp.create_target(
137
            'foo', hosts=['foo'], alive_test=AliveTest.ICMP_PING
138
        )
139
140
        self.connection.send.has_been_called_with(
141
            '<create_target>'
142
            '<name>foo</name>'
143
            '<hosts>foo</hosts>'
144
            '<alive_tests>ICMP Ping</alive_tests>'
145
            '</create_target>'
146
        )
147
148
    def test_create_target_invalid_alive_tests(self):
149
        with self.assertRaises(InvalidArgumentType):
150
            self.gmp.create_target('foo', hosts=['foo'], alive_test='foo')
151
152
    def test_create_target_with_allow_simultaneous_ips(self):
153
        self.gmp.create_target(
154
            'foo', hosts=['foo'], allow_simultaneous_ips=True
155
        )
156
157
        self.connection.send.has_been_called_with(
158
            '<create_target>'
159
            '<name>foo</name>'
160
            '<hosts>foo</hosts>'
161
            '<allow_simultaneous_ips>1</allow_simultaneous_ips>'
162
            '</create_target>'
163
        )
164
165
        self.gmp.create_target(
166
            'foo', hosts=['foo'], allow_simultaneous_ips=False
167
        )
168
169
        self.connection.send.has_been_called_with(
170
            '<create_target>'
171
            '<name>foo</name>'
172
            '<hosts>foo</hosts>'
173
            '<allow_simultaneous_ips>0</allow_simultaneous_ips>'
174
            '</create_target>'
175
        )
176
177
    def test_create_target_with_reverse_lookup_only(self):
178
        self.gmp.create_target('foo', hosts=['foo'], reverse_lookup_only=True)
179
180
        self.connection.send.has_been_called_with(
181
            '<create_target>'
182
            '<name>foo</name>'
183
            '<hosts>foo</hosts>'
184
            '<reverse_lookup_only>1</reverse_lookup_only>'
185
            '</create_target>'
186
        )
187
188
        self.gmp.create_target('foo', hosts=['foo'], reverse_lookup_only=False)
189
190
        self.connection.send.has_been_called_with(
191
            '<create_target>'
192
            '<name>foo</name>'
193
            '<hosts>foo</hosts>'
194
            '<reverse_lookup_only>0</reverse_lookup_only>'
195
            '</create_target>'
196
        )
197
198
    def test_create_target_with_reverse_lookup_unify(self):
199
        self.gmp.create_target('foo', hosts=['foo'], reverse_lookup_unify=True)
200
201
        self.connection.send.has_been_called_with(
202
            '<create_target>'
203
            '<name>foo</name>'
204
            '<hosts>foo</hosts>'
205
            '<reverse_lookup_unify>1</reverse_lookup_unify>'
206
            '</create_target>'
207
        )
208
209
        self.gmp.create_target('foo', hosts=['foo'], reverse_lookup_unify=False)
210
211
        self.connection.send.has_been_called_with(
212
            '<create_target>'
213
            '<name>foo</name>'
214
            '<hosts>foo</hosts>'
215
            '<reverse_lookup_unify>0</reverse_lookup_unify>'
216
            '</create_target>'
217
        )
218
219
    def test_create_target_with_port_range(self):
220
        self.gmp.create_target('foo', hosts=['foo'], port_range='bar')
221
222
        self.connection.send.has_been_called_with(
223
            '<create_target>'
224
            '<name>foo</name>'
225
            '<hosts>foo</hosts>'
226
            '<port_range>bar</port_range>'
227
            '</create_target>'
228
        )
229
230
    def test_create_target_with_port_list_id(self):
231
        self.gmp.create_target('foo', hosts=['foo'], port_list_id='pl1')
232
233
        self.connection.send.has_been_called_with(
234
            '<create_target>'
235
            '<name>foo</name>'
236
            '<hosts>foo</hosts>'
237
            '<port_list id="pl1"/>'
238
            '</create_target>'
239
        )
240