Completed
Push — master ( 0d5311...c5499f )
by Jaspar
22s queued 17s
created

tests.protocols.gmpv208.testcmds.test_create_credential   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 435
Duplicated Lines 91.26 %

Importance

Changes 0
Metric Value
eloc 220
dl 397
loc 435
rs 9.1199
c 0
b 0
f 0
wmc 41

25 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpCreateCredentialTestCase.test_create_snmp_credential_invalid_privacy_algorithm() 17 17 3
A GmpCreateCredentialTestCase.test_create_usk_credential_missing_private_key() 6 6 2
A GmpCreateCredentialTestCase.test_create_cc_credential_with_private_key() 10 10 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_with_community() 11 11 1
A GmpCreateCredentialTestCase.test_create_credential_invalid_credential_type() 9 9 4
A GmpCreateCredentialTestCase.test_create_up_credential_with_allow_insecure() 32 32 1
A GmpCreateCredentialTestCase.test_create_smime_credential() 9 9 1
A GmpCreateCredentialTestCase.test_create_usk_credential_with_key_phrase() 11 11 1
A GmpCreateCredentialTestCase.test_create_cc_credential_missing_certificate() 4 4 2
A GmpCreateCredentialTestCase.test_create_pw_credential_missing_password() 4 4 2
A GmpCreateCredentialTestCase.test_create_usk_credential() 10 10 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_auth_algorithm_sha1() 10 10 1
A GmpCreateCredentialTestCase.test_create_up_credential_missing_name() 13 13 3
A GmpCreateCredentialTestCase.test_create_snmp_credential_auth_algorithm_md5() 10 10 1
A GmpCreateCredentialTestCase.test_create_pw_credential() 8 8 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_with_privacy_password() 11 11 1
A GmpCreateCredentialTestCase.test_create_up_credential() 11 11 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_invalid_auth_algorithm() 20 20 4
A GmpCreateCredentialTestCase.test_create_usk_credential_missing_login() 6 6 2
A GmpCreateCredentialTestCase.test_create_pgp_credential() 9 9 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_with_privacy_algorithm_aes() 11 11 1
A GmpCreateCredentialTestCase.test_create_cc_credential() 9 9 1
A GmpCreateCredentialTestCase.test_create_smime_credential_missing_certificate() 4 4 2
A GmpCreateCredentialTestCase.test_create_snmp_credential_with_privacy_algorithm_des() 11 11 1
A GmpCreateCredentialTestCase.test_create_pgp_credential_missing_public_key() 4 4 2

How to fix   Duplicated Code    Complexity   

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:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like tests.protocols.gmpv208.testcmds.test_create_credential often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2020-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
from gvm.protocols.gmpv208 import (
23
    CredentialType,
24
    SnmpAuthAlgorithm,
25
    SnmpPrivacyAlgorithm,
26
)
27
28
29 View Code Duplication
class GmpCreateCredentialTestCase:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
30
    def test_create_up_credential_missing_name(self):
31
        with self.assertRaises(RequiredArgument):
32
            self.gmp.create_credential(
33
                name='',
34
                credential_type=CredentialType.USERNAME_PASSWORD,
35
                login='foo',
36
            )
37
38
        with self.assertRaises(RequiredArgument):
39
            self.gmp.create_credential(
40
                name=None,
41
                credential_type=CredentialType.USERNAME_PASSWORD,
42
                login='foo',
43
            )
44
45
    def test_create_up_credential(self):
46
        self.gmp.create_credential(
47
            name='foo',
48
            credential_type=CredentialType.USERNAME_PASSWORD,
49
            comment='bar',
50
            login='Max',
51
            password='123',
52
        )
53
54
        self.connection.send.has_been_called_with(
55
            '<create_credential>'
56
            '<name>foo</name>'
57
            '<type>up</type>'
58
            '<comment>bar</comment>'
59
            '<login>Max</login>'
60
            '<password>123</password>'
61
            '</create_credential>'
62
        )
63
64
    def test_create_up_credential_with_allow_insecure(self):
