Skip to content

Commit 61456c9

Browse files
committed
add new test
1 parent 6a47617 commit 61456c9

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

tests/test_api.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,103 @@ def test_get_system_stats(self):
158158
self.assertEqual(result.get("status"), "ok")
159159
self.assertIn("stats", result)
160160

161+
def test_bulk_operations_workflow(self):
162+
"""Test complete bulk operations workflow: create users, unlock bulk, and fetch all users."""
163+
# Step 1: Create 10 users using bulk creation
164+
users_data = []
165+
for i in range(10):
166+
users_data.append({
167+
"profile": {
168+
"email": f"bulktest{random.randint(1000, 999999)}@example.com",
169+
"name": f"Bulk Test User {i+1}",
170+
"phone": str(random.randint(1000000, 9999999)),
171+
}
172+
})
173+
174+
# Create users in bulk
175+
bulk_options = {"finaltime": "1y", "slidingtime": "30d"}
176+
create_result = self.api.create_users_bulk(users_data, bulk_options)
177+
178+
# Verify bulk creation was successful
179+
self.assertIsInstance(create_result, dict)
180+
self.assertEqual(create_result.get("status"), "ok")
181+
self.assertIn("created", create_result)
182+
self.assertEqual(len(create_result["created"]), 10)
183+
184+
# Store created user tokens for verification
185+
created_tokens = [user["token"] for user in create_result["created"]]
186+
187+
# Step 2: Initiate bulk list unlock operation
188+
unlock_result = self.api.bulk_list_unlock()
189+
190+
# Verify unlock operation was successful
191+
self.assertIsInstance(unlock_result, dict)
192+
self.assertEqual(unlock_result.get("status"), "ok")
193+
self.assertIn("unlockuuid", unlock_result)
194+
195+
unlock_uuid = unlock_result["unlockuuid"]
196+
197+
# Step 3: Fetch all users using bulk list operation
198+
# First, get the total count by fetching with a large limit
199+
bulk_users_result = self.api.bulk_list_users(unlock_uuid, offset=0, limit=100)
200+
201+
# Verify bulk fetch was successful
202+
self.assertIsInstance(bulk_users_result, dict)
203+
self.assertEqual(bulk_users_result.get("status"), "ok")
204+
self.assertIn("rows", bulk_users_result)
205+
206+
# Verify we can find our created users in the bulk results
207+
bulk_users = bulk_users_result["rows"]
208+
found_created_users = 0
209+
210+
for bulk_user in bulk_users:
211+
if "token" in bulk_user and bulk_user["token"] in created_tokens:
212+
found_created_users += 1
213+
# Verify the user data matches what we created
214+
user_profile = bulk_user.get("profile", {})
215+
self.assertIn("email", user_profile)
216+
self.assertIn("name", user_profile)
217+
self.assertIn("phone", user_profile)
218+
219+
# Verify we found all our created users
220+
self.assertEqual(found_created_users, 10,
221+
f"Expected to find 10 created users in bulk results, but found {found_created_users}")
222+
223+
# Step 4: Test pagination by fetching users in smaller batches
224+
paginated_users = []
225+
offset = 0
226+
limit = 5
227+
228+
while True:
229+
page_result = self.api.bulk_list_users(unlock_uuid, offset=offset, limit=limit)
230+
self.assertEqual(page_result.get("status"), "ok")
231+
232+
page_users = page_result.get("rows", [])
233+
if not page_users:
234+
break
235+
236+
paginated_users.extend(page_users)
237+
offset += limit
238+
239+
# Safety break to prevent infinite loops
240+
if offset > 100:
241+
break
242+
243+
# Verify pagination worked and we got users
244+
self.assertGreater(len(paginated_users), 0, "Pagination should return some users")
245+
246+
# Clean up: Delete the created users
247+
for token in created_tokens:
248+
try:
249+
delete_result = self.api.delete_user("token", token)
250+
# Don't fail the test if cleanup fails
251+
if delete_result.get("status") != "ok":
252+
print(f"Warning: Failed to delete user with token {token}")
253+
except Exception as e:
254+
print(f"Warning: Exception during cleanup for token {token}: {str(e)}")
255+
256+
return created_tokens
257+
161258

162259
if __name__ == "__main__":
163260
unittest.main()

0 commit comments

Comments
 (0)