Passed
Pull Request — master (#133)
by
unknown
01:41
created

tests.protocols.gmpv8.test_create_target   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 219
rs 10
c 0
b 0
f 0
wmc 21

16 Methods

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