blob: de5fcf2d69c5e4c500318fa0240b2eded9271ceb [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
94 # Storage should be able to read that object.
95 # TODO(jcgregorio) This should fail once pickle support is removed.
96 s = Storage(FILENAME)
97 credentials = s.get()
98 self.assertNotEquals(None, credentials)
99 self.assertEquals('foo', credentials.access_token)
100
101 # Now write it back out and confirm it has been rewritten as JSON
102 s.put(credentials)
103 f = file(FILENAME)
104 data = simplejson.load(f)
105 f.close()
106
107 self.assertEquals(data['access_token'], 'foo')
108 self.assertEquals(data['_class'], 'OAuth2Credentials')
Joe Beda17311fb2011-09-20 14:43:08 -0700109 self.assertEquals(data['_module'], OAuth2Credentials.__module__)
Joe Gregorio562b7312011-09-15 09:06:38 -0400110
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400111 def test_token_refresh(self):
112 # Write a file with a pickled OAuth2Credentials.
113 access_token = 'foo'
114 client_id = 'some_client_id'
115 client_secret = 'cOuDdkfjxxnv+'
116 refresh_token = '1/0/a.df219fjls0'
117 token_expiry = datetime.datetime.utcnow()
118 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
119 user_agent = 'refresh_checker/1.0'
120
121 credentials = OAuth2Credentials(
122 access_token, client_id, client_secret,
123 refresh_token, token_expiry, token_uri,
124 user_agent)
125
126 s = Storage(FILENAME)
127 s.put(credentials)
128 credentials = s.get()
129 new_cred = copy.copy(credentials)
130 new_cred.access_token = 'bar'
131 s.put(new_cred)
132
133 credentials._refresh(lambda x: x)
134 self.assertEquals(credentials.access_token, 'bar')
135
136
Joe Gregorio562b7312011-09-15 09:06:38 -0400137 def test_access_token_credentials(self):
138 access_token = 'foo'
139 user_agent = 'refresh_checker/1.0'
140
141 credentials = AccessTokenCredentials(access_token, user_agent)
142
143 s = Storage(FILENAME)
144 credentials = s.put(credentials)
145 credentials = s.get()
146
147 self.assertNotEquals(None, credentials)
148 self.assertEquals('foo', credentials.access_token)
149
150 def test_multistore_non_existent_file(self):
151 store = multistore_file.get_credential_storage(
152 FILENAME,
153 'some_client_id',
154 'user-agent/1.0',
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400155 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400156
157 credentials = store.get()
158 self.assertEquals(None, credentials)
159
160 def test_multistore_file(self):
161 access_token = 'foo'
162 client_secret = 'cOuDdkfjxxnv+'
163 refresh_token = '1/0/a.df219fjls0'
164 token_expiry = datetime.datetime.utcnow()
165 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
166 user_agent = 'refresh_checker/1.0'
167 client_id = 'some_client_id'
168
169 credentials = OAuth2Credentials(
170 access_token, client_id, client_secret,
171 refresh_token, token_expiry, token_uri,
172 user_agent)
173
174 store = multistore_file.get_credential_storage(
175 FILENAME,
176 credentials.client_id,
177 credentials.user_agent,
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400178 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400179
180 store.put(credentials)
181 credentials = store.get()
182
183 self.assertNotEquals(None, credentials)
184 self.assertEquals('foo', credentials.access_token)
185
186if __name__ == '__main__':
187 unittest.main()