blob: 7ff1eeb541be715d3fdca2e72fe26f3fb229ecf2 [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):
101 # Write a file with a pickled OAuth2Credentials.
102 access_token = 'foo'
103 client_id = 'some_client_id'
104 client_secret = 'cOuDdkfjxxnv+'
105 refresh_token = '1/0/a.df219fjls0'
106 token_expiry = datetime.datetime.utcnow()
107 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
108 user_agent = 'refresh_checker/1.0'
109
110 credentials = OAuth2Credentials(
111 access_token, client_id, client_secret,
112 refresh_token, token_expiry, token_uri,
113 user_agent)
114
115 s = Storage(FILENAME)
116 s.put(credentials)
117 credentials = s.get()
118 new_cred = copy.copy(credentials)
119 new_cred.access_token = 'bar'
120 s.put(new_cred)
121
122 credentials._refresh(lambda x: x)
123 self.assertEquals(credentials.access_token, 'bar')
124
125
Joe Gregorio562b7312011-09-15 09:06:38 -0400126 def test_access_token_credentials(self):
127 access_token = 'foo'
128 user_agent = 'refresh_checker/1.0'
129
130 credentials = AccessTokenCredentials(access_token, user_agent)
131
132 s = Storage(FILENAME)
133 credentials = s.put(credentials)
134 credentials = s.get()
135
136 self.assertNotEquals(None, credentials)
137 self.assertEquals('foo', credentials.access_token)
Joe Gregorio9b8bec62012-01-17 11:35:32 -0500138 mode = os.stat(FILENAME).st_mode
139
140 if os.name == 'posix':
141 self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode)))
142
143 def test_read_only_file_fail_lock(self):
144 access_token = 'foo'
145 client_secret = 'cOuDdkfjxxnv+'
146 refresh_token = '1/0/a.df219fjls0'
147 token_expiry = datetime.datetime.utcnow()
148 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
149 user_agent = 'refresh_checker/1.0'
150 client_id = 'some_client_id'
151
152 credentials = OAuth2Credentials(
153 access_token, client_id, client_secret,
154 refresh_token, token_expiry, token_uri,
155 user_agent)
156
157 open(FILENAME, 'a+b').close()
158 os.chmod(FILENAME, 0400)
159
160 store = multistore_file.get_credential_storage(
161 FILENAME,
162 credentials.client_id,
163 credentials.user_agent,
164 ['some-scope', 'some-other-scope'])
165
166 store.put(credentials)
167 if os.name == 'posix':
168 self.assertTrue(store._multistore._read_only)
169 os.chmod(FILENAME, 0600)
170
Joe Gregorio562b7312011-09-15 09:06:38 -0400171
172 def test_multistore_non_existent_file(self):
173 store = multistore_file.get_credential_storage(
174 FILENAME,
175 'some_client_id',
176 'user-agent/1.0',
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400177 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400178
179 credentials = store.get()
180 self.assertEquals(None, credentials)
181
182 def test_multistore_file(self):
183 access_token = 'foo'
184 client_secret = 'cOuDdkfjxxnv+'
185 refresh_token = '1/0/a.df219fjls0'
186 token_expiry = datetime.datetime.utcnow()
187 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
188 user_agent = 'refresh_checker/1.0'
189 client_id = 'some_client_id'
190
191 credentials = OAuth2Credentials(
192 access_token, client_id, client_secret,
193 refresh_token, token_expiry, token_uri,
194 user_agent)
195
196 store = multistore_file.get_credential_storage(
197 FILENAME,
198 credentials.client_id,
199 credentials.user_agent,
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400200 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400201
202 store.put(credentials)
203 credentials = store.get()
204
205 self.assertNotEquals(None, credentials)
206 self.assertEquals('foo', credentials.access_token)
207
Joe Gregorio9b8bec62012-01-17 11:35:32 -0500208 if os.name == 'posix':
209 self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode)))
210
Joe Gregorio562b7312011-09-15 09:06:38 -0400211if __name__ == '__main__':
212 unittest.main()