Skip to content

Commit 10eee9b

Browse files
authored
Allow searching for users with all roles in a tenant (#637)
* Allow searching for users with all roles in a tenant * Oopsy, forgot import * Tests
1 parent 3770d75 commit 10eee9b

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

descope/management/common.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,3 @@ def sort_to_dict(sort: List[Sort]) -> list:
407407
}
408408
)
409409
return sort_list
410-
411-
412-
def map_to_values_object(input_map: dict):
413-
if not input_map:
414-
return {}
415-
return {k: {"values": v} for k, v in input_map.items()}

descope/management/user.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
MgmtV1,
1010
Sort,
1111
associated_tenants_to_dict,
12-
map_to_values_object,
1312
sort_to_dict,
1413
)
1514
from descope.management.user_pwd import UserPassword
@@ -666,7 +665,9 @@ def search_all(
666665
to_modified_time (int): Optional int, only include users whose last modification/update occurred on or before this time (in Unix epoch milliseconds)
667666
user_ids (List[str]): Optional list of user IDs to filter by
668667
tenant_role_ids (dict): Optional mapping of tenant ID to list of role IDs.
668+
Dict value is in the form of {"tenant_id": {"values":["role_id1", "role_id2"], "and": True}} if you want to match all roles (AND) or any role (OR).
669669
tenant_role_names (dict): Optional mapping of tenant ID to list of role names.
670+
Dict value is in the form of {"tenant_id": {"values":["role_name1", "role_name2"], "and": True}} if you want to match all roles (AND) or any role (OR).
670671
671672
Return value (dict):
672673
Return dict in the format
@@ -733,9 +734,9 @@ def search_all(
733734
body["toModifiedTime"] = to_modified_time
734735

735736
if tenant_role_ids is not None:
736-
body["tenantRoleIds"] = map_to_values_object(tenant_role_ids)
737+
body["tenantRoleIds"] = tenant_role_ids
737738
if tenant_role_names is not None:
738-
body["tenantRoleNames"] = map_to_values_object(tenant_role_names)
739+
body["tenantRoleNames"] = tenant_role_names
739740

740741
response = self._auth.do_post(
741742
MgmtV1.users_search_path,
@@ -786,7 +787,9 @@ def search_all_test_users(
786787
from_modified_time (int): Optional int, only include users whose last modification/update occurred on or after this time (in Unix epoch milliseconds)
787788
to_modified_time (int): Optional int, only include users whose last modification/update occurred on or before this time (in Unix epoch milliseconds)
788789
tenant_role_ids (dict): Optional mapping of tenant ID to list of role IDs.
790+
Dict value is in the form of {"tenant_id": {"values":["role_id1", "role_id2"], "and": True}} if you want to match all roles (AND) or any role (OR).
789791
tenant_role_names (dict): Optional mapping of tenant ID to list of role names.
792+
Dict value is in the form of {"tenant_id": {"values":["role_name1", "role_name2"], "and": True}} if you want to match all roles (AND) or any role (OR).
790793
791794
Return value (dict):
792795
Return dict in the format
@@ -850,9 +853,9 @@ def search_all_test_users(
850853
body["toModifiedTime"] = to_modified_time
851854

852855
if tenant_role_ids is not None:
853-
body["tenantRoleIds"] = map_to_values_object(tenant_role_ids)
856+
body["tenantRoleIds"] = tenant_role_ids
854857
if tenant_role_names is not None:
855-
body["tenantRoleNames"] = map_to_values_object(tenant_role_names)
858+
body["tenantRoleNames"] = tenant_role_names
856859

857860
response = self._auth.do_post(
858861
MgmtV1.test_users_search_path,

tests/management/test_user.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def test_invite_batch(self):
323323
users = resp["users"]
324324
self.assertEqual(users[0]["id"], "u1")
325325

326-
expectedUsers = {
326+
expected_users = {
327327
"users": [
328328
{
329329
"loginId": "[email protected]",
@@ -369,7 +369,7 @@ def test_invite_batch(self):
369369
"x-descope-project-id": self.dummy_project_id,
370370
},
371371
params=None,
372-
json=expectedUsers,
372+
json=expected_users,
373373
allow_redirects=False,
374374
verify=True,
375375
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -403,8 +403,8 @@ def test_invite_batch(self):
403403
send_sms=True,
404404
)
405405

406-
del expectedUsers["users"][0]["hashedPassword"]
407-
expectedUsers["users"][0]["password"] = "clear"
406+
del expected_users["users"][0]["hashedPassword"]
407+
expected_users["users"][0]["password"] = "clear"
408408
mock_post.assert_called_with(
409409
f"{common.DEFAULT_BASE_URL}{MgmtV1.user_create_batch_path}",
410410
headers={
@@ -413,7 +413,7 @@ def test_invite_batch(self):
413413
"x-descope-project-id": self.dummy_project_id,
414414
},
415415
params=None,
416-
json=expectedUsers,
416+
json=expected_users,
417417
allow_redirects=False,
418418
verify=True,
419419
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -426,7 +426,7 @@ def test_invite_batch(self):
426426
send_sms=True,
427427
)
428428

429-
del expectedUsers["users"][0]["password"]
429+
del expected_users["users"][0]["password"]
430430
mock_post.assert_called_with(
431431
f"{common.DEFAULT_BASE_URL}{MgmtV1.user_create_batch_path}",
432432
headers={
@@ -435,7 +435,7 @@ def test_invite_batch(self):
435435
"x-descope-project-id": self.dummy_project_id,
436436
},
437437
params=None,
438-
json=expectedUsers,
438+
json=expected_users,
439439
allow_redirects=False,
440440
verify=True,
441441
timeout=DEFAULT_TIMEOUT_SECONDS,
@@ -1056,8 +1056,8 @@ def test_search_all(self):
10561056
)
10571057
mock_post.return_value = network_resp
10581058
resp = self.client.mgmt.user.search_all(
1059-
tenant_role_ids={"tenant1": ["roleA", "roleB"]},
1060-
tenant_role_names={"tenant2": ["admin", "user"]},
1059+
tenant_role_ids={"tenant1": {"values": ["roleA", "roleB"], "and": True}},
1060+
tenant_role_names={"tenant2": {"values": ["admin", "user"], "and": False}},
10611061
)
10621062
users = resp["users"]
10631063
self.assertEqual(len(users), 2)
@@ -1078,8 +1078,8 @@ def test_search_all(self):
10781078
"page": 0,
10791079
"testUsersOnly": False,
10801080
"withTestUser": False,
1081-
"tenantRoleIds": {"tenant1": {"values": ["roleA", "roleB"]}},
1082-
"tenantRoleNames": {"tenant2": {"values": ["admin", "user"]}},
1081+
"tenantRoleIds": {"tenant1": {"values": ["roleA", "roleB"], "and": True}},
1082+
"tenantRoleNames": {"tenant2": {"values": ["admin", "user"], "and": False}},
10831083
},
10841084
allow_redirects=False,
10851085
verify=True,
@@ -1302,8 +1302,8 @@ def test_search_all_test_users(self):
13021302
)
13031303
mock_post.return_value = network_resp
13041304
resp = self.client.mgmt.user.search_all_test_users(
1305-
tenant_role_ids={"tenant1": ["roleA", "roleB"]},
1306-
tenant_role_names={"tenant2": ["admin", "user"]},
1305+
tenant_role_ids={"tenant1": {"values": ["roleA", "roleB"], "and": True}},
1306+
tenant_role_names={"tenant2": {"values": ["admin", "user"], "and": False}},
13071307
)
13081308
users = resp["users"]
13091309
self.assertEqual(len(users), 2)
@@ -1324,8 +1324,8 @@ def test_search_all_test_users(self):
13241324
"page": 0,
13251325
"testUsersOnly": True,
13261326
"withTestUser": True,
1327-
"tenantRoleIds": {"tenant1": {"values": ["roleA", "roleB"]}},
1328-
"tenantRoleNames": {"tenant2": {"values": ["admin", "user"]}},
1327+
"tenantRoleIds": {"tenant1": {"values": ["roleA", "roleB"], "and": True}},
1328+
"tenantRoleNames": {"tenant2": {"values": ["admin", "user"], "and": False}},
13291329
},
13301330
allow_redirects=False,
13311331
verify=True,

0 commit comments

Comments
 (0)