blob: 5ada4a38fc037b0149230d4ee9831c157216efe3 [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 Gregorio562b7312011-09-15 09:06:38 -040026import os
27import pickle
28import unittest
29import datetime
30
31
32try: # pragma: no cover
33 import simplejson
34except ImportError: # pragma: no cover
35 try:
36 # Try to import from django, should work on App Engine
37 from django.utils import simplejson
38 except ImportError:
39 # Should work for Python2.6 and higher.
40 import json as simplejson
41
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040042from apiclient.http import HttpMockSequence
Joe Gregorio562b7312011-09-15 09:06:38 -040043
44from oauth2client.client import OAuth2Credentials
45from oauth2client.client import AccessTokenCredentials
46from oauth2client.client import AssertionCredentials
47from oauth2client.file import Storage
48from oauth2client import multistore_file
49
50
51FILENAME = os.path.join(os.path.dirname(__file__), 'test_file_storage.data')
52
53
54class OAuth2ClientFileTests(unittest.TestCase):
55
56 def tearDown(self):
57 try:
58 os.unlink(FILENAME)
59 except OSError:
60 pass
61
62 def setUp(self):
63 try:
64 os.unlink(FILENAME)
65 except OSError:
66 pass
67
68 def test_non_existent_file_storage(self):
69 s = Storage(FILENAME)
70 credentials = s.get()
71 self.assertEquals(None, credentials)
72
73 def test_pickle_and_json_interop(self):
74 # Write a file with a pickled OAuth2Credentials.
75 access_token = 'foo'
76 client_id = 'some_client_id'
77 client_secret = 'cOuDdkfjxxnv+'
78 refresh_token = '1/0/a.df219fjls0'
79 token_expiry = datetime.datetime.utcnow()
80 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
81 user_agent = 'refresh_checker/1.0'
82
83 credentials = OAuth2Credentials(
84 access_token, client_id, client_secret,
85 refresh_token, token_expiry, token_uri,
86 user_agent)
87
88 f = open(FILENAME, 'w')
89 pickle.dump(credentials, f)
90 f.close()
91
92 # Storage should be able to read that object.
93 # TODO(jcgregorio) This should fail once pickle support is removed.
94 s = Storage(FILENAME)
95 credentials = s.get()
96 self.assertNotEquals(None, credentials)
97 self.assertEquals('foo', credentials.access_token)
98
99 # Now write it back out and confirm it has been rewritten as JSON
100 s.put(credentials)
101 f = file(FILENAME)
102 data = simplejson.load(f)
103 f.close()
104
105 self.assertEquals(data['access_token'], 'foo')
106 self.assertEquals(data['_class'], 'OAuth2Credentials')
107 self.assertEquals(data['_module'], 'oauth2client.client')
108
Joe Gregoriod2ee4d82011-09-15 14:32:45 -0400109 def test_token_refresh(self):
110 # Write a file with a pickled OAuth2Credentials.
111 access_token = 'foo'
112 client_id = 'some_client_id'
113 client_secret = 'cOuDdkfjxxnv+'
114 refresh_token = '1/0/a.df219fjls0'
115 token_expiry = datetime.datetime.utcnow()
116 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
117 user_agent = 'refresh_checker/1.0'
118
119 credentials = OAuth2Credentials(
120 access_token, client_id, client_secret,
121 refresh_token, token_expiry, token_uri,
122 user_agent)
123
124 s = Storage(FILENAME)
125 s.put(credentials)
126 credentials = s.get()
127 new_cred = copy.copy(credentials)
128 new_cred.access_token = 'bar'
129 s.put(new_cred)
130
131 credentials._refresh(lambda x: x)
132 self.assertEquals(credentials.access_token, 'bar')
133
134
Joe Gregorio562b7312011-09-15 09:06:38 -0400135 def test_access_token_credentials(self):
136 access_token = 'foo'
137 user_agent = 'refresh_checker/1.0'
138
139 credentials = AccessTokenCredentials(access_token, user_agent)
140
141 s = Storage(FILENAME)
142 credentials = s.put(credentials)
143 credentials = s.get()
144
145 self.assertNotEquals(None, credentials)
146 self.assertEquals('foo', credentials.access_token)
147
148 def test_multistore_non_existent_file(self):
149 store = multistore_file.get_credential_storage(
150 FILENAME,
151 'some_client_id',
152 'user-agent/1.0',
153 'some-scope')
154
155 credentials = store.get()
156 self.assertEquals(None, credentials)
157
158 def test_multistore_file(self):
159 access_token = 'foo'
160 client_secret = 'cOuDdkfjxxnv+'
161 refresh_token = '1/0/a.df219fjls0'
162 token_expiry = datetime.datetime.utcnow()
163 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
164 user_agent = 'refresh_checker/1.0'
165 client_id = 'some_client_id'
166
167 credentials = OAuth2Credentials(
168 access_token, client_id, client_secret,
169 refresh_token, token_expiry, token_uri,
170 user_agent)
171
172 store = multistore_file.get_credential_storage(
173 FILENAME,
174 credentials.client_id,
175 credentials.user_agent,
176 'some-scope')
177
178 store.put(credentials)
179 credentials = store.get()
180
181 self.assertNotEquals(None, credentials)
182 self.assertEquals('foo', credentials.access_token)
183
184if __name__ == '__main__':
185 unittest.main()