From 031fce9fd8b661caebbce2c7d507536a94f23e42 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 6 Jul 2022 11:50:54 -0400 Subject: [PATCH] UT: Add test_invalid_values() --- ...test_single_value_credential_components.py | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/monkey/tests/unit_tests/common/credentials/test_single_value_credential_components.py b/monkey/tests/unit_tests/common/credentials/test_single_value_credential_components.py index 2c138f9d6..51d02ae04 100644 --- a/monkey/tests/unit_tests/common/credentials/test_single_value_credential_components.py +++ b/monkey/tests/unit_tests/common/credentials/test_single_value_credential_components.py @@ -15,7 +15,14 @@ PARAMETRIZED_PARAMETER_VALUES = [ ] -def build_credential_dict(credential_component_type: CredentialComponentType, key: str, value: str): + + +INVALID_VALUES = { + CredentialComponentType.USERNAME: (None, 1, 2.0), + CredentialComponentType.PASSWORD: (None, 1, 2.0), +} + +def build_component_dict(credential_component_type: CredentialComponentType, key: str, value: str): return {"credential_type": credential_component_type.name, key: value} @@ -30,7 +37,7 @@ def test_credential_component_serialize( serialized_object = schema.dump(constructed_object) - assert serialized_object == build_credential_dict(credential_component_type, key, value) + assert serialized_object == build_component_dict(credential_component_type, key, value) @pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES) @@ -38,7 +45,7 @@ def test_credential_component_deserialize( credential_component_class, schema_class, credential_component_type, key, value ): schema = schema_class() - credential_dict = build_credential_dict(credential_component_type, key, value) + credential_dict = build_component_dict(credential_component_type, key, value) expected_deserialized_object = credential_component_class(value) deserialized_object = credential_component_class(**schema.load(credential_dict)) @@ -50,7 +57,7 @@ def test_credential_component_deserialize( def test_invalid_credential_type( credential_component_class, schema_class, credential_component_type, key, value ): - invalid_component_dict = build_credential_dict(credential_component_type, key, value) + invalid_component_dict = build_component_dict(credential_component_type, key, value) invalid_component_dict["credential_type"] = "INVALID" schema = schema_class() @@ -62,7 +69,7 @@ def test_invalid_credential_type( def test_encorrect_credential_type( credential_component_class, schema_class, credential_component_type, key, value ): - incorrect_component_dict = build_credential_dict(credential_component_type, key, value) + incorrect_component_dict = build_component_dict(credential_component_type, key, value) incorrect_component_dict["credential_type"] = ( CredentialComponentType.USERNAME.name if credential_component_type != CredentialComponentType.USERNAME @@ -72,3 +79,14 @@ def test_encorrect_credential_type( with pytest.raises(ValidationError): credential_component_class(**schema.load(incorrect_component_dict)) + +@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES) +def test_invalid_values( + credential_component_class, schema_class, credential_component_type, key, value +): + schema = schema_class() + + for invalid_value in INVALID_VALUES[credential_component_type]: + component_dict = build_component_dict(credential_component_type, key, invalid_value) + with pytest.raises(ValidationError): + credential_component_class(**schema.load(component_dict))