forked from IdentityPython/pyMDOC-CBOR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_09_errors_field.py
More file actions
147 lines (118 loc) · 3.75 KB
/
test_09_errors_field.py
File metadata and controls
147 lines (118 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Test support for the 'errors' field in MobileDocument.
ISO 18013-5 specifies that when status != 0, documents may contain
an 'errors' field describing which elements were not available.
"""
import cbor2
from pymdoccbor.mdoc.issuer import MdocCborIssuer
from pymdoccbor.mdoc.verifier import MobileDocument
from pymdoccbor.tests.cert_data import CERT_DATA
from pymdoccbor.tests.micov_data import MICOV_DATA
from pymdoccbor.tests.pkey import PKEY
def test_mobile_document_with_errors_field():
"""Test that MobileDocument accepts an 'errors' field."""
mdoc = MdocCborIssuer(
private_key=PKEY,
alg="ES256",
cert_info=CERT_DATA
)
mdoc.new(
data=MICOV_DATA,
doctype="org.micov.medical.1",
validity={
"issuance_date": "2024-12-31",
"expiry_date": "2050-12-31"
},
)
document = mdoc.signed["documents"][0]
# Add errors field (simulating status 20 - elements not present)
document['errors'] = {
'org.micov.medical.1': {
'missing_element': 1 # Error code for element not present
}
}
# Should not raise TypeError
doc = MobileDocument(**document)
assert doc.doctype == "org.micov.medical.1"
assert doc.errors is not None
assert isinstance(doc.errors, dict)
def test_mobile_document_without_errors_field():
"""Test that MobileDocument works without 'errors' field (backward compatibility)."""
mdoc = MdocCborIssuer(
private_key=PKEY,
alg="ES256",
cert_info=CERT_DATA
)
mdoc.new(
data=MICOV_DATA,
doctype="org.micov.medical.1",
validity={
"issuance_date": "2024-12-31",
"expiry_date": "2050-12-31"
},
)
document = mdoc.signed["documents"][0]
# No errors field
doc = MobileDocument(**document)
assert doc.doctype == "org.micov.medical.1"
assert doc.errors == {} # Should default to empty dict
def test_mobile_document_dump_with_errors():
"""Test that dump() includes errors field when present."""
mdoc = MdocCborIssuer(
private_key=PKEY,
alg="ES256",
cert_info=CERT_DATA
)
mdoc.new(
data=MICOV_DATA,
doctype="org.micov.medical.1",
validity={
"issuance_date": "2024-12-31",
"expiry_date": "2050-12-31"
},
)
document = mdoc.signed["documents"][0]
# Add errors field
errors_data = {
'org.micov.medical.1': {
'missing_element': 1
}
}
document['errors'] = errors_data
doc = MobileDocument(**document)
dump = doc.dump()
assert dump
assert isinstance(dump, bytes)
# Decode and verify errors field is present
decoded = cbor2.loads(dump)
# The dump is wrapped in a CBORTag, so we need to access .value
if hasattr(decoded, 'value'):
decoded = decoded.value
assert 'errors' in decoded
assert decoded['errors'] == errors_data
def test_mobile_document_dump_without_errors():
"""Test that dump() works without errors field (backward compatibility)."""
mdoc = MdocCborIssuer(
private_key=PKEY,
alg="ES256",
cert_info=CERT_DATA
)
mdoc.new(
data=MICOV_DATA,
doctype="org.micov.medical.1",
validity={
"issuance_date": "2024-12-31",
"expiry_date": "2050-12-31"
},
)
document = mdoc.signed["documents"][0]
doc = MobileDocument(**document)
dump = doc.dump()
assert dump
assert isinstance(dump, bytes)
# Decode and verify errors field is NOT present
decoded = cbor2.loads(dump)
if hasattr(decoded, 'value'):
decoded = decoded.value
# errors field should not be in dump if it's empty
assert 'errors' not in decoded