65
        self.gmp.create_credential(
66
            name='foo',
67
            credential_type=CredentialType.USERNAME_PASSWORD,
68
            comment='bar',
69
            login='Max',
70
            password='123',
71
            allow_insecure=True,
72
        )
73
74
        self.connection.send.has_been_called_with(
75
            '<create_credential>'
76
            '<name>foo</name>'
77
            '<type>up</type>'
78
            '<comment>bar</comment>'
79
            '<allow_insecure>1</allow_insecure>'
80
            '<login>Max</login>'
81
            '<password>123</password>'
82
            '</create_credential>'
83
        )
84
85
        self.gmp.create_credential(
86
            name='foo',
87
            credential_type=CredentialType.USERNAME_PASSWORD,
88
            comment='bar',
89
            login='Max',
90
            password='123',
91
            allow_insecure=False,
92
        )
93
94
        self.connection.send.has_been_called_with(
95
            '<create_credential>'
96
            '<name>foo</name>'
97
            '<type>up</type>'
98
            '<comment>bar</comment>'
99
            '<allow_insecure>0</allow_insecure>'
100
            '<login>Max</login>'
101
            '<password>123</password>'
102
            '</create_credential>'
103
        )
104
105
    def test_create_cc_credential_missing_certificate(self):
106
        with self.assertRaises(RequiredArgument):
107
            self.gmp.create_credential(
108
                name='foo', credential_type=CredentialType.CLIENT_CERTIFICATE
109
            )
110
111
    def test_create_cc_credential(self):
112
        self.gmp.create_credential(
113
            name='foo',
114
            credential_type=CredentialType.CLIENT_CERTIFICATE,
115
            certificate='abcdef',
116
        )
117
118
        self.connection.send.has_been_called_with(
119
            '<create_credential>'
120
            '<name>foo</name>'
121
            '<type>cc</type>'
122
            '<certificate>abcdef</certificate>'
123
            '</create_credential>'
124
        )
125
126
    def test_create_cc_credential_with_private_key(self):
127
        self.gmp.create_credential(
128
            name='foo',
129
            credential_type=CredentialType.CLIENT_CERTIFICATE,
130
            certificate='abcdef',
131
            private_key='123456',
132
        )
133
134
        self.connection.send.has_been_called_with(
135
            '<create_credential>'
136
            '<name>foo</name>'
137
            '<type>cc</type>'
138
            '<certificate>abcdef</certificate>'
139
            '<key>'
140
            '<private>123456</private>'
141
            '</key>'
142
            '</create_credential>'
143
        )
144
145
    def test_create_usk_credential_missing_private_key(self):
146
        with self.assertRaises(RequiredArgument):
147
            self.gmp.create_credential(
148
                name='foo',
149
                credential_type=CredentialType.USERNAME_SSH_KEY,
150
                login='foo',
151
            )
152
153
    def test_create_usk_credential_missing_login(self):
154
        with self.assertRaises(RequiredArgument):
155
            self.gmp.create_credential(
156
                name='foo',
157
                credential_type=CredentialType.USERNAME_SSH_KEY,
158
                private_key='123456',
159
            )
160
161
    def test_create_usk_credential(self):
162
        self.gmp.create_credential(
163
            name='foo',
164
            credential_type=CredentialType.USERNAME_SSH_KEY,
165
            private_key='123456',
166
            login='foo',
167
        )
168
169
        self.connection.send.has_been_called_with(
170
            '<create_credential>'
171
            '<name>foo</name>'
172
            '<type>usk</type>'
173
            '<login>foo</login>'
174
            '<key>'
175
            '<private>123456</private>'
176
            '</key>'
177
            '</create_credential>'
178
        )
179
180
    def test_create_usk_credential_with_key_phrase(self):
181
        self.gmp.create_credential(
182
            name='foo',
183
            credential_type=CredentialType.USERNAME_SSH_KEY,
184
            private_key='123456',
185
            login='foo',
186
            key_phrase='abcdef',
187
        )
188
189
        self.connection.send.has_been_called_with(
190
            '<create_credential>'
191
            '<name>foo</name>'
192
            '<type>usk</type>'
193
            '<login>foo</login>'
194
            '<key>'
195
            '<private>123456</private>'
196
            '<phrase>abcdef</phrase>'
197
            '</key>'
198
            '</create_credential>'
199
        )
