Skip to content

Commit 2d07b4c

Browse files
Validate atleast one personal data is present in a data agreement.
- Added validation function to 'personal_data' field in data agreement schema class - Refactored test case, to instantiate the data agreement in the setUp() phase. - Added unit test to make sure an exception is raise if personal data list is empty. Signed-off-by: George J Padayatti <[email protected]>
1 parent 2b83f29 commit 2d07b4c

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

mydata_did/v1_0/models/data_agreement_model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,8 @@ def validate_usage_purpose_description(self, value):
579579
# Data agreement personal data (attributes)
580580
personal_data = fields.List(
581581
fields.Nested(DataAgreementPersonalDataSchema),
582-
required=True
582+
required=True,
583+
validate=validate.Length(min=1)
583584
)
584585

585586
# Data agreement DPIA metadata

mydata_did/v1_0/models/tests/test_data_agreement_model.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
class TestDataAgreementV1Model(AsyncTestCase):
88

9-
def test_data_agreement_model(self):
10-
data_agreement_dict = {
9+
def setUp(self) -> None:
10+
self.data_agreement_dict = {
1111
"@context": "https://raw.githubusercontent.com/decentralised-dataexchange/automated-data-agreements/main/interface-specs/data-agreement-schema/v1/data-agreement-schema-context.jsonld",
1212
"data_controller_name": "XYZ Corp",
1313
"data_controller_url": "https://xyz.com",
@@ -37,7 +37,10 @@ def test_data_agreement_model(self):
3737
}
3838
}
3939

40-
data_agreement: DataAgreementV1 = DataAgreementV1Schema().load(data_agreement_dict)
40+
def test_data_agreement_model(self):
41+
42+
data_agreement: DataAgreementV1 = DataAgreementV1Schema().load(
43+
self.data_agreement_dict)
4144

4245
assert data_agreement.pii_controller_name == "XYZ Corp"
4346

@@ -46,9 +49,32 @@ def test_data_agreement_model(self):
4649
self.assertIsNone(
4750
data_agreement.personal_data[0].attribute_id, "Attribute ID should be None")
4851

49-
data_agreement_dict.pop("data_controller_name")
52+
self.data_agreement_dict.pop("data_controller_name")
5053
with self.assertRaises(ValidationError) as ctx:
51-
data_agreement: DataAgreementV1 = DataAgreementV1Schema().load(data_agreement_dict)
54+
data_agreement: DataAgreementV1 = DataAgreementV1Schema().load(
55+
self.data_agreement_dict)
5256

5357
self.assertTrue(
5458
"Missing data for required field." in str(ctx.exception))
59+
60+
def test_data_agreement_model_without_personal_data(self):
61+
self.data_agreement_dict.pop("personal_data")
62+
63+
with self.assertRaises(ValidationError) as ctx:
64+
data_agreement: DataAgreementV1 = DataAgreementV1Schema().load(
65+
self.data_agreement_dict)
66+
67+
self.assertTrue(
68+
"Missing data for required field." in str(ctx.exception)
69+
)
70+
71+
def test_data_agreement_model_with_empty_personal_data(self):
72+
self.data_agreement_dict["personal_data"] = []
73+
74+
with self.assertRaises(ValidationError) as ctx:
75+
data_agreement: DataAgreementV1 = DataAgreementV1Schema().load(
76+
self.data_agreement_dict)
77+
78+
self.assertTrue(
79+
"Shorter than minimum length 1." in str(ctx.exception)
80+
)

0 commit comments

Comments
 (0)