Completed
Push — master ( 42f470...4e3fbe )
by
unknown
25s queued 10s
created

tests.protocols.gmpv8.test_create_credential   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 295
Duplicated Lines 8.14 %

Importance

Changes 0
Metric Value
eloc 134
dl 24
loc 295
rs 9.68
c 0
b 0
f 0
wmc 34

22 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpCreateCredentialTestCase.setUp() 0 3 1
A GmpCreateCredentialTestCase.test_create_pgp_credential_missing_public_key() 0 4 2
A GmpCreateCredentialTestCase.test_create_smime_credential_missing_certificate() 0 4 2
A GmpCreateCredentialTestCase.test_create_snmp_credential_auth_algorithm_sha1() 0 7 1
A GmpCreateCredentialTestCase.test_create_usk_credential() 0 7 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_with_privacy_algorithm_aes() 0 7 1
A GmpCreateCredentialTestCase.test_create_smime_credential() 0 6 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_with_privacy_algorithm_des() 0 7 1
A GmpCreateCredentialTestCase.test_create_cc_credential() 0 6 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_with_privacy_password() 0 7 1
A GmpCreateCredentialTestCase.test_create_cc_credential_with_private_key() 0 7 1
A GmpCreateCredentialTestCase.test_create_pgp_credential() 0 6 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_invalid_auth_algorithm() 14 14 4
A GmpCreateCredentialTestCase.test_create_usk_credential_missing_private_key() 0 4 2
A GmpCreateCredentialTestCase.test_create_usk_credential_missing_login() 0 4 2
A GmpCreateCredentialTestCase.test_create_up_credential() 0 7 1
A GmpCreateCredentialTestCase.test_create_usk_credential_with_key_phrase() 0 7 1
A GmpCreateCredentialTestCase.test_create_snmp_credential_with_community() 0 7 1
A GmpCreateCredentialTestCase.test_create_up_credential_missing_name() 0 8 3
A GmpCreateCredentialTestCase.test_create_snmp_credential_invalid_privacy_algorithm() 10 10 3
A GmpCreateCredentialTestCase.test_create_snmp_credential_auth_algorithm_md5() 0 7 1
A GmpCreateCredentialTestCase.test_create_cc_credential_missing_certificate() 0 4 2

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
2
# -*- coding: utf-8 -*-
3
# Copyright (C) 2018 Greenbone Networks GmbH
4
#
5
# SPDX-License-Identifier: GPL-3.0-or-later
6
#
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
import unittest
21
22
from gvm.errors import RequiredArgument, InvalidArgument
23
from gvm.protocols.gmpv8 import Gmp
24
25
from .. import MockConnection
26
27
28
class GmpCreateCredentialTestCase(unittest.TestCase):
29
30
    def setUp(self):
31
        self.connection = MockConnection()
32
        self.gmp = Gmp(self.connection)
33
34
    def test_create_up_credential_missing_name(self):
35
        with self.assertRaises(RequiredArgument):
36
            self.gmp.create_credential(
37
                name='', credential_type='up', login='foo')
38
39
        with self.assertRaises(RequiredArgument):
40
            self.gmp.create_credential(
41
                name=None, credential_type='up', login='foo')
42
43
    def test_create_up_credential(self):
44
        self.gmp.create_credential(
45
            name='foo', credential_type='up', comment='bar',
46
            allow_insecure=True, login='Max', password='123')
47
48
        self.connection.send.has_been_called_with(
49
            '<create_credential>'
50
            '<name>foo</name>'
51
            '<type>up</type>'
52
            '<comment>bar</comment>'
53
            '<allow_insecure>1</allow_insecure>'
54
            '<login>Max</login>'
55
            '<password>123</password>'
56
            '</create_credential>'
57
        )
58
59
    def test_create_cc_credential_missing_certificate(self):
60
        with self.assertRaises(RequiredArgument):
61
            self.gmp.create_credential(
62
                name='foo', credential_type='cc')
63
64
    def test_create_cc_credential(self):
65
        self.gmp.create_credential(
66
            name='foo', credential_type='cc', certificate='abcdef')
