@@ -291,3 +291,215 @@ def test_get_user_email_from_handle(mock_get_all_users):
291291292292 )
293293 assert users .get_user_email_from_handle (client , "@unknown_name" ) is None
294+
295+
296+ @patch ("integrations.slack.users.SlackClientManager" )
297+ def test_replace_users_emails_with_mention_success (mock_slack_client_manager ):
298+ mock_client = MagicMock ()
299+ mock_slack_client_manager .get_client .return_value = mock_client
300+ mock_client .users_lookupByEmail .return_value = {"user" : {"id" : "U12345" }}
301+
302+ text = "Please contact [email protected] for more information." 303+ result = users .replace_users_emails_with_mention (text )
304+
305+ assert result == "Please contact <@U12345> for more information."
306+ mock_client .users_lookupByEmail .assert_called_once_with (
307+ 308+ )
309+
310+
311+ @patch ("integrations.slack.users.SlackClientManager" )
312+ def test_replace_users_emails_with_mention_multiple_emails (mock_slack_client_manager ):
313+ mock_client = MagicMock ()
314+ mock_slack_client_manager .get_client .return_value = mock_client
315+
316+ def mock_lookup_by_email (email ):
317+ 318+ return {"user" : {"id" : "U12345" }}
319+ elif email == "[email protected] " :
320+ return {"user" : {"id" : "U67890" }}
321+ return None
322+
323+ mock_client .users_lookupByEmail .side_effect = mock_lookup_by_email
324+
325+ 326+ result = users .replace_users_emails_with_mention (text )
327+
328+ assert result == "Contact <@U12345> or <@U67890> for help."
329+ assert mock_client .users_lookupByEmail .call_count == 2
330+
331+
332+ @patch ("integrations.slack.users.SlackClientManager" )
333+ def test_replace_users_emails_with_mention_no_user_found (mock_slack_client_manager ):
334+ mock_client = MagicMock ()
335+ mock_slack_client_manager .get_client .return_value = mock_client
336+ mock_client .users_lookupByEmail .return_value = None
337+
338+ text = "Please contact [email protected] for more information." 339+ result = users .replace_users_emails_with_mention (text )
340+
341+ assert result == "Please contact [email protected] for more information." 342+ mock_client .
users_lookupByEmail .
assert_called_once_with (
email = "[email protected] " )
343+
344+
345+ @patch ("integrations.slack.users.SlackClientManager" )
346+ def test_replace_users_emails_with_mention_no_user_id (mock_slack_client_manager ):
347+ mock_client = MagicMock ()
348+ mock_slack_client_manager .get_client .return_value = mock_client
349+ mock_client .users_lookupByEmail .return_value = {"user" : {}}
350+
351+ text = "Please contact [email protected] for more information." 352+ result = users .replace_users_emails_with_mention (text )
353+
354+ assert result == "Please contact [email protected] for more information." 355+ mock_client .users_lookupByEmail .assert_called_once_with (
356+ 357+ )
358+
359+
360+ @patch ("integrations.slack.users.SlackClientManager" )
361+ def test_replace_users_emails_with_mention_no_client (mock_slack_client_manager ):
362+ mock_slack_client_manager .get_client .return_value = None
363+
364+ text = "Please contact [email protected] for more information." 365+ result = users .replace_users_emails_with_mention (text )
366+
367+ assert result == "Please contact [email protected] for more information." 368+
369+
370+ @patch ("integrations.slack.users.SlackClientManager" )
371+ def test_replace_users_emails_with_mention_no_emails (mock_slack_client_manager ):
372+ mock_client = MagicMock ()
373+ mock_slack_client_manager .get_client .return_value = mock_client
374+
375+ text = "This text has no email addresses in it."
376+ result = users .replace_users_emails_with_mention (text )
377+
378+ assert result == "This text has no email addresses in it."
379+ mock_client .users_lookupByEmail .assert_not_called ()
380+
381+
382+ @patch ("integrations.slack.users.SlackClientManager" )
383+ def test_replace_users_emails_with_mention_empty_text (mock_slack_client_manager ):
384+ mock_client = MagicMock ()
385+ mock_slack_client_manager .get_client .return_value = mock_client
386+
387+ text = ""
388+ result = users .replace_users_emails_with_mention (text )
389+
390+ assert result == ""
391+ mock_client .users_lookupByEmail .assert_not_called ()
392+
393+
394+ @patch ("integrations.slack.users.replace_users_emails_with_mention" )
395+ def test_replace_users_emails_in_dict_string_value (mock_replace_function ):
396+ mock_replace_function .return_value = "replaced text"
397+
398+ 399+ result = users .replace_users_emails_in_dict (data )
400+
401+ assert result == "replaced text"
402+ mock_replace_function .
assert_called_once_with (
"[email protected] " )
403+
404+
405+ @patch ("integrations.slack.users.replace_users_emails_with_mention" )
406+ def test_replace_users_emails_in_dict_simple_dict (mock_replace_function ):
407+ mock_replace_function .side_effect = lambda x : x .replace (
408+ 409+ )
410+
411+ data = {
"message" :
"Contact [email protected] " ,
"title" :
"Important" }
412+ result = users .replace_users_emails_in_dict (data )
413+
414+ expected = {"message" : "Contact <@U12345>" , "title" : "Important" }
415+ assert result == expected
416+
417+
418+ @patch ("integrations.slack.users.replace_users_emails_with_mention" )
419+ def test_replace_users_emails_in_dict_nested_dict (mock_replace_function ):
420+ mock_replace_function .side_effect = lambda x : (
421+ x .
replace (
"[email protected] " ,
"<@U67890>" )
if "[email protected] " in x else x 422+ )
423+
424+ data = {
425+ "user" : {
"contact" :
"[email protected] " ,
"name" :
"Jane" },
426+ "metadata" : {"created_by" : "system" },
427+ }
428+ result = users .replace_users_emails_in_dict (data )
429+
430+ expected = {
431+ "user" : {"contact" : "<@U67890>" , "name" : "Jane" },
432+ "metadata" : {"created_by" : "system" },
433+ }
434+ assert result == expected
435+
436+
437+ @patch ("integrations.slack.users.replace_users_emails_with_mention" )
438+ def test_replace_users_emails_in_dict_list_of_strings (mock_replace_function ):
439+ mock_replace_function .side_effect = lambda x : (
440+ x .
replace (
"[email protected] " ,
"<@U11111>" )
if "[email protected] " in x else x 441+ )
442+
443+ data = [
"Contact [email protected] " ,
"No email here" ,
"Another message" ]
444+ result = users .replace_users_emails_in_dict (data )
445+
446+ expected = ["Contact <@U11111>" , "No email here" , "Another message" ]
447+ assert result == expected
448+
449+
450+ @patch ("integrations.slack.users.replace_users_emails_with_mention" )
451+ def test_replace_users_emails_in_dict_list_of_dicts (mock_replace_function ):
452+ mock_replace_function .side_effect = lambda x : (
453+ x .
replace (
"[email protected] " ,
"<@U22222>" )
if "[email protected] " in x else x 454+ )
455+
456+ data = [
457+ {
"message" :
"Contact [email protected] " ,
"priority" :
"high" },
458+ {"message" : "No email" , "priority" : "low" },
459+ ]
460+ result = users .replace_users_emails_in_dict (data )
461+
462+ expected = [
463+ {"message" : "Contact <@U22222>" , "priority" : "high" },
464+ {"message" : "No email" , "priority" : "low" },
465+ ]
466+ assert result == expected
467+
468+
469+ @patch ("integrations.slack.users.replace_users_emails_with_mention" )
470+ def test_replace_users_emails_in_dict_mixed_types (mock_replace_function ):
471+ def mock_replacement (x ):
472+ 473+ return x .
replace (
"[email protected] " ,
"<@U33333>" )
474+ return x
475+
476+ mock_replace_function .side_effect = mock_replacement
477+
478+ data = {
479+ "text" :
"Contact [email protected] " ,
480+ "number" : 42 ,
481+ "boolean" : True ,
482+ "null_value" : None ,
483+ "list" : [
"[email protected] " ,
123 ,
False ],
484+ }
485+ result = users .replace_users_emails_in_dict (data )
486+
487+ expected = {
488+ "text" : "Contact <@U33333>" ,
489+ "number" : 42 ,
490+ "boolean" : True ,
491+ "null_value" : None ,
492+ "list" : ["<@U33333>" , 123 , False ],
493+ }
494+ assert result == expected
495+
496+
497+ @patch ("integrations.slack.users.replace_users_emails_with_mention" )
498+ def test_replace_users_emails_in_dict_non_string_non_dict_non_list (
499+ mock_replace_function ,
500+ ):
501+ data = 42
502+ result = users .replace_users_emails_in_dict (data )
503+
504+ assert result == 42
505+ mock_replace_function .assert_not_called ()
0 commit comments