blob: a34cce761d769ce182e85c360ab1fc64e758a6e0 [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
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040033from apiclient.http import HttpMockSequence
Joe Gregorio549230c2012-01-11 10:38:05 -050034from oauth2client import multistore_file
35from oauth2client.anyjson import simplejson
Joe Gregorio562b7312011-09-15 09:06:38 -040036from oauth2client.client import AccessTokenCredentials
37from oauth2client.client import AssertionCredentials
Joe Gregorio549230c2012-01-11 10:38:05 -050038from oauth2client.client import OAuth2Credentials
Joe Gregorio562b7312011-09-15 09:06:38 -040039from oauth2client.file import Storage
Joe Gregorio562b7312011-09-15 09:06:38 -040040
41
Joe Beda17311fb2011-09-20 14:43:08 -070042FILENAME = tempfile.mktemp('oauth2client_test.data')
Joe Gregorio562b7312011-09-15 09:06:38 -040043
44
45class OAuth2ClientFileTests(unittest.TestCase):
46
47 def tearDown(self):
48 try:
49 os.unlink(FILENAME)
50 except OSError:
51 pass
52
53 def setUp(self):
54 try:
55 os.unlink(FILENAME)
56 except OSError:
57 pass
58
59 def test_non_existent_file_storage(self):
60 s = Storage(FILENAME)
61 credentials = s.get()
62 self.assertEquals(None, credentials)
63
64 def test_pickle_and_json_interop(self):
65 # Write a file with a pickled OAuth2Credentials.
66 access_token = 'foo'
67 client_id = 'some_client_id'
68 client_secret = 'cOuDdkfjxxnv+'
69 refresh_token = '1/0/a.df219fjls0'
70 token_expiry = datetime.datetime.utcnow()
71 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
72 user_agent = 'refresh_checker/1.0'
73
74 credentials = OAuth2Credentials(
75 access_token, client_id, client_secret,
76 refresh_token, token_expiry, token_uri,
77 user_agent)
78
79 f = open(FILENAME, 'w')
80 pickle.dump(credentials, f)
81 f.close()
82
Joe Gregorioec555842011-10-27 11:10:39 -040083 # Storage should be not be able to read that object, as the capability to
84 # read and write credentials as pickled objects has been removed.
Joe Gregorio562b7312011-09-15 09:06:38 -040085 s = Storage(FILENAME)
Joe Gregorioec555842011-10-27 11:10:39 -040086 read_credentials = s.get()
87 self.assertEquals(None, read_credentials)
Joe Gregorio562b7312011-09-15 09:06:38 -040088
89 # Now write it back out and confirm it has been rewritten as JSON
90 s.put(credentials)
91 f = file(FILENAME)
92 data = simplejson.load(f)
93 f.close()
94
95 self.assertEquals(data['access_token'], 'foo')
96 self.assertEquals(data['_class'], 'OAuth2Credentials')
Joe Beda17311fb2011-09-20 14:43:08 -070097 self.assertEquals(data['_module'], OAuth2Credentials.__module__)
Joe Gregorio562b7312011-09-15 09:06:38 -040098
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040099 def test_token_refresh(self):
100 # Write a file with a pickled OAuth2Credentials.
101 access_token = 'foo'
102 client_id = 'some_client_id'
103 client_secret = 'cOuDdkfjxxnv+'
104 refresh_token = '1/0/a.df219fjls0'
105 token_expiry = datetime.datetime.utcnow()
106 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
107 user_agent = 'refresh_checker/1.0'
108
109 credentials = OAuth2Credentials(
110 access_token, client_id, client_secret,
111 refresh_token, token_expiry, token_uri,
112 user_agent)
113
114 s = Storage(FILENAME)
115 s.put(credentials)
116 credentials = s.get()
117 new_cred = copy.copy(credentials)
118 new_cred.access_token = 'bar'
119 s.put(new_cred)
120
121 credentials._refresh(lambda x: x)
122 self.assertEquals(credentials.access_token, 'bar')
123
124
Joe Gregorio562b7312011-09-15 09:06:38 -0400125 def test_access_token_credentials(self):
126 access_token = 'foo'
127 user_agent = 'refresh_checker/1.0'
128
129 credentials = AccessTokenCredentials(access_token, user_agent)
130
131 s = Storage(FILENAME)
132 credentials = s.put(credentials)
133 credentials = s.get()
134
135 self.assertNotEquals(None, credentials)
136 self.assertEquals('foo', credentials.access_token)
137
138 def test_multistore_non_existent_file(self):
139 store = multistore_file.get_credential_storage(
140 FILENAME,
141 'some_client_id',
142 'user-agent/1.0',
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400143 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400144
145 credentials = store.get()
146 self.assertEquals(None, credentials)
147
148 def test_multistore_file(self):
149 access_token = 'foo'
150 client_secret = 'cOuDdkfjxxnv+'
151 refresh_token = '1/0/a.df219fjls0'
152 token_expiry = datetime.datetime.utcnow()
153 token_uri = 'https://www.google.com/accounts/o8/oauth2/token'
154 user_agent = 'refresh_checker/1.0'
155 client_id = 'some_client_id'
156
157 credentials = OAuth2Credentials(
158 access_token, client_id, client_secret,
159 refresh_token, token_expiry, token_uri,
160 user_agent)
161
162 store = multistore_file.get_credential_storage(
163 FILENAME,
164 credentials.client_id,
165 credentials.user_agent,
Joe Gregoriof2f8a5a2011-10-14 15:11:29 -0400166 ['some-scope', 'some-other-scope'])
Joe Gregorio562b7312011-09-15 09:06:38 -0400167
168 store.put(credentials)
169 credentials = store.get()
170
171 self.assertNotEquals(None, credentials)
172 self.assertEquals('foo', credentials.access_token)
173
174if __name__ == '__main__':
175 unittest.main()