@@ -143,15 +143,39 @@ def test_ctor__http_property_new(self):
143143 self .assertIs (client ._http , session )
144144 self .assertEqual (AuthorizedSession .call_count , 1 )
145145
146+ def test_from_service_account_info (self ):
147+ klass = self ._get_target_class ()
148+
149+ info = {"dummy" : "value" , "valid" : "json" }
150+ constructor_patch = mock .patch (
151+ "google.oauth2.service_account.Credentials.from_service_account_info" ,
152+ return_value = _make_credentials (),
153+ )
154+
155+ with constructor_patch as constructor :
156+ client_obj = klass .from_service_account_info (info )
157+
158+ self .assertIs (client_obj ._credentials , constructor .return_value )
159+ self .assertIsNone (client_obj ._http_internal )
160+ constructor .assert_called_once_with (info )
161+
162+ def test_from_service_account_info_w_explicit_credentials (self ):
163+ KLASS = self ._get_target_class ()
164+
165+ info = {"dummy" : "value" , "valid" : "json" }
166+
167+ with self .assertRaises (TypeError ):
168+ KLASS .from_service_account_info (info , credentials = mock .sentinel .credentials )
169+
146170 def test_from_service_account_json (self ):
147171 from google .cloud import _helpers
148172
149173 klass = self ._get_target_class ()
150174
151- # Mock both the file opening and the credentials constructor.
152175 info = {"dummy" : "value" , "valid" : "json" }
153- json_fi = io .StringIO (_helpers ._bytes_to_unicode (json .dumps (info )))
154- file_open_patch = mock .patch ("io.open" , return_value = json_fi )
176+ json_file = io .StringIO (_helpers ._bytes_to_unicode (json .dumps (info )))
177+
178+ file_open_patch = mock .patch ("io.open" , return_value = json_file )
155179 constructor_patch = mock .patch (
156180 "google.oauth2.service_account.Credentials." "from_service_account_info" ,
157181 return_value = _make_credentials (),
@@ -167,14 +191,6 @@ def test_from_service_account_json(self):
167191 file_open .assert_called_once_with (mock .sentinel .filename , "r" , encoding = "utf-8" )
168192 constructor .assert_called_once_with (info )
169193
170- def test_from_service_account_json_bad_args (self ):
171- KLASS = self ._get_target_class ()
172-
173- with self .assertRaises (TypeError ):
174- KLASS .from_service_account_json (
175- mock .sentinel .filename , credentials = mock .sentinel .credentials
176- )
177-
178194
179195class Test_ClientProjectMixin (unittest .TestCase ):
180196 @staticmethod
@@ -377,6 +393,40 @@ def test_constructor_explicit_unicode(self):
377393 PROJECT = u"PROJECT"
378394 self ._explicit_ctor_helper (PROJECT )
379395
396+ def _from_service_account_info_helper (self , project = None ):
397+ klass = self ._get_target_class ()
398+
399+ info = {"dummy" : "value" , "valid" : "json" }
400+ kwargs = {}
401+
402+ if project is None :
403+ expected_project = "eye-d-of-project"
404+ else :
405+ expected_project = project
406+ kwargs ["project" ] = project
407+
408+ info ["project_id" ] = expected_project
409+
410+ constructor_patch = mock .patch (
411+ "google.oauth2.service_account.Credentials.from_service_account_info" ,
412+ return_value = _make_credentials (),
413+ )
414+
415+ with constructor_patch as constructor :
416+ client_obj = klass .from_service_account_info (info , ** kwargs )
417+
418+ self .assertIs (client_obj ._credentials , constructor .return_value )
419+ self .assertIsNone (client_obj ._http_internal )
420+ self .assertEqual (client_obj .project , expected_project )
421+
422+ constructor .assert_called_once_with (info )
423+
424+ def test_from_service_account_info (self ):
425+ self ._from_service_account_info_helper ()
426+
427+ def test_from_service_account_info_with_project (self ):
428+ self ._from_service_account_info_helper (project = "prah-jekt" )
429+
380430 def _from_service_account_json_helper (self , project = None ):
381431 from google .cloud import _helpers
382432
0 commit comments