blob: 596a4b6a3fb8f472f57fb3832b174c8b012522d2 [file] [log] [blame]
Joe Gregorio562b7312011-09-15 09:06:38 -04001#!/usr/bin/python2.4
2#
3# Copyright 2010 Google Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18"""Oauth2client.file tests
19
20Unit tests for oauth2client.file
21"""
22
23__author__ = 'jcgregorio@google.com (Joe Gregorio)'
24
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040025import copy
Joe Beda17311fb2011-09-20 14:43:08 -070026import datetime
27import httplib2
Joe Gregorio562b7312011-09-15 09:06:38 -040028import os
29import pickle
Joe Gregorio9b8bec62012-01-17 11:35:32 -050030import stat
Joe Beda17311fb2011-09-20 14:43:08 -070031import tempfile
Joe Gregorio562b7312011-09-15 09:06:38 -040032import unittest
Joe Gregorio562b7312011-09-15 09:06:38 -040033
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040034from apiclient.http import HttpMockSequence
Joe Gregorio549230c2012-01-11 10:38:05 -050035from oauth2client import multistore_file
36from oauth2client.anyjson import simplejson
Joe Gregorio562b7312011-09-15 09:06:38 -040037from oauth2client.client import AccessTokenCredentials
38from oauth2client.client import AssertionCredentials
Joe Gregorio549230c2012-01-11 10:38:05 -050039from oauth2client.client import OAuth2Credentials
Joe Gregorio562b7312011-09-15 09:06:38 -040040from oauth2client.file import Storage
Joe Gregorio562b7312011-09-15 09:06:38 -040041
42
Joe Beda17311fb2011-09-20 14:43:08 -070043FILENAME = tempfile.mktemp('oauth2client_test.data')
Joe Gregorio562b7312011-09-15 09:06:38 -040044
45
46class OAuth2ClientFileTests(unittest.TestCase):
47
48 def tearDown(self):
49 try:
50 os.unlink(FILENAME)
51 except OSError:
52 pass
53
54 def setUp(self):
55 try:
56 os.unlink(FILENAME)
57 except OSError:
58 pass
59
60 def test_non_existent_file_storage(self):
61 s = Storage(FILENAME)
62 credentials = s.get()
63 self.assertEquals(None, credentials)
64
65 def test_pickle_and_json_interop(self):
66 # Write a file with a pickled OAuth2Credentials.
67 access_token = 'foo'
68 client_id = 'some_client_id'
69 client_secret = 'cOuDdkfjxxnv+'
70 refresh_token = '1/0/a.df219fjls0'
71 token_expiry = datetime.datetime.utcnow()
72 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
73 user_agent = 'refresh_checker/1.0'
74
75 credentials = OAuth2Credentials(
76 access_token, client_id, client_secret,
77 refresh_token, token_expiry, token_uri,
78 user_agent)
79
80 f = open(FILENAME, 'w')
81 pickle.dump(credentials, f)
82 f.close()
83
Joe Gregorioec555842011-10-27 11:10:39 -040084 # Storage should be not be able to read that object, as the capability to
85 # read and write credentials as pickled objects has been removed.
Joe Gregorio562b7312011-09-15 09:06:38 -040086 s = Storage(FILENAME)
Joe Gregorioec555842011-10-27 11:10:39 -040087 read_credentials = s.get()
88 self.assertEquals(None, read_credentials)
Joe Gregorio562b7312011-09-15 09:06:38 -040089
90 # Now write it back out and confirm it has been rewritten as JSON
91 s.put(credentials)
92 f = file(FILENAME)
93 data = simplejson.load(f)
94 f.close()
95
96 self.assertEquals(data['access_token'], 'foo')
97 self.assertEquals(data['_class'], 'OAuth2Credentials')
Joe Beda17311fb2011-09-20 14:43:08 -070098 self.assertEquals(data['_module'], OAuth2Credentials.__module__)
Joe Gregorio562b7312011-09-15 09:06:38 -040099
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400100 def test_token_refresh(self):
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400101 access_token = 'foo'
102 client_id = 'some_client_id'
103 client_secret = 'cOuDdkfjxxnv+'
104 refresh_token = '1/0/a.df219fjls0'
105 token_expiry = datetime.datetime.utcnow()
106 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
107 user_agent = 'refresh_checker/1.0'
108
109 credentials = OAuth2Credentials(
110 access_token, client_id, client_secret,
111 refresh_token, token_expiry, token_uri,
112 user_agent)
113
114 s = Storage(FILENAME)
115 s.put(credentials)
116 credentials = s.get()
117 new_cred = copy.copy(credentials)
118 new_cred.access_token = 'bar'
119 s.put(new_cred)
120
121 credentials._refresh(lambda x: x)
122 self.assertEquals(credentials.access_token, 'bar')
123
Joe Gregorioec75dc12012-02-06 13:40:42 -0500124 def test_credentials_delete(self):
125 access_token = 'foo'
126 client_id = 'some_client_id'
127 client_secret = 'cOuDdkfjxxnv+'
128 refresh_token = '1/0/a.df219fjls0'
129 token_expiry = datetime.datetime.utcnow()
130 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
131 user_agent = 'refresh_checker/1.0'
132
133 credentials = OAuth2Credentials(
134 access_token, client_id, client_secret,
135 refresh_token, token_expiry, token_uri,
136 user_agent)
137
138 s = Storage(FILENAME)
139 s.put(credentials)
140 credentials = s.get()
141 self.assertNotEquals(None, credentials)
142 s.delete()
143 credentials = s.get()
144 self.assertEquals(None, credentials)
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400145
Joe Gregorio562b7312011-09-15 09:06:38 -0400146 def test_access_token_credentials(self):
147 access_token = 'foo'
148 user_agent = 'refresh_checker/1.0'
149
150 credentials = AccessTokenCredentials(access_token, user_agent)
151
152 s = Storage(FILENAME)
153 credentials = s.put(credentials)
154 credentials = s.get()
155
156 self.assertNotEquals(None, credentials)
157 self.assertEquals('foo', credentials.access_token)
Joe Gregorio9b8bec62012-01-17 11:35:32 -0500158 mode = os.stat(FILENAME).st_mode
159
160 if os.name == 'posix':
161 self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode)))
162
163 def test_read_only_file_fail_lock(self):
164 access_token = 'foo'
165 client_secret = 'cOuDdkfjxxnv+'
166 refresh_token = '1/0/a.df219fjls0'
167 token_expiry = datetime.datetime.utcnow()
168 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
169 user_agent = 'refresh_checker/1.0'
170 client_id = 'some_client_id'
171
172 credentials = OAuth2Credentials(
173 access_token, client_id, client_secret,
174 refresh_token, token_expiry, token_uri,
175 user_agent)
176
177 open(FILENAME, 'a+b').close()
178 os.chmod(FILENAME, 0400)
179
180 store = multistore_file.get_credential_storage(
181 FILENAME,
182 credentials.client_id,
183 credentials.user_agent,
184 ['some-scope', 'some-other-scope'])
185
186 store.put(credentials)
187 if os.name == 'posix':
188 self.assertTrue(store._multistore._read_only)
189 os.chmod(FILENAME, 0600)
190
Joe Gregorio562b7312011-09-15 09:06:38 -0400191
192 def test_multistore_non_existent_file(self):
193 store = multistore_file.get_credential_storage(
194 FILENAME,
195 'some_client_id',
196 'user-agent/1.0',
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400197 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400198
199 credentials = store.get()
200 self.assertEquals(None, credentials)
201
202 def test_multistore_file(self):
203 access_token = 'foo'
204 client_secret = 'cOuDdkfjxxnv+'
205 refresh_token = '1/0/a.df219fjls0'
206 token_expiry = datetime.datetime.utcnow()
207 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
208 user_agent = 'refresh_checker/1.0'
209 client_id = 'some_client_id'
210
211 credentials = OAuth2Credentials(
212 access_token, client_id, client_secret,
213 refresh_token, token_expiry, token_uri,
214 user_agent)
215
216 store = multistore_file.get_credential_storage(
217 FILENAME,
218 credentials.client_id,
219 credentials.user_agent,
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400220 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400221
222 store.put(credentials)
223 credentials = store.get()
224
225 self.assertNotEquals(None, credentials)
226 self.assertEquals('foo', credentials.access_token)
227
Joe Gregorioec75dc12012-02-06 13:40:42 -0500228 store.delete()
229 credentials = store.get()
230
231 self.assertEquals(None, credentials)
232
Joe Gregorio9b8bec62012-01-17 11:35:32 -0500233 if os.name == 'posix':
234 self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode)))
235
Joe Gregorio562b7312011-09-15 09:06:38 -0400236if __name__ == '__main__':
237 unittest.main()