Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 1 | #!/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 | |
| 20 | Unit tests for oauth2client.file |
| 21 | """ |
| 22 | |
| 23 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 24 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 25 | import copy |
Joe Beda | 17311fb | 2011-09-20 14:43:08 -0700 | [diff] [blame] | 26 | import datetime |
| 27 | import httplib2 |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 28 | import os |
| 29 | import pickle |
Joe Beda | 17311fb | 2011-09-20 14:43:08 -0700 | [diff] [blame] | 30 | import tempfile |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 31 | import unittest |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 32 | |
| 33 | |
| 34 | try: # pragma: no cover |
| 35 | import simplejson |
| 36 | except 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 Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 44 | from apiclient.http import HttpMockSequence |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 45 | |
| 46 | from oauth2client.client import OAuth2Credentials |
| 47 | from oauth2client.client import AccessTokenCredentials |
| 48 | from oauth2client.client import AssertionCredentials |
| 49 | from oauth2client.file import Storage |
| 50 | from oauth2client import multistore_file |
| 51 | |
| 52 | |
Joe Beda | 17311fb | 2011-09-20 14:43:08 -0700 | [diff] [blame] | 53 | FILENAME = tempfile.mktemp('oauth2client_test.data') |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 54 | |
| 55 | |
| 56 | class 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 Beda | 17311fb | 2011-09-20 14:43:08 -0700 | [diff] [blame] | 109 | self.assertEquals(data['_module'], OAuth2Credentials.__module__) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 110 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 111 | 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 Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 137 | 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 Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 155 | ['some-scope', 'some-other-scope']) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 156 | |
| 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 Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 178 | ['some-scope', 'some-other-scope']) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 179 | |
| 180 | store.put(credentials) |
| 181 | credentials = store.get() |
| 182 | |
| 183 | self.assertNotEquals(None, credentials) |
| 184 | self.assertEquals('foo', credentials.access_token) |
| 185 | |
| 186 | if __name__ == '__main__': |
| 187 | unittest.main() |