Skip to content

Commit

Permalink
Merge pull request #304 from blockchain-certificates/feat/vc-v2-name-…
Browse files Browse the repository at this point in the history
…description

Support credential subject as array
  • Loading branch information
lemoustachiste authored Nov 7, 2024
2 parents 023d197 + cd605bd commit deafa4a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cert_issuer/models/verifiable_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ def validate_credential_subject (credential_subject, credential_schema):
if not isinstance(credential_schema, list):
credential_schema = [credential_schema]

if not isinstance(credential_subject, list):
credential_subject = [credential_subject]

for schema in credential_schema:
schema_url = schema['id']
local_filename, headers = urlretrieve(schema_url)
with open(local_filename) as f:
schema = json.load(f)
jsonschema_validate(credential_subject, schema)
for subject in credential_subject:
jsonschema_validate(subject, schema)
pass

def validate_issuer (certificate_issuer):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cert-core>=3.0.0
cert-schema>=3.6.0
cert-schema>=3.7.2
merkletools==1.0.3
configargparse==0.13.0
glob2==0.6
Expand Down
100 changes: 100 additions & 0 deletions tests/v3_certificate_validation/test_unit_credential_subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
from cert_issuer.models.verifiable_credential import validate_credential_subject

class UnitValidationV3 (unittest.TestCase):
def test_null (self):
credential_subject = None

# TODO: ideally this would be stubbed to have better control over the test
# TODO: however urlretrieve and consumption is a bit convoluted to mock
credential_schema = {
"id": "https://www.blockcerts.org/samples/3.0/example-id-card-schema.json",
"type": "JsonSchema"
}

try:
validate_credential_subject(credential_subject, credential_schema)
except:
assert True
return

assert False

def test_conforms_to_schema (self):
credential_subject = {
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
Expand All @@ -29,6 +47,88 @@ def test_conforms_to_schema (self):
return

assert True

def test_array_conforms_to_schema (self):
credential_subject = [
{
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
"language": "en",
"name": "John Smith",
"nationality": "Canada",
"DOB": "05/10/1983",
"height": "1.80m",
"residentialAddressStreet": "6 Maple Tree street",
"residentialAddressTown": "Toronto",
"residentialAddressPostCode": "YYZYUL"
},
{
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
"language": "fr",
"name": "Jean Forgeron",
"nationality": "Canada",
"DOB": "05/10/1983",
"height": "1.80m",
"residentialAddressStreet": "6 rue des Érables",
"residentialAddressTown": "Montréal",
"residentialAddressPostCode": "YYZYUL"
}
]

# TODO: ideally this would be stubbed to have better control over the test
# TODO: however urlretrieve and consumption is a bit convoluted to mock
credential_schema = {
"id": "https://www.blockcerts.org/samples/3.0/example-id-card-schema.json",
"type": "JsonSchema"
}

try:
validate_credential_subject(credential_subject, credential_schema)
except:
assert False
return

assert True

def test_array_does_not_conform_to_schema (self):
credential_subject = [
{
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
"language": "en",
"name": "John Smith",
"nationality": "Canada",
"DOB": "05/10/1983",
"height": "1.80m",
"residentialAddressStreet": "6 Maple Tree street",
"residentialAddressTown": "Toronto",
"residentialAddressPostCode": "YYZYUL"
},
{
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
"language": "fr",
"name": "Jean Forgeron",
"DOB": "05/10/1983",
"height": "1.80m",
"residentialAddressStreet": "6 rue des Érables",
"residentialAddressTown": "Montréal",
"residentialAddressPostCode": "YYZYUL"
}
]

# TODO: ideally this would be stubbed to have better control over the test
# TODO: however urlretrieve and consumption is a bit convoluted to mock
credential_schema = {
"id": "https://www.blockcerts.org/samples/3.0/example-id-card-schema.json",
"type": "JsonSchema"
}

try:
validate_credential_subject(credential_subject, credential_schema)
except:
assert True
return

assert False

def test_does_not_conform_to_schema (self):
credential_subject = {
"id": "did:example:ebfeb1f712ebc6f1c276e12ec21",
Expand Down

0 comments on commit deafa4a

Please sign in to comment.