| Total Complexity | 41 |
| Total Lines | 437 |
| Duplicated Lines | 90.85 % |
| Changes | 0 | ||
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:
Complex classes like tests.protocols.gmpv9.test_create_credential often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 2 | # Copyright (C) 2020 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.gmpv9 import ( |
||
| 23 | CredentialType, |
||
| 24 | SnmpAuthAlgorithm, |
||
| 25 | SnmpPrivacyAlgorithm, |
||
| 26 | ) |
||
| 27 | |||
| 28 | from . import Gmpv9TestCase |
||
| 29 | |||
| 30 | |||
| 31 | View Code Duplication | class GmpCreateCredentialTestCase(Gmpv9TestCase): |
|
|
|
|||
| 32 | def test_create_up_credential_missing_name(self): |
||
| 33 | with self.assertRaises(RequiredArgument): |
||
| 34 | self.gmp.create_credential( |
||
| 35 | name='', |
||
| 36 | credential_type=CredentialType.USERNAME_PASSWORD, |
||
| 37 | login='foo', |
||
| 38 | ) |
||
| 39 | |||
| 40 | with self.assertRaises(RequiredArgument): |
||
| 41 | self.gmp.create_credential( |
||
| 42 | name=None, |
||
| 43 | credential_type=CredentialType.USERNAME_PASSWORD, |
||
| 44 | login='foo', |
||
| 45 | ) |
||
| 46 | |||
| 47 | def test_create_up_credential(self): |
||
| 48 | self.gmp.create_credential( |
||
| 49 | name='foo', |
||
| 50 | credential_type=CredentialType.USERNAME_PASSWORD, |
||
| 51 | comment='bar', |
||
| 52 | login='Max', |
||
| 53 | password='123', |
||
| 54 | ) |
||
| 55 | |||
| 56 | self.connection.send.has_been_called_with( |
||
| 57 | '<create_credential>' |
||
| 58 | '<name>foo</name>' |
||
| 59 | '<type>up</type>' |
||
| 60 | '<comment>bar</comment>' |
||
| 61 | '<login>Max</login>' |
||
| 62 | '<password>123</password>' |
||
| 63 | '</create_credential>' |
||
| 64 | ) |
||
| 65 | |||
| 66 | def test_create_up_credential_with_allow_insecure(self): |
||
| 67 | self.gmp.create_credential( |
||
| 68 | name='foo', |
||
| 69 | credential_type=CredentialType.USERNAME_PASSWORD, |
||
| 70 | comment='bar', |
||
| 71 | login='Max', |
||
| 72 | password='123', |
||
| 73 | allow_insecure=True, |
||
| 74 | ) |
||
| 75 | |||
| 76 | self.connection.send.has_been_called_with( |
||
| 77 | '<create_credential>' |
||
| 78 | '<name>foo</name>' |
||
| 79 | '<type>up</type>' |
||
| 80 | '<comment>bar</comment>' |
||
| 81 | '<allow_insecure>1</allow_insecure>' |
||
| 82 | '<login>Max</login>' |
||
| 83 | '<password>123</password>' |
||
| 84 | '</create_credential>' |
||
| 85 | ) |
||
| 86 | |||
| 87 | self.gmp.create_credential( |
||
| 88 | name='foo', |
||
| 89 | credential_type=CredentialType.USERNAME_PASSWORD, |
||
| 90 | comment='bar', |
||
| 91 | login='Max', |
||
| 92 | password='123', |
||
| 93 | allow_insecure=False, |
||
| 94 | ) |
||
| 95 | |||
| 96 | self.connection.send.has_been_called_with( |
||
| 97 | '<create_credential>' |
||
| 98 | '<name>foo</name>' |
||
| 99 | '<type>up</type>' |
||
| 100 | '<comment>bar</comment>' |
||
| 101 | '<allow_insecure>0</allow_insecure>' |
||
| 102 | '<login>Max</login>' |
||
| 103 | '<password>123</password>' |
||
| 104 | '</create_credential>' |
||
| 105 | ) |
||
| 106 | |||
| 107 | def test_create_cc_credential_missing_certificate(self): |
||
| 108 | with self.assertRaises(RequiredArgument): |
||
| 109 | self.gmp.create_credential( |
||
| 110 | name='foo', credential_type=CredentialType.CLIENT_CERTIFICATE |
||
| 111 | ) |
||
| 112 | |||
| 113 | def test_create_cc_credential(self): |
||
| 114 | self.gmp.create_credential( |
||
| 115 | name='foo', |
||
| 116 | credential_type=CredentialType.CLIENT_CERTIFICATE, |
||
| 117 | certificate='abcdef', |
||
| 118 | ) |
||
| 119 | |||
| 120 | self.connection.send.has_been_called_with( |
||
| 121 | '<create_credential>' |
||
| 122 | '<name>foo</name>' |
||
| 123 | '<type>cc</type>' |
||
| 124 | '<certificate>abcdef</certificate>' |
||
| 125 | '</create_credential>' |
||
| 126 | ) |
||
| 127 | |||
| 128 | def test_create_cc_credential_with_private_key(self): |
||
| 129 | self.gmp.create_credential( |
||
| 130 | name='foo', |
||
| 131 | credential_type=CredentialType.CLIENT_CERTIFICATE, |
||
| 132 | certificate='abcdef', |
||
| 133 | private_key='123456', |
||
| 134 | ) |
||
| 135 | |||
| 136 | self.connection.send.has_been_called_with( |
||
| 137 | '<create_credential>' |
||
| 138 | '<name>foo</name>' |
||
| 139 | '<type>cc</type>' |
||
| 140 | '<certificate>abcdef</certificate>' |
||
| 141 | '<key>' |
||
| 142 | '<private>123456</private>' |
||
| 143 | '</key>' |
||
| 144 | '</create_credential>' |
||
| 145 | ) |
||
| 146 | |||
| 147 | def test_create_usk_credential_missing_private_key(self): |
||
| 148 | with self.assertRaises(RequiredArgument): |
||
| 149 | self.gmp.create_credential( |
||
| 150 | name='foo', |
||
| 151 | credential_type=CredentialType.USERNAME_SSH_KEY, |
||
| 152 | login='foo', |
||
| 153 | ) |
||
| 154 | |||
| 155 | def test_create_usk_credential_missing_login(self): |
||
| 156 | with self.assertRaises(RequiredArgument): |
||
| 157 | self.gmp.create_credential( |
||
| 158 | name='foo', |
||
| 159 | credential_type=CredentialType.USERNAME_SSH_KEY, |
||
| 160 | private_key='123456', |
||
| 161 | ) |
||
| 162 | |||
| 163 | def test_create_usk_credential(self): |
||
| 164 | self.gmp.create_credential( |
||
| 165 | name='foo', |
||
| 166 | credential_type=CredentialType.USERNAME_SSH_KEY, |
||
| 167 | private_key='123456', |
||
| 168 | login='foo', |
||
| 169 | ) |
||
| 170 | |||
| 171 | self.connection.send.has_been_called_with( |
||
| 172 | '<create_credential>' |
||
| 173 | '<name>foo</name>' |
||
| 174 | '<type>usk</type>' |
||
| 175 | '<login>foo</login>' |
||
| 176 | '<key>' |
||
| 177 | '<private>123456</private>' |
||
| 178 | '</key>' |
||
| 179 | '</create_credential>' |
||
| 180 | ) |
||
| 181 | |||
| 182 | def test_create_usk_credential_with_key_phrase(self): |
||
| 183 | self.gmp.create_credential( |
||
| 184 | name='foo', |
||
| 185 | credential_type=CredentialType.USERNAME_SSH_KEY, |
||
| 186 | private_key='123456', |
||
| 187 | login='foo', |
||
| 188 | key_phrase='abcdef', |
||
| 189 | ) |
||
| 190 | |||
| 191 | self.connection.send.has_been_called_with( |
||
| 192 | '<create_credential>' |
||
| 193 | '<name>foo</name>' |
||
| 194 | '<type>usk</type>' |
||
| 195 | '<login>foo</login>' |
||
| 196 | '<key>' |
||
| 197 | '<private>123456</private>' |
||
| 198 | '<phrase>abcdef</phrase>' |
||
| 199 | '</key>' |
||
| 200 | '</create_credential>' |
||
| 201 | ) |
||
| 202 | |||
| 203 | def test_create_snmp_credential_invalid_auth_algorithm(self): |
||
| 204 | with self.assertRaises(InvalidArgumentType): |
||
| 205 | self.gmp.create_credential( |
||
| 206 | name='foo', credential_type=CredentialType.SNMP, login='foo' |
||
| 207 | ) |
||
| 208 | |||
| 209 | with self.assertRaises(InvalidArgumentType): |
||
| 210 | self.gmp.create_credential( |
||
| 211 | name='foo', |
||
| 212 | credential_type=CredentialType.SNMP, |
||
| 213 | login='foo', |
||
| 214 | auth_algorithm='', |
||
| 215 | ) |
||
| 216 | |||
| 217 | with self.assertRaises(InvalidArgumentType): |
||
| 218 | self.gmp.create_credential( |
||
| 219 | name='foo', |
||
| 220 | credential_type=CredentialType.SNMP, |
||
| 221 | login='foo', |
||
| 222 | auth_algorithm='bar', |
||
| 223 | ) |
||
| 224 | |||
| 225 | def test_create_snmp_credential_auth_algorithm_md5(self): |
||
| 226 | self.gmp.create_credential( |
||
| 227 | name='foo', |
||
| 228 | credential_type=CredentialType.SNMP, |
||
| 229 | login='foo', |
||
| 230 | auth_algorithm=SnmpAuthAlgorithm.MD5, |
||
| 231 | ) |
||
| 232 | |||
| 233 | self.connection.send.has_been_called_with( |
||
| 234 | '<create_credential>' |
||
| 235 | '<name>foo</name>' |
||
| 236 | '<type>snmp</type>' |
||
| 237 | '<login>foo</login>' |
||
| 238 | '<auth_algorithm>md5</auth_algorithm>' |
||
| 239 | '</create_credential>' |
||
| 240 | ) |
||
| 241 | |||
| 242 | def test_create_snmp_credential_auth_algorithm_sha1(self): |
||
| 243 | self.gmp.create_credential( |
||
| 244 | name='foo', |
||
| 245 | credential_type=CredentialType.SNMP, |
||
| 246 | login='foo', |
||
| 247 | auth_algorithm=SnmpAuthAlgorithm.SHA1, |
||
| 248 | ) |
||
| 249 | |||
| 250 | self.connection.send.has_been_called_with( |
||
| 251 | '<create_credential>' |
||
| 252 | '<name>foo</name>' |
||
| 253 | '<type>snmp</type>' |
||
| 254 | '<login>foo</login>' |
||
| 255 | '<auth_algorithm>sha1</auth_algorithm>' |
||
| 256 | '</create_credential>' |
||
| 257 | ) |
||
| 258 | |||
| 259 | def test_create_snmp_credential_with_community(self): |
||
| 260 | self.gmp.create_credential( |
||
| 261 | name='foo', |
||
| 262 | credential_type=CredentialType.SNMP, |
||
| 263 | login='foo', |
||
| 264 | auth_algorithm=SnmpAuthAlgorithm.SHA1, |
||
| 265 | community='ipsum', |
||
| 266 | ) |
||
| 267 | |||
| 268 | self.connection.send.has_been_called_with( |
||
| 269 | '<create_credential>' |
||
| 270 | '<name>foo</name>' |
||
| 271 | '<type>snmp</type>' |
||
| 272 | '<login>foo</login>' |
||
| 273 | '<auth_algorithm>sha1</auth_algorithm>' |
||
| 274 | '<community>ipsum</community>' |
||
| 275 | '</create_credential>' |
||
| 276 | ) |
||
| 277 | |||
| 278 | def test_create_snmp_credential_invalid_privacy_algorithm(self): |
||
| 279 | with self.assertRaises(InvalidArgumentType): |
||
| 280 | self.gmp.create_credential( |
||
| 281 | name='foo', |
||
| 282 | credential_type=CredentialType.SNMP, |
||
| 283 | login='foo', |
||
| 284 | auth_algorithm=SnmpAuthAlgorithm.SHA1, |
||
| 285 | privacy_algorithm='', |
||
| 286 | ) |
||
| 287 | |||
| 288 | with self.assertRaises(InvalidArgumentType): |
||
| 289 | self.gmp.create_credential( |
||
| 290 | name='foo', |
||
| 291 | credential_type=CredentialType.SNMP, |
||
| 292 | login='foo', |
||
| 293 | auth_algorithm=SnmpAuthAlgorithm.SHA1, |
||
| 294 | privacy_algorithm='foo', |
||
| 295 | ) |
||
| 296 | |||
| 297 | def test_create_snmp_credential_with_privacy_algorithm_aes(self): |
||
| 298 | self.gmp.create_credential( |
||
| 299 | name='foo', |
||
| 300 | credential_type=CredentialType.SNMP, |
||
| 301 | login='foo', |
||
| 302 | auth_algorithm=SnmpAuthAlgorithm.SHA1, |
||
| 303 | privacy_algorithm=SnmpPrivacyAlgorithm.AES, |
||
| 304 | ) |
||
| 305 | |||
| 306 | self.connection.send.has_been_called_with( |
||
| 307 | '<create_credential>' |
||
| 308 | '<name>foo</name>' |
||
| 309 | '<type>snmp</type>' |
||
| 310 | '<login>foo</login>' |
||
| 311 | '<auth_algorithm>sha1</auth_algorithm>' |
||
| 312 | '<privacy>' |
||
| 313 | '<algorithm>aes</algorithm>' |
||
| 314 | '</privacy>' |
||
| 315 | '</create_credential>' |
||
| 316 | ) |
||
| 317 | |||
| 318 | def test_create_snmp_credential_with_privacy_algorithm_des(self): |
||
| 319 | self.gmp.create_credential( |
||
| 320 | name='foo', |
||
| 321 | credential_type=CredentialType.SNMP, |
||
| 322 | login='foo', |
||
| 323 | auth_algorithm=SnmpAuthAlgorithm.SHA1, |
||
| 324 | privacy_algorithm=SnmpPrivacyAlgorithm.DES, |
||
| 325 | ) |
||
| 326 | |||
| 327 | self.connection.send.has_been_called_with( |
||
| 328 | '<create_credential>' |
||
| 329 | '<name>foo</name>' |
||
| 330 | '<type>snmp</type>' |
||
| 331 | '<login>foo</login>' |
||
| 332 | '<auth_algorithm>sha1</auth_algorithm>' |
||
| 333 | '<privacy>' |
||
| 334 | '<algorithm>des</algorithm>' |
||
| 335 | '</privacy>' |
||
| 336 | '</create_credential>' |
||
| 337 | ) |
||
| 338 | |||
| 339 | def test_create_snmp_credential_with_privacy_password(self): |
||
| 340 | self.gmp.create_credential( |
||
| 341 | name='foo', |
||
| 342 | credential_type=CredentialType.SNMP, |
||
| 343 | login='foo', |
||
| 344 | auth_algorithm=SnmpAuthAlgorithm.SHA1, |
||
| 345 | privacy_password='123', |
||
| 346 | ) |
||
| 347 | |||
| 348 | self.connection.send.has_been_called_with( |
||
| 349 | '<create_credential>' |
||
| 350 | '<name>foo</name>' |
||
| 351 | '<type>snmp</type>' |
||
| 352 | '<login>foo</login>' |
||
| 353 | '<auth_algorithm>sha1</auth_algorithm>' |
||
| 354 | '<privacy>' |
||
| 355 | '<password>123</password>' |
||
| 356 | '</privacy>' |
||
| 357 | '</create_credential>' |
||
| 358 | ) |
||
| 359 | |||
| 360 | def test_create_smime_credential(self): |
||
| 361 | self.gmp.create_credential( |
||
| 362 | name='foo', |
||
| 363 | credential_type=CredentialType.SMIME_CERTIFICATE, |
||
| 364 | certificate='ipsum', |
||
| 365 | ) |
||
| 366 | |||
| 367 | self.connection.send.has_been_called_with( |
||
| 368 | '<create_credential>' |
||
| 369 | '<name>foo</name>' |
||
| 370 | '<type>smime</type>' |
||
| 371 | '<certificate>ipsum</certificate>' |
||
| 372 | '</create_credential>' |
||
| 373 | ) |
||
| 374 | |||
| 375 | def test_create_smime_credential_missing_certificate(self): |
||
| 376 | with self.assertRaises(RequiredArgument): |
||
| 377 | self.gmp.create_credential( |
||
| 378 | name='foo', credential_type=CredentialType.SMIME_CERTIFICATE |
||
| 379 | ) |
||
| 380 | |||
| 381 | def test_create_pgp_credential(self): |
||
| 382 | self.gmp.create_credential( |
||
| 383 | name='foo', |
||
| 384 | credential_type=CredentialType.PGP_ENCRYPTION_KEY, |
||
| 385 | public_key='ipsum', |
||
| 386 | ) |
||
| 387 | |||
| 388 | self.connection.send.has_been_called_with( |
||
| 389 | '<create_credential>' |
||
| 390 | '<name>foo</name>' |
||
| 391 | '<type>pgp</type>' |
||
| 392 | '<key>' |
||
| 393 | '<public>ipsum</public>' |
||
| 394 | '</key>' |
||
| 395 | '</create_credential>' |
||
| 396 | ) |
||
| 397 | |||
| 398 | def test_create_pgp_credential_missing_public_key(self): |
||
| 399 | with self.assertRaises(RequiredArgument): |
||
| 400 | self.gmp.create_credential( |
||
| 401 | name='foo', credential_type=CredentialType.PGP_ENCRYPTION_KEY |
||
| 402 | ) |
||
| 403 | |||
| 404 | def test_create_credential_invalid_credential_type(self): |
||
| 405 | with self.assertRaises(InvalidArgumentType): |
||
| 406 | self.gmp.create_credential(name='foo', credential_type=None) |
||
| 407 | |||
| 408 | with self.assertRaises(InvalidArgumentType): |
||
| 409 | self.gmp.create_credential(name='foo', credential_type='') |
||
| 410 | |||
| 411 | with self.assertRaises(InvalidArgumentType): |
||
| 412 | self.gmp.create_credential(name='foo', credential_type='bar') |
||
| 413 | |||
| 414 | def test_create_pw_credential_missing_password(self): |
||
| 415 | with self.assertRaises(RequiredArgument): |
||
| 416 | self.gmp.create_credential( |
||
| 417 | name='foo', credential_type=CredentialType.PASSWORD_ONLY |
||
| 418 | ) |
||
| 419 | |||
| 420 | def test_create_pw_credential(self): |
||
| 421 | self.gmp.create_credential( |
||
| 422 | name='foo', |
||
| 423 | credential_type=CredentialType.PASSWORD_ONLY, |
||
| 424 | password='foo', |
||
| 425 | ) |
||
| 426 | self.connection.send.has_been_called_with( |
||
| 427 | '<create_credential>' |
||
| 428 | '<name>foo</name>' |
||
| 429 | '<type>pw</type>' |
||
| 430 | '<password>foo</password>' |
||
| 431 | '</create_credential>' |
||
| 432 | ) |
||
| 433 | |||
| 434 | |||
| 435 | if __name__ == '__main__': |
||
| 436 | unittest.main() |
||
| 437 |