Skip to content

Commit

Permalink
records: Validate length of TransactionSpecification.text field
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Oct 22, 2017
1 parent 43534c9 commit a23c8d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions netsgiro/records.py
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 = [
Expand Down
15 changes: 15 additions & 0 deletions tests/test_record_writing.py
Expand Up @@ -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,
Expand Down

0 comments on commit a23c8d3

Please sign in to comment.