Skip to content

Commit

Permalink
test: add google credentials unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Oct 18, 2023
1 parent 4cc84ec commit b6b3414
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
24 changes: 23 additions & 1 deletion tests/test_google_credentials.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
5 changes: 2 additions & 3 deletions toucan_connectors/google_credentials.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from google.oauth2.service_account import Credentials
from pydantic import BaseModel, Field, HttpUrl, validator

CREDENTIALS_INFO_MESSAGE = (
Expand Down Expand Up @@ -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())

0 comments on commit b6b3414

Please sign in to comment.