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