-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_connections.py
244 lines (200 loc) · 7.82 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
from typing import 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 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"]