|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import io |
| 16 | +import json |
15 | 17 | import unittest |
16 | 18 |
|
17 | 19 | import mock |
@@ -90,21 +92,32 @@ def test_ctor_bad_credentials(self): |
90 | 92 | self._make_one(credentials=CREDENTIALS) |
91 | 93 |
|
92 | 94 | def test_from_service_account_json(self): |
93 | | - KLASS = self._get_target_class() |
| 95 | + from google.cloud import _helpers |
| 96 | + |
| 97 | + klass = self._get_target_class() |
94 | 98 |
|
| 99 | + # Mock both the file opening and the credentials constructor. |
| 100 | + info = {'dummy': 'value', 'valid': 'json'} |
| 101 | + json_fi = io.StringIO(_helpers._bytes_to_unicode(json.dumps(info))) |
| 102 | + file_open_patch = mock.patch( |
| 103 | + 'io.open', return_value=json_fi) |
95 | 104 | constructor_patch = mock.patch( |
96 | 105 | 'google.oauth2.service_account.Credentials.' |
97 | | - 'from_service_account_file', |
| 106 | + 'from_service_account_info', |
98 | 107 | return_value=_make_credentials()) |
99 | 108 |
|
100 | | - with constructor_patch as constructor: |
101 | | - client_obj = KLASS.from_service_account_json( |
102 | | - mock.sentinel.filename) |
| 109 | + with file_open_patch as file_open: |
| 110 | + with constructor_patch as constructor: |
| 111 | + client_obj = klass.from_service_account_json( |
| 112 | + mock.sentinel.filename) |
103 | 113 |
|
104 | 114 | self.assertIs( |
105 | 115 | client_obj._credentials, constructor.return_value) |
106 | 116 | self.assertIsNone(client_obj._http_internal) |
107 | | - constructor.assert_called_once_with(mock.sentinel.filename) |
| 117 | + # Check that mocks were called as expected. |
| 118 | + file_open.assert_called_once_with( |
| 119 | + mock.sentinel.filename, 'r', encoding='utf-8') |
| 120 | + constructor.assert_called_once_with(info) |
108 | 121 |
|
109 | 122 | def test_from_service_account_json_bad_args(self): |
110 | 123 | KLASS = self._get_target_class() |
@@ -221,3 +234,47 @@ def test_ctor_explicit_bytes(self): |
221 | 234 | def test_ctor_explicit_unicode(self): |
222 | 235 | PROJECT = u'PROJECT' |
223 | 236 | self._explicit_ctor_helper(PROJECT) |
| 237 | + |
| 238 | + def _from_service_account_json_helper(self, project=None): |
| 239 | + from google.cloud import _helpers |
| 240 | + |
| 241 | + klass = self._get_target_class() |
| 242 | + |
| 243 | + info = {'dummy': 'value', 'valid': 'json'} |
| 244 | + if project is None: |
| 245 | + expected_project = 'eye-d-of-project' |
| 246 | + else: |
| 247 | + expected_project = project |
| 248 | + |
| 249 | + info['project_id'] = expected_project |
| 250 | + # Mock both the file opening and the credentials constructor. |
| 251 | + json_fi = io.StringIO(_helpers._bytes_to_unicode(json.dumps(info))) |
| 252 | + file_open_patch = mock.patch( |
| 253 | + 'io.open', return_value=json_fi) |
| 254 | + constructor_patch = mock.patch( |
| 255 | + 'google.oauth2.service_account.Credentials.' |
| 256 | + 'from_service_account_info', |
| 257 | + return_value=_make_credentials()) |
| 258 | + |
| 259 | + with file_open_patch as file_open: |
| 260 | + with constructor_patch as constructor: |
| 261 | + kwargs = {} |
| 262 | + if project is not None: |
| 263 | + kwargs['project'] = project |
| 264 | + client_obj = klass.from_service_account_json( |
| 265 | + mock.sentinel.filename, **kwargs) |
| 266 | + |
| 267 | + self.assertIs( |
| 268 | + client_obj._credentials, constructor.return_value) |
| 269 | + self.assertIsNone(client_obj._http_internal) |
| 270 | + self.assertEqual(client_obj.project, expected_project) |
| 271 | + # Check that mocks were called as expected. |
| 272 | + file_open.assert_called_once_with( |
| 273 | + mock.sentinel.filename, 'r', encoding='utf-8') |
| 274 | + constructor.assert_called_once_with(info) |
| 275 | + |
| 276 | + def test_from_service_account_json(self): |
| 277 | + self._from_service_account_json_helper() |
| 278 | + |
| 279 | + def test_from_service_account_json_project_set(self): |
| 280 | + self._from_service_account_json_helper(project='prah-jekt') |
0 commit comments