From a23c8d3d92609d5d8e2f7c4873b3d1486cefcd01 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Sun, 22 Oct 2017 22:54:13 +0200 Subject: [PATCH] records: Validate length of TransactionSpecification.text field --- docs/changelog.rst | 1 + netsgiro/records.py | 4 ++-- tests/test_record_writing.py | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 7e3058b..cf7a400 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -18,6 +18,7 @@ v1.0.1 (UNRELEASED) - :attr:`netsgiro.records.TransactionAmountItem1.kid` (max 25 chars) - :attr:`netsgiro.records.TransactionAmountItem3.text` (max 40 chars) + - :attr:`netsgiro.records.TransactionSpecification.text` (max 40 chars) - :attr:`netsgiro.records.AvtaleGiroAgreement.kid` (max 25 chars) Previously the string was accepted and the record generated invalid OCR data. diff --git a/netsgiro/records.py b/netsgiro/records.py index 775dfae..9f4b4f2 100644 --- a/netsgiro/records.py +++ b/netsgiro/records.py @@ -5,7 +5,7 @@ from typing import Iterable, List, Optional, Sequence, Tuple, Union import attr -from attr.validators import instance_of, optional +from attr.validators import optional import netsgiro from netsgiro.converters import ( @@ -626,7 +626,7 @@ class TransactionSpecification(TransactionRecord): column_number = attr.ib(convert=int) text = attr.ib( convert=stripped_newlines(fixed_len_str(40, str)), - validator=instance_of(str)) + validator=optional(str_of_max_length(40))) RECORD_TYPE = netsgiro.RecordType.TRANSACTION_SPECIFICATION _PATTERNS = [ diff --git a/tests/test_record_writing.py b/tests/test_record_writing.py index 1ec3fd7..e359aba 100644 --- a/tests/test_record_writing.py +++ b/tests/test_record_writing.py @@ -409,6 +409,21 @@ def test_transaction_specification_text_behavior(text, expected): assert record.text == expected +def test_transaction_specification_raises_if_text_is_too_long(): + with pytest.raises(ValueError) as exc_info: + netsgiro.records.TransactionSpecification( + service_code=netsgiro.ServiceCode.AVTALEGIRO, + transaction_type=( + netsgiro.TransactionType.AVTALEGIRO_WITH_BANK_NOTIFICATION), + transaction_number=1, + line_number=1, + column_number=1, + text='Fooo' * 12, # Max 40 chars + ) + + assert 'text must be at most 40 chars' in str(exc_info) + + def test_avtalegiro_agreement(): record = netsgiro.records.AvtaleGiroAgreement( service_code=netsgiro.ServiceCode.AVTALEGIRO,