From f5df6d4905b562a8ffda689e56a3d021c8f77fc4 Mon Sep 17 00:00:00 2001 From: "andrey.goncharov" Date: Tue, 27 Jun 2017 17:57:27 +0300 Subject: [PATCH 1/4] Added LimitedLengthStringField --- plenum/common/messages/fields.py | 18 +++++++++++++++ .../test_limited_length_string_field.py | 22 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 plenum/test/input_validation/fields_validation/test_limited_length_string_field.py diff --git a/plenum/common/messages/fields.py b/plenum/common/messages/fields.py index f16a6024ff..ca2a8dd5a8 100644 --- a/plenum/common/messages/fields.py +++ b/plenum/common/messages/fields.py @@ -90,6 +90,24 @@ def _specific_validation(self, val): return 'empty string' +class LimitedLengthStringField(NonEmptyStringField): + _base_types = (str,) + + def __init__(self, max_length: int, **kwargs): + if max_length < 1: + raise Exception('Incorrect validation parameters') + + super().__init__(**kwargs) + self._max_length = max_length + + def _specific_validation(self, val): + super_ret = super()._specific_validation(val) + if super_ret: + return super_ret + if len(val) > self._max_length: + return '{} is longer than {} symbols'.format(val, self._max_length) + + class SignatureField(FieldBase): _base_types = (str, type(None)) # TODO do nothing because EmptySignature should be raised somehow diff --git a/plenum/test/input_validation/fields_validation/test_limited_length_string_field.py b/plenum/test/input_validation/fields_validation/test_limited_length_string_field.py new file mode 100644 index 0000000000..b3b1d7c065 --- /dev/null +++ b/plenum/test/input_validation/fields_validation/test_limited_length_string_field.py @@ -0,0 +1,22 @@ +import pytest +from plenum.common.messages.fields import LimitedLengthStringField + + +def test_incorrect_max_length(): + with pytest.raises(Exception): + LimitedLengthStringField(max_length=0) + + +def test_empty_string(): + validator = LimitedLengthStringField(max_length=1) + assert validator.validate("") + + +def test_valid_string(): + validator = LimitedLengthStringField(max_length=1) + assert not validator.validate("x") + + +def test_long_string(): + validator = LimitedLengthStringField(max_length=1) + assert validator.validate("xx") From cd6bd5d2d326ba992e2a6abcf9e005d39bcf2bb8 Mon Sep 17 00:00:00 2001 From: "andrey.goncharov" Date: Tue, 27 Jun 2017 18:30:15 +0300 Subject: [PATCH 2/4] Code review fixes --- plenum/common/messages/fields.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/plenum/common/messages/fields.py b/plenum/common/messages/fields.py index ca2a8dd5a8..63b4640c20 100644 --- a/plenum/common/messages/fields.py +++ b/plenum/common/messages/fields.py @@ -90,21 +90,18 @@ def _specific_validation(self, val): return 'empty string' -class LimitedLengthStringField(NonEmptyStringField): +class LimitedLengthStringField(FieldBase): _base_types = (str,) def __init__(self, max_length: int, **kwargs): - if max_length < 1: - raise Exception('Incorrect validation parameters') - super().__init__(**kwargs) self._max_length = max_length def _specific_validation(self, val): - super_ret = super()._specific_validation(val) - if super_ret: - return super_ret + if not val: + return 'empty string' if len(val) > self._max_length: + val = val[100:] + '...' return '{} is longer than {} symbols'.format(val, self._max_length) From cd244bc2d6782b3a2004935e853e18584aaef6c6 Mon Sep 17 00:00:00 2001 From: "andrey.goncharov" Date: Tue, 27 Jun 2017 19:10:27 +0300 Subject: [PATCH 3/4] Code review fixes --- plenum/common/messages/fields.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plenum/common/messages/fields.py b/plenum/common/messages/fields.py index 63b4640c20..2b1e6253b2 100644 --- a/plenum/common/messages/fields.py +++ b/plenum/common/messages/fields.py @@ -94,6 +94,7 @@ class LimitedLengthStringField(FieldBase): _base_types = (str,) def __init__(self, max_length: int, **kwargs): + assert max_length > 0, 'should be greater than 0' super().__init__(**kwargs) self._max_length = max_length From d7fcdb0fb10cce1248d9222e97c7d2522afd45bc Mon Sep 17 00:00:00 2001 From: "andrey.goncharov" Date: Tue, 27 Jun 2017 19:13:29 +0300 Subject: [PATCH 4/4] Code review fixes --- plenum/common/messages/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plenum/common/messages/fields.py b/plenum/common/messages/fields.py index 2b1e6253b2..7c3237b88b 100644 --- a/plenum/common/messages/fields.py +++ b/plenum/common/messages/fields.py @@ -102,7 +102,7 @@ def _specific_validation(self, val): if not val: return 'empty string' if len(val) > self._max_length: - val = val[100:] + '...' + val = val[:100] + ('...' if len(val) > 100 else '') return '{} is longer than {} symbols'.format(val, self._max_length)