Passed
Pull Request — master (#89)
by
unknown
01:45
created

tests.protocols.gmpv8.test_modify_credential   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 298
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 146
dl 0
loc 298
rs 10
c 0
b 0
f 0
wmc 30

21 Methods

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