Esempio n. 1
0
class TemporaryExposureKeySchema(Schema):
    """
    Validate and deserialize the raw data into the corresponding TemporaryExposureKey object.
    """

    key_data = Base64String(required=True, length=16)
    rolling_start_number = fields.Integer(required=True, validate=Range(min=0, max=sys.maxsize))
    rolling_period = fields.Integer(
        required=False, validate=Range(min=0, max=config.MAX_ROLLING_PERIOD)
    )

    @post_load
    def create_tek(  # pylint: disable=no-self-use
        self, data: Dict, **kwargs: Any
    ) -> TemporaryExposureKey:
        """
        Return the TemporaryExposureKey object associated with the deserialized data.

        :param data: the deserialized data.
        :param kwargs: the additional unused arguments coming from the decorator.
        :return: the TemporaryExposureKey object associated with the given data.
        """
        return TemporaryExposureKey(**data)
def test_base64_string_length(value: str) -> None:
    with raises(ValidationError):
        Base64String(min_encoded_length=1,
                     max_encoded_length=5).validate("123456")  # type:ignore
def test_base64_string_min_length() -> None:
    with raises(ValidationError):
        Base64String(min_encoded_length=5).validate("1234")  # type:ignore
def test_base64_string_failure_on_init() -> None:
    with raises(ValueError):
        Base64String(validate=lambda value: print("dummy"))
def test_base64_string_failure_on_value(length: Optional[int],
                                        value: str) -> None:
    with raises(ValidationError):
        Base64String(length=length).validate(value)  # type:ignore
def test_base64_string_success(value: str) -> None:
    bas64encoded = base64.b64encode(value.encode("utf-8"))

    Base64String().validate(bas64encoded)  # type:ignore
    Base64String(length=len(value)).validate(bas64encoded)  # type:ignore