Passed
Push — master ( 2e5e35...7fce3f )
by Björn
35:10 queued 33:35
created

GmpCreateScannerTestCase.test_create_scanner_with_comment()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 1
dl 0
loc 12
rs 9.9
c 0
b 0
f 0
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
import unittest
20
21
from gvm.errors import RequiredArgument, InvalidArgumentType
22
23
from gvm.protocols.gmpv214 import ScannerType
24
25
26
class GmpCreateScannerTestCase:
27
    def test_create_scanner(self):
28
        self.gmp.create_scanner(
29
            name='foo',
30
            host='localhost',
31
            port=1234,
32
            scanner_type=ScannerType.OSP_SCANNER_TYPE,
33
            credential_id='c1',
34
        )
35
36
        self.connection.send.has_been_called_with(
37
            '<create_scanner>'
38
            '<name>foo</name>'
39
            '<host>localhost</host>'
40
            '<port>1234</port>'
41
            '<type>1</type>'
42
            '<credential id="c1"/>'
43
            '</create_scanner>'
44
        )
45
46
    def test_create_scanner_missing_name(self):
47
        with self.assertRaises(RequiredArgument):
48
            self.gmp.create_scanner(
49
                name=None,
50
                host='localhost',
51
                port=1234,
52
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
53
                credential_id='c1',
54
            )
55
56
        with self.assertRaises(RequiredArgument):
57
            self.gmp.create_scanner(
58
                name='',
59
                host='localhost',
60
                port=1234,
61
                scanner_type='1',
62
                credential_id='c1',
63
            )
64
65
    def test_create_scanner_missing_host(self):
66
        with self.assertRaises(RequiredArgument):
67
            self.gmp.create_scanner(
68
                name='foo',
69
                host=None,
70
                port=1234,
71
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
72
                credential_id='c1',
73
            )
74
75
        with self.assertRaises(RequiredArgument):
76
            self.gmp.create_scanner(
77
                name='foo',
78
                host='',
79
                port=1234,
80
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
81
                credential_id='c1',
82
            )
83
84
    def test_create_scanner_missing_port(self):
85
        with self.assertRaises(RequiredArgument):
86
            self.gmp.create_scanner(
87
                name='foo',
88
                host='localhost',
89
                port=None,
90
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
91
                credential_id='c1',
92
            )
93
94
        with self.assertRaises(RequiredArgument):
95
            self.gmp.create_scanner(
96
                name='foo',
97
                host='localhost',
98
                port='',
99
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
100
                credential_id='c1',
101
            )
102
103
    def test_create_scanner_missing_scanner_type(self):
104
        with self.assertRaises(RequiredArgument):
105
            self.gmp.create_scanner(
106
                name='foo',
107
                host='localhost',
108
                port=1234,
109
                scanner_type=None,
110
                credential_id='c1',
111
            )
112
113
        with self.assertRaises(RequiredArgument):
114
            self.gmp.create_scanner(
115
                name='foo',
116
                host='localhost',
117
                port=1234,
118
                scanner_type='',
119
                credential_id='c1',
120
            )
121
122
    def test_create_scanner_missing_credential_id(self):
123
        with self.assertRaises(RequiredArgument):
124
            self.gmp.create_scanner(
125
                name='foo',
126
                host='localhost',
127
                port=1234,
128
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
129
                credential_id=None,
130
            )
131
132
        with self.assertRaises(RequiredArgument):
133
            self.gmp.create_scanner(
134
                name='foo',
135
                host='localhost',
136
                port=1234,
137
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
138
                credential_id='',
139
            )
140
141
    def test_create_scanner_invalid_scanner_type(self):
142
        with self.assertRaises(InvalidArgumentType):
143
            self.gmp.create_scanner(
144
                name='foo',
145
                host='localhost',
146
                port=1234,
147
                scanner_type='bar',
148
                credential_id='c1',
149
            )
150
151
        with self.assertRaises(AttributeError):
152
            self.gmp.create_scanner(
153
                name='foo',
154
                host='localhost',
155
                port=1234,
156
                scanner_type=ScannerType.GMP_SCANNER_TYPE,  # pylint: disable=no-member
157
                credential_id='c1',
158
            )
159
160
        with self.assertRaises(InvalidArgumentType):
161
            self.gmp.create_scanner(
162
                name='foo',
163
                host='localhost',
164
                port=1234,
165
                scanner_type='55',
166
                credential_id='c1',
167
            )
168
169
    def test_create_scanner_with_ca_pub(self):
170
        self.gmp.create_scanner(
171
            name='foo',
172
            host='localhost',
173
            port=1234,
174
            ca_pub='foo',
175
            scanner_type=ScannerType.OSP_SCANNER_TYPE,
176
            credential_id='c1',
177
        )
178
179
        self.connection.send.has_been_called_with(
180
            '<create_scanner>'
181
            '<name>foo</name>'
182
            '<host>localhost</host>'
183
            '<port>1234</port>'
184
            '<type>1</type>'
185
            '<ca_pub>foo</ca_pub>'
186
            '<credential id="c1"/>'
187
            '</create_scanner>'
188
        )
189
190
    def test_create_scanner_with_comment(self):
191
        self.gmp.create_scanner(
192
            name='foo',
193
            host='localhost',
194
            port=1234,
195
            scanner_type=ScannerType.OSP_SCANNER_TYPE,
196
            credential_id='c1',
197
            comment='bar',
198
        )
199
200
        self.connection.send.has_been_called_with(
201
            '<create_scanner>'
202
            '<name>foo</name>'
203
            '<host>localhost</host>'
204
            '<port>1234</port>'
205
            '<type>1</type>'
206
            '<credential id="c1"/>'
207
            '<comment>bar</comment>'
208
            '</create_scanner>'
209
        )
210
211
212
if __name__ == '__main__':
213
    unittest.main()
214