def test_option_insert_missing_ids_x(): doc_raw = { "@context": "https://www.w3.org/ns/did/v1", "authentication": [ { "type": "Ed25519VerificationKey2018", "controller": "did:example:123", "publicKeyBase58": "1234", }, ], } with pytest.raises(DIDDocumentError): DIDDocument.deserialize(doc_raw, options={DIDDocumentOption.insert_missing_ids})
def test_dereference_and_membership_check(): doc_raw = { "@context": "https://www.w3.org/ns/did/v1", "id": "did:example:123", "authentication": [ { "id": "did:example:123#keys-0", "type": "Ed25519VerificationKey2018", "controller": "did:example:123", "publicKeyBase58": "1234", }, ], "assertionMethod": [ { "id": "did:example:123#keys-0", "type": "Ed25519VerificationKey2018", "controller": "did:example:123", "publicKeyBase58": "1234", }, ], "service": [{ "id": "did:example:123#service-0", "type": "example", "serviceEndpoint": "https://example.com", }], } doc = DIDDocument.deserialize(doc_raw) vmethod = doc.dereference("did:example:123#keys-0") assert not doc.verification_method assert vmethod in doc.authentication assert vmethod in doc.assertion_method
def test_key_rotation_from_doc(): doc = DIDDocument.deserialize(DOC6) vmethod0 = doc.dereference("did:example:123#key-0") builder = DIDDocumentBuilder.from_doc(doc) builder.verification_method.remove(vmethod0) builder.authentication.remove(vmethod0.id) assert builder.build().serialize() == { "@context": ["https://www.w3.org/ns/did/v1"], "id": "did:example:123", "authentication": [ { "id": "did:example:123#auth-0", "type": "Ed25519VerificationKey2018", "controller": "did:example:123", "publicKeyBase58": "abcd", }, ], "service": [{ "id": "did:example:123#service-0", "type": "example", "serviceEndpoint": "https://example.com", }], }
def test_builder_from_doc(): doc = DIDDocument.deserialize(DOC6) builder = DIDDocumentBuilder.from_doc(doc) with builder.verification_methods.defaults() as vmethods: vmethods.add(suite=VerificationSuite("Example", "publicKeyExample"), material="1234") assert len(builder.build().serialize()["verificationMethod"]) == 2
def test_dereference_x(): doc = DIDDocument.deserialize(DOC0) with pytest.raises(InvalidDIDUrlError): doc.dereference("bogus") with pytest.raises(ResourceIDNotFound): doc.dereference("did:example:123#bogus")
def test_vmethod_relationships(): """Test checking whether a verification method is in a relationship.""" doc = DIDDocument.deserialize(DOC0) auth0: VerificationMethod = doc.dereference( DOC0["authentication"][0]["id"]) assert isinstance(auth0, VerificationMethod) assert auth0 in doc.authentication assert auth0 not in doc.assertion_method
def test_dereference(): """Test DID Doc dereferencing a URL.""" doc = DIDDocument.deserialize(DOC0) auth0: VerificationMethod = doc.dereference( DOC0["authentication"][0]["id"]) assert isinstance(auth0, VerificationMethod) assert auth0.serialize() == DOC0["authentication"][0] service0: Service = doc.dereference(DOC0["service"][0]["id"]) assert isinstance(service0, Service) assert service0.serialize() == DOC0["service"][0]
def test_builder_from_doc_remove(): doc = DIDDocument.deserialize(DOC6) builder = DIDDocumentBuilder.from_doc(doc) vmethod = builder.verification_method.add(ExampleVerificationMethod, public_key_example="1234") assert len(builder.build().serialize()["verificationMethod"]) == 2 builder.verification_method.remove(vmethod) assert len(builder.build().serialize()["verificationMethod"]) == 1 service = builder.service.add("example", "http://example.com", "ident") assert len(builder.build().serialize()["service"]) == 2 builder.service.remove(service) assert len(builder.build().serialize()["service"]) == 1 assertion = builder.assertion_method.add( ExampleVerificationMethod, ident="123", public_key_example="1234", ) assert len(builder.build().serialize()["assertionMethod"]) == 1 builder.assertion_method.remove(assertion) assert "assertionMethod" not in builder.build().serialize()
def test_builder_from_doc_remove(): doc = DIDDocument.deserialize(DOC6) builder = DIDDocumentBuilder.from_doc(doc) with builder.verification_methods.defaults() as vmethods: vmethod = vmethods.add(suite=VerificationSuite("Example", "publicKeyExample"), material="1234") assert len(builder.build().serialize()["verificationMethod"]) == 2 builder.verification_methods.remove(vmethod) assert len(builder.build().serialize()["verificationMethod"]) == 1 service = builder.services.add("example", "http://example.com", "ident") assert len(builder.build().serialize()["service"]) == 2 builder.services.remove(service) assert len(builder.build().serialize()["service"]) == 1 assertion = builder.assertion_method.add( ident="123", suite=VerificationSuite("Example", "publicKeyExample"), material="1234", ) assert len(builder.build().serialize()["assertionMethod"]) == 1 builder.assertion_method.remove(assertion) assert "assertionMethod" not in builder.build().serialize()
def test_builder_from_doc(): doc = DIDDocument.deserialize(DOC6) builder = DIDDocumentBuilder.from_doc(doc) builder.verification_method.add(ExampleVerificationMethod, public_key_example="1234") assert len(builder.build().serialize()["verificationMethod"]) == 2
def test_didcomm_service_deserialized(): """Test whether a DIDCommService is returned when deserialized.""" doc = DIDDocument.deserialize(DOC7) assert isinstance(doc.service[0], DIDCommService)
def test_extra_preserved(): """Test whether extra attributes are preserved.""" doc_raw = copy.deepcopy(DOC6) doc_raw["additionalAttribute"] = {"extra": "junk"} doc = DIDDocument.deserialize(doc_raw) assert "additionalAttribute" in doc.serialize()
def test_serialization(doc_raw): """Test serialization and deserialization.""" doc = DIDDocument.deserialize(doc_raw) assert doc.serialize() == doc_raw
def test_fails_invalid(doc): """Test invalid docs fail.""" with pytest.raises((ValueError, DIDDocumentError)): DIDDocument.deserialize(doc)
def test_validate(doc): """Test valid docs pass.""" DIDDocument.deserialize(doc)