import pytest
from zap_schema.identity import (
Did,
DidDocument,
DidMethod,
VerificationMethod,
VerificationMethodType,
parse_did,
create_did_from_key,
create_did_from_web,
base58_encode,
base58_decode,
IdentityError,
)
class TestDid:
def test_create_lux_did(self):
did = Did(method=DidMethod.LUX, id="z6MkTest123")
assert did.method == DidMethod.LUX
assert did.id == "z6MkTest123"
def test_create_key_did(self):
did = Did(method=DidMethod.KEY, id="z6MkTestKey456")
assert did.method == DidMethod.KEY
assert did.id == "z6MkTestKey456"
def test_create_web_did(self):
did = Did(method=DidMethod.WEB, id="example.com:user:alice")
assert did.method == DidMethod.WEB
assert did.id == "example.com:user:alice"
def test_did_uri(self):
did = Did(method=DidMethod.LUX, id="z6MkTest123")
assert did.uri() == "did:lux:z6MkTest123"
def test_did_equality(self):
did1 = Did(method=DidMethod.LUX, id="z6MkTest123")
did2 = Did(method=DidMethod.LUX, id="z6MkTest123")
did3 = Did(method=DidMethod.LUX, id="z6MkDifferent")
assert did1.uri() == did2.uri()
assert did1.uri() != did3.uri()
class TestParseDid:
def test_parse_lux_did(self):
did = parse_did("did:lux:z6MkTest123")
assert did.method == DidMethod.LUX
assert did.id == "z6MkTest123"
def test_parse_key_did(self):
did = parse_did("did:key:z6MkTestKey456")
assert did.method == DidMethod.KEY
assert did.id == "z6MkTestKey456"
def test_parse_web_did(self):
did = parse_did("did:web:example.com:user:alice")
assert did.method == DidMethod.WEB
assert did.id == "example.com:user:alice"
def test_parse_invalid(self):
with pytest.raises((ValueError, IdentityError)):
parse_did("invalid")
def test_parse_missing_method(self):
with pytest.raises((ValueError, IdentityError)):
parse_did("did:")
class TestDidDocument:
def test_create_did_document(self):
did = Did(method=DidMethod.LUX, id="z6MkTest123")
doc = did.document()
assert doc.id == "did:lux:z6MkTest123"
def test_did_document_to_json(self):
did = Did(method=DidMethod.LUX, id="z6MkTest123")
doc = did.document()
json_str = doc.to_json()
assert '"id": "did:lux:z6MkTest123"' in json_str or '"id":"did:lux:z6MkTest123"' in json_str
class TestBase58:
def test_base58_encode(self):
data = b"hello"
encoded = base58_encode(data)
assert len(encoded) > 0
def test_base58_decode(self):
data = b"hello"
encoded = base58_encode(data)
decoded = base58_decode(encoded)
assert decoded == data
def test_base58_roundtrip(self):
test_cases = [
b"a",
b"hello world",
bytes(range(1, 256)), ]
for data in test_cases:
encoded = base58_encode(data)
decoded = base58_decode(encoded)
assert decoded == data
class TestCreateDidFromKey:
def test_create_did_from_key(self):
public_key = bytes([i % 256 for i in range(1952)])
did = create_did_from_key(public_key)
assert did.method == DidMethod.KEY
assert did.id.startswith("z")
class TestCreateDidFromWeb:
def test_create_did_from_web(self):
did = create_did_from_web("example.com")
assert did.method == DidMethod.WEB
assert did.id == "example.com"
def test_create_did_from_web_with_path(self):
did = create_did_from_web("example.com", "users/alice")
assert did.method == DidMethod.WEB
assert "example.com" in did.id