Codebase rewritten to:
tangled.org/atpasser.poxiao-labs.work/atpasser
1"""Test cases for the DID class in atpasser.uri.identifier module."""
2
3import pytest
4from atpasser.uri.identifier import DID
5from atpasser.uri.exceptions import InvalidDIDError, ResolutionError
6
7
8class TestDID:
9 """Test cases for the DID class."""
10
11 def test_valid_did_plc(self):
12 """Test creating a DID with a valid did:plc format."""
13 did_str = "did:plc:z72i7hdynmk6r22z27h6tvur"
14 did = DID(did_str)
15
16 assert str(did) == did_str
17 assert did.uri == did_str
18
19 def test_valid_did_web(self):
20 """Test creating a DID with a valid did:web format."""
21 did_str = "did:web:blueskyweb.xyz"
22 did = DID(did_str)
23
24 assert str(did) == did_str
25 assert did.uri == did_str
26
27 def test_valid_did_with_various_characters(self):
28 """Test creating a DID with various valid characters."""
29 did_str = "did:method:val:two-with_underscores.and-dashes"
30 did = DID(did_str)
31
32 assert str(did) == did_str
33 assert did.uri == did_str
34
35 def test_invalid_did_wrong_format(self):
36 """Test that a DID with wrong format raises InvalidDIDError."""
37 did_str = "not-a-did"
38
39 with pytest.raises(InvalidDIDError, match="invalid format"):
40 DID(did_str)
41
42 def test_invalid_did_uppercase_method(self):
43 """Test that a DID with uppercase method raises InvalidDIDError."""
44 did_str = "did:METHOD:val"
45
46 with pytest.raises(InvalidDIDError, match="invalid format"):
47 DID(did_str)
48
49 def test_invalid_did_method_with_numbers(self):
50 """Test that a DID with method containing numbers raises InvalidDIDError."""
51 did_str = "did:m123:val"
52
53 with pytest.raises(InvalidDIDError, match="invalid format"):
54 DID(did_str)
55
56 def test_invalid_did_empty_identifier(self):
57 """Test that a DID with empty identifier raises InvalidDIDError."""
58 did_str = "did:method:"
59
60 with pytest.raises(InvalidDIDError, match="invalid format"):
61 DID(did_str)
62
63 def test_invalid_did_ends_with_colon(self):
64 """Test that a DID ending with colon raises InvalidDIDError."""
65 did_str = "did:method:val:"
66
67 with pytest.raises(InvalidDIDError, match="invalid format"):
68 DID(did_str)
69
70 def test_invalid_did_too_long(self):
71 """Test that a DID that is too long raises InvalidDIDError."""
72 # Create a DID that exceeds the 2048 character limit
73 long_identifier = "a" * 2040
74 did_str = f"did:method:{long_identifier}"
75
76 with pytest.raises(InvalidDIDError, match="exceeds maximum length"):
77 DID(did_str)
78
79 def test_did_equality(self):
80 """Test DID equality comparison."""
81 did_str = "did:plc:z72i7hdynmk6r22z27h6tvur"
82 did1 = DID(did_str)
83 did2 = DID(did_str)
84
85 assert did1 == did2
86 assert did1 != "not a did object"
87
88 def test_did_string_representation(self):
89 """Test DID string representation."""
90 did_str = "did:plc:z72i7hdynmk6r22z27h6tvur"
91 did = DID(did_str)
92
93 assert str(did) == did_str
94
95 def test_did_fetch_plc_method(self):
96 """Test fetching a DID document for did:plc method."""
97 did_str = "did:plc:z72i7hdynmk6r22z27h6tvur"
98 did = DID(did_str)
99
100 # This test may fail if there's no internet connection or if the PLC directory is down
101 try:
102 document = did.fetch()
103 assert isinstance(document, list)
104 assert len(document) > 0
105 except ResolutionError:
106 # If resolution fails, we'll skip this test
107 pytest.skip("Failed to resolve DID document")
108
109 def test_did_fetch_web_method(self):
110 """Test fetching a DID document for did:web method."""
111 did_str = "did:web:blueskyweb.xyz"
112 did = DID(did_str)
113
114 # This test may fail if there's no internet connection or if the web server is down
115 try:
116 document = did.fetch()
117 assert isinstance(document, list)
118 assert len(document) > 0
119 except ResolutionError:
120 # If resolution fails, we'll skip this test
121 pytest.skip("Failed to resolve DID document")
122
123 def test_did_fetch_unsupported_method(self):
124 """Test that fetching a DID document with unsupported method raises InvalidDIDError."""
125 did_str = "did:unsupported:method"
126 did = DID(did_str)
127
128 with pytest.raises(InvalidDIDError, match="unsupported DID method"):
129 did.fetch()
130
131 def test_did_fetch_web_empty_domain(self):
132 """Test that fetching a DID document with empty domain raises InvalidDIDError."""
133 did_str = "did:web:"
134 did = DID(did_str)
135
136 with pytest.raises(InvalidDIDError, match="invalid format"):
137 did.fetch()