• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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
15import json
16
17import httplib2
18from six.moves import http_client
19from six.moves import urllib
20import unittest2
21
22from oauth2client import GOOGLE_TOKEN_INFO_URI
23from oauth2client.client import GoogleCredentials
24from oauth2client.contrib.gce import AppAssertionCredentials
25
26
27class TestComputeEngine(unittest2.TestCase):
28
29    def test_application_default(self):
30        default_creds = GoogleCredentials.get_application_default()
31        self.assertIsInstance(default_creds, AppAssertionCredentials)
32
33    def test_token_info(self):
34        credentials = AppAssertionCredentials([])
35        http = httplib2.Http()
36
37        # First refresh to get the access token.
38        self.assertIsNone(credentials.access_token)
39        credentials.refresh(http)
40        self.assertIsNotNone(credentials.access_token)
41
42        # Then check the access token against the token info API.
43        query_params = {'access_token': credentials.access_token}
44        token_uri = (GOOGLE_TOKEN_INFO_URI + '?' +
45                     urllib.parse.urlencode(query_params))
46        response, content = http.request(token_uri)
47        self.assertEqual(response.status, http_client.OK)
48
49        content = content.decode('utf-8')
50        payload = json.loads(content)
51        self.assertEqual(payload['access_type'], 'offline')
52        self.assertLessEqual(int(payload['expires_in']), 3600)
53
54
55if __name__ == '__main__':
56    unittest2.main()
57