UT: Use dict for key,value in credential components tests

The SSHKeypair credential component has two fields (public, private),
not just a single value. This commit modifies the tests to be able to
support credential components with multiple fields.
This commit is contained in:
Mike Salvatore 2022-07-06 12:25:01 -04:00
parent 58fcc3761c
commit e92de42da9
1 changed files with 43 additions and 35 deletions

View File

@ -1,3 +1,5 @@
from typing import Any, Mapping
import pytest import pytest
from marshmallow.exceptions import ValidationError from marshmallow.exceptions import ValidationError
@ -8,72 +10,74 @@ from common.credentials.password import PasswordSchema
from common.credentials.username import UsernameSchema from common.credentials.username import UsernameSchema
PARAMETRIZED_PARAMETER_NAMES = ( PARAMETRIZED_PARAMETER_NAMES = (
"credential_component_class, schema_class, credential_component_type, key, value" "credential_component_class, schema_class, credential_component_type, credential_component_data"
) )
PARAMETRIZED_PARAMETER_VALUES = [ PARAMETRIZED_PARAMETER_VALUES = [
(Password, PasswordSchema, CredentialComponentType.PASSWORD, "password", "123456"), (Username, UsernameSchema, CredentialComponentType.USERNAME, {"username": "test_user"}),
(Username, UsernameSchema, CredentialComponentType.USERNAME, "username", "test_user"), (Password, PasswordSchema, CredentialComponentType.PASSWORD, {"password": "123456"}),
( (
LMHash, LMHash,
LMHashSchema, LMHashSchema,
CredentialComponentType.LM_HASH, CredentialComponentType.LM_HASH,
"lm_hash", {"lm_hash": "E52CAC67419A9A224A3B108F3FA6CB6D"},
"E52CAC67419A9A224A3B108F3FA6CB6D",
), ),
( (
NTHash, NTHash,
NTHashSchema, NTHashSchema,
CredentialComponentType.NT_HASH, CredentialComponentType.NT_HASH,
"nt_hash", {"nt_hash": "E52CAC67419A9A224A3B108F3FA6CB6D"},
"E52CAC67419A9A224A3B108F3FA6CB6D",
), ),
] ]
INVALID_VALUES = { INVALID_COMPONENT_DATA = {
CredentialComponentType.USERNAME: (None, 1, 2.0), CredentialComponentType.USERNAME: ({"username": None}, {"username": 1}, {"username": 2.0}),
CredentialComponentType.PASSWORD: (None, 1, 2.0), CredentialComponentType.PASSWORD: ({"password": None}, {"password": 1}, {"password": 2.0}),
CredentialComponentType.LM_HASH: ( CredentialComponentType.LM_HASH: (
None, {"lm_hash": None},
1, {"lm_hash": 1},
2.0, {"lm_hash": 2.0},
"0123456789012345678901234568901", {"lm_hash": "0123456789012345678901234568901"},
"E52GAC67419A9A224A3B108F3FA6CB6D", {"lm_hash": "E52GAC67419A9A224A3B108F3FA6CB6D"},
), ),
CredentialComponentType.NT_HASH: ( CredentialComponentType.NT_HASH: (
None, {"nt_hash": None},
1, {"nt_hash": 1},
2.0, {"nt_hash": 2.0},
"0123456789012345678901234568901", {"nt_hash": "0123456789012345678901234568901"},
"E52GAC67419A9A224A3B108F3FA6CB6D", {"nt_hash": "E52GAC67419A9A224A3B108F3FA6CB6D"},
), ),
} }
def build_component_dict(credential_component_type: CredentialComponentType, key: str, value: str): def build_component_dict(
return {"credential_type": credential_component_type.name, key: value} credential_component_type: CredentialComponentType, credential_component_data: Mapping[str, Any]
):
return {"credential_type": credential_component_type.name, **credential_component_data}
@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES) @pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES)
def test_credential_component_serialize( def test_credential_component_serialize(
credential_component_class, schema_class, credential_component_type, key, value credential_component_class, schema_class, credential_component_type, credential_component_data
): ):
schema = schema_class() schema = schema_class()
constructed_object = credential_component_class(value) constructed_object = credential_component_class(**credential_component_data)
serialized_object = schema.dump(constructed_object) serialized_object = schema.dump(constructed_object)
assert serialized_object == build_component_dict(credential_component_type, key, value) assert serialized_object == build_component_dict(
credential_component_type, credential_component_data
)
@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES) @pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES)
def test_credential_component_deserialize( def test_credential_component_deserialize(
credential_component_class, schema_class, credential_component_type, key, value credential_component_class, schema_class, credential_component_type, credential_component_data
): ):
schema = schema_class() schema = schema_class()
credential_dict = build_component_dict(credential_component_type, key, value) credential_dict = build_component_dict(credential_component_type, credential_component_data)
expected_deserialized_object = credential_component_class(value) expected_deserialized_object = credential_component_class(**credential_component_data)
deserialized_object = credential_component_class(**schema.load(credential_dict)) deserialized_object = credential_component_class(**schema.load(credential_dict))
@ -82,9 +86,11 @@ def test_credential_component_deserialize(
@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES) @pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES)
def test_invalid_credential_type( def test_invalid_credential_type(
credential_component_class, schema_class, credential_component_type, key, value credential_component_class, schema_class, credential_component_type, credential_component_data
): ):
invalid_component_dict = build_component_dict(credential_component_type, key, value) invalid_component_dict = build_component_dict(
credential_component_type, credential_component_data
)
invalid_component_dict["credential_type"] = "INVALID" invalid_component_dict["credential_type"] = "INVALID"
schema = schema_class() schema = schema_class()
@ -94,9 +100,11 @@ def test_invalid_credential_type(
@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES) @pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES)
def test_encorrect_credential_type( def test_encorrect_credential_type(
credential_component_class, schema_class, credential_component_type, key, value credential_component_class, schema_class, credential_component_type, credential_component_data
): ):
incorrect_component_dict = build_component_dict(credential_component_type, key, value) incorrect_component_dict = build_component_dict(
credential_component_type, credential_component_data
)
incorrect_component_dict["credential_type"] = ( incorrect_component_dict["credential_type"] = (
CredentialComponentType.USERNAME.name CredentialComponentType.USERNAME.name
if credential_component_type != CredentialComponentType.USERNAME if credential_component_type != CredentialComponentType.USERNAME
@ -110,11 +118,11 @@ def test_encorrect_credential_type(
@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES) @pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES)
def test_invalid_values( def test_invalid_values(
credential_component_class, schema_class, credential_component_type, key, value credential_component_class, schema_class, credential_component_type, credential_component_data
): ):
schema = schema_class() schema = schema_class()
for invalid_value in INVALID_VALUES[credential_component_type]: for invalid_component_data in INVALID_COMPONENT_DATA[credential_component_type]:
component_dict = build_component_dict(credential_component_type, key, invalid_value) component_dict = build_component_dict(credential_component_type, invalid_component_data)
with pytest.raises(ValidationError): with pytest.raises(ValidationError):
credential_component_class(**schema.load(component_dict)) credential_component_class(**schema.load(component_dict))