Skip to content

Commit

Permalink
[GMail] Validate Google Directory auth (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
timgrein authored Sep 4, 2023
1 parent e852b8f commit 9663e22
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
9 changes: 9 additions & 0 deletions connectors/sources/gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ async def validate_config(self):
f"Subject field value needs to be a valid email address. '{subject}' is invalid."
)

await self._validate_google_directory_auth()
await self._validate_gmail_auth()

async def _validate_gmail_auth(self):
Expand All @@ -188,6 +189,14 @@ async def _validate_gmail_auth(self):
f"GMail authentication was not successful. Check the values of the following fields: '{SERVICE_ACCOUNT_CREDENTIALS_LABEL}', '{SUBJECT_LABEL}' and '{CUSTOMER_ID_LABEL}'. Also make sure that the OAuth2 scopes for GMail are setup correctly."
) from e

async def _validate_google_directory_auth(self):
try:
await self._google_directory_client.ping()
except AuthError as e:
raise ConfigurableFieldValueError(
f"Google Directory authentication was not successful. Check the values of the following fields: '{SERVICE_ACCOUNT_CREDENTIALS_LABEL}', '{SUBJECT_LABEL}' and '{CUSTOMER_ID_LABEL}'. Also make sure that the OAuth2 scopes for Google Directory are setup correctly."
) from e

def advanced_rules_validators(self):
return [GMailAdvancedRulesValidator()]

Expand Down
25 changes: 23 additions & 2 deletions tests/sources/test_gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ async def test_ping_google_directory_client_fails(
await source.ping()

@pytest.mark.asyncio
async def test_validate_config_valid(self, patch_gmail_client):
async def test_validate_config_valid(
self, patch_gmail_client, patch_google_directory_client
):
valid_json = '{"project_id": "dummy123"}'

async with create_gmail_source() as source:
Expand All @@ -219,6 +221,7 @@ async def test_validate_config_valid(self, patch_gmail_client):
source.configuration.get_field("subject").value = SUBJECT

patch_gmail_client.ping = AsyncMock()
patch_google_directory_client.ping = AsyncMock()

try:
await source.validate_config()
Expand All @@ -244,18 +247,36 @@ async def test_validate_config_invalid_subject(self):
await source.validate_config()

@pytest.mark.asyncio
async def test_validate_config_invalid_gmail_auth(self, patch_gmail_client):
async def test_validate_config_invalid_gmail_auth(
self, patch_gmail_client, patch_google_directory_client
):
async with create_gmail_source() as source:
patch_gmail_client.ping = AsyncMock(
side_effect=AuthError("some auth error")
)
patch_google_directory_client.ping = AsyncMock()

with pytest.raises(ConfigurableFieldValueError) as e:
await source.validate_config()

# Make sure this is a GMail auth error
assert "GMail auth" in str(e.value)

@pytest.mark.asyncio
async def test_validate_config_invalid_google_directory_auth(
self, patch_google_directory_client
):
async with create_gmail_source() as source:
patch_google_directory_client.ping = AsyncMock(
side_effect=AuthError("some auth error")
)

with pytest.raises(ConfigurableFieldValueError) as e:
await source.validate_config()

# Make sure this is a Google Directory auth error
assert "Google Directory auth" in str(e.value)

@pytest.mark.asyncio
async def test_get_access_control_with_dls_disabled(
self, patch_google_directory_client
Expand Down

0 comments on commit 9663e22

Please sign in to comment.