@@ -256,6 +256,7 @@ def test_lookup_user_directly_server_dc(self, users_mixin):
256256 users_mixin .jira .user_find_by_user_string .return_value = [
257257 {
258258 "key" : "server-user-key" ,
259+ "name" : "server-user-name" ,
259260 "displayName" : "Test User" ,
260261 "emailAddress" : "test@example.com" ,
261262 }
@@ -268,7 +269,32 @@ def test_lookup_user_directly_server_dc(self, users_mixin):
268269 # Call the method
269270 account_id = users_mixin ._lookup_user_directly ("Test User" )
270271
271- # Verify result
272+ # Verify result - should now return name instead of key for Server/DC
273+ assert account_id == "server-user-name"
274+ # Verify API call with username parameter for Server/DC
275+ users_mixin .jira .user_find_by_user_string .assert_called_once_with (
276+ username = "Test User" , start = 0 , limit = 1
277+ )
278+
279+ def test_lookup_user_directly_server_dc_key_fallback (self , users_mixin ):
280+ """Test _lookup_user_directly for Server/DC falls back to key when name is not available."""
281+ # Mock the API response
282+ users_mixin .jira .user_find_by_user_string .return_value = [
283+ {
284+ "key" : "server-user-key" , # Only key, no name
285+ "displayName" : "Test User" ,
286+ "emailAddress" : "test@example.com" ,
287+ }
288+ ]
289+
290+ # Mock config.is_cloud to return False for Server/DC
291+ users_mixin .config = MagicMock ()
292+ users_mixin .config .is_cloud = False
293+
294+ # Call the method
295+ account_id = users_mixin ._lookup_user_directly ("Test User" )
296+
297+ # Verify result - should fallback to key when name is missing
272298 assert account_id == "server-user-key"
273299 # Verify API call with username parameter for Server/DC
274300 users_mixin .jira .user_find_by_user_string .assert_called_once_with (
@@ -301,14 +327,18 @@ def test_lookup_user_directly_jira_data_center_key(self, users_mixin):
301327 }
302328 ]
303329
330+ # Mock config.is_cloud to return False for Server/DC
331+ users_mixin .config = MagicMock ()
332+ users_mixin .config .is_cloud = False
333+
304334 # Call the method
305335 account_id = users_mixin ._lookup_user_directly ("Test User" )
306336
307337 # Verify result
308338 assert account_id == "data-center-key"
309339 # Verify API call
310340 users_mixin .jira .user_find_by_user_string .assert_called_once_with (
311- query = "Test User" , start = 0 , limit = 1
341+ username = "Test User" , start = 0 , limit = 1
312342 )
313343
314344 def test_lookup_user_directly_jira_data_center_name (self , users_mixin ):
@@ -322,14 +352,18 @@ def test_lookup_user_directly_jira_data_center_name(self, users_mixin):
322352 }
323353 ]
324354
355+ # Mock config.is_cloud to return False for Server/DC
356+ users_mixin .config = MagicMock ()
357+ users_mixin .config .is_cloud = False
358+
325359 # Call the method
326360 account_id = users_mixin ._lookup_user_directly ("Test User" )
327361
328362 # Verify result
329363 assert account_id == "data-center-name"
330364 # Verify API call
331365 users_mixin .jira .user_find_by_user_string .assert_called_once_with (
332- query = "Test User" , start = 0 , limit = 1
366+ username = "Test User" , start = 0 , limit = 1
333367 )
334368
335369 def test_lookup_user_directly_error (self , users_mixin ):
@@ -382,22 +416,31 @@ def test_lookup_user_by_permissions_not_found(self, users_mixin):
382416 # Verify result
383417 assert account_id is None
384418
385- def test_lookup_user_by_permissions_jira_data_center_key (self , users_mixin ):
386- """Test _lookup_user_by_permissions when only 'key' is available (Data Center)."""
419+ def test_lookup_user_by_permissions_jira_data_center (self , users_mixin ):
420+ """Test _lookup_user_by_permissions when both 'key' and 'name' are available (Data Center)."""
387421 # Mock requests.get
388422 with patch ("requests.get" ) as mock_get :
389423 mock_response = MagicMock ()
390424 mock_response .status_code = 200
391425 mock_response .json .return_value = {
392- "users" : [{"key" : "data-center-permissions-key" }]
426+ "users" : [
427+ {
428+ "key" : "data-center-permissions-key" ,
429+ "name" : "data-center-permissions-name" ,
430+ }
431+ ]
393432 }
394433 mock_get .return_value = mock_response
395434
435+ # Mock config.is_cloud to return False for Server/DC
436+ users_mixin .config = MagicMock ()
437+ users_mixin .config .is_cloud = False
438+
396439 # Call the method
397440 account_id = users_mixin ._lookup_user_by_permissions ("username" )
398441
399- # Verify result
400- assert account_id == "data-center-permissions-key "
442+ # Verify result - should prioritize name for Server/DC
443+ assert account_id == "data-center-permissions-name "
401444 # Verify API call
402445 mock_get .assert_called_once ()
403446 assert mock_get .call_args [0 ][0 ].endswith ("/user/permission/search" )
@@ -406,22 +449,28 @@ def test_lookup_user_by_permissions_jira_data_center_key(self, users_mixin):
406449 "permissions" : "BROWSE" ,
407450 }
408451
409- def test_lookup_user_by_permissions_jira_data_center_name (self , users_mixin ):
410- """Test _lookup_user_by_permissions when only 'name' is available (Data Center)."""
452+ def test_lookup_user_by_permissions_jira_data_center_key_fallback (
453+ self , users_mixin
454+ ):
455+ """Test _lookup_user_by_permissions when only 'key' is available (Data Center)."""
411456 # Mock requests.get
412457 with patch ("requests.get" ) as mock_get :
413458 mock_response = MagicMock ()
414459 mock_response .status_code = 200
415460 mock_response .json .return_value = {
416- "users" : [{"name " : "data-center-permissions-name " }]
461+ "users" : [{"key " : "data-center-permissions-key " }]
417462 }
418463 mock_get .return_value = mock_response
419464
465+ # Mock config.is_cloud to return False for Server/DC
466+ users_mixin .config = MagicMock ()
467+ users_mixin .config .is_cloud = False
468+
420469 # Call the method
421470 account_id = users_mixin ._lookup_user_by_permissions ("username" )
422471
423- # Verify result
424- assert account_id == "data-center-permissions-name "
472+ # Verify result - should fallback to key when name is missing
473+ assert account_id == "data-center-permissions-key "
425474 # Verify API call
426475 mock_get .assert_called_once ()
427476 assert mock_get .call_args [0 ][0 ].endswith ("/user/permission/search" )
@@ -439,3 +488,31 @@ def test_lookup_user_by_permissions_error(self, users_mixin):
439488
440489 # Verify result
441490 assert account_id is None
491+
492+ def test_lookup_user_by_permissions_jira_data_center_name_only (self , users_mixin ):
493+ """Test _lookup_user_by_permissions when only 'name' is available (Data Center)."""
494+ # Mock requests.get
495+ with patch ("requests.get" ) as mock_get :
496+ mock_response = MagicMock ()
497+ mock_response .status_code = 200
498+ mock_response .json .return_value = {
499+ "users" : [{"name" : "data-center-permissions-name" }]
500+ }
501+ mock_get .return_value = mock_response
502+
503+ # Mock config.is_cloud to return False for Server/DC
504+ users_mixin .config = MagicMock ()
505+ users_mixin .config .is_cloud = False
506+
507+ # Call the method
508+ account_id = users_mixin ._lookup_user_by_permissions ("username" )
509+
510+ # Verify result - should use name when that's all that's available
511+ assert account_id == "data-center-permissions-name"
512+ # Verify API call
513+ mock_get .assert_called_once ()
514+ assert mock_get .call_args [0 ][0 ].endswith ("/user/permission/search" )
515+ assert mock_get .call_args [1 ]["params" ] == {
516+ "query" : "username" ,
517+ "permissions" : "BROWSE" ,
518+ }
0 commit comments