Skip to content

Commit

Permalink
fix: pass kwargs through in 'from_service_account_json' (#109)
Browse files Browse the repository at this point in the history
Also, pin 'googleapis-common-protos' for Python 2.7 tests.

Closes #108.
  • Loading branch information
tseaver committed Jun 25, 2021
1 parent 1d3e273 commit efc4bbf
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
2 changes: 1 addition & 1 deletion google/cloud/client.py
Expand Up @@ -106,7 +106,7 @@ def from_service_account_json(cls, json_credentials_path, *args, **kwargs):
with io.open(json_credentials_path, "r", encoding="utf-8") as json_fi:
credentials_info = json.load(json_fi)

return cls.from_service_account_info(credentials_info)
return cls.from_service_account_info(credentials_info, *args, **kwargs)


class Client(_ClientFactoryMixin):
Expand Down
1 change: 1 addition & 0 deletions testing/constraints-2.7.txt
@@ -1,2 +1,3 @@
google-api-core==1.21.0
googleapis-common-protos<1.53.0
six==1.12.0
73 changes: 67 additions & 6 deletions tests/unit/test_client.py
Expand Up @@ -420,17 +420,16 @@ def test_constructor_explicit_unicode(self):
def _from_service_account_info_helper(self, project=None):
klass = self._get_target_class()

info = {"dummy": "value", "valid": "json"}
default_project = "eye-d-of-project"
info = {"dummy": "value", "valid": "json", "project_id": default_project}
kwargs = {}

if project is None:
expected_project = "eye-d-of-project"
expected_project = default_project
else:
expected_project = project
kwargs["project"] = project

info["project_id"] = expected_project

constructor_patch = mock.patch(
"google.oauth2.service_account.Credentials.from_service_account_info",
return_value=_make_credentials(),
Expand All @@ -451,18 +450,45 @@ def test_from_service_account_info(self):
def test_from_service_account_info_with_project(self):
self._from_service_account_info_helper(project="prah-jekt")

def test_from_service_account_info_with_posarg(self):
class Derived(self._get_target_class()):
def __init__(self, required, **kwargs):
super(Derived, self).__init__(**kwargs)
self.required = required

project = "eye-d-of-project"
info = {"dummy": "value", "valid": "json", "project_id": project}

# Mock both the file opening and the credentials constructor.
constructor_patch = mock.patch(
"google.oauth2.service_account.Credentials.from_service_account_info",
return_value=_make_credentials(),
)

with constructor_patch as constructor:
client_obj = Derived.from_service_account_info(info, "REQUIRED")

self.assertIsInstance(client_obj, Derived)
self.assertIs(client_obj._credentials, constructor.return_value)
self.assertIsNone(client_obj._http_internal)
self.assertEqual(client_obj.project, project)
self.assertEqual(client_obj.required, "REQUIRED")

# Check that mocks were called as expected.
constructor.assert_called_once_with(info)

def _from_service_account_json_helper(self, project=None):
from google.cloud import _helpers

klass = self._get_target_class()

info = {"dummy": "value", "valid": "json"}
default_project = "eye-d-of-project"
info = {"dummy": "value", "valid": "json", "project_id": default_project}
if project is None:
expected_project = "eye-d-of-project"
else:
expected_project = project

info["project_id"] = expected_project
# Mock both the file opening and the credentials constructor.
json_fi = io.StringIO(_helpers._bytes_to_unicode(json.dumps(info)))
file_open_patch = mock.patch("io.open", return_value=json_fi)
Expand Down Expand Up @@ -492,3 +518,38 @@ def test_from_service_account_json(self):

def test_from_service_account_json_project_set(self):
self._from_service_account_json_helper(project="prah-jekt")

def test_from_service_account_json_with_posarg(self):
from google.cloud import _helpers

class Derived(self._get_target_class()):
def __init__(self, required, **kwargs):
super(Derived, self).__init__(**kwargs)
self.required = required

project = "eye-d-of-project"
info = {"dummy": "value", "valid": "json", "project_id": project}

# Mock both the file opening and the credentials constructor.
json_fi = io.StringIO(_helpers._bytes_to_unicode(json.dumps(info)))
file_open_patch = mock.patch("io.open", return_value=json_fi)
constructor_patch = mock.patch(
"google.oauth2.service_account.Credentials.from_service_account_info",
return_value=_make_credentials(),
)

with file_open_patch as file_open:
with constructor_patch as constructor:
client_obj = Derived.from_service_account_json(
mock.sentinel.filename, "REQUIRED"
)

self.assertIsInstance(client_obj, Derived)
self.assertIs(client_obj._credentials, constructor.return_value)
self.assertIsNone(client_obj._http_internal)
self.assertEqual(client_obj.project, project)
self.assertEqual(client_obj.required, "REQUIRED")

# Check that mocks were called as expected.
file_open.assert_called_once_with(mock.sentinel.filename, "r", encoding="utf-8")
constructor.assert_called_once_with(info)

0 comments on commit efc4bbf

Please sign in to comment.