blob: 2d6f3b5db365f0c921b26e696cd5253d4bb59e7d [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 Beda17311fb2011-09-20 14:43:08 -070030import tempfile
Joe Gregorio562b7312011-09-15 09:06:38 -040031import unittest
Joe Gregorio562b7312011-09-15 09:06:38 -040032
33
34try: # pragma: no cover
35 import simplejson
36except ImportError: # pragma: no cover
37 try:
38 # Try to import from django, should work on App Engine
39 from django.utils import simplejson
40 except ImportError:
41 # Should work for Python2.6 and higher.
42 import json as simplejson
43
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040044from apiclient.http import HttpMockSequence
Joe Gregorio562b7312011-09-15 09:06:38 -040045
46from oauth2client.client import OAuth2Credentials
47from oauth2client.client import AccessTokenCredentials
48from oauth2client.client import AssertionCredentials
49from oauth2client.file import Storage
50from oauth2client import multistore_file
51
52
Joe Beda17311fb2011-09-20 14:43:08 -070053FILENAME = tempfile.mktemp('oauth2client_test.data')
Joe Gregorio562b7312011-09-15 09:06:38 -040054
55
56class OAuth2ClientFileTests(unittest.TestCase):
57
58 def tearDown(self):
59 try:
60 os.unlink(FILENAME)
61 except OSError:
62 pass
63
64 def setUp(self):
65 try:
66 os.unlink(FILENAME)
67 except OSError:
68 pass
69
70 def test_non_existent_file_storage(self):
71 s = Storage(FILENAME)
72 credentials = s.get()
73 self.assertEquals(None, credentials)
74
75 def test_pickle_and_json_interop(self):
76 # Write a file with a pickled OAuth2Credentials.
77 access_token = 'foo'
78 client_id = 'some_client_id'
79 client_secret = 'cOuDdkfjxxnv+'
80 refresh_token = '1/0/a.df219fjls0'
81 token_expiry = datetime.datetime.utcnow()
82 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
83 user_agent = 'refresh_checker/1.0'
84
85 credentials = OAuth2Credentials(
86 access_token, client_id, client_secret,
87 refresh_token, token_expiry, token_uri,
88 user_agent)
89
90 f = open(FILENAME, 'w')
91 pickle.dump(credentials, f)
92 f.close()
93
Joe Gregorioec555842011-10-27 11:10:39 -040094 # Storage should be not be able to read that object, as the capability to
95 # read and write credentials as pickled objects has been removed.
Joe Gregorio562b7312011-09-15 09:06:38 -040096 s = Storage(FILENAME)
Joe Gregorioec555842011-10-27 11:10:39 -040097 read_credentials = s.get()
98 self.assertEquals(None, read_credentials)
Joe Gregorio562b7312011-09-15 09:06:38 -040099
100 # Now write it back out and confirm it has been rewritten as JSON
101 s.put(credentials)
102 f = file(FILENAME)
103 data = simplejson.load(f)
104 f.close()
105
106 self.assertEquals(data['access_token'], 'foo')
107 self.assertEquals(data['_class'], 'OAuth2Credentials')
Joe Beda17311fb2011-09-20 14:43:08 -0700108 self.assertEquals(data['_module'], OAuth2Credentials.__module__)
Joe Gregorio562b7312011-09-15 09:06:38 -0400109
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400110 def test_token_refresh(self):
111 # Write a file with a pickled OAuth2Credentials.
112 access_token = 'foo'
113 client_id = 'some_client_id'
114 client_secret = 'cOuDdkfjxxnv+'
115 refresh_token = '1/0/a.df219fjls0'
116 token_expiry = datetime.datetime.utcnow()
117 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
118 user_agent = 'refresh_checker/1.0'
119
120 credentials = OAuth2Credentials(
121 access_token, client_id, client_secret,
122 refresh_token, token_expiry, token_uri,
123 user_agent)
124
125 s = Storage(FILENAME)
126 s.put(credentials)
127 credentials = s.get()
128 new_cred = copy.copy(credentials)
129 new_cred.access_token = 'bar'
130 s.put(new_cred)
131
132 credentials._refresh(lambda x: x)
133 self.assertEquals(credentials.access_token, 'bar')
134
135
Joe Gregorio562b7312011-09-15 09:06:38 -0400136 def test_access_token_credentials(self):
137 access_token = 'foo'
138 user_agent = 'refresh_checker/1.0'
139
140 credentials = AccessTokenCredentials(access_token, user_agent)
141
142 s = Storage(FILENAME)
143 credentials = s.put(credentials)
144 credentials = s.get()
145
146 self.assertNotEquals(None, credentials)
147 self.assertEquals('foo', credentials.access_token)
148
149 def test_multistore_non_existent_file(self):
150 store = multistore_file.get_credential_storage(
151 FILENAME,
152 'some_client_id',
153 'user-agent/1.0',
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400154 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400155
156 credentials = store.get()
157 self.assertEquals(None, credentials)
158
159 def test_multistore_file(self):
160 access_token = 'foo'
161 client_secret = 'cOuDdkfjxxnv+'
162 refresh_token = '1/0/a.df219fjls0'
163 token_expiry = datetime.datetime.utcnow()
164 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
165 user_agent = 'refresh_checker/1.0'
166 client_id = 'some_client_id'
167
168 credentials = OAuth2Credentials(
169 access_token, client_id, client_secret,
170 refresh_token, token_expiry, token_uri,
171 user_agent)
172
173 store = multistore_file.get_credential_storage(
174 FILENAME,
175 credentials.client_id,
176 credentials.user_agent,
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400177 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400178
179 store.put(credentials)
180 credentials = store.get()
181
182 self.assertNotEquals(None, credentials)
183 self.assertEquals('foo', credentials.access_token)
184
185if __name__ == '__main__':
186 unittest.main()