Skip to content

Commit

Permalink
fix: add error handling for list groups with members
Browse files Browse the repository at this point in the history
  • Loading branch information
gcharest authored Oct 1, 2024
1 parent 128719a commit 98f61ca
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
28 changes: 19 additions & 9 deletions app/integrations/google_workspace/google_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,26 @@ def list_groups_with_members(
groups_with_members = []
for group in groups:
logger.info(f"Getting members for group: {group['email']}")
members = list_group_members(
group["email"], fields="members(email, role, type, status)"
)
try:
members = list_group_members(
group["email"], fields="members(email, role, type, status)"
)
except Exception as e:
logger.warning(f"Error getting members for group {group['email']}: {e}")
continue
if members and members_details:
detailed_members = []
for member in members:
logger.info(f"Getting user details for member: {member['email']}")
detailed_members.append(
get_user(member["email"], fields="name, primaryEmail")
try:
for member in members:
logger.info(f"Getting user details for member: {member['email']}")
detailed_members.append(
get_user(member["email"], fields="name, primaryEmail")
)
group["members"] = detailed_members
groups_with_members.append(group)
except Exception as e:
logger.warning(
f"Error getting user details for group {member['email']}: {e}"
)
group["members"] = detailed_members
groups_with_members.append(group)
continue
return groups_with_members
50 changes: 50 additions & 0 deletions app/tests/integrations/google_workspace/test_google_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,33 @@ def test_list_groups_with_members_filtered(
assert mock_get_user.call_count == 2


@patch("integrations.google_workspace.google_directory.list_groups")
@patch("integrations.google_workspace.google_directory.list_group_members")
@patch("integrations.google_workspace.google_directory.get_user")
def test_list_groups_with_members_error_in_list_group_members(
mock_get_user,
mock_list_group_members,
mock_list_groups,
google_groups,
google_group_members,
google_users,
google_groups_w_users
):
groups = google_groups(2)
group_members = [Exception("Error fetching group members"), google_group_members(2)]
users = google_users(2)

mock_list_groups.return_value = groups
mock_list_group_members.side_effect = group_members
mock_get_user.side_effect = users

# Only the second group should be processed
expected_groups_with_users = [groups[1]]
expected_groups_with_users[0]["members"] = users

assert google_directory.list_groups_with_members() == expected_groups_with_users


@patch("integrations.google_workspace.google_directory.list_groups")
@patch("integrations.google_workspace.google_directory.list_group_members")
@patch("integrations.google_workspace.google_directory.get_user")
Expand Down Expand Up @@ -437,6 +464,29 @@ def test_list_groups_with_members_without_members_enabled(
mock_get_user.assert_not_called()


@patch("integrations.google_workspace.google_directory.list_groups")
@patch("integrations.google_workspace.google_directory.list_group_members")
@patch("integrations.google_workspace.google_directory.get_user")
def test_list_groups_with_members_error_in_get_user(
mock_get_user,
mock_list_group_members,
mock_list_groups,
google_groups,
google_group_members,
google_users,
):
groups = google_groups(2)
group_members = [google_group_members(2), google_group_members(2)]
users = [Exception("Error fetching user details"), google_users(1)[0]]

mock_list_groups.return_value = groups
mock_list_group_members.side_effect = group_members
mock_get_user.side_effect = users

# No groups should be processed due to error in get_user
assert google_directory.list_groups_with_members() == []


@patch("integrations.google_workspace.google_directory.list_groups")
def test_list_groups_with_members_skips_when_no_groups(mock_list_groups):
mock_list_groups.return_value = []
Expand Down

0 comments on commit 98f61ca

Please sign in to comment.