67
68
        self.connection.send.has_been_called_with(
69
            '<create_credential>'
70
            '<name>foo</name>'
71
            '<type>cc</type>'
72
            '<certificate>abcdef</certificate>'
73
            '</create_credential>'
74
        )
75
76
    def test_create_cc_credential_with_private_key(self):
77
        self.gmp.create_credential(
78
            name='foo', credential_type='cc', certificate='abcdef',
79
            private_key='123456')
80
81
        self.connection.send.has_been_called_with(
82
            '<create_credential>'
83
            '<name>foo</name>'
84
            '<type>cc</type>'
85
            '<certificate>abcdef</certificate>'
86
            '<key>'
87
            '<private>123456</private>'
88
            '</key>'
89
            '</create_credential>'
90
        )
91
92
    def test_create_usk_credential_missing_private_key(self):
93
        with self.assertRaises(RequiredArgument):
94
            self.gmp.create_credential(
95
                name='foo', credential_type='usk', login='foo')
96
97
98
    def test_create_usk_credential_missing_login(self):
99
        with self.assertRaises(RequiredArgument):
100
            self.gmp.create_credential(
101
                name='foo', credential_type='usk', private_key='123456')
102
103
    def test_create_usk_credential(self):
104
        self.gmp.create_credential(
105
            name='foo', credential_type='usk', private_key='123456',
106
            login='foo')
107
108
        self.connection.send.has_been_called_with(
109
            '<create_credential>'
110
            '<name>foo</name>'
111
            '<type>usk</type>'
112
            '<login>foo</login>'
113
            '<key>'
114
            '<private>123456</private>'
115
            '</key>'
116
            '</create_credential>'
117
        )
118
119
    def test_create_usk_credential_with_key_phrase(self):
120
        self.gmp.create_credential(
121
            name='foo', credential_type='usk', private_key='123456',
122
            login='foo', key_phrase='abcdef')
123
124
        self.connection.send.has_been_called_with(
125
            '<create_credential>'
126
            '<name>foo</name>'
127
            '<type>usk</type>'
128
            '<login>foo</login>'
129
            '<key>'
130
            '<private>123456</private>'
131
            '<phrase>abcdef</phrase>'
132
            '</key>'
133
            '</create_credential>'
134
        )
135
136 View Code Duplication
    def test_create_snmp_credential_invalid_auth_algorithm(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
137
        with self.assertRaises(InvalidArgument):
138
            self.gmp.create_credential(
139
                name='foo', credential_type='snmp', login='foo')
140
141
        with self.assertRaises(InvalidArgument):
142
            self.gmp.create_credential(
143
                name='foo', credential_type='snmp', login='foo',
144
                auth_algorithm='')
145
146
        with self.assertRaises(InvalidArgument):
147
            self.gmp.create_credential(
148
                name='foo', credential_type='snmp', login='foo',
149
                auth_algorithm='bar')
150
151
    def test_create_snmp_credential_auth_algorithm_md5(self):
152
        self.gmp.create_credential(
153
            name='foo', credential_type='snmp', login='foo',
154
            auth_algorithm='md5')
155
156
        self.connection.send.has_been_called_with(
157
            '<create_credential>'
158
            '<name>foo</name>'
159
            '<type>snmp</type>'
160
            '<login>foo</login>'
161
            '<auth_algorithm>md5</auth_algorithm>'
162
            '</create_credential>'
163
        )
164
165
    def test_create_snmp_credential_auth_algorithm_sha1(self):
166
        self.gmp.create_credential(
167
            name='foo', credential_type='snmp', login='foo',
168
            auth_algorithm='sha1')
169
170
        self.connection.send.has_been_called_with(
171
            '<create_credential>'
172
            '<name>foo</name>'
173
            '<type>snmp</type>'
174
            '<login>foo</login>'
175
            '<auth_algorithm>sha1</auth_algorithm>'
176
            '</create_credential>'
177
        )
178
179
    def test_create_snmp_credential_with_community(self):
180
        self.gmp.create_credential(
181
            name='foo', credential_type='snmp', login='foo',
182
            auth_algorithm='sha1', community='ipsum')
183
184
        self.connection.send.has_been_called_with(
185
            '<create_credential>'
186
            '<name>foo</name>'
187
            '<type>snmp</type>'
188
            '<login>foo</login>'
189
            '<auth_algorithm>sha1</auth_algorithm>'
190
            '<community>ipsum</community>'
191
            '</create_credential>'
192
        )
193
194 View Code Duplication
    def test_create_snmp_credential_invalid_privacy_algorithm(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
195
        with self.assertRaises(InvalidArgument):
196
            self.gmp.create_credential(
197
                name='foo', credential_type='snmp', login='foo',
198
                auth_algorithm='sha1', privacy_algorithm='')
199
200
        with self.assertRaises(InvalidArgument):
201
            self.gmp.create_credential(
202
                name='foo', credential_type='snmp', login='foo',
203
                auth_algorithm='sha1', privacy_algorithm='foo')
204
205
    def test_create_snmp_credential_with_privacy_algorithm_aes(self):
206
        self.gmp.create_credential(
207
            name='foo', credential_type='snmp', login='foo',
208
            auth_algorithm='sha1', privacy_algorithm='aes')
209
210
        self.connection.send.has_been_called_with(
211
            '<create_credential>'
212
            '<name>foo</name>'
213
            '<type>snmp</type>'
214
            '<login>foo</login>'
215
            '<auth_algorithm>sha1</auth_algorithm>'
216
            '<privacy>'
217
            '<algorithm>aes</algorithm>'
218
            '</privacy>'
219
            '</create_credential>'
220
        )
221
222
    def test_create_snmp_credential_with_privacy_algorithm_des(self):
223
        self.gmp.create_credential(
224
            name='foo', credential_type='snmp', login='foo',
225
            auth_algorithm='sha1', privacy_algorithm='des')
226
227
        self.connection.send.has_been_called_with(
228
            '<create_credential>'
229
            '<name>foo</name>'
230
            '<type>snmp</type>'
231
            '<login>foo</login>'
232
            '<auth_algorithm>sha1</auth_algorithm>'
233
            '<privacy>'
234
            '<algorithm>des</algorithm>'
235
            '</privacy>'
236
            '</create_credential>'
237
        )
238
239
    def test_create_snmp_credential_with_privacy_password(self):
240
        self.gmp.create_credential(
241
            name='foo', credential_type='snmp', login='foo',
242
            auth_algorithm='sha1', privacy_password='123')
243
244
        self.connection.send.has_been_called_with(
245
            '<create_credential>'
246
            '<name>foo</name>'
247
            '<type>snmp</type>'
248
            '<login>foo</login>'
249
            '<auth_algorithm>sha1</auth_algorithm>'
250
            '<privacy>'
251
            '<password>123</password>'
252
            '</privacy>'
253
            '</create_credential>'
254
        )
255
256
    def test_create_smime_credential(self):
257
        self.gmp.create_credential(
258
            name='foo', credential_type='smime', certificate='ipsum')
259
260
        self.connection.send.has_been_called_with(
261
            '<create_credential>'
262
            '<name>foo</name>'
263
            '<type>smime</type>'
264
            '<certificate>ipsum</certificate>'
265
            '</create_credential>'
266
        )
267
268
    def test_create_smime_credential_missing_certificate(self):
269
        with self.assertRaises(RequiredArgument):
270
            self.gmp.create_credential(
271
                name='foo', credential_type='smime')
272
273
    def test_create_pgp_credential(self):
274
        self.gmp.create_credential(
275
            name='foo', credential_type='pgp', public_key='ipsum')
276
277
        self.connection.send.has_been_called_with(
278
            '<create_credential>'
279
            '<name>foo</name>'
280
            '<type>pgp</type>'
281
            '<key>'
282
            '<public>ipsum</public>'
283
            '</key>'
284
            '</create_credential>'
285
        )
286
287
    def test_create_pgp_credential_missing_public_key(self):
288
        with self.assertRaises(RequiredArgument):
289
            self.gmp.create_credential(
290
                name='foo', credential_type='pgp')
291
292
293
if __name__ == '__main__':
294
    unittest.main()
295