blob: 7c656770db0c24d51993481eb90cb2598ec04ca6 [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 Gregorio0fd18532012-08-24 15:54:40 -040035from oauth2client import file
36from oauth2client import locked_file
Joe Gregorio549230c2012-01-11 10:38:05 -050037from oauth2client import multistore_file
38from oauth2client.anyjson import simplejson
Joe Gregorio562b7312011-09-15 09:06:38 -040039from oauth2client.client import AccessTokenCredentials
40from oauth2client.client import AssertionCredentials
Joe Gregorio549230c2012-01-11 10:38:05 -050041from oauth2client.client import OAuth2Credentials
Joe Gregorio562b7312011-09-15 09:06:38 -040042
43
Joe Beda17311fb2011-09-20 14:43:08 -070044FILENAME = tempfile.mktemp('oauth2client_test.data')
Joe Gregorio562b7312011-09-15 09:06:38 -040045
46
47class OAuth2ClientFileTests(unittest.TestCase):
48
49 def tearDown(self):
50 try:
51 os.unlink(FILENAME)
52 except OSError:
53 pass
54
55 def setUp(self):
56 try:
57 os.unlink(FILENAME)
58 except OSError:
59 pass
60
61 def test_non_existent_file_storage(self):
Joe Gregorio0fd18532012-08-24 15:54:40 -040062 s = file.Storage(FILENAME)
Joe Gregorio562b7312011-09-15 09:06:38 -040063 credentials = s.get()
64 self.assertEquals(None, credentials)
65
Joe Gregorio0fd18532012-08-24 15:54:40 -040066 def test_no_sym_link_credentials(self):
67 if hasattr(os, 'symlink'):
68 SYMFILENAME = FILENAME + '.sym'
69 os.symlink(FILENAME, SYMFILENAME)
70 s = file.Storage(SYMFILENAME)
71 try:
72 s.get()
73 self.fail('Should have raised an exception.')
74 except file.CredentialsFileSymbolicLinkError:
75 pass
76 finally:
77 os.unlink(SYMFILENAME)
78
Joe Gregorio562b7312011-09-15 09:06:38 -040079 def test_pickle_and_json_interop(self):
80 # Write a file with a pickled OAuth2Credentials.
81 access_token = 'foo'
82 client_id = 'some_client_id'
83 client_secret = 'cOuDdkfjxxnv+'
84 refresh_token = '1/0/a.df219fjls0'
85 token_expiry = datetime.datetime.utcnow()
86 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
87 user_agent = 'refresh_checker/1.0'
88
89 credentials = OAuth2Credentials(
90 access_token, client_id, client_secret,
91 refresh_token, token_expiry, token_uri,
92 user_agent)
93
94 f = open(FILENAME, 'w')
95 pickle.dump(credentials, f)
96 f.close()
97
Joe Gregorioec555842011-10-27 11:10:39 -040098 # Storage should be not be able to read that object, as the capability to
99 # read and write credentials as pickled objects has been removed.
Joe Gregorio0fd18532012-08-24 15:54:40 -0400100 s = file.Storage(FILENAME)
Joe Gregorioec555842011-10-27 11:10:39 -0400101 read_credentials = s.get()
102 self.assertEquals(None, read_credentials)
Joe Gregorio562b7312011-09-15 09:06:38 -0400103
104 # Now write it back out and confirm it has been rewritten as JSON
105 s.put(credentials)
Joe Gregorio0fd18532012-08-24 15:54:40 -0400106 f = open(FILENAME)
Joe Gregorio562b7312011-09-15 09:06:38 -0400107 data = simplejson.load(f)
108 f.close()
109
110 self.assertEquals(data['access_token'], 'foo')
111 self.assertEquals(data['_class'], 'OAuth2Credentials')
Joe Beda17311fb2011-09-20 14:43:08 -0700112 self.assertEquals(data['_module'], OAuth2Credentials.__module__)
Joe Gregorio562b7312011-09-15 09:06:38 -0400113
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400114 def test_token_refresh(self):
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400115 access_token = 'foo'
116 client_id = 'some_client_id'
117 client_secret = 'cOuDdkfjxxnv+'
118 refresh_token = '1/0/a.df219fjls0'
119 token_expiry = datetime.datetime.utcnow()
120 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
121 user_agent = 'refresh_checker/1.0'
122
123 credentials = OAuth2Credentials(
124 access_token, client_id, client_secret,
125 refresh_token, token_expiry, token_uri,
126 user_agent)
127
Joe Gregorio0fd18532012-08-24 15:54:40 -0400128 s = file.Storage(FILENAME)
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400129 s.put(credentials)
130 credentials = s.get()
131 new_cred = copy.copy(credentials)
132 new_cred.access_token = 'bar'
133 s.put(new_cred)
134
135 credentials._refresh(lambda x: x)
136 self.assertEquals(credentials.access_token, 'bar')
137
Joe Gregorioec75dc12012-02-06 13:40:42 -0500138 def test_credentials_delete(self):
139 access_token = 'foo'
140 client_id = 'some_client_id'
141 client_secret = 'cOuDdkfjxxnv+'
142 refresh_token = '1/0/a.df219fjls0'
143 token_expiry = datetime.datetime.utcnow()
144 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
145 user_agent = 'refresh_checker/1.0'
146
147 credentials = OAuth2Credentials(
148 access_token, client_id, client_secret,
149 refresh_token, token_expiry, token_uri,
150 user_agent)
151
Joe Gregorio0fd18532012-08-24 15:54:40 -0400152 s = file.Storage(FILENAME)
Joe Gregorioec75dc12012-02-06 13:40:42 -0500153 s.put(credentials)
154 credentials = s.get()
155 self.assertNotEquals(None, credentials)
156 s.delete()
157 credentials = s.get()
158 self.assertEquals(None, credentials)
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400159
Joe Gregorio562b7312011-09-15 09:06:38 -0400160 def test_access_token_credentials(self):
161 access_token = 'foo'
162 user_agent = 'refresh_checker/1.0'
163
164 credentials = AccessTokenCredentials(access_token, user_agent)
165
Joe Gregorio0fd18532012-08-24 15:54:40 -0400166 s = file.Storage(FILENAME)
Joe Gregorio562b7312011-09-15 09:06:38 -0400167 credentials = s.put(credentials)
168 credentials = s.get()
169
170 self.assertNotEquals(None, credentials)
171 self.assertEquals('foo', credentials.access_token)
Joe Gregorio9b8bec62012-01-17 11:35:32 -0500172 mode = os.stat(FILENAME).st_mode
173
174 if os.name == 'posix':
175 self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode)))
176
177 def test_read_only_file_fail_lock(self):
178 access_token = 'foo'
179 client_secret = 'cOuDdkfjxxnv+'
180 refresh_token = '1/0/a.df219fjls0'
181 token_expiry = datetime.datetime.utcnow()
182 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
183 user_agent = 'refresh_checker/1.0'
184 client_id = 'some_client_id'
185
186 credentials = OAuth2Credentials(
187 access_token, client_id, client_secret,
188 refresh_token, token_expiry, token_uri,
189 user_agent)
190
191 open(FILENAME, 'a+b').close()
192 os.chmod(FILENAME, 0400)
193
194 store = multistore_file.get_credential_storage(
195 FILENAME,
196 credentials.client_id,
197 credentials.user_agent,
198 ['some-scope', 'some-other-scope'])
199
200 store.put(credentials)
201 if os.name == 'posix':
202 self.assertTrue(store._multistore._read_only)
203 os.chmod(FILENAME, 0600)
204
Joe Gregorio0fd18532012-08-24 15:54:40 -0400205 def test_multistore_no_symbolic_link_files(self):
206 if hasattr(os, 'symlink'):
207 SYMFILENAME = FILENAME + 'sym'
208 os.symlink(FILENAME, SYMFILENAME)
209 store = multistore_file.get_credential_storage(
210 SYMFILENAME,
211 'some_client_id',
212 'user-agent/1.0',
213 ['some-scope', 'some-other-scope'])
214 try:
215 store.get()
216 self.fail('Should have raised an exception.')
217 except locked_file.CredentialsFileSymbolicLinkError:
218 pass
219 finally:
220 os.unlink(SYMFILENAME)
Joe Gregorio562b7312011-09-15 09:06:38 -0400221
222 def test_multistore_non_existent_file(self):
223 store = multistore_file.get_credential_storage(
224 FILENAME,
225 'some_client_id',
226 'user-agent/1.0',
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400227 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400228
229 credentials = store.get()
230 self.assertEquals(None, credentials)
231
232 def test_multistore_file(self):
233 access_token = 'foo'
234 client_secret = 'cOuDdkfjxxnv+'
235 refresh_token = '1/0/a.df219fjls0'
236 token_expiry = datetime.datetime.utcnow()
237 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
238 user_agent = 'refresh_checker/1.0'
239 client_id = 'some_client_id'
240
241 credentials = OAuth2Credentials(
242 access_token, client_id, client_secret,
243 refresh_token, token_expiry, token_uri,
244 user_agent)
245
246 store = multistore_file.get_credential_storage(
247 FILENAME,
248 credentials.client_id,
249 credentials.user_agent,
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400250 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400251
252 store.put(credentials)
253 credentials = store.get()
254
255 self.assertNotEquals(None, credentials)
256 self.assertEquals('foo', credentials.access_token)
257
Joe Gregorioec75dc12012-02-06 13:40:42 -0500258 store.delete()
259 credentials = store.get()
260
261 self.assertEquals(None, credentials)
262
Joe Gregorio9b8bec62012-01-17 11:35:32 -0500263 if os.name == 'posix':
264 self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode)))
265
Joe Gregorio562b7312011-09-15 09:06:38 -0400266if __name__ == '__main__':
267 unittest.main()