-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_did_doc.py
413 lines (362 loc) · 15 KB
/
test_did_doc.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import json
import pytest
from peerdid.core.did_doc_types import (
VerificationMethodField,
DIDCommServicePeerDID,
)
from peerdid.did_doc import DIDDocPeerDID
from peerdid.errors import MalformedPeerDIDDocError
from peerdid.types import (
VerificationMaterialFormatPeerDID,
VerificationMethodTypeAuthentication,
VerificationMethodTypeAgreement,
)
from tests.test_vectors import (
DID_DOC_NUMALGO_O_BASE58,
DID_DOC_NUMALGO_O_MULTIBASE,
DID_DOC_NUMALGO_O_JWK,
DID_DOC_NUMALGO_2_BASE58,
DID_DOC_NUMALGO_2_MULTIBASE,
DID_DOC_NUMALGO_2_JWK,
PEER_DID_NUMALGO_0,
PEER_DID_NUMALGO_2,
DID_DOC_NUMALGO_2_MULTIBASE_2_SERVICES,
PEER_DID_NUMALGO_2_2_SERVICES,
DID_DOC_NUMALGO_2_MULTIBASE_NO_SERVICES,
PEER_DID_NUMALGO_2_NO_SERVICES,
PEER_DID_NUMALGO_2_MINIMAL_SERVICES,
DID_DOC_NUMALGO_2_MULTIBASE_MINIMAL_SERVICES,
)
@pytest.mark.parametrize(
"did_doc_json, expected_format, expected_field, expected_type",
[
pytest.param(
DID_DOC_NUMALGO_O_BASE58,
VerificationMaterialFormatPeerDID.BASE58,
VerificationMethodField.BASE58,
VerificationMethodTypeAuthentication.ED25519_VERIFICATION_KEY_2018,
id="numalgo_0_base58",
),
pytest.param(
DID_DOC_NUMALGO_O_MULTIBASE,
VerificationMaterialFormatPeerDID.MULTIBASE,
VerificationMethodField.MULTIBASE,
VerificationMethodTypeAuthentication.ED25519_VERIFICATION_KEY_2020,
id="numalgo_0_multibase",
),
pytest.param(
DID_DOC_NUMALGO_O_JWK,
VerificationMaterialFormatPeerDID.JWK,
VerificationMethodField.JWK,
VerificationMethodTypeAuthentication.JSON_WEB_KEY_2020,
id="numalgo_0_jwk",
),
],
)
def test_did_doc_from_json_numalgo_0(
did_doc_json, expected_format, expected_field, expected_type
):
did_doc = DIDDocPeerDID.from_json(did_doc_json)
assert isinstance(did_doc, DIDDocPeerDID)
assert did_doc.did == PEER_DID_NUMALGO_0
assert did_doc.key_agreement == []
assert did_doc.service is None
assert len(did_doc.authentication) == 1
auth = did_doc.authentication[0]
expected_auth = json.loads(did_doc_json)["authentication"][0]
assert auth.id == expected_auth["id"]
assert auth.controller == PEER_DID_NUMALGO_0
assert auth.ver_material.format == expected_format
assert auth.ver_material.type == expected_type
assert auth.ver_material.value == expected_auth[expected_field.value]
assert did_doc.auth_kids == [expected_auth["id"]]
assert did_doc.agreement_kids == []
@pytest.mark.parametrize(
"did_doc_json, expected_format, expected_field, expected_auth_type, expected_agreem_type",
[
pytest.param(
DID_DOC_NUMALGO_2_BASE58,
VerificationMaterialFormatPeerDID.BASE58,
VerificationMethodField.BASE58,
VerificationMethodTypeAuthentication.ED25519_VERIFICATION_KEY_2018,
VerificationMethodTypeAgreement.X25519_KEY_AGREEMENT_KEY_2019,
id="numalgo_2_base58",
),
pytest.param(
DID_DOC_NUMALGO_2_MULTIBASE,
VerificationMaterialFormatPeerDID.MULTIBASE,
VerificationMethodField.MULTIBASE,
VerificationMethodTypeAuthentication.ED25519_VERIFICATION_KEY_2020,
VerificationMethodTypeAgreement.X25519_KEY_AGREEMENT_KEY_2020,
id="numalgo_2_multibase",
),
pytest.param(
DID_DOC_NUMALGO_2_JWK,
VerificationMaterialFormatPeerDID.JWK,
VerificationMethodField.JWK,
VerificationMethodTypeAuthentication.JSON_WEB_KEY_2020,
VerificationMethodTypeAgreement.JSON_WEB_KEY_2020,
id="numalgo_2_jwk",
),
],
)
def test_did_doc_from_json_numalgo_2(
did_doc_json,
expected_format,
expected_field,
expected_auth_type,
expected_agreem_type,
):
did_doc = DIDDocPeerDID.from_json(did_doc_json)
assert isinstance(did_doc, DIDDocPeerDID)
assert did_doc.did == PEER_DID_NUMALGO_2
assert len(did_doc.key_agreement) == 1
assert len(did_doc.authentication) == 2
auth = did_doc.authentication[0]
expected_auth = json.loads(did_doc_json)["authentication"][0]
assert auth.id == expected_auth["id"]
assert auth.controller == PEER_DID_NUMALGO_2
assert auth.ver_material.format == expected_format
assert auth.ver_material.type == expected_auth_type
assert auth.ver_material.value == expected_auth[expected_field.value]
agreement = did_doc.key_agreement[0]
expected_agreement = json.loads(did_doc_json)["keyAgreement"][0]
assert agreement.id == expected_agreement["id"]
assert agreement.controller == PEER_DID_NUMALGO_2
assert agreement.ver_material.format == expected_format
assert agreement.ver_material.type == expected_agreem_type
assert agreement.ver_material.value == expected_agreement[expected_field.value]
services = did_doc.service
expected_service = json.loads(did_doc_json)["service"][0]
assert services is not None
assert len(services) == 1
service = services[0]
assert isinstance(service, DIDCommServicePeerDID)
assert service.id == expected_service["id"]
assert service.service_endpoint == expected_service["serviceEndpoint"]
assert service.routing_keys == expected_service["routingKeys"]
assert service.accept == expected_service["accept"]
assert did_doc.auth_kids == [
v["id"] for v in json.loads(did_doc_json)["authentication"]
]
assert did_doc.agreement_kids == [expected_agreement["id"]]
def test_did_doc_from_json_numalgo_2_service_2_elements():
did_doc = DIDDocPeerDID.from_json(DID_DOC_NUMALGO_2_MULTIBASE_2_SERVICES)
assert isinstance(did_doc, DIDDocPeerDID)
assert did_doc.did == PEER_DID_NUMALGO_2_2_SERVICES
services = did_doc.service
assert services is not None
assert len(services) == 2
service_1 = services[0]
expected_service_1 = json.loads(DID_DOC_NUMALGO_2_MULTIBASE_2_SERVICES)["service"][
0
]
assert isinstance(service_1, DIDCommServicePeerDID)
assert service_1.id == expected_service_1["id"]
assert service_1.service_endpoint == expected_service_1["serviceEndpoint"]
assert service_1.routing_keys == expected_service_1["routingKeys"]
assert service_1.accept == []
service_2 = services[1]
expected_service_2 = json.loads(DID_DOC_NUMALGO_2_MULTIBASE_2_SERVICES)["service"][
1
]
assert isinstance(service_2, dict)
assert service_2 == expected_service_2
def test_did_doc_from_json_numalgo_2_no_service():
did_doc = DIDDocPeerDID.from_json(DID_DOC_NUMALGO_2_MULTIBASE_NO_SERVICES)
assert isinstance(did_doc, DIDDocPeerDID)
assert did_doc.did == PEER_DID_NUMALGO_2_NO_SERVICES
assert len(did_doc.key_agreement) == 1
assert len(did_doc.authentication) == 1
assert did_doc.service is None
def test_did_doc_from_json_numalgo_2_minimal_service():
did_doc = DIDDocPeerDID.from_json(DID_DOC_NUMALGO_2_MULTIBASE_MINIMAL_SERVICES)
assert isinstance(did_doc, DIDDocPeerDID)
assert did_doc.did == PEER_DID_NUMALGO_2_MINIMAL_SERVICES
assert len(did_doc.key_agreement) == 1
assert len(did_doc.authentication) == 2
assert did_doc.service is not None
assert len(did_doc.service) == 1
service = did_doc.service[0]
expected_service = json.loads(DID_DOC_NUMALGO_2_MULTIBASE_MINIMAL_SERVICES)[
"service"
][0]
assert isinstance(service, DIDCommServicePeerDID)
assert service.id == expected_service["id"]
assert service.service_endpoint == expected_service["serviceEndpoint"]
assert service.routing_keys == []
assert service.accept == []
def test_did_doc_id_only():
didDoc = DIDDocPeerDID.from_json(
"""
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V"
}
"""
)
assert didDoc.did == "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V"
def test_did_doc_invalid_json():
with pytest.raises(
MalformedPeerDIDDocError,
match=r"Invalid peer DID Doc.*Invalid JSON",
):
DIDDocPeerDID.from_json(
"""
sdfasdfsf{sdfsdfasdf...
"""
)
def test_did_doc_from_invalid_json_no_id():
with pytest.raises(
MalformedPeerDIDDocError,
match=r"Invalid peer DID Doc.*No 'id' field",
):
DIDDocPeerDID.from_json(
"""
{
"authentication": [
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"type": "Ed25519VerificationKey2020",
"controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V"
}
]
}
"""
)
def test_did_doc_from_invalid_ver_method_no_id():
with pytest.raises(
MalformedPeerDIDDocError,
match=r"Invalid peer DID Doc.*No 'id' field in method",
):
DIDDocPeerDID.from_json(
"""
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"authentication": [
{
"type": "Ed25519VerificationKey2020",
"controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V"
}
]
}
"""
)
def test_did_doc_from_invalid_ver_method_no_type():
with pytest.raises(
MalformedPeerDIDDocError,
match=r"Invalid peer DID Doc.*No 'type' field in method",
):
DIDDocPeerDID.from_json(
"""
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"authentication": [
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V"
}
]
}
"""
)
def test_did_doc_from_invalid_ver_method_no_controller():
with pytest.raises(
MalformedPeerDIDDocError,
match=r"Invalid peer DID Doc.*No 'controller' field in method",
):
DIDDocPeerDID.from_json(
"""
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"authentication": [
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"type": "Ed25519VerificationKey2020",
"publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V"
}
]
}
"""
)
def test_did_doc_from_invalid_ver_method_no_value():
with pytest.raises(
MalformedPeerDIDDocError,
match=r"Invalid peer DID Doc.*No public key field",
):
DIDDocPeerDID.from_json(
"""
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"authentication": [
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"type": "Ed25519VerificationKey2020",
"controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V"
}
]
}
"""
)
def test_did_doc_from_invalid_ver_method_invalid_type():
with pytest.raises(
MalformedPeerDIDDocError,
match=r"Invalid peer DID Doc.*Unknown verification method type",
):
DIDDocPeerDID.from_json(
"""
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"authentication": [
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"type": "Unkknown",
"controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"publicKeyMultibase": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V"
}
]
}
"""
)
def test_did_doc_from_invalid_ver_method_invalid_field():
with pytest.raises(
MalformedPeerDIDDocError,
match=r"Invalid peer DID Doc.*No public key field",
):
DIDDocPeerDID.from_json(
"""
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"authentication": [
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"type": "Ed25519VerificationKey2020",
"controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"publicKeyJwk": "z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V"
}
]
}
"""
)
def test_did_doc_from_invalid_ver_method_invalid_value_jwk():
with pytest.raises(
MalformedPeerDIDDocError,
match=r"Invalid peer DID Doc.*No 'crv' field in JWK",
):
DIDDocPeerDID.from_json(
"""
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"authentication": [
{
"id": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V#6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"type": "JsonWebKey2020",
"controller": "did:peer:0z6MkqRYqQiSgvZQdnBytw86Qbs2ZWUkGv22od935YF4s8M7V",
"publicKeyJwk": "sdfsdf{sfsdfdf"
}
]
}
"""
)