Skip to content

Commit

Permalink
Merge pull request #44 from epandurski/master
Browse files Browse the repository at this point in the history
Add configData field to AccountConfigSchema
  • Loading branch information
epandurski authored Sep 2, 2023
2 parents af77f49 + b176869 commit f174736
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions swpt_creditors/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
PIN_REGEX = r"^[0-9]{4,10}$"
TRANSFER_NOTE_MAX_BYTES = 500
TRANSFER_NOTE_FORMAT_REGEX = r"^[0-9A-Za-z.-]{0,8}$"
CONFIG_DATA_MAX_BYTES = 2000

CT_DIRECT = "direct"

Expand Down
29 changes: 28 additions & 1 deletion swpt_creditors/schemas/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
fields,
ValidationError,
validate,
validates,
validates_schema,
pre_dump,
post_dump,
Expand All @@ -15,7 +16,12 @@
from swpt_pythonlib.utils import i64_to_u64
from swpt_pythonlib.swpt_uris import make_account_uri
from swpt_creditors import models
from swpt_creditors.models import MIN_INT64, MAX_INT64, TRANSFER_NOTE_MAX_BYTES
from swpt_creditors.models import (
MIN_INT64,
MAX_INT64,
TRANSFER_NOTE_MAX_BYTES,
CONFIG_DATA_MAX_BYTES,
)
from .common import (
ObjectReferenceSchema,
AccountIdentitySchema,
Expand Down Expand Up @@ -835,6 +841,27 @@ class AccountConfigSchema(
example=False,
),
)
config_data = fields.String(
load_default="",
validate=validate.Length(max=CONFIG_DATA_MAX_BYTES),
data_key="configData",
metadata=dict(
description=(
"The creditor's configuration data. Different implementations"
" may use different formats for this field. Will always be"
" present in the responses from the server."
),
example="",
),
)

@validates("config_data")
def validate_config_data(self, value):
if len(value.encode("utf8")) > CONFIG_DATA_MAX_BYTES:
raise ValidationError(
"The total byte-length of the config exceeds"
f" {CONFIG_DATA_MAX_BYTES} bytes."
)

@pre_dump
def process_account_data_instance(self, obj, many):
Expand Down
1 change: 1 addition & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ def test_create_account(client, creditor):
"allowUnsafeDeletion": False,
"negligibleAmount": 1e30,
"scheduledForDeletion": False,
"configData": "",
"latestUpdateAt": latestUpdateAt,
},
"display": {
Expand Down
32 changes: 32 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ def test_serialize_account_config(app):
config_flags=models.DEFAULT_CONFIG_FLAGS
| models.AccountData.CONFIG_SCHEDULED_FOR_DELETION_FLAG,
allow_unsafe_deletion=True,
config_data="",
config_latest_update_id=1,
config_latest_update_ts=datetime(2020, 1, 1),
)
Expand All @@ -764,12 +765,14 @@ def test_serialize_account_config(app):
"negligibleAmount": 101.0,
"scheduledForDeletion": True,
"allowUnsafeDeletion": True,
"configData": "",
"latestUpdateId": 1,
"latestUpdateAt": "2020-01-01T00:00:00",
}

ac.negligible_amount = models.DEFAULT_NEGLIGIBLE_AMOUNT
ac.config_flags = 0
ac.config_data = 'TEST_CONFIG_DATA'
ac.allow_unsafe_deletion = False
assert acs.dump(ac) == {
"type": "AccountConfig",
Expand All @@ -780,6 +783,7 @@ def test_serialize_account_config(app):
"negligibleAmount": models.DEFAULT_NEGLIGIBLE_AMOUNT,
"scheduledForDeletion": False,
"allowUnsafeDeletion": False,
"configData": "TEST_CONFIG_DATA",
"latestUpdateId": 1,
"latestUpdateAt": "2020-01-01T00:00:00",
}
Expand All @@ -802,6 +806,7 @@ def test_deserialize_account_config(app):
"is_scheduled_for_deletion": True,
"allow_unsafe_deletion": False,
"latest_update_id": 2,
"config_data": "",
}

data = acs.load(
Expand All @@ -811,6 +816,7 @@ def test_deserialize_account_config(app):
"allowUnsafeDeletion": True,
"scheduledForDeletion": False,
"latestUpdateId": 2,
"configData": "TEST_CONFIG",
}
)
assert data == {
Expand All @@ -819,6 +825,7 @@ def test_deserialize_account_config(app):
"is_scheduled_for_deletion": False,
"allow_unsafe_deletion": True,
"latest_update_id": 2,
"config_data": "TEST_CONFIG",
}

with pytest.raises(ValidationError, match="Invalid type."):
Expand Down Expand Up @@ -878,6 +885,31 @@ def test_deserialize_account_config(app):
}"""
)

with pytest.raises(ValidationError, match="Longer than maximum length"):
acs.loads(
"""{
"negligibleAmount": 1e10,
"allowUnsafeDeletion": true,
"scheduledForDeletion": false,
"latestUpdateId": 1,
"configData": "%s"
}""" % (2001 * 'x')
)

with pytest.raises(
ValidationError,
match="The total byte-length of the config exceeds",
):
acs.loads(
"""{
"negligibleAmount": 1e10,
"allowUnsafeDeletion": true,
"scheduledForDeletion": false,
"latestUpdateId": 1,
"configData": "%s"
}""" % (1001 * 'Щ')
)


def test_serialize_account_info(app):
ad = models.AccountData(
Expand Down

0 comments on commit f174736

Please sign in to comment.