-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_credentials.py
311 lines (249 loc) · 10 KB
/
test_credentials.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
import asyncio
import pytest
from app.dependencies.auth import AcaPyAuthVerified, acapy_auth, acapy_auth_verified
from app.event_handling.sse_listener import SseListener
from app.models.tenants import CreateTenantResponse
from app.routes.definitions import (
CreateCredentialDefinition,
CreateSchema,
CredentialSchema,
create_credential_definition,
create_schema,
)
from app.routes.issuer import router
from app.tests.util.ecosystem_connections import FaberAliceConnect, MeldCoAliceConnect
from app.tests.util.trust_registry import register_issuer
from app.tests.util.webhooks import check_webhook_state
from app.util.string import random_version
from shared import RichAsyncClient
from shared.models.webhook_topics import CredentialExchange
CREDENTIALS_BASE_PATH = router.prefix
@pytest.fixture(scope="session")
async def schema_definition(
mock_governance_auth: AcaPyAuthVerified,
) -> CredentialSchema:
definition = CreateSchema(
name="test_schema", version=random_version(), attribute_names=["speed"]
)
schema_definition_result = await create_schema(definition, mock_governance_auth)
return schema_definition_result
@pytest.fixture(scope="session")
async def schema_definition_alt(
mock_governance_auth: AcaPyAuthVerified,
) -> CredentialSchema:
definition = CreateSchema(
name="test_schema_alt", version=random_version(), attribute_names=["speed"]
)
schema_definition_result = await create_schema(definition, mock_governance_auth)
return schema_definition_result
@pytest.fixture(scope="session")
async def credential_definition_id(
schema_definition: CredentialSchema,
faber_client: RichAsyncClient,
) -> str:
await register_issuer(faber_client, schema_definition.id)
# Support revocation false here because revocation is tested elsewhere.
# No revocation is a fair bit faster to run
definition = CreateCredentialDefinition(
tag="tag", schema_id=schema_definition.id, support_revocation=False
)
auth = acapy_auth_verified(acapy_auth(faber_client.headers["x-api-key"]))
result = await create_credential_definition(definition, auth)
return result.id
@pytest.fixture(scope="session")
async def credential_definition_id_revocable(
schema_definition_alt: CredentialSchema,
faber_client: RichAsyncClient,
) -> str:
await register_issuer(faber_client, schema_definition_alt.id)
definition = CreateCredentialDefinition(
tag="tag", schema_id=schema_definition_alt.id, support_revocation=True
)
auth = acapy_auth_verified(acapy_auth(faber_client.headers["x-api-key"]))
result = await create_credential_definition(definition, auth)
return result.id
@pytest.fixture(scope="function")
async def credential_exchange_id(
faber_client: RichAsyncClient,
credential_definition_id: str,
faber_and_alice_connection: FaberAliceConnect,
alice_member_client: RichAsyncClient,
):
credential = {
"protocol_version": "v1",
"connection_id": faber_and_alice_connection.faber_connection_id,
"indy_credential_detail": {
"credential_definition_id": credential_definition_id,
"attributes": {"speed": "average"},
},
}
response = await faber_client.post(
CREDENTIALS_BASE_PATH,
json=credential,
)
credential_exchange = response.json()
credential_exchange_id = credential_exchange["credential_id"]
assert credential_exchange["protocol_version"] == "v1"
assert await check_webhook_state(
client=faber_client,
topic="credentials",
filter_map={
"state": "offer-sent",
"credential_id": credential_exchange["credential_id"],
},
)
await asyncio.sleep(0.1) # credential may take moment to reflect after webhook
response = await alice_member_client.get(
CREDENTIALS_BASE_PATH,
params={"connection_id": faber_and_alice_connection.alice_connection_id},
)
records = response.json()
assert len(records) > 0
return credential_exchange_id
@pytest.fixture(scope="function")
async def issue_credential_to_alice(
faber_client: RichAsyncClient,
credential_definition_id: str,
faber_and_alice_connection: FaberAliceConnect,
alice_member_client: RichAsyncClient,
alice_tenant: CreateTenantResponse,
) -> CredentialExchange:
credential = {
"protocol_version": "v1",
"connection_id": faber_and_alice_connection.faber_connection_id,
"indy_credential_detail": {
"credential_definition_id": credential_definition_id,
"attributes": {"speed": "10"},
},
}
alice_credentials_listener = SseListener(
topic="credentials", wallet_id=alice_tenant.wallet_id
)
# create and send credential offer- issuer
await faber_client.post(
CREDENTIALS_BASE_PATH,
json=credential,
)
payload = await alice_credentials_listener.wait_for_event(
field="connection_id",
field_id=faber_and_alice_connection.alice_connection_id,
desired_state="offer-received",
)
alice_credential_id = payload["credential_id"]
# send credential request - holder
response = await alice_member_client.post(
f"{CREDENTIALS_BASE_PATH}/{alice_credential_id}/request", json={}
)
await alice_credentials_listener.wait_for_event(
field="credential_id", field_id=alice_credential_id, desired_state="done"
)
return response.json()
@pytest.fixture(scope="session")
async def meld_co_credential_definition_id(
schema_definition: CredentialSchema, # pylint: disable=redefined-outer-name
meld_co_client: RichAsyncClient,
) -> str:
await register_issuer(meld_co_client, schema_definition.id)
# Support revocation false here because revocation is tested elsewhere.
# No revocation is a fair bit faster to run
definition = CreateCredentialDefinition(
tag="tag", schema_id=schema_definition.id, support_revocation=False
)
auth = acapy_auth_verified(acapy_auth(meld_co_client.headers["x-api-key"]))
result = await create_credential_definition(definition, auth)
return result.id
@pytest.fixture(scope="function")
async def meld_co_issue_credential_to_alice(
meld_co_client: RichAsyncClient,
meld_co_credential_definition_id: str, # pylint: disable=redefined-outer-name
meld_co_and_alice_connection: MeldCoAliceConnect,
alice_member_client: RichAsyncClient,
alice_tenant: CreateTenantResponse,
) -> CredentialExchange:
credential = {
"protocol_version": "v1",
"connection_id": meld_co_and_alice_connection.meld_co_connection_id,
"indy_credential_detail": {
"credential_definition_id": meld_co_credential_definition_id,
"attributes": {"speed": "10"},
},
}
alice_credentials_listener = SseListener(
topic="credentials", wallet_id=alice_tenant.wallet_id
)
# create and send credential offer- issuer
await meld_co_client.post(
CREDENTIALS_BASE_PATH,
json=credential,
)
payload = await alice_credentials_listener.wait_for_event(
field="connection_id",
field_id=meld_co_and_alice_connection.alice_connection_id,
desired_state="offer-received",
)
alice_credential_id = payload["credential_id"]
# send credential request - holder
response = await alice_member_client.post(
f"{CREDENTIALS_BASE_PATH}/{alice_credential_id}/request", json={}
)
await alice_credentials_listener.wait_for_event(
field="credential_id", field_id=alice_credential_id, desired_state="done"
)
return response.json()
@pytest.mark.anyio
@pytest.mark.parametrize("save_exchange_record", [False, True])
@pytest.mark.parametrize("protocol_version", ["v1", "v2"])
async def test_issue_credential_with_save_exchange_record(
faber_client: RichAsyncClient,
credential_definition_id: str, # pylint: disable=redefined-outer-name
faber_and_alice_connection: FaberAliceConnect,
alice_member_client: RichAsyncClient,
alice_tenant: CreateTenantResponse,
save_exchange_record: bool,
protocol_version: str,
) -> CredentialExchange:
credential = {
"protocol_version": protocol_version,
"connection_id": faber_and_alice_connection.faber_connection_id,
"indy_credential_detail": {
"credential_definition_id": credential_definition_id,
"attributes": {"speed": "10"},
},
"save_exchange_record": save_exchange_record,
}
alice_credentials_listener = SseListener(
topic="credentials", wallet_id=alice_tenant.wallet_id
)
# create and send credential offer- issuer
await faber_client.post(
CREDENTIALS_BASE_PATH,
json=credential,
)
payload = await alice_credentials_listener.wait_for_event(
field="connection_id",
field_id=faber_and_alice_connection.alice_connection_id,
desired_state="offer-received",
)
alice_credential_id = payload["credential_id"]
# send credential request - holder
await alice_member_client.post(
f"{CREDENTIALS_BASE_PATH}/{alice_credential_id}/request",
)
await alice_credentials_listener.wait_for_event(
field="credential_id", field_id=alice_credential_id, desired_state="done"
)
# get exchange records from alice side -- should be empty regardless
alice_cred_ex_recs = (
await alice_member_client.get(f"{CREDENTIALS_BASE_PATH}")
).json()
# faber requesting auto_remove only removes their cred ex recs
# Alice cred ex recs should be empty regardless
assert len(alice_cred_ex_recs) == 0
# get exchange records from faber side:
faber_cred_ex_recs = (await faber_client.get(f"{CREDENTIALS_BASE_PATH}")).json()
if save_exchange_record:
assert len(faber_cred_ex_recs) == 1 # Save record is True, should be 1 record
cred_ex_id = faber_cred_ex_recs[0]["credential_id"]
await faber_client.delete(f"{CREDENTIALS_BASE_PATH}/{cred_ex_id}") # Clean up
else:
assert len(faber_cred_ex_recs) == 0 # default is to remove records