• 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 mock
16
17import google.auth.credentials
18import google_auth_httplib2
19import httplib2
20import oauth2client.client
21import unittest2 as unittest
22
23from googleapiclient import _auth
24
25
26class TestAuthWithGoogleAuth(unittest.TestCase):
27    def setUp(self):
28        _auth.HAS_GOOGLE_AUTH = True
29        _auth.HAS_OAUTH2CLIENT = False
30
31    def tearDown(self):
32        _auth.HAS_GOOGLE_AUTH = True
33        _auth.HAS_OAUTH2CLIENT = True
34
35    def test_default_credentials(self):
36        with mock.patch('google.auth.default', autospec=True) as default:
37            default.return_value = (
38                mock.sentinel.credentials, mock.sentinel.project)
39
40            credentials = _auth.default_credentials()
41
42            self.assertEqual(credentials, mock.sentinel.credentials)
43
44    def test_with_scopes_non_scoped(self):
45        credentials = mock.Mock(spec=google.auth.credentials.Credentials)
46
47        returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
48
49        self.assertEqual(credentials, returned)
50
51    def test_with_scopes_scoped(self):
52        class CredentialsWithScopes(
53                google.auth.credentials.Credentials,
54                google.auth.credentials.Scoped):
55            pass
56
57        credentials = mock.Mock(spec=CredentialsWithScopes)
58        credentials.requires_scopes = True
59
60        returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
61
62        self.assertNotEqual(credentials, returned)
63        self.assertEqual(returned, credentials.with_scopes.return_value)
64        credentials.with_scopes.assert_called_once_with(mock.sentinel.scopes)
65
66    def test_authorized_http(self):
67        credentials = mock.Mock(spec=google.auth.credentials.Credentials)
68
69        authorized_http = _auth.authorized_http(credentials)
70
71        self.assertIsInstance(
72            authorized_http,
73            google_auth_httplib2.AuthorizedHttp)
74        self.assertEqual(authorized_http.credentials, credentials)
75        self.assertIsInstance(authorized_http.http, httplib2.Http)
76        self.assertIsInstance(authorized_http.http.timeout, int)
77        self.assertGreater(authorized_http.http.timeout, 0)
78
79
80class TestAuthWithOAuth2Client(unittest.TestCase):
81    def setUp(self):
82        _auth.HAS_GOOGLE_AUTH = False
83        _auth.HAS_OAUTH2CLIENT = True
84
85    def tearDown(self):
86        _auth.HAS_GOOGLE_AUTH = True
87        _auth.HAS_OAUTH2CLIENT = True
88
89    def test_default_credentials(self):
90        default_patch = mock.patch(
91            'oauth2client.client.GoogleCredentials.get_application_default')
92
93        with default_patch as default:
94            default.return_value = mock.sentinel.credentials
95
96            credentials = _auth.default_credentials()
97
98            self.assertEqual(credentials, mock.sentinel.credentials)
99
100    def test_with_scopes_non_scoped(self):
101        credentials = mock.Mock(spec=oauth2client.client.Credentials)
102
103        returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
104
105        self.assertEqual(credentials, returned)
106
107    def test_with_scopes_scoped(self):
108        credentials = mock.Mock(spec=oauth2client.client.GoogleCredentials)
109        credentials.create_scoped_required.return_value = True
110
111        returned = _auth.with_scopes(credentials, mock.sentinel.scopes)
112
113        self.assertNotEqual(credentials, returned)
114        self.assertEqual(returned, credentials.create_scoped.return_value)
115        credentials.create_scoped.assert_called_once_with(mock.sentinel.scopes)
116
117    def test_authorized_http(self):
118        credentials = mock.Mock(spec=oauth2client.client.Credentials)
119
120        authorized_http = _auth.authorized_http(credentials)
121
122        http = credentials.authorize.call_args[0][0]
123
124        self.assertEqual(authorized_http, credentials.authorize.return_value)
125        self.assertIsInstance(http, httplib2.Http)
126        self.assertIsInstance(http.timeout, int)
127        self.assertGreater(http.timeout, 0)
128
129
130class TestAuthWithoutAuth(unittest.TestCase):
131
132    def setUp(self):
133        _auth.HAS_GOOGLE_AUTH = False
134        _auth.HAS_OAUTH2CLIENT = False
135
136    def tearDown(self):
137        _auth.HAS_GOOGLE_AUTH = True
138        _auth.HAS_OAUTH2CLIENT = True
139
140    def test_default_credentials(self):
141        with self.assertRaises(EnvironmentError):
142            print(_auth.default_credentials())
143
144
145class TestGoogleAuthWithoutHttplib2(unittest.TestCase):
146    def setUp(self):
147        _auth.google_auth_httplib2 = None
148
149    def tearDown(self):
150        _auth.google_auth_httplib2 = google_auth_httplib2
151
152    def test_default_credentials(self):
153        credentials = mock.Mock(spec=google.auth.credentials.Credentials)
154        with self.assertRaises(ValueError):
155            _auth.authorized_http(credentials)
156