From dafdd4b5d5af80d5bff1e15327f1a491214ee03f Mon Sep 17 00:00:00 2001 From: Guillaume Charest <1690085+gcharest@users.noreply.github.com> Date: Fri, 10 Jan 2025 14:56:27 -0500 Subject: [PATCH] Feat/aws groups sync completion notification (#709) * feat: Enhance group synchronization feedback * test: Update group synchronization tests with datetime mocking --- app/modules/aws/groups.py | 7 +++++ app/tests/modules/aws/test_aws_groups.py | 38 ++++++++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/modules/aws/groups.py b/app/modules/aws/groups.py index 48134fe3..5d4b342f 100644 --- a/app/modules/aws/groups.py +++ b/app/modules/aws/groups.py @@ -1,3 +1,4 @@ +from datetime import datetime import os from modules.aws import identity_center from modules.permissions import handler as permissions @@ -62,6 +63,7 @@ def request_groups_sync(client, body, respond, args, logger): ) logger.info("Synchronizing AWS Identity Center Groups.") respond("AWS Groups Memberships Synchronization Initiated.") + start_time = datetime.now() identity_center.synchronize( enable_users_sync=False, enable_user_create=False, @@ -69,6 +71,11 @@ def request_groups_sync(client, body, respond, args, logger): enable_membership_delete=True, pre_processing_filters=pre_processing_filters, ) + end_time = datetime.now() + time = end_time - start_time + respond( + f"AWS Groups Memberships Synchronization Completed in {time.total_seconds():.6f} seconds." + ) else: logger.error(f"User {requestor_email} does not have permission to sync groups.") respond("You do not have permission to sync groups.") diff --git a/app/tests/modules/aws/test_aws_groups.py b/app/tests/modules/aws/test_aws_groups.py index d3ce36cc..20566c83 100644 --- a/app/tests/modules/aws/test_aws_groups.py +++ b/app/tests/modules/aws/test_aws_groups.py @@ -1,4 +1,5 @@ from unittest.mock import patch, MagicMock, call, ANY +import datetime from modules.aws import groups @@ -62,11 +63,12 @@ def test_aws_groups_command_handles_list_command(mock_request_groups_list): logger.info.assert_not_called() +@patch("modules.aws.groups.datetime") @patch("modules.aws.groups.slack_users") @patch("modules.aws.groups.permissions") @patch("modules.aws.groups.identity_center") def test_request_groups_sync_synchronizes_groups( - mock_identity_center, mock_permissions, mock_slack_users + mock_identity_center, mock_permissions, mock_slack_users, mock_datetime ): client = MagicMock() body = MagicMock() @@ -77,6 +79,10 @@ def test_request_groups_sync_synchronizes_groups( mock_slack_users.get_user_email_from_body.return_value = "admin.user@test.com" mock_permissions.is_user_member_of_groups.return_value = True mock_identity_center.synchronize.return_value = None + mock_datetime.now.side_effect = [ + datetime.datetime(2021, 1, 1, 0, 0, 0), + datetime.datetime(2021, 1, 1, 0, 0, 20), + ] groups.request_groups_sync(client, body, respond, args, logger) @@ -92,24 +98,37 @@ def test_request_groups_sync_synchronizes_groups( enable_membership_delete=True, pre_processing_filters=[], ) - respond.assert_called_once_with("AWS Groups Memberships Synchronization Initiated.") + respond.assert_has_calls( + [ + call("AWS Groups Memberships Synchronization Initiated."), + call( + "AWS Groups Memberships Synchronization Completed in 20.000000 seconds." + ), + ] + ) +@patch("modules.aws.groups.datetime") @patch("modules.aws.groups.slack_users") -@patch("modules.aws.groups.permissions") +@patch( + "modules.aws.groups.permissions", +) @patch("modules.aws.groups.identity_center") def test_request_groups_sync_synchronizes_groups_with_args( - mock_identity_center, mock_permissions, mock_slack_users + mock_identity_center, mock_permissions, mock_slack_users, mock_datetime ): client = MagicMock() body = MagicMock() respond = MagicMock() args = ["group1", "group2"] logger = MagicMock() - mock_slack_users.get_user_email_from_body.return_value = "admin.user@test.com" mock_permissions.is_user_member_of_groups.return_value = True mock_identity_center.synchronize.return_value = None + mock_datetime.now.side_effect = [ + datetime.datetime(2021, 1, 1, 0, 0, 0), + datetime.datetime(2021, 1, 1, 0, 0, 20), + ] groups.request_groups_sync(client, body, respond, args, logger) @@ -125,7 +144,14 @@ def test_request_groups_sync_synchronizes_groups_with_args( enable_membership_delete=True, pre_processing_filters=ANY, ) - respond.assert_called_once_with("AWS Groups Memberships Synchronization Initiated.") + respond.assert_has_calls( + [ + call("AWS Groups Memberships Synchronization Initiated."), + call( + "AWS Groups Memberships Synchronization Completed in 20.000000 seconds." + ), + ] + ) @patch("modules.aws.groups.slack_users")