Skip to content

Commit

Permalink
Feat/aws groups sync completion notification (#709)
Browse files Browse the repository at this point in the history
* feat: Enhance group synchronization feedback

* test: Update group synchronization tests with datetime mocking
  • Loading branch information
gcharest authored Jan 10, 2025
1 parent a330efe commit dafdd4b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
7 changes: 7 additions & 0 deletions app/modules/aws/groups.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
import os
from modules.aws import identity_center
from modules.permissions import handler as permissions
Expand Down Expand Up @@ -62,13 +63,19 @@ 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,
enable_membership_create=True,
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.")
Expand Down
38 changes: 32 additions & 6 deletions app/tests/modules/aws/test_aws_groups.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest.mock import patch, MagicMock, call, ANY
import datetime
from modules.aws import groups


Expand Down Expand Up @@ -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()
Expand All @@ -77,6 +79,10 @@ def test_request_groups_sync_synchronizes_groups(
mock_slack_users.get_user_email_from_body.return_value = "[email protected]"
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)

Expand All @@ -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 = "[email protected]"
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)

Expand All @@ -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")
Expand Down

0 comments on commit dafdd4b

Please sign in to comment.