Completed
Push — master ( b519bb...486e1a )
by Jaspar
25s queued 15s
created

tests.protocols.gmpv208.entities.scanners.test_create_scanner   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 191
Duplicated Lines 44.5 %

Importance

Changes 0
Metric Value
eloc 123
dl 85
loc 191
rs 10
c 0
b 0
f 0
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpCreateScannerTestMixin.test_create_scanner() 0 11 1
A GmpCreateScannerTestMixin.test_create_scanner_missing_name() 17 17 3
A GmpCreateScannerTestMixin.test_create_scanner_invalid_scanner_type() 0 17 3
A GmpCreateScannerTestMixin.test_create_scanner_with_comment() 0 12 1
A GmpCreateScannerTestMixin.test_create_scanner_missing_host() 17 17 3
A GmpCreateScannerTestMixin.test_create_scanner_missing_scanner_type() 17 17 3
A GmpCreateScannerTestMixin.test_create_scanner_missing_port() 17 17 3
A GmpCreateScannerTestMixin.test_create_scanner_missing_credential_id() 17 17 3
A GmpCreateScannerTestMixin.test_create_scanner_with_ca_pub() 0 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018-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.gmpv208 import ScannerType
22
23
24
class GmpCreateScannerTestMixin:
25
    def test_create_scanner(self):
26
        self.gmp.create_scanner(
27
            name='foo',
28
            host='localhost',
29
            port=1234,
30
            scanner_type=ScannerType.OSP_SCANNER_TYPE,
31
            credential_id='c1',
32
        )
33
34
        self.connection.send.has_been_called_with(
35
            '<create_scanner>'
36
            '<name>foo</name>'
37
            '<host>localhost</host>'
38
            '<port>1234</port>'
39
            '<type>1</type>'
40
            '<credential id="c1"/>'
41
            '</create_scanner>'
42
        )
43
44 View Code Duplication
    def test_create_scanner_missing_name(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
45
        with self.assertRaises(RequiredArgument):
46
            self.gmp.create_scanner(
47
                name=None,
48
                host='localhost',
49
                port=1234,
50
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
51
                credential_id='c1',
52
            )
53
54
        with self.assertRaises(RequiredArgument):
55
            self.gmp.create_scanner(
56
                name='',
57
                host='localhost',
58
                port=1234,
59
                scanner_type='1',
60
                credential_id='c1',
61
            )
62
63 View Code Duplication
    def test_create_scanner_missing_host(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
64
        with self.assertRaises(RequiredArgument):
65
            self.gmp.create_scanner(
66
                name='foo',
67
                host=None,
68
                port=1234,
69
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
70
                credential_id='c1',
71
            )
72
73
        with self.assertRaises(RequiredArgument):
74
            self.gmp.create_scanner(
75
                name='foo',
76
                host='',
77
                port=1234,
78
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
79
                credential_id='c1',
80
            )
81
82 View Code Duplication
    def test_create_scanner_missing_port(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
83
        with self.assertRaises(RequiredArgument):
84
            self.gmp.create_scanner(
85
                name='foo',
86
                host='localhost',
87
                port=None,
88
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
89
                credential_id='c1',
90
            )
91
92
        with self.assertRaises(RequiredArgument):
93
            self.gmp.create_scanner(
94
                name='foo',
95
                host='localhost',
96
                port='',
97
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
98
                credential_id='c1',
99
            )
100
101 View Code Duplication
    def test_create_scanner_missing_scanner_type(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
102
        with self.assertRaises(RequiredArgument):
103
            self.gmp.create_scanner(
104
                name='foo',
105
                host='localhost',
106
                port=1234,
107
                scanner_type=None,
108
                credential_id='c1',
109
            )
110
111
        with self.assertRaises(RequiredArgument):
112
            self.gmp.create_scanner(
113
                name='foo',
114
                host='localhost',
115
                port=1234,
116
                scanner_type='',
117
                credential_id='c1',
118
            )
119
120 View Code Duplication
    def test_create_scanner_missing_credential_id(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
121
        with self.assertRaises(RequiredArgument):
122
            self.gmp.create_scanner(
123
                name='foo',
124
                host='localhost',
125
                port=1234,
126
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
127
                credential_id=None,
128
            )
129
130
        with self.assertRaises(RequiredArgument):
131
            self.gmp.create_scanner(
132
                name='foo',
133
                host='localhost',
134
                port=1234,
135
                scanner_type=ScannerType.OSP_SCANNER_TYPE,
136
                credential_id='',
137
            )
138
139
    def test_create_scanner_invalid_scanner_type(self):
140
        with self.assertRaises(InvalidArgumentType):
141
            self.gmp.create_scanner(
142
                name='foo',
143
                host='localhost',
144
                port=1234,
145
                scanner_type='bar',
146
                credential_id='c1',
147
            )
148
149
        with self.assertRaises(InvalidArgumentType):
150
            self.gmp.create_scanner(
151
                name='foo',
152
                host='localhost',
153
                port=1234,
154
                scanner_type='55',
155
                credential_id='c1',
156
            )
157
158
    def test_create_scanner_with_ca_pub(self):
159
        self.gmp.create_scanner(
160
            name='foo',
161
            host='localhost',
162
            port=1234,
163
            ca_pub='foo',
164
            scanner_type=ScannerType.OSP_SCANNER_TYPE,
165
            credential_id='c1',
166
        )
167
168
        self.connection.send.has_been_called_with(
169
            '<create_scanner>'
170
            '<name>foo</name>'
171
            '<host>localhost</host>'
172
            '<port>1234</port>'
173
            '<type>1</type>'
174
            '<ca_pub>foo</ca_pub>'
175
            '<credential id="c1"/>'
176
            '</create_scanner>'
177
        )
178
179
    def test_create_scanner_with_comment(self):
180
        self.gmp.create_scanner(
181
            name='foo',
182
            host='localhost',
183
            port=1234,
184
            scanner_type=ScannerType.OSP_SCANNER_TYPE,
185
            credential_id='c1',
186
            comment='bar',
187
        )
188
189
        self.connection.send.has_been_called_with(
190
            '<create_scanner>'
191
            '<name>foo</name>'
192
            '<host>localhost</host>'
193
            '<port>1234</port>'
194
            '<type>1</type>'
195
            '<credential id="c1"/>'
196
            '<comment>bar</comment>'
197
            '</create_scanner>'
198
        )
199