Passed
Pull Request — master (#436)
by Jaspar
04:52
created

tests.protocols.gmpv208.testcmds.test_modify_credential   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 220
Duplicated Lines 84.55 %

Importance

Changes 0
Metric Value
eloc 95
dl 186
loc 220
rs 10
c 0
b 0
f 0
wmc 26

18 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpModifyCredentialTestCase.test_modify_credential_missing_private_key() 3 3 2
A GmpModifyCredentialTestCase.test_modify_credential_with_password() 5 5 1
A GmpModifyCredentialTestCase.test_modify_credential_with_privacy_password() 5 5 1
A GmpModifyCredentialTestCase.test_modify_credential_with_private_key_and_key_phrase() 7 7 1
A GmpModifyCredentialTestCase.test_modify_credential_with_certificate() 5 5 1
A GmpModifyCredentialTestCase.test_modify_credential_with_auth_algorithm() 17 17 1
A GmpModifyCredentialTestCase.test_modify_credential_with_privacy_algorithm() 19 19 1
A GmpModifyCredentialTestCase.test_modify_credential_missing_credential_id() 9 9 4
A GmpModifyCredentialTestCase.test_modify_credential_with_name() 5 5 1
A GmpModifyCredentialTestCase.test_modify_credential_invalid_privacy_algorithm() 7 7 3
A GmpModifyCredentialTestCase.test_modify_credential_with_allow_insecure() 13 13 1
A GmpModifyCredentialTestCase.test_modify_credential_with_community() 5 5 1
A GmpModifyCredentialTestCase.test_modify_credential_with_login() 5 5 1
A GmpModifyCredentialTestCase.test_modify_credential_with_public_key() 5 5 1
A GmpModifyCredentialTestCase.test_modify_credential_with_comment() 5 5 1
A GmpModifyCredentialTestCase.test_modify_credential() 5 5 1
A GmpModifyCredentialTestCase.test_modify_credential_missing_key_phrase() 3 3 2
A GmpModifyCredentialTestCase.test_modify_credential_invalid_auth_algorithm() 3 3 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
# -*- 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 SnmpAuthAlgorithm, SnmpPrivacyAlgorithm
23
24
25 View Code Duplication
class GmpModifyCredentialTestCase:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
26
    def test_modify_credential(self):
27
        self.gmp.modify_credential(credential_id='c1')
28
29
        self.connection.send.has_been_called_with(
30
            '<modify_credential credential_id="c1"/>'
31
        )
32
33
    def test_modify_credential_missing_credential_id(self):
34
        with self.assertRaises(RequiredArgument):
35
            self.gmp.modify_credential(None)
36
37
        with self.assertRaises(RequiredArgument):
38
            self.gmp.modify_credential('')
39
40
        with self.assertRaises(RequiredArgument):
41
            self.gmp.modify_credential(credential_id='')
42
43
    def test_modify_credential_with_name(self):
44
        self.gmp.modify_credential(credential_id='c1', name='foo')
45
46
        self.connection.send.has_been_called_with(
47
            '<modify_credential credential_id="c1">'
48
            '<name>foo</name>'
49
            '</modify_credential>'
50
        )
51
52
    def test_modify_credential_with_comment(self):
53
        self.gmp.modify_credential(credential_id='c1', comment='foo')
54
55
        self.connection.send.has_been_called_with(
56
            '<modify_credential credential_id="c1">'
57
            '<comment>foo</comment>'
58
            '</modify_credential>'
59
        )
60
61
    def test_modify_credential_with_certificate(self):
62
        self.gmp.modify_credential(credential_id='c1', certificate='abcdef')
63
64
        self.connection.send.has_been_called_with(
65
            '<modify_credential credential_id="c1">'
66
            '<certificate>abcdef</certificate>'
67
            '</modify_credential>'
68
        )
69
70
    def test_modify_credential_with_private_key_and_key_phrase(self):
71
        self.gmp.modify_credential(
72
            credential_id='c1', private_key='123456', key_phrase='foo'
73
        )
74
75
        self.connection.send.has_been_called_with(
76
            '<modify_credential credential_id="c1">'
77
            '<key>'
78
            '<phrase>foo</phrase>'
79
            '<private>123456</private>'
80
            '</key>'
81
            '</modify_credential>'
82
        )
83
84
    def test_modify_credential_missing_private_key(self):
85
        with self.assertRaises(RequiredArgument):
86
            self.gmp.modify_credential(credential_id='c1', key_phrase='foo')
87
88
    def test_modify_credential_missing_key_phrase(self):
89
        with self.assertRaises(RequiredArgument):
90
            self.gmp.modify_credential(credential_id='c1', private_key='123456')
91
92
    def test_modify_credential_with_allow_insecure(self):
93
        self.gmp.modify_credential(credential_id='c1', allow_insecure=True)
94
95
        self.connection.send.has_been_called_with(
96
            '<modify_credential credential_id="c1">'
97
            '<allow_insecure>1</allow_insecure>'
98
            '</modify_credential>'
99
        )
100
101
        self.gmp.modify_credential(credential_id='c1', allow_insecure=False)
102
103
        self.connection.send.has_been_called_with(
104
            '<modify_credential credential_id="c1">'
105
            '<allow_insecure>0</allow_insecure>'
106
            '</modify_credential>'
107
        )
108
109
    def test_modify_credential_with_login(self):
110
        self.gmp.modify_credential(credential_id='c1', login='foo')
111
112
        self.connection.send.has_been_called_with(
113
            '<modify_credential credential_id="c1">'
114
            '<login>foo</login>'
115
            '</modify_credential>'
116
        )
117
118
    def test_modify_credential_with_password(self):
119
        self.gmp.modify_credential(credential_id='c1', password='foo')
120
121
        self.connection.send.has_been_called_with(
122
            '<modify_credential credential_id="c1">'
123
            '<password>foo</password>'
124
            '</modify_credential>'
125
        )
126
127
    def test_modify_credential_with_auth_algorithm(self):
128
        self.gmp.modify_credential(
129
            credential_id='c1', auth_algorithm=SnmpAuthAlgorithm.MD5
130
        )
131
132
        self.connection.send.has_been_called_with(
133
            '<modify_credential credential_id="c1">'
134
            '<auth_algorithm>md5</auth_algorithm>'
135
            '</modify_credential>'
136
        )
137
138
        self.gmp.modify_credential(
139
            credential_id='c1', auth_algorithm=SnmpAuthAlgorithm.SHA1
140
        )
141
142
        self.connection.send.has_been_called_with(
143
            '<modify_credential credential_id="c1">'
144
            '<auth_algorithm>sha1</auth_algorithm>'
145
            '</modify_credential>'
146
        )
147
148
    def test_modify_credential_invalid_auth_algorithm(self):
149
        with self.assertRaises(InvalidArgumentType):
150
            self.gmp.modify_credential(credential_id='c1', auth_algorithm='foo')
151
152
    def test_modify_credential_with_community(self):
153
        self.gmp.modify_credential(credential_id='c1', community='foo')
154
155
        self.connection.send.has_been_called_with(
156
            '<modify_credential credential_id="c1">'
157
            '<community>foo</community>'
158
            '</modify_credential>'
159
        )
160
161
    def test_modify_credential_with_privacy_algorithm(self):
162
        self.gmp.modify_credential(
163
            credential_id='c1', privacy_algorithm=SnmpPrivacyAlgorithm.AES
164
        )
165
166
        self.connection.send.has_been_called_with(
167
            '<modify_credential credential_id="c1">'
168
            '<privacy>'
169
            '<algorithm>aes</algorithm>'
170
            '</privacy>'
171
            '</modify_credential>'
172
        )
173
174
        self.gmp.modify_credential(
175
            credential_id='c1', privacy_algorithm=SnmpPrivacyAlgorithm.DES
176
        )
177
178
        self.connection.send.has_been_called_with(
179
            '<modify_credential credential_id="c1">'
180
            '<privacy>'
181
            '<algorithm>des</algorithm>'
182
            '</privacy>'
183
            '</modify_credential>'
184
        )
185
186
    def test_modify_credential_invalid_privacy_algorithm(self):
187
        with self.assertRaises(InvalidArgumentType):
188
            self.gmp.modify_credential(credential_id='c1', privacy_algorithm='')
189
190
        with self.assertRaises(InvalidArgumentType):
191
            self.gmp.modify_credential(
192
                credential_id='c1', privacy_algorithm='foo'
193
            )
194
195
    def test_modify_credential_with_privacy_password(self):
196
        self.gmp.modify_credential(credential_id='c1', privacy_password='foo')
197
198
        self.connection.send.has_been_called_with(
199
            '<modify_credential credential_id="c1">'
200
            '<privacy>'
201
            '<password>foo</password>'
202
            '</privacy>'
203
            '</modify_credential>'
204
        )
205
206
    def test_modify_credential_with_public_key(self):
207
        self.gmp.modify_credential(credential_id='c1', public_key='foo')
208
209
        self.connection.send.has_been_called_with(
210
            '<modify_credential credential_id="c1">'
211
            '<key>'
212
            '<public>foo</public>'
213
            '</key>'
214
            '</modify_credential>'
215
        )
216
217
218
if __name__ == '__main__':
219
    unittest.main()
220