From b6b3414c372882e4c1e005b02289c26c4b8ceafa Mon Sep 17 00:00:00 2001 From: Eric Jolibois Date: Wed, 18 Oct 2023 18:38:59 +0200 Subject: [PATCH] test: add google credentials unit test --- tests/test_google_credentials.py | 24 +++++++++++++++++++++++- toucan_connectors/google_credentials.py | 5 ++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/test_google_credentials.py b/tests/test_google_credentials.py index bda442335..b6c2fc910 100644 --- a/tests/test_google_credentials.py +++ b/tests/test_google_credentials.py @@ -1,4 +1,26 @@ -from toucan_connectors.google_credentials import GoogleCredentials +from pytest_mock import MockFixture + +from toucan_connectors.google_credentials import GoogleCredentials, get_google_oauth2_credentials + + +def test_google_credentials(mocker: MockFixture): + conf = { + 'type': 'service_account', + 'project_id': 'my_project_id', + 'private_key_id': 'my_private_key_id', + 'private_key': '-----BEGIN PRIVATE KEY-----\naaa\nbbb\n-----END PRIVATE KEY-----\n', + 'client_email': 'my_client_email', + 'client_id': 'my_client_id', + 'auth_uri': 'https://accounts.google.com/o/oauth2/auth', + 'token_uri': 'https://oauth2.googleapis.com/token', + 'auth_provider_x509_cert_url': 'https://www.googleapis.com/oauth2/v1/certs', + 'client_x509_cert_url': 'https://www.googleapis.com/robot/v1/metadata/x509/xxx.iam.gserviceaccount.com', # noqa: E501 + } + credentials = GoogleCredentials(**conf) + # Ensure `Credentials` is called with the right values of secrets + mock_credentials = mocker.patch('toucan_connectors.google_credentials.Credentials') + get_google_oauth2_credentials(credentials) + mock_credentials.from_service_account_info.assert_called_once_with(conf) def test_unespace_break_lines(): diff --git a/toucan_connectors/google_credentials.py b/toucan_connectors/google_credentials.py index acd93d1f0..fc160b662 100644 --- a/toucan_connectors/google_credentials.py +++ b/toucan_connectors/google_credentials.py @@ -1,3 +1,4 @@ +from google.oauth2.service_account import Credentials from pydantic import BaseModel, Field, HttpUrl, validator CREDENTIALS_INFO_MESSAGE = ( @@ -53,7 +54,5 @@ def unescape_break_lines(cls, v): return v.replace('\\n', '\n') -def get_google_oauth2_credentials(google_credentials): - from google.oauth2.service_account import Credentials - +def get_google_oauth2_credentials(google_credentials: GoogleCredentials) -> Credentials: return Credentials.from_service_account_info(google_credentials.dict())