200
201
    def test_create_snmp_credential_invalid_auth_algorithm(self):
202
        with self.assertRaises(InvalidArgumentType):
203
            self.gmp.create_credential(
204
                name='foo', credential_type=CredentialType.SNMP, login='foo'
205
            )
206
207
        with self.assertRaises(InvalidArgumentType):
208
            self.gmp.create_credential(
209
                name='foo',
210
                credential_type=CredentialType.SNMP,
211
                login='foo',
212
                auth_algorithm='',
213
            )
214
215
        with self.assertRaises(InvalidArgumentType):
216
            self.gmp.create_credential(
217
                name='foo',
218
                credential_type=CredentialType.SNMP,
219
                login='foo',
220
                auth_algorithm='bar',
221
            )
222
223
    def test_create_snmp_credential_auth_algorithm_md5(self):
224
        self.gmp.create_credential(
225
            name='foo',
226
            credential_type=CredentialType.SNMP,
227
            login='foo',
228
            auth_algorithm=SnmpAuthAlgorithm.MD5,
229
        )
230
231
        self.connection.send.has_been_called_with(
232
            '<create_credential>'
233
            '<name>foo</name>'
234
            '<type>snmp</type>'
235
            '<login>foo</login>'
236
            '<auth_algorithm>md5</auth_algorithm>'
237
            '</create_credential>'
238
        )
239
240
    def test_create_snmp_credential_auth_algorithm_sha1(self):
241
        self.gmp.create_credential(
242
            name='foo',
243
            credential_type=CredentialType.SNMP,
244
            login='foo',
245
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
246
        )
247
248
        self.connection.send.has_been_called_with(
249
            '<create_credential>'
250
            '<name>foo</name>'
251
            '<type>snmp</type>'
252
            '<login>foo</login>'
253
            '<auth_algorithm>sha1</auth_algorithm>'
254
            '</create_credential>'
255
        )
256
257
    def test_create_snmp_credential_with_community(self):
258
        self.gmp.create_credential(
259
            name='foo',
260
            credential_type=CredentialType.SNMP,
261
            login='foo',
262
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
263
            community='ipsum',
264
        )
265
266
        self.connection.send.has_been_called_with(
267
            '<create_credential>'
268
            '<name>foo</name>'
269
            '<type>snmp</type>'
270
            '<login>foo</login>'
271
            '<auth_algorithm>sha1</auth_algorithm>'
272
            '<community>ipsum</community>'
273
            '</create_credential>'
274
        )
275
276
    def test_create_snmp_credential_invalid_privacy_algorithm(self):
277
        with self.assertRaises(InvalidArgumentType):
278
            self.gmp.create_credential(
279
                name='foo',
280
                credential_type=CredentialType.SNMP,
281
                login='foo',
282
                auth_algorithm=SnmpAuthAlgorithm.SHA1,
283
                privacy_algorithm='',
284
            )
285
286
        with self.assertRaises(InvalidArgumentType):
287
            self.gmp.create_credential(
288
                name='foo',
289
                credential_type=CredentialType.SNMP,
290
                login='foo',
291
                auth_algorithm=SnmpAuthAlgorithm.SHA1,
292
                privacy_algorithm='foo',
293
            )
294
295
    def test_create_snmp_credential_with_privacy_algorithm_aes(self):
296
        self.gmp.create_credential(
297
            name='foo',
298
            credential_type=CredentialType.SNMP,
299
            login='foo',
300
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
301
            privacy_algorithm=SnmpPrivacyAlgorithm.AES,
302
        )
303
304
        self.connection.send.has_been_called_with(
305
            '<create_credential>'
306
            '<name>foo</name>'
307
            '<type>snmp</type>'
308
            '<login>foo</login>'
309
            '<auth_algorithm>sha1</auth_algorithm>'
310
            '<privacy>'
311
            '<algorithm>aes</algorithm>'
312
            '</privacy>'
313
            '</create_credential>'
314
        )
315
316
    def test_create_snmp_credential_with_privacy_algorithm_des(self):
317
        self.gmp.create_credential(
318
            name='foo',
319
            credential_type=CredentialType.SNMP,
320
            login='foo',
321
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
322
            privacy_algorithm=SnmpPrivacyAlgorithm.DES,
323
        )
324
325
        self.connection.send.has_been_called_with(
326
            '<create_credential>'
327
            '<name>foo</name>'
328
            '<type>snmp</type>'
329
            '<login>foo</login>'
330
            '<auth_algorithm>sha1</auth_algorithm>'
331
            '<privacy>'
332
            '<algorithm>des</algorithm>'
333
            '</privacy>'
334
            '</create_credential>'
335
        )
336
337
    def test_create_snmp_credential_with_privacy_password(self):
338
        self.gmp.create_credential(
339
            name='foo',
340
            credential_type=CredentialType.SNMP,
341
            login='foo',
342
            auth_algorithm=SnmpAuthAlgorithm.SHA1,
343
            privacy_password='123',
344
        )
345
346
        self.connection.send.has_been_called_with(
347
            '<create_credential>'
348
            '<name>foo</name>'
349
            '<type>snmp</type>'
350
            '<login>foo</login>'
351
            '<auth_algorithm>sha1</auth_algorithm>'
352
            '<privacy>'
353
            '<password>123</password>'
354
            '</privacy>'
355
            '</create_credential>'
356
        )
357
358
    def test_create_smime_credential(self):
359
        self.gmp.create_credential(
360
            name='foo',
361
            credential_type=CredentialType.SMIME_CERTIFICATE,
362
            certificate='ipsum',
363
        )
364
365
        self.connection.send.has_been_called_with(
366
            '<create_credential>'
367
            '<name>foo</name>'
368
            '<type>smime</type>'
369
            '<certificate>ipsum</certificate>'
370
            '</create_credential>'
371
        )
372
373
    def test_create_smime_credential_missing_certificate(self):
374
        with self.assertRaises(RequiredArgument):
375
            self.gmp.create_credential(
376
                name='foo', credential_type=CredentialType.SMIME_CERTIFICATE
377
            )
378
379
    def test_create_pgp_credential(self):
380
        self.gmp.create_credential(
381
            name='foo',
382
            credential_type=CredentialType.PGP_ENCRYPTION_KEY,
383
            public_key='ipsum',
384
        )
385
386
        self.connection.send.has_been_called_with(
387
            '<create_credential>'
388
            '<name>foo</name>'
389
            '<type>pgp</type>'
390
            '<key>'
391
            '<public>ipsum</public>'
392
            '</key>'
393
            '</create_credential>'
394
        )
395
396
    def test_create_pgp_credential_missing_public_key(self):
397
        with self.assertRaises(RequiredArgument):
398
            self.gmp.create_credential(
399
                name='foo', credential_type=CredentialType.PGP_ENCRYPTION_KEY
400
            )
401
402
    def test_create_credential_invalid_credential_type(self):
403
        with self.assertRaises(InvalidArgumentType):
404
            self.gmp.create_credential(name='foo', credential_type=None)
405
406
        with self.assertRaises(InvalidArgumentType):
407
            self.gmp.create_credential(name='foo', credential_type='')
408
409
        with self.assertRaises(InvalidArgumentType):
410
            self.gmp.create_credential(name='foo', credential_type='bar')
411
412
    def test_create_pw_credential_missing_password(self):
413
        with self.assertRaises(RequiredArgument):
414
            self.gmp.create_credential(
415
                name='foo', credential_type=CredentialType.PASSWORD_ONLY
416
            )
417
418
    def test_create_pw_credential(self):
419
        self.gmp.create_credential(
420
            name='foo',
421
            credential_type=CredentialType.PASSWORD_ONLY,
422
            password='foo',
423
        )
424
        self.connection.send.has_been_called_with(
425
            '<create_credential>'
426
            '<name>foo</name>'
427
            '<type>pw</type>'
428
            '<password>foo</password>'
429
            '</create_credential>'
430
        )
431
432
433
if __name__ == '__main__':
434
    unittest.main()
435