| OLD | NEW |
| (Empty) |
| 1 # Copyright 2014 Google Inc. All rights reserved. | |
| 2 # | |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 # you may not use this file except in compliance with the License. | |
| 5 # You may obtain a copy of the License at | |
| 6 # | |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 # | |
| 9 # Unless required by applicable law or agreed to in writing, software | |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 # See the License for the specific language governing permissions and | |
| 13 # limitations under the License. | |
| 14 | |
| 15 | |
| 16 """Tests for oauth2client.gce. | |
| 17 | |
| 18 Unit tests for oauth2client.gce. | |
| 19 """ | |
| 20 | |
| 21 __author__ = 'jcgregorio@google.com (Joe Gregorio)' | |
| 22 | |
| 23 import unittest | |
| 24 | |
| 25 import httplib2 | |
| 26 import mock | |
| 27 | |
| 28 from oauth2client.client import AccessTokenRefreshError | |
| 29 from oauth2client.client import Credentials | |
| 30 from oauth2client.client import save_to_well_known_file | |
| 31 from oauth2client.gce import AppAssertionCredentials | |
| 32 | |
| 33 | |
| 34 class AssertionCredentialsTests(unittest.TestCase): | |
| 35 | |
| 36 def test_good_refresh(self): | |
| 37 http = mock.MagicMock() | |
| 38 http.request = mock.MagicMock( | |
| 39 return_value=(mock.Mock(status=200), | |
| 40 '{"accessToken": "this-is-a-token"}')) | |
| 41 | |
| 42 c = AppAssertionCredentials(scope=['http://example.com/a', | |
| 43 'http://example.com/b']) | |
| 44 self.assertEquals(None, c.access_token) | |
| 45 c.refresh(http) | |
| 46 self.assertEquals('this-is-a-token', c.access_token) | |
| 47 | |
| 48 http.request.assert_called_once_with( | |
| 49 'http://metadata.google.internal/0.1/meta-data/service-accounts/' | |
| 50 'default/acquire' | |
| 51 '?scope=http%3A%2F%2Fexample.com%2Fa%20http%3A%2F%2Fexample.com%2Fb') | |
| 52 | |
| 53 def test_fail_refresh(self): | |
| 54 http = mock.MagicMock() | |
| 55 http.request = mock.MagicMock(return_value=(mock.Mock(status=400), '{}')) | |
| 56 | |
| 57 c = AppAssertionCredentials(scope=['http://example.com/a', | |
| 58 'http://example.com/b']) | |
| 59 self.assertRaises(AccessTokenRefreshError, c.refresh, http) | |
| 60 | |
| 61 def test_to_from_json(self): | |
| 62 c = AppAssertionCredentials(scope=['http://example.com/a', | |
| 63 'http://example.com/b']) | |
| 64 json = c.to_json() | |
| 65 c2 = Credentials.new_from_json(json) | |
| 66 | |
| 67 self.assertEqual(c.access_token, c2.access_token) | |
| 68 | |
| 69 def test_create_scoped_required_without_scopes(self): | |
| 70 credentials = AppAssertionCredentials([]) | |
| 71 self.assertTrue(credentials.create_scoped_required()) | |
| 72 | |
| 73 def test_create_scoped_required_with_scopes(self): | |
| 74 credentials = AppAssertionCredentials(['dummy_scope']) | |
| 75 self.assertFalse(credentials.create_scoped_required()) | |
| 76 | |
| 77 def test_create_scoped(self): | |
| 78 credentials = AppAssertionCredentials([]) | |
| 79 new_credentials = credentials.create_scoped(['dummy_scope']) | |
| 80 self.assertNotEqual(credentials, new_credentials) | |
| 81 self.assertTrue(isinstance(new_credentials, AppAssertionCredentials)) | |
| 82 self.assertEqual('dummy_scope', new_credentials.scope) | |
| 83 | |
| 84 def test_get_access_token(self): | |
| 85 http = mock.MagicMock() | |
| 86 http.request = mock.MagicMock( | |
| 87 return_value=(mock.Mock(status=200), | |
| 88 '{"accessToken": "this-is-a-token"}')) | |
| 89 | |
| 90 credentials = AppAssertionCredentials(['dummy_scope']) | |
| 91 token = credentials.get_access_token(http=http) | |
| 92 self.assertEqual('this-is-a-token', token.access_token) | |
| 93 self.assertEqual(None, token.expires_in) | |
| 94 | |
| 95 http.request.assert_called_once_with( | |
| 96 'http://metadata.google.internal/0.1/meta-data/service-accounts/' | |
| 97 'default/acquire?scope=dummy_scope') | |
| 98 | |
| 99 def test_save_to_well_known_file(self): | |
| 100 import os | |
| 101 ORIGINAL_ISDIR = os.path.isdir | |
| 102 try: | |
| 103 os.path.isdir = lambda path: True | |
| 104 credentials = AppAssertionCredentials([]) | |
| 105 self.assertRaises(NotImplementedError, save_to_well_known_file, | |
| 106 credentials) | |
| 107 finally: | |
| 108 os.path.isdir = ORIGINAL_ISDIR | |
| OLD | NEW |