Completed
Push — master ( b519bb...486e1a )
by Jaspar
25s queued 15s
created

tests.protocols.gmpv208.entities.credentials.test_modify_credential   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 92
dl 0
loc 209
rs 10
c 0
b 0
f 0
wmc 26

18 Methods

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