Skip to content

Commit

Permalink
feat: Add maximum due date validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrelg committed May 2, 2022
1 parent 99c0ed2 commit 460fffa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions netsgiro/objects.py
Expand Up @@ -30,7 +30,7 @@
TransmissionStart,
)
from netsgiro.records import parse as records_parse
from netsgiro.validators import str_of_length, validate_minimum_date
from netsgiro.validators import str_of_length, validate_due_date

if TYPE_CHECKING:
from netsgiro.enums import AvtaleGiroRegistrationType
Expand Down Expand Up @@ -382,7 +382,7 @@ def add_payment_request(

if validate_due_date:
# Make sure we're not passing invalid due dates
validate_minimum_date(due_date)
validate_due_date(due_date)

if bank_notification:
transaction_type = TransactionType.AVTALEGIRO_WITH_BANK_NOTIFICATION
Expand Down
15 changes: 12 additions & 3 deletions netsgiro/validators.py
@@ -1,5 +1,5 @@
"""Custom validators for :mod:`attrs`."""
from datetime import datetime
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any, Callable

from attr import Attribute
Expand Down Expand Up @@ -40,11 +40,20 @@ def validator(instance: object, attribute: Attribute, value: Any) -> None:
return validator


def validate_minimum_date(value: 'date') -> None:
def validate_due_date(value: 'date') -> None:
"""Make sure payment request dates are gt the minimum allowed date."""
if value < get_minimum_due_date(now=datetime.now(tz=OSLO_TZ)):
now = datetime.now(tz=OSLO_TZ)

if value < get_minimum_due_date(now=now):
raise ValueError(
'The minimum due date of a transaction is today + 4 calendar days.'
' OCR files with due dates earlier than this will be rejected when'
' submitted.'
)

if value > (now + timedelta(days=365)).date():
raise ValueError(
'The maximum due date of a transaction is 12 months in the future.'
' OCR files with due dates later than this will be rejected when'
' submitted.'
)
18 changes: 14 additions & 4 deletions tests/test_utils.py
@@ -1,8 +1,10 @@
from datetime import datetime, timedelta

import holidays
import pytest

from netsgiro.utils import OSLO_TZ, get_minimum_due_date
from netsgiro.validators import validate_due_date

monday = datetime(2022, 3, 28, 13, 59, tzinfo=OSLO_TZ)

Expand All @@ -24,8 +26,8 @@ def test_minimum_due_date_after_cutoff():
generated after 14:00 Norwegian time should be adjusted by 5 days.
"""
assert (
get_minimum_due_date(monday_after_cutoff)
== (monday_after_cutoff + timedelta(days=5)).date()
get_minimum_due_date(monday_after_cutoff)
== (monday_after_cutoff + timedelta(days=5)).date()
)


Expand All @@ -49,8 +51,8 @@ def test_minimum_due_date_with_holidays_after_cutoff():
holidays is upped to 3, so we expect 2 more days of offsetting.
"""
assert (
get_minimum_due_date(day_before_easter_after_cutoff)
== (day_before_easter_after_cutoff + timedelta(days=8)).date()
get_minimum_due_date(day_before_easter_after_cutoff)
== (day_before_easter_after_cutoff + timedelta(days=8)).date()
)


Expand All @@ -70,3 +72,11 @@ def test_minimum_due_date_without_holiday_dependency():
sys.modules['holidays'] = None
assert get_minimum_due_date(monday) == (monday + timedelta(days=4)).date()
sys.modules['holidays'] = holidays


def test_validate_due_date():
with pytest.raises(ValueError, match='The minimum due date of a transaction'):
validate_due_date(datetime.now().date())

with pytest.raises(ValueError, match='The maximum due date of a transaction'):
validate_due_date((datetime.now() + timedelta(days=366)).date())

0 comments on commit 460fffa

Please sign in to comment.