-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_connections.py
333 lines (277 loc) · 10.8 KB
/
test_connections.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import asyncio
from typing import List, Optional
import pytest
from assertpy import assert_that
from fastapi import HTTPException
from app.models.connections import AcceptInvitation, CreateInvitation
from app.routes.connections import router
from app.tests.util.connections import BobAliceConnect, create_bob_alice_connection
from app.tests.util.webhooks import check_webhook_state
from shared import RichAsyncClient
BASE_PATH = router.prefix
@pytest.mark.anyio
@pytest.mark.parametrize(
"alias,multi_use,use_public_did",
[
(None, False, False),
("alias", False, False),
("alias", True, False),
("alias", False, True),
("alias", True, True),
],
)
async def test_create_invitation_no_public_did(
bob_member_client: RichAsyncClient, # bob has no public did
alias: Optional[str],
multi_use: bool,
use_public_did: bool,
):
invite_json = CreateInvitation(
alias=alias, multi_use=multi_use, use_public_did=use_public_did
).model_dump()
if use_public_did:
with pytest.raises(HTTPException) as exc_info:
# regular holders cannot `use_public_did` as they do not have a public did
await bob_member_client.post(
f"{BASE_PATH}/create-invitation", json=invite_json
)
assert exc_info.value.status_code == 400
assert (
exc_info.value.detail
== """{"detail":"Cannot create public invitation with no public DID."}"""
)
else:
response = await bob_member_client.post(
f"{BASE_PATH}/create-invitation", json=invite_json
)
assert response.status_code == 200
invitation = response.json()
assert_that(invitation["connection_id"]).is_not_empty()
assert_that(invitation["invitation"]).is_instance_of(dict).contains(
"@id", "@type", "recipientKeys", "serviceEndpoint"
)
assert_that(invitation["invitation_url"]).matches(r"^https?://")
@pytest.mark.anyio
async def test_accept_invitation(
bob_member_client: RichAsyncClient,
alice_member_client: RichAsyncClient,
):
alias = "test_alias"
invitation_response = await bob_member_client.post(f"{BASE_PATH}/create-invitation")
invitation = invitation_response.json()
accept_invite_json = AcceptInvitation(
alias=alias,
invitation=invitation["invitation"],
).model_dump()
accept_response = await alice_member_client.post(
f"{BASE_PATH}/accept-invitation",
json=accept_invite_json,
)
connection_record = accept_response.json()
assert_that(connection_record).contains(
"connection_id", "state", "created_at", "updated_at", "invitation_key"
)
assert_that(connection_record).has_state("request-sent")
assert_that(connection_record["alias"]).is_equal_to(alias)
assert await check_webhook_state(
client=alice_member_client,
topic="connections",
state="completed",
filter_map={
"connection_id": connection_record["connection_id"],
},
)
@pytest.mark.anyio
async def test_get_connections(
bob_member_client: RichAsyncClient,
alice_member_client: RichAsyncClient,
):
connection_alias = "TempAliceBobConnection"
bob_and_alice_connection = await create_bob_alice_connection(
alice_member_client, bob_member_client, alias=connection_alias
)
alice_connection_id = bob_and_alice_connection.alice_connection_id
bob_connection_id = bob_and_alice_connection.bob_connection_id
alice_connection = (
await alice_member_client.get(f"{BASE_PATH}/{alice_connection_id}")
).json()
bob_connection = (
await bob_member_client.get(f"{BASE_PATH}/{bob_connection_id}")
).json()
alice_invitation_msg_id_value = alice_connection["invitation_msg_id"]
alice_did = alice_connection["my_did"]
alice_connection_alias = (
await alice_member_client.get(f"{BASE_PATH}?alias={connection_alias}")
).json()[0]["alias"]
assert alice_connection_alias == connection_alias
alice_state = (
await alice_member_client.get(f"{BASE_PATH}?state=completed")
).json()[0]["state"]
assert alice_state == "completed"
alice_key = (
await alice_member_client.get(
f"{BASE_PATH}?invitation_key={bob_connection['invitation_key']}"
)
).json()[0]["invitation_key"]
assert alice_key == alice_connection["invitation_key"]
alice_invitation_msg_id = (
await alice_member_client.get(
f"{BASE_PATH}?invitation_msg_id={alice_invitation_msg_id_value}"
)
).json()[0]["invitation_msg_id"]
assert alice_invitation_msg_id == alice_invitation_msg_id_value
alice_my_did = (
await alice_member_client.get(f"{BASE_PATH}?my_did={alice_did}")
).json()[0]["my_did"]
assert alice_my_did == alice_did
alice_their_did = (
await alice_member_client.get(
f"{BASE_PATH}?their_did={bob_connection['my_did']}"
)
).json()[0]["their_did"]
assert alice_their_did == alice_connection["their_did"]
with pytest.raises(HTTPException) as exc:
await alice_member_client.get(
f"{BASE_PATH}?their_public_did={bob_connection['their_public_did']}"
)
assert exc.value.status_code == 422
alice_their_role = (
await alice_member_client.get(f"{BASE_PATH}?their_role=inviter")
).json()[0]["their_role"]
assert alice_their_role == "inviter"
# clean up temp connection
await alice_member_client.delete(f"{BASE_PATH}/{alice_connection_id}")
await bob_member_client.delete(f"{BASE_PATH}/{bob_connection_id}")
@pytest.mark.anyio
async def test_get_connection_by_id(
bob_member_client: RichAsyncClient,
):
invitation_response = await bob_member_client.post(f"{BASE_PATH}/create-invitation")
invitation = invitation_response.json()
connection_id = invitation["connection_id"]
connection_response = await bob_member_client.get(f"{BASE_PATH}/{connection_id}")
connection_record = connection_response.json()
assert connection_response.status_code == 200
assert_that(connection_record).contains(
"connection_id", "state", "created_at", "updated_at", "invitation_key"
)
@pytest.mark.anyio
async def test_delete_connection(
bob_member_client: RichAsyncClient,
):
invitation_response = await bob_member_client.post(f"{BASE_PATH}/create-invitation")
invitation = invitation_response.json()
connection_id = invitation["connection_id"]
response = await bob_member_client.delete(f"{BASE_PATH}/{connection_id}")
assert response.status_code == 204
with pytest.raises(HTTPException) as exc:
response = await bob_member_client.get(f"{BASE_PATH}/{connection_id}")
assert exc.value.status_code == 404
@pytest.mark.anyio
async def test_bob_and_alice_connect(
bob_member_client: RichAsyncClient,
alice_member_client: RichAsyncClient,
):
invitation_response = await bob_member_client.post(
f"{BASE_PATH}/create-invitation",
)
invitation = invitation_response.json()
accept_response = await alice_member_client.post(
f"{BASE_PATH}/accept-invitation",
json={"invitation": invitation["invitation"]},
)
connection_record = accept_response.json()
assert await check_webhook_state(
client=alice_member_client,
topic="connections",
state="completed",
filter_map={
"connection_id": connection_record["connection_id"],
},
)
alice_connection_id = connection_record["connection_id"]
bob_connection_id = invitation["connection_id"]
bob_connection = (
await bob_member_client.get(f"{BASE_PATH}/{bob_connection_id}")
).json()
alice_connection = (
await alice_member_client.get(f"{BASE_PATH}/{alice_connection_id}")
).json()
assert "completed" in alice_connection["state"]
assert "completed" in bob_connection["state"]
@pytest.mark.anyio
async def test_get_connections_paginated(
bob_member_client: RichAsyncClient, alice_member_client: RichAsyncClient
):
num_connections_to_test = 5
test_alias = "test_pagination"
bob_alice_connections: List[BobAliceConnect] = []
for _ in range(num_connections_to_test):
bob_and_alice_connection = await create_bob_alice_connection(
alice_member_client, bob_member_client, alias=test_alias
)
bob_alice_connections += (bob_and_alice_connection,)
# Test different limits
for limit in range(1, num_connections_to_test + 2):
num_tries = 0
retry = True
while retry and num_tries < 5: # Handle case where record doesn't exist yet
response = await alice_member_client.get(
BASE_PATH,
params={
"alias": test_alias,
"limit": limit,
},
)
connections = response.json()
if len(connections) != min(limit, num_connections_to_test):
num_tries += 1
await asyncio.sleep(0.2)
else:
retry = False
assert (
not retry
), f"Expected {limit} records, got {len(connections)}: {connections}"
# Test offset greater than number of records
response = await alice_member_client.get(
BASE_PATH,
params={
"alias": test_alias,
"limit": 1,
"offset": num_connections_to_test,
},
)
connections = response.json()
assert len(connections) == 0
# Test fetching unique records with pagination
# TODO: Skipping for now; we require ACA-Py / Askar record ordering to guarantee unique records across pages
# prev_connections = []
# for offset in range(num_connections_to_test):
# response = await alice_member_client.get(
# BASE_PATH,
# params={
# "alias": test_alias,
# "limit": 1,
# "offset": offset,
# },
# )
# connections = response.json()
# assert len(connections) == 1
# record = connections[0]
# assert record not in prev_connections
# prev_connections += (record,)
# Test invalid limit and offset values
invalid_params = [
{"limit": -1}, # must be positive
{"offset": -1}, # must be positive
{"limit": 0}, # must be greater than 0
{"limit": 10001}, # must be less than or equal to max in ACA-Py: 10'000
]
for params in invalid_params:
with pytest.raises(HTTPException) as exc:
await alice_member_client.get(BASE_PATH, params=params)
assert exc.value.status_code == 422
# Clean up connections
for conn in bob_alice_connections:
await alice_member_client.delete(f"{BASE_PATH}/{conn.alice_connection_id}")
await bob_member_client.delete(f"{BASE_PATH}/{conn.bob_connection_id}")