Skip to content

Commit d559bb1

Browse files
authored
Update login ID (#242)
* Update login ID + tests related to descope/etc#2985 Leave new id as empty to remove ID last login ID cannot be removed * remove settings string
1 parent d9598a3 commit d559bb1

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ descope_client.mgmt.user.update(
515515
)
516516

517517
# Update explicit data for a user rather than overriding all fields
518+
descope_client.mgmt.user.update_login_id(
519+
login_id="[email protected]",
520+
new_login_id="[email protected]"
521+
)
518522
descope_client.mgmt.user.update_phone(
519523
login_id="[email protected]",
520524
phone="+18005551234",

descope/management/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class MgmtV1:
1717
users_search_path = "/v1/mgmt/user/search"
1818
user_get_provider_token = "/v1/mgmt/user/provider/token"
1919
user_update_status_path = "/v1/mgmt/user/update/status"
20+
user_update_login_id_path = "/v1/mgmt/user/update/loginid"
2021
user_update_email_path = "/v1/mgmt/user/update/email"
2122
user_update_phone_path = "/v1/mgmt/user/update/phone"
2223
user_update_name_path = "/v1/mgmt/user/update/name"

descope/management/user.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,34 @@ def deactivate(
438438
)
439439
return response.json()
440440

441+
def update_login_id(
442+
self,
443+
login_id: str,
444+
new_login_id: str = None,
445+
) -> dict:
446+
"""
447+
Update login id of user, leave new login empty to remove the ID.
448+
A user must have at least one login ID. Trying to remove the last one will fail.
449+
450+
Args:
451+
login_id (str): The login ID of the user to update.
452+
new_login_id (str): New login ID to set for the user.
453+
454+
Return value (dict):
455+
Return dict in the format
456+
{"user": {}}
457+
Containing the updated user information.
458+
459+
Raise:
460+
AuthException: raised if the update operation fails
461+
"""
462+
response = self._auth.do_post(
463+
MgmtV1.user_update_login_id_path,
464+
{"loginId": login_id, "newLoginId": new_login_id},
465+
pswd=self._auth.management_key,
466+
)
467+
return response.json()
468+
441469
def update_email(
442470
self,
443471
login_id: str,

tests/management/test_user.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,42 @@ def test_deactivate(self):
559559
timeout=DEFAULT_TIMEOUT_SECONDS,
560560
)
561561

562+
def test_update_login_id(self):
563+
# Test failed flows
564+
with patch("requests.post") as mock_post:
565+
mock_post.return_value.ok = False
566+
self.assertRaises(
567+
AuthException,
568+
self.client.mgmt.user.update_login_id,
569+
"valid-id",
570+
571+
)
572+
573+
# Test success flow
574+
with patch("requests.post") as mock_post:
575+
network_resp = mock.Mock()
576+
network_resp.ok = True
577+
network_resp.json.return_value = json.loads("""{"user": {"id": "[email protected]"}}""")
578+
mock_post.return_value = network_resp
579+
resp = self.client.mgmt.user.update_login_id("valid-id", "[email protected]")
580+
user = resp["user"]
581+
self.assertEqual(user["id"], "[email protected]")
582+
mock_post.assert_called_with(
583+
f"{common.DEFAULT_BASE_URL}{MgmtV1.user_update_login_id_path}",
584+
headers={
585+
**common.default_headers,
586+
"Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}",
587+
},
588+
params=None,
589+
json={
590+
"loginId": "valid-id",
591+
"newLoginId": "[email protected]",
592+
},
593+
allow_redirects=False,
594+
verify=True,
595+
timeout=DEFAULT_TIMEOUT_SECONDS,
596+
)
597+
562598
def test_update_email(self):
563599
# Test failed flows
564600
with patch("requests.post") as mock_post:

0 commit comments

Comments
 (0)