-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathtest_organizations.py
310 lines (243 loc) · 9.33 KB
/
test_organizations.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
# What this tests ?
## Tests /organization endpoints.
import pytest
import asyncio
import aiohttp
import time, uuid
from openai import AsyncOpenAI
async def new_user(
session,
i,
user_id=None,
budget=None,
budget_duration=None,
models=["azure-models"],
team_id=None,
user_email=None,
):
url = "http://0.0.0.0:4000/user/new"
headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
data = {
"models": models,
"aliases": {"mistral-7b": "gpt-3.5-turbo"},
"duration": None,
"max_budget": budget,
"budget_duration": budget_duration,
"user_email": user_email,
}
if user_id is not None:
data["user_id"] = user_id
if team_id is not None:
data["team_id"] = team_id
async with session.post(url, headers=headers, json=data) as response:
status = response.status
response_text = await response.text()
print(f"Response {i} (Status code: {status}):")
print(response_text)
print()
if status != 200:
raise Exception(
f"Request {i} did not return a 200 status code: {status}, response: {response_text}"
)
return await response.json()
async def new_organization(session, i, organization_alias, max_budget=None):
url = "http://0.0.0.0:4000/organization/new"
headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
data = {
"organization_alias": organization_alias,
"models": ["azure-models"],
"max_budget": max_budget,
}
async with session.post(url, headers=headers, json=data) as response:
status = response.status
response_text = await response.text()
print(f"Response {i} (Status code: {status}):")
print(response_text)
print()
if status != 200:
raise Exception(f"Request {i} did not return a 200 status code: {status}")
return await response.json()
async def add_member_to_org(
session, i, organization_id, user_id, user_role="internal_user"
):
url = "http://0.0.0.0:4000/organization/member_add"
headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
data = {
"organization_id": organization_id,
"member": {
"user_id": user_id,
"role": user_role,
},
}
async with session.post(url, headers=headers, json=data) as response:
status = response.status
response_text = await response.text()
print(f"Response {i} (Status code: {status}):")
print(response_text)
print()
if status != 200:
raise Exception(f"Request {i} did not return a 200 status code: {status}")
return await response.json()
async def update_member_role(
session, i, organization_id, user_id, user_role="internal_user"
):
url = "http://0.0.0.0:4000/organization/member_update"
headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
data = {
"organization_id": organization_id,
"user_id": user_id,
"role": user_role,
}
async with session.patch(url, headers=headers, json=data) as response:
status = response.status
response_text = await response.text()
print(f"Response {i} (Status code: {status}):")
print(response_text)
print()
if status != 200:
raise Exception(f"Request {i} did not return a 200 status code: {status}")
return await response.json()
async def delete_member_from_org(session, i, organization_id, user_id):
url = "http://0.0.0.0:4000/organization/member_delete"
headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
data = {
"organization_id": organization_id,
"user_id": user_id,
}
async with session.delete(url, headers=headers, json=data) as response:
status = response.status
response_text = await response.text()
print(f"Response {i} (Status code: {status}):")
print(response_text)
print()
if status != 200:
raise Exception(f"Request {i} did not return a 200 status code: {status}")
return await response.json()
async def delete_organization(session, i, organization_id):
url = "http://0.0.0.0:4000/organization/delete"
headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
data = {"organization_ids": [organization_id]}
async with session.delete(url, headers=headers, json=data) as response:
status = response.status
response_text = await response.text()
print(f"Response {i} (Status code: {status}):")
print(response_text)
print()
if status != 200:
raise Exception(f"Request {i} did not return a 200 status code: {status}")
return await response.json()
async def list_organization(session, i):
url = "http://0.0.0.0:4000/organization/list"
headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
async with session.get(url, headers=headers) as response:
status = response.status
response_json = await response.json()
print(f"Response {i} (Status code: {status}):")
print()
if status != 200:
raise Exception(f"Request {i} did not return a 200 status code: {status}")
return response_json
@pytest.mark.asyncio
async def test_organization_new():
"""
Make 20 parallel calls to /organization/new. Assert all worked.
"""
organization_alias = f"Organization: {uuid.uuid4()}"
async with aiohttp.ClientSession() as session:
tasks = [
new_organization(
session=session, i=0, organization_alias=organization_alias
)
for i in range(1, 20)
]
await asyncio.gather(*tasks)
@pytest.mark.asyncio
async def test_organization_list():
"""
create 2 new Organizations
check if the Organization list is not empty
"""
organization_alias = f"Organization: {uuid.uuid4()}"
async with aiohttp.ClientSession() as session:
tasks = [
new_organization(
session=session, i=0, organization_alias=organization_alias
)
for i in range(1, 2)
]
await asyncio.gather(*tasks)
response_json = await list_organization(session, i=0)
print(len(response_json))
if len(response_json) == 0:
raise Exception("Return empty list of organization")
@pytest.mark.asyncio
async def test_organization_delete():
"""
create a new organization
delete the organization
check if the Organization list is set
"""
organization_alias = f"Organization: {uuid.uuid4()}"
async with aiohttp.ClientSession() as session:
tasks = [
new_organization(
session=session, i=0, organization_alias=organization_alias
)
]
await asyncio.gather(*tasks)
response_json = await list_organization(session, i=0)
print(len(response_json))
organization_id = response_json[0]["organization_id"]
await delete_organization(session, i=0, organization_id=organization_id)
response_json = await list_organization(session, i=0)
print(len(response_json))
@pytest.mark.asyncio
async def test_organization_member_flow():
"""
create a new organization
add a new member to the organization
check if the member is added to the organization
update the member's role in the organization
delete the member from the organization
check if the member is deleted from the organization
"""
organization_alias = f"Organization: {uuid.uuid4()}"
async with aiohttp.ClientSession() as session:
response_json = await new_organization(
session=session, i=0, organization_alias=organization_alias
)
organization_id = response_json["organization_id"]
response_json = await list_organization(session, i=0)
print(len(response_json))
new_user_response_json = await new_user(
session=session, i=0, user_email=f"test_user_{uuid.uuid4()}@example.com"
)
user_id = new_user_response_json["user_id"]
await add_member_to_org(
session, i=0, organization_id=organization_id, user_id=user_id
)
response_json = await list_organization(session, i=0)
print(len(response_json))
for orgs in response_json:
tmp_organization_id = orgs["organization_id"]
if (
tmp_organization_id is not None
and tmp_organization_id == organization_id
):
user_id = orgs["members"][0]["user_id"]
response_json = await list_organization(session, i=0)
print(len(response_json))
await update_member_role(
session,
i=0,
organization_id=organization_id,
user_id=user_id,
user_role="org_admin",
)
response_json = await list_organization(session, i=0)
print(len(response_json))
await delete_member_from_org(
session, i=0, organization_id=organization_id, user_id=user_id
)
response_json = await list_organization(session, i=0)
print(len(response_json))