-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_main.py
202 lines (175 loc) · 6.39 KB
/
test_main.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
import pytest
import pytest_asyncio
import didkit
import json
from uuid import uuid4
JWK = '{"kty":"OKP","crv":"Ed25519","x":"PBcY2yJ4h_cLUnQNcYhplu9KQQBNpGxP4sYcMPdlu6I","d":"n5WUFIghmRYZi0rEYo2lz-Zg2B9B1KW4MYfJXwOXfyI"}'
class TestKeyMethods:
def test_get_library_version(self):
assert type(didkit.get_version()) is str
def test_generates_ed25519_key(self):
key = json.loads(didkit.generate_ed25519_key())
assert "kty" in key.keys()
assert "crv" in key.keys()
assert "x" in key.keys()
assert "d" in key.keys()
def test_key_to_did(self):
assert (
didkit.key_to_did("key", JWK)
== "did:key:z6MkiVpwA241guqtKWAkohHpcAry7S94QQb6ukW3GcCsugbK"
)
@pytest.mark.asyncio
async def test_key_to_verification_method(self):
assert (
await didkit.key_to_verification_method("key", JWK)
== "did:key:z6MkiVpwA241guqtKWAkohHpcAry7S94QQb6ukW3GcCsugbK#z6MkiVpwA241guqtKWAkohHpcAry7S94QQb6ukW3GcCsugbK"
)
class TestCredentialMethods:
did = didkit.key_to_did("key", JWK)
credential = {
"@context": "https://www.w3.org/2018/credentials/v1",
"id": "http://example.org/credentials/3731",
"type": ["VerifiableCredential"],
"issuer": did,
"issuanceDate": "2020-08-19T21:41:50Z",
"credentialSubject": {
"id": "did:example:d23dd687a7dc6787646f2eb98d0",
},
}
options = {}
@pytest_asyncio.fixture
async def setup(self):
vm = await didkit.key_to_verification_method("key", JWK)
self.options = {
"proofPurpose": "assertionMethod",
"verificationMethod": vm,
}
@pytest.mark.asyncio
async def test_raises_on_issue_with_empty_objects(self):
with pytest.raises(ValueError):
await didkit.issue_credential("{}", "{}", "{}")
@pytest.mark.asyncio
async def test_issues_credentials(self):
credential = await didkit.issue_credential(
json.dumps(self.credential), json.dumps(self.options), JWK
)
result = json.loads(
await didkit.verify_credential(
credential, '{"proofPurpose":"assertionMethod"}'
)
)
assert not result["errors"]
@pytest.mark.asyncio
async def test_issues_credential_plugfest(self):
credential = {
"@context": [
"https://www.w3.org/2018/credentials/v1",
"https://purl.imsglobal.org/spec/ob/v3p0/context.json",
],
"id": "urn:uuid:0",
"type": [
"VerifiableCredential",
"OpenBadgeCredential",
],
"name": "..",
"issuer": {
"type": ["Profile"],
"id": self.did,
"name": "..",
"url": "https://example.com/",
"image": {
"id": "https://example.com/image",
"type": "Image"
}
},
"issuanceDate": "2020-08-19T21:41:50Z",
"credentialSubject": {
"type": ["AchievementSubject"],
"id": self.did,
"achievement": {
"id": "urn:uuid:0",
"type": ["Achievement"],
"name": "..",
"description": "..",
"criteria": {
"narrative": ".."
},
"image": {
"id": "https://example.com/image",
"type": "Image"
}
}
}
}
credential = await didkit.issue_credential(
json.dumps(credential), json.dumps(self.options), JWK
)
result = json.loads(
await didkit.verify_credential(
credential, '{"proofPurpose":"assertionMethod"}'
)
)
assert not result["errors"]
class TestPresentationMethods:
did = didkit.key_to_did("key", JWK)
presentation = {
"@context": ["https://www.w3.org/2018/credentials/v1"],
"id": "http://example.org/presentations/3731",
"type": ["VerifiablePresentation"],
"holder": did,
"verifiableCredential": {
"@context": "https://www.w3.org/2018/credentials/v1",
"id": "http://example.org/credentials/3731",
"type": ["VerifiableCredential"],
"issuer": "did:example:30e07a529f32d234f6181736bd3",
"issuanceDate": "2020-08-19T21:41:50Z",
"credentialSubject": {
"id": "did:example:d23dd687a7dc6787646f2eb98d0",
},
},
}
options = {}
@pytest_asyncio.fixture
async def setup(self):
self.vm = await didkit.key_to_verification_method("key", JWK)
self.options = {
"proofPurpose": "authentication",
"verificationMethod": self.vm,
}
@pytest.mark.asyncio
async def test_raises_on_present_with_empty_objects(self):
with pytest.raises(ValueError):
await didkit.issue_presentation("{}", "{}", "{}")
@pytest.mark.asyncio
async def test_verify_issued_presentation(self):
presentation = await didkit.issue_presentation(
json.dumps(self.presentation), json.dumps(self.options), JWK
)
result = json.loads(
await didkit.verify_presentation(
presentation, json.dumps(self.options)
)
)
assert not result["errors"]
class TestAuthMethods:
did = didkit.key_to_did("key", JWK)
options = {}
@pytest_asyncio.fixture
async def setup(self):
vm = await didkit.key_to_verification_method("key", JWK)
self.options = {
"proofPurpose": "authentication",
"verificationMethod": vm,
"challenge": str(uuid4()),
}
@pytest.mark.asyncio
async def test_raises_on_present_with_empty_objects(self):
with pytest.raises(ValueError):
await didkit.did_auth("", "{}", "{}")
@pytest.mark.asyncio
async def test_issue_and_verify_didauth_verifiable_presentation(self):
presentation = await didkit.did_auth(self.did, json.dumps(self.options), JWK)
result = json.loads(
await didkit.verify_presentation(presentation, json.dumps(self.options))
)
assert not result["errors"]