Completed
Push — master ( f3cde9...b114e7 )
by
unknown
13s queued 11s
created

tests.protocols.gmpv7.test_modify_credential   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 160
dl 0
loc 327
rs 10
c 0
b 0
f 0
wmc 29

18 Methods

Rating   Name   Duplication   Size   Complexity  
A GmpModifyCredentialTestCase.test_modify_credential_with_key_phrase_and_private_key() 0 24 1
A GmpModifyCredentialTestCase.test_modify_credential_with_login() 0 8 1
A GmpModifyCredentialTestCase.test_modify_credential_with_allow_insecure() 0 19 1
A GmpModifyCredentialTestCase.test_modify_credential_invalid_privacy_alogorithm() 0 11 3
A GmpModifyCredentialTestCase.test_modify_credential_with_private_key() 0 8 1
A GmpModifyCredentialTestCase.test_modify_credential_with_privacy_alogorithm_and_password() 0 24 1
A GmpModifyCredentialTestCase.test_modify_credential_missing_credential_id() 0 14 4
A GmpModifyCredentialTestCase.test_modify_credential_with_certificate() 0 8 1
A GmpModifyCredentialTestCase.test_modify_credential_invalid_auth_alogorithm() 0 11 3
A GmpModifyCredentialTestCase.test_modify_credential_with_key_phrase_and_missing_private_key() 0 19 4
A GmpModifyCredentialTestCase.test_modify_credential_with_password() 0 8 1
A GmpModifyCredentialTestCase.setUp() 0 3 1
A GmpModifyCredentialTestCase.test_modify_credential_with_privacy_alogorithm() 0 21 1
A GmpModifyCredentialTestCase.test_modify_credential_with_name() 0 8 1
A GmpModifyCredentialTestCase.test_modify_credential_invalid_credential_type() 0 5 2
A GmpModifyCredentialTestCase.test_modify_credential_with_auth_alogorithm() 0 19 1
A GmpModifyCredentialTestCase.test_modify_credential_with_comment() 0 8 1
A GmpModifyCredentialTestCase.test_modify_credential_with_community() 0 8 1
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 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, InvalidArgument
22
from gvm.protocols.gmpv7 import Gmp
23
24
from .. import MockConnection
25
26
27
class GmpModifyCredentialTestCase(unittest.TestCase):
28
29
    def setUp(self):
30
        self.connection = MockConnection()
31
        self.gmp = Gmp(self.connection)
32
33
    def test_modify_credential_invalid_credential_type(self):
34
        with self.assertRaises(InvalidArgument):
35
            self.gmp.modify_credential(
36
                credential_id='c1',
37
                credential_type='foo',
38
            )
39
40
    def test_modify_credential_missing_credential_id(self):
41
        with self.assertRaises(RequiredArgument):
42
            self.gmp.modify_credential(
43
                credential_id=None,
44
            )
45
46
        with self.assertRaises(RequiredArgument):
47
            self.gmp.modify_credential(
48
                credential_id='',
49
            )
50
51
        with self.assertRaises(RequiredArgument):
52
            self.gmp.modify_credential(
53
                '',
54
            )
55
56
    def test_modify_credential_with_comment(self):
57
        self.gmp.modify_credential(
58
            credential_id='c1',
59
            comment='foo'
60
        )
61
62
        self.connection.send.has_been_called_with(
63
            '<modify_credential credential_id="c1">'
64
            '<comment>foo</comment>'
65
            '</modify_credential>'
66
        )
67
68
    def test_modify_credential_with_name(self):
69
        self.gmp.modify_credential(
70
            credential_id='c1',
71
            name='foo'
72
        )
73
74
        self.connection.send.has_been_called_with(
75
            '<modify_credential credential_id="c1">'
76
            '<name>foo</name>'
77
            '</modify_credential>'
78
        )
79
80
    def test_modify_credential_with_allow_insecure(self):
81
        self.gmp.modify_credential(
82
            credential_id='c1',
83
            allow_insecure=True,
84
        )
85
86
        self.connection.send.has_been_called_with(
87
            '<modify_credential credential_id="c1">'
88
            '<allow_insecure>1</allow_insecure>'
89
            '</modify_credential>'
90
        )
91
92
        self.gmp.modify_credential(
93
            credential_id='c1',
94
            allow_insecure=False,
95
        )
96
97
        self.connection.send.has_been_called_with(
98
            '<modify_credential credential_id="c1">'
99
            '<allow_insecure>0</allow_insecure>'
100
            '</modify_credential>'
101
        )
102
103
    def test_modify_credential_with_certificate(self):
104
        self.gmp.modify_credential(
105
            credential_id='c1',
106
            certificate='foo',
107
        )
108
109
        self.connection.send.has_been_called_with(
110
            '<modify_credential credential_id="c1">'
111
            '<certificate>foo</certificate>'
112
            '</modify_credential>'
113
        )
114
115
    def test_modify_credential_with_private_key(self):
116
        self.gmp.modify_credential(
117
            credential_id='c1',
118
            private_key='foo'
119
        )
120
121
        self.connection.send.has_been_called_with(
122
            '<modify_credential credential_id="c1">'
123
            '<key>'
124
            '<private>foo</private>'
125
            '</key>'
126
            '</modify_credential>'
127
        )
128
129
    def test_modify_credential_with_key_phrase_and_private_key(self):
130
        self.gmp.modify_credential(
131
            credential_id='c1',
132
            key_phrase='',
133
            private_key='foo'
134
        )
135
136
        self.connection.send.has_been_called_with(
137
            '<modify_credential credential_id="c1">'
138
            '<key>'
139
            '<private>foo</private>'
140
            '<phrase></phrase>'
141
            '</key>'
142
            '</modify_credential>'
143
        )
144
145
        self.gmp.modify_credential(
146
            credential_id='c1',
147
            key_phrase='bar',
148
            private_key='foo'
149
        )
150
151
        self.connection.send.has_been_called_with(
152
            '<modify_credential credential_id="c1">'
153
            '<key>'
154
            '<private>foo</private>'
155
            '<phrase>bar</phrase>'
156
            '</key>'
157
            '</modify_credential>'
158
        )
159
160
    def test_modify_credential_with_key_phrase_and_missing_private_key(self):
161
        with self.assertRaises(RequiredArgument):
162
            self.gmp.modify_credential(
163
                credential_id='c1',
164
                key_phrase='',
165
                private_key=''
166
            )
167
168
        with self.assertRaises(RequiredArgument):
169
            self.gmp.modify_credential(
170
                credential_id='c1',
171
                key_phrase='bar',
172
            )
173
174
        with self.assertRaises(RequiredArgument):
175
            self.gmp.modify_credential(
176
                credential_id='c1',
177
                key_phrase='bar',
178
                private_key=None,
179
            )
180
181
    def test_modify_credential_with_login(self):
182
        self.gmp.modify_credential(
183
            credential_id='c1',
184
            login='foo'
185
        )
186
187
        self.connection.send.has_been_called_with(
188
            '<modify_credential credential_id="c1">'
189
            '<login>foo</login>'
190
            '</modify_credential>'
191
        )
192
193
    def test_modify_credential_with_password(self):
194
        self.gmp.modify_credential(
195
            credential_id='c1',
196
            password='foo'
197
        )
198
199
        self.connection.send.has_been_called_with(
200
            '<modify_credential credential_id="c1">'
201
            '<password>foo</password>'
202
            '</modify_credential>'
203
        )
204
205
    def test_modify_credential_with_community(self):
206
        self.gmp.modify_credential(
207
            credential_id='c1',
208
            community='foo'
209
        )
210
211
        self.connection.send.has_been_called_with(
212
            '<modify_credential credential_id="c1">'
213
            '<community>foo</community>'
214
            '</modify_credential>'
215
        )
216
217
    def test_modify_credential_with_privacy_alogorithm(self):
218
        self.gmp.modify_credential(
219
            credential_id='c1',
220
            privacy_algorithm='aes',
221
        )
222
223
        self.connection.send.has_been_called_with(
224
            '<modify_credential credential_id="c1">'
225
            '<privacy>'
226
            '<algorithm>aes</algorithm>'
227
            '</privacy>'
228
            '</modify_credential>'
229
        )
230
231
        self.gmp.modify_credential(
232
            credential_id='c1',
233
            privacy_algorithm='des',
234
        )
235
236
        self.connection.send.has_been_called_with(
237
            '<modify_credential credential_id="c1">'
238
            '<privacy>'
239
            '<algorithm>des</algorithm>'
240
            '</privacy>'
241
            '</modify_credential>'
242
        )
243
244
    def test_modify_credential_with_privacy_alogorithm_and_password(self):
245
        self.gmp.modify_credential(
246
            credential_id='c1',
247
            privacy_algorithm='aes',
248
            privacy_password='foo',
249
        )
250
251
        self.connection.send.has_been_called_with(
252
            '<modify_credential credential_id="c1">'
253
            '<privacy>'
254
            '<algorithm>aes</algorithm>'
255
            '<password>foo</password>'
256
            '</privacy>'
257
            '</modify_credential>'
258
        )
259
260
        self.gmp.modify_credential(
261
            credential_id='c1',
262
            privacy_algorithm='aes',
263
            privacy_password='',
264
        )
265
266
        self.connection.send.has_been_called_with(
267
            '<modify_credential credential_id="c1">'
268
            '<privacy>'
269
            '<algorithm>aes</algorithm>'
270
            '<password></password>'
271
            '</privacy>'
272
            '</modify_credential>'
273
        )
274
275
    def test_modify_credential_invalid_privacy_alogorithm(self):
276
        with self.assertRaises(InvalidArgument):
277
            self.gmp.modify_credential(
278
                credential_id='c1',
279
                privacy_algorithm='',
280
            )
281
282
        with self.assertRaises(InvalidArgument):
283
            self.gmp.modify_credential(
284
                credential_id='c1',
285
                privacy_algorithm='foo',
286
            )
287
288
    def test_modify_credential_with_auth_alogorithm(self):
289
        self.gmp.modify_credential(
290
            credential_id='c1',
291
            auth_algorithm='md5',
292
        )
293
294
        self.connection.send.has_been_called_with(
295
            '<modify_credential credential_id="c1">'
296
            '<auth_algorithm>md5</auth_algorithm>'
297
            '</modify_credential>'
298
        )
299
300
        self.gmp.modify_credential(
301
            credential_id='c1',
302
            auth_algorithm='sha1',
303
        )
304
305
        self.connection.send.has_been_called_with(
306
            '<modify_credential credential_id="c1">'
307
            '<auth_algorithm>sha1</auth_algorithm>'
308
            '</modify_credential>'
309
        )
310
311
    def test_modify_credential_invalid_auth_alogorithm(self):
312
        with self.assertRaises(InvalidArgument):
313
            self.gmp.modify_credential(
314
                credential_id='c1',
315
                auth_algorithm='',
316
            )
317
318
        with self.assertRaises(InvalidArgument):
319
            self.gmp.modify_credential(
320
                credential_id='c1',
321
                auth_algorithm='foo',
322
            )
323
324
325
if __name__ == '__main__':
326
    unittest.main()
327