Skip to content

Commit fb0fc48

Browse files
committed
test bulk creation
1 parent d9f9ccd commit fb0fc48

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

databunkerpro/api.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,52 @@ def create_user(
107107
data.update(options)
108108
return self._make_request("UserCreate", "POST", data, request_metadata)
109109

110+
def create_users_bulk(
111+
self,
112+
records: List[Dict[str, Any]],
113+
options: Optional[Dict[str, Any]] = None,
114+
request_metadata: Optional[Dict[str, Any]] = None,
115+
) -> Dict[str, Any]:
116+
"""
117+
Create multiple users in bulk with their profiles and group information.
118+
119+
Args:
120+
records (List[Dict[str, Any]]): List of user records to create, each containing:
121+
- profile (Dict[str, Any]): User profile data
122+
- groupname (str | int, optional): Group name or ID
123+
- groupid (int, optional): Group ID
124+
- rolename (str | int, optional): Role name or ID
125+
- roleid (int, optional): Role ID
126+
options (Dict[str, Any], optional): Global options for all users:
127+
- finaltime (str, optional): Global expiration time for all users
128+
- slidingtime (str, optional): Global sliding time period for all users
129+
request_metadata (Dict[str, Any], optional): Additional metadata for the request
130+
131+
Returns:
132+
Dict[str, Any]: The API response containing information about created users
133+
134+
Example:
135+
>>> api.create_users_bulk([
136+
... {
137+
... "profile": {"email": "user1@example.com", "name": "User One"},
138+
... "groupname": "premium",
139+
... "rolename": "admin"
140+
... },
141+
... {
142+
... "profile": {"email": "user2@example.com", "name": "User Two"},
143+
... "groupid": 123,
144+
... "rolename": "team-member"
145+
... }
146+
... ], {
147+
... "finaltime": "2024-12-31",
148+
... "slidingtime": "30d"
149+
... })
150+
"""
151+
data = {"records": records}
152+
if options:
153+
data.update(options)
154+
return self._make_request("UserCreateBulk", "POST", data, request_metadata)
155+
110156
def get_user(
111157
self,
112158
mode: str,

tests/test_api.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,46 @@ def test_create_user(self):
7373
self.assertIn("token", result)
7474
return user_data["email"]
7575

76+
def test_create_users_bulk(self):
77+
"""Test bulk user creation."""
78+
# Create test data for multiple users
79+
users_data = [
80+
{
81+
"profile": {
82+
"email": f"test{random.randint(1000, 999999)}@example.com",
83+
"name": f"Test User {random.randint(1000, 999999)}",
84+
"phone": str(random.randint(1000, 999999)),
85+
},
86+
# "groupname": "test-group",
87+
# "rolename": "test-role"
88+
},
89+
{
90+
"profile": {
91+
"email": f"test{random.randint(1000, 999999)}@example.com",
92+
"name": f"Test User {random.randint(1000, 999999)}",
93+
"phone": str(random.randint(1000, 999999)),
94+
},
95+
# "groupid": 1,
96+
# "roleid": 1
97+
}
98+
]
99+
# Test bulk creation with global options
100+
options = {
101+
"finaltime": "1y",
102+
"slidingtime": "30d"
103+
}
104+
result = self.api.create_users_bulk(users_data, options)
105+
self.assertIsInstance(result, dict)
106+
self.assertEqual(result.get("status"), "ok")
107+
self.assertIn("created", result)
108+
self.assertEqual(len(result["created"]), len(users_data))
109+
# Verify each created user
110+
for user_record in result["created"]:
111+
self.assertIn("token", user_record)
112+
self.assertIn("profile", user_record)
113+
self.assertEqual(user_record["profile"]["email"], user_record["profile"]["email"])
114+
return [user["profile"]["email"] for user in users_data]
115+
76116
def test_get_user(self):
77117
"""Test user retrieval."""
78118
email = self.test_create_user()

0 commit comments

Comments
 (0)