forked from p15670423/monkey
UT: Use parametrization to remove duplicate credential component tests
This commit is contained in:
parent
9a45d777ca
commit
be9889c9d1
|
@ -1,49 +0,0 @@
|
|||
from copy import deepcopy
|
||||
|
||||
import pytest
|
||||
from marshmallow.exceptions import ValidationError
|
||||
|
||||
from common.credentials import CredentialComponentType, Password
|
||||
from common.credentials.password import PasswordSchema
|
||||
|
||||
PASSWORD_VALUE = "123456"
|
||||
PASSWORD_DICT = {
|
||||
"credential_type": CredentialComponentType.PASSWORD.name,
|
||||
"password": PASSWORD_VALUE,
|
||||
}
|
||||
|
||||
|
||||
def test_password_serialize():
|
||||
schema = PasswordSchema()
|
||||
password = Password(PASSWORD_VALUE)
|
||||
|
||||
serialized_password = schema.dump(password)
|
||||
|
||||
assert serialized_password == PASSWORD_DICT
|
||||
|
||||
|
||||
def test_password_deserialize():
|
||||
schema = PasswordSchema()
|
||||
|
||||
password = Password(**schema.load(PASSWORD_DICT))
|
||||
|
||||
assert password.credential_type == CredentialComponentType.PASSWORD
|
||||
assert password.password == PASSWORD_VALUE
|
||||
|
||||
|
||||
def test_invalid_credential_type():
|
||||
invalid_password_dict = deepcopy(PASSWORD_DICT)
|
||||
invalid_password_dict["credential_type"] = "INVALID"
|
||||
schema = PasswordSchema()
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
Password(**schema.load(invalid_password_dict))
|
||||
|
||||
|
||||
def test_incorrect_credential_type():
|
||||
invalid_password_dict = deepcopy(PASSWORD_DICT)
|
||||
invalid_password_dict["credential_type"] = CredentialComponentType.USERNAME.name
|
||||
schema = PasswordSchema()
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
Password(**schema.load(invalid_password_dict))
|
|
@ -0,0 +1,74 @@
|
|||
import pytest
|
||||
from marshmallow.exceptions import ValidationError
|
||||
|
||||
from common.credentials import CredentialComponentType, Password, Username
|
||||
from common.credentials.password import PasswordSchema
|
||||
from common.credentials.username import UsernameSchema
|
||||
|
||||
PARAMETRIZED_PARAMETER_NAMES = (
|
||||
"credential_component_class, schema_class, credential_component_type, key, value"
|
||||
)
|
||||
|
||||
PARAMETRIZED_PARAMETER_VALUES = [
|
||||
(Password, PasswordSchema, CredentialComponentType.PASSWORD, "password", "123456"),
|
||||
(Username, UsernameSchema, CredentialComponentType.USERNAME, "username", "test_user"),
|
||||
]
|
||||
|
||||
|
||||
def build_credential_dict(credential_component_type: CredentialComponentType, key: str, value: str):
|
||||
return {"credential_type": credential_component_type.name, key: value}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES)
|
||||
def test_credential_component_serialize(
|
||||
credential_component_class, schema_class, credential_component_type, key, value
|
||||
):
|
||||
schema = schema_class()
|
||||
print(type(schema))
|
||||
constructed_object = credential_component_class(value)
|
||||
print(type(constructed_object))
|
||||
|
||||
serialized_object = schema.dump(constructed_object)
|
||||
|
||||
assert serialized_object == build_credential_dict(credential_component_type, key, value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES)
|
||||
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)
|
||||
expected_deserialized_object = credential_component_class(value)
|
||||
|
||||
deserialized_object = credential_component_class(**schema.load(credential_dict))
|
||||
|
||||
assert deserialized_object == expected_deserialized_object
|
||||
|
||||
|
||||
@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES)
|
||||
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["credential_type"] = "INVALID"
|
||||
schema = schema_class()
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
credential_component_class(**schema.load(invalid_component_dict))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(PARAMETRIZED_PARAMETER_NAMES, PARAMETRIZED_PARAMETER_VALUES)
|
||||
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["credential_type"] = (
|
||||
CredentialComponentType.USERNAME.name
|
||||
if credential_component_type != CredentialComponentType.USERNAME
|
||||
else CredentialComponentType.PASSWORD
|
||||
)
|
||||
schema = schema_class()
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
credential_component_class(**schema.load(incorrect_component_dict))
|
|
@ -1,49 +0,0 @@
|
|||
from copy import deepcopy
|
||||
|
||||
import pytest
|
||||
from marshmallow.exceptions import ValidationError
|
||||
|
||||
from common.credentials import CredentialComponentType, Username
|
||||
from common.credentials.username import UsernameSchema
|
||||
|
||||
USERNAME_VALUE = "test_user"
|
||||
USERNAME_DICT = {
|
||||
"credential_type": CredentialComponentType.USERNAME.name,
|
||||
"username": USERNAME_VALUE,
|
||||
}
|
||||
|
||||
|
||||
def test_username_serialize():
|
||||
schema = UsernameSchema()
|
||||
username = Username(USERNAME_VALUE)
|
||||
|
||||
serialized_username = schema.dump(username)
|
||||
|
||||
assert serialized_username == USERNAME_DICT
|
||||
|
||||
|
||||
def test_username_deserialize():
|
||||
schema = UsernameSchema()
|
||||
|
||||
username = Username(**schema.load(USERNAME_DICT))
|
||||
|
||||
assert username.credential_type == CredentialComponentType.USERNAME
|
||||
assert username.username == USERNAME_VALUE
|
||||
|
||||
|
||||
def test_invalid_credential_type():
|
||||
invalid_username_dict = deepcopy(USERNAME_DICT)
|
||||
invalid_username_dict["credential_type"] = "INVALID"
|
||||
schema = UsernameSchema()
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
Username(**schema.load(invalid_username_dict))
|
||||
|
||||
|
||||
def test_incorrect_credential_type():
|
||||
invalid_username_dict = deepcopy(USERNAME_DICT)
|
||||
invalid_username_dict["credential_type"] = CredentialComponentType.PASSWORD.name
|
||||
schema = UsernameSchema()
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
Username(**schema.load(invalid_username_dict))
|
Loading…
Reference in New Issue