Skip to content

Commit 765dc4c

Browse files
author
Nivedithaa Mahendran
committed
update
1 parent debfd6a commit 765dc4c

2 files changed

Lines changed: 40 additions & 13 deletions

File tree

src/mas/devops/users.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,30 +228,36 @@ def get_or_create_user(self, payload):
228228
"""
229229
Get an existing user or create a new one if not found.
230230
231-
This method is idempotent - if the user already exists (identified by payload["id"]),
232-
their existing record is returned without modification. If the user doesn't exist,
233-
they are created with the provided payload.
231+
This method is idempotent - if the user already exists (identified by payload["id"]
232+
for version < 9.1 or payload["personid"] for version >= 9.1), their existing record
233+
is returned without modification. If the user doesn't exist, they are created with
234+
the provided payload.
234235
235236
For MAS version >= 9.1, this method uses the Manage API masapiuser endpoint.
236237
For earlier versions, it uses the Core API v3/users endpoint.
237238
238239
Args:
239240
payload (dict): User definition dictionary containing user details.
240-
Must include "id" field as the unique identifier.
241+
Must include "id" field (version < 9.1) or "personid" field
242+
(version >= 9.1) as the unique identifier.
241243
242244
Returns:
243245
dict: The user record (either existing or newly created).
244246
245247
Raises:
246248
Exception: If user creation fails with an unexpected status code.
247249
"""
248-
existing_user = self.get_user(payload["id"])
250+
# Determine the user ID field based on version
251+
user_id_field = "personid" if Version(self.mas_version) >= Version('9.1') else "id"
252+
user_id = payload[user_id_field]
253+
254+
existing_user = self.get_user(user_id)
249255

250256
if existing_user is not None:
251257
self.logger.info(f"Existing user {existing_user['id']} found")
252258
return existing_user
253259

254-
self.logger.info(f"Creating new user {payload['id']}")
260+
self.logger.info(f"Creating new user {user_id}")
255261

256262
# For MAS version >= 9.1, use the Manage API masapiuser endpoint
257263
if Version(self.mas_version) >= Version('9.1'):
@@ -266,7 +272,7 @@ def get_or_create_user(self, payload):
266272
"Content-Type": "application/json",
267273
"apikey": maxadmin_manage_api_key["apikey"]
268274
}
269-
self.logger.info(f"Creating new user {payload['id']} with Manage API with payload {payload}")
275+
self.logger.info(f"Creating new user {user_id} with Manage API with payload {payload}")
270276
response = requests.post(
271277
url,
272278
json=payload,

test/src/test_users.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,17 @@ def test_get_or_create_user_exists(user_utils, requests_mock, mock_manage_api_ke
367367
request_headers={"apikey": mock_manage_api_key["apikey"]},
368368
json={"id": user_id},
369369
status_code=201,
370-
additional_matcher=lambda req: additional_matcher(req, json={"id": user_id}, cert=PEM_PATH)
370+
additional_matcher=lambda req: additional_matcher(req, json={"personid": user_id}, cert=PEM_PATH)
371371
)
372372

373-
assert user_utils.get_or_create_user({"id": user_id}) == {"id": user_id, "displayName": user_id}
373+
# Use correct payload structure based on version
374+
from packaging.version import Version
375+
if Version(user_utils.mas_version) >= Version('9.1'):
376+
payload = {"personid": user_id}
377+
else:
378+
payload = {"id": user_id}
379+
380+
assert user_utils.get_or_create_user(payload) == {"id": user_id, "displayName": user_id}
374381
assert get.call_count == 1
375382
assert post_core.call_count == 0
376383
assert post_manage.call_count == 0
@@ -395,10 +402,17 @@ def test_get_or_create_user_notfound(user_utils, requests_mock, mock_manage_api_
395402
request_headers={"apikey": mock_manage_api_key["apikey"]},
396403
json={"id": user_id, "displayName": user_id},
397404
status_code=201,
398-
additional_matcher=lambda req: additional_matcher(req, json={"id": user_id}, cert=PEM_PATH)
405+
additional_matcher=lambda req: additional_matcher(req, json={"personid": user_id}, cert=PEM_PATH)
399406
)
400407

401-
assert user_utils.get_or_create_user({"id": user_id}) == {"id": user_id, "displayName": user_id}
408+
# Use correct payload structure based on version
409+
from packaging.version import Version
410+
if Version(user_utils.mas_version) >= Version('9.1'):
411+
payload = {"personid": user_id}
412+
else:
413+
payload = {"id": user_id}
414+
415+
assert user_utils.get_or_create_user(payload) == {"id": user_id, "displayName": user_id}
402416
assert get.call_count == 1
403417
# Check that the correct endpoint was called based on version
404418
if user_utils.mas_version >= '9.1':
@@ -428,11 +442,18 @@ def test_get_or_create_user_error(user_utils, requests_mock, mock_manage_api_key
428442
request_headers={"apikey": mock_manage_api_key["apikey"]},
429443
json={"error": "unknown"},
430444
status_code=500,
431-
additional_matcher=lambda req: additional_matcher(req, json={"id": user_id}, cert=PEM_PATH)
445+
additional_matcher=lambda req: additional_matcher(req, json={"personid": user_id}, cert=PEM_PATH)
432446
)
433447

448+
# Use correct payload structure based on version
449+
from packaging.version import Version
450+
if Version(user_utils.mas_version) >= Version('9.1'):
451+
payload = {"personid": user_id}
452+
else:
453+
payload = {"id": user_id}
454+
434455
with pytest.raises(Exception):
435-
user_utils.get_or_create_user({"id": user_id})
456+
user_utils.get_or_create_user(payload)
436457
assert get.call_count == 1
437458
# Check that the correct endpoint was called based on version
438459
if user_utils.mas_version >= '9.1':

0 commit comments

Comments
 (0)