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 Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 30 | import stat |
Joe Beda | 17311fb | 2011-09-20 14:43:08 -0700 | [diff] [blame] | 31 | import tempfile |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 32 | import unittest |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 33 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 34 | from apiclient.http import HttpMockSequence |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 35 | from oauth2client import multistore_file |
| 36 | from oauth2client.anyjson import simplejson |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 37 | from oauth2client.client import AccessTokenCredentials |
| 38 | from oauth2client.client import AssertionCredentials |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 39 | from oauth2client.client import OAuth2Credentials |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 40 | from oauth2client.file import Storage |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 41 | |
| 42 | |
Joe Beda | 17311fb | 2011-09-20 14:43:08 -0700 | [diff] [blame] | 43 | FILENAME = tempfile.mktemp('oauth2client_test.data') |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 44 | |
| 45 | |
| 46 | class OAuth2ClientFileTests(unittest.TestCase): |
| 47 | |
| 48 | def tearDown(self): |
| 49 | try: |
| 50 | os.unlink(FILENAME) |
| 51 | except OSError: |
| 52 | pass |
| 53 | |
| 54 | def setUp(self): |
| 55 | try: |
| 56 | os.unlink(FILENAME) |
| 57 | except OSError: |
| 58 | pass |
| 59 | |
| 60 | def test_non_existent_file_storage(self): |
| 61 | s = Storage(FILENAME) |
| 62 | credentials = s.get() |
| 63 | self.assertEquals(None, credentials) |
| 64 | |
| 65 | def test_pickle_and_json_interop(self): |
| 66 | # Write a file with a pickled OAuth2Credentials. |
| 67 | access_token = 'foo' |
| 68 | client_id = 'some_client_id' |
| 69 | client_secret = 'cOuDdkfjxxnv+' |
| 70 | refresh_token = '1/0/a.df219fjls0' |
| 71 | token_expiry = datetime.datetime.utcnow() |
| 72 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
| 73 | user_agent = 'refresh_checker/1.0' |
| 74 | |
| 75 | credentials = OAuth2Credentials( |
| 76 | access_token, client_id, client_secret, |
| 77 | refresh_token, token_expiry, token_uri, |
| 78 | user_agent) |
| 79 | |
| 80 | f = open(FILENAME, 'w') |
| 81 | pickle.dump(credentials, f) |
| 82 | f.close() |
| 83 | |
Joe Gregorio | ec55584 | 2011-10-27 11:10:39 -0400 | [diff] [blame] | 84 | # Storage should be not be able to read that object, as the capability to |
| 85 | # read and write credentials as pickled objects has been removed. |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 86 | s = Storage(FILENAME) |
Joe Gregorio | ec55584 | 2011-10-27 11:10:39 -0400 | [diff] [blame] | 87 | read_credentials = s.get() |
| 88 | self.assertEquals(None, read_credentials) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 89 | |
| 90 | # Now write it back out and confirm it has been rewritten as JSON |
| 91 | s.put(credentials) |
| 92 | f = file(FILENAME) |
| 93 | data = simplejson.load(f) |
| 94 | f.close() |
| 95 | |
| 96 | self.assertEquals(data['access_token'], 'foo') |
| 97 | self.assertEquals(data['_class'], 'OAuth2Credentials') |
Joe Beda | 17311fb | 2011-09-20 14:43:08 -0700 | [diff] [blame] | 98 | self.assertEquals(data['_module'], OAuth2Credentials.__module__) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 99 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 100 | def test_token_refresh(self): |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 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 | |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame^] | 124 | def test_credentials_delete(self): |
| 125 | access_token = 'foo' |
| 126 | client_id = 'some_client_id' |
| 127 | client_secret = 'cOuDdkfjxxnv+' |
| 128 | refresh_token = '1/0/a.df219fjls0' |
| 129 | token_expiry = datetime.datetime.utcnow() |
| 130 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
| 131 | user_agent = 'refresh_checker/1.0' |
| 132 | |
| 133 | credentials = OAuth2Credentials( |
| 134 | access_token, client_id, client_secret, |
| 135 | refresh_token, token_expiry, token_uri, |
| 136 | user_agent) |
| 137 | |
| 138 | s = Storage(FILENAME) |
| 139 | s.put(credentials) |
| 140 | credentials = s.get() |
| 141 | self.assertNotEquals(None, credentials) |
| 142 | s.delete() |
| 143 | credentials = s.get() |
| 144 | self.assertEquals(None, credentials) |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 145 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 146 | def test_access_token_credentials(self): |
| 147 | access_token = 'foo' |
| 148 | user_agent = 'refresh_checker/1.0' |
| 149 | |
| 150 | credentials = AccessTokenCredentials(access_token, user_agent) |
| 151 | |
| 152 | s = Storage(FILENAME) |
| 153 | credentials = s.put(credentials) |
| 154 | credentials = s.get() |
| 155 | |
| 156 | self.assertNotEquals(None, credentials) |
| 157 | self.assertEquals('foo', credentials.access_token) |
Joe Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 158 | mode = os.stat(FILENAME).st_mode |
| 159 | |
| 160 | if os.name == 'posix': |
| 161 | self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode))) |
| 162 | |
| 163 | def test_read_only_file_fail_lock(self): |
| 164 | access_token = 'foo' |
| 165 | client_secret = 'cOuDdkfjxxnv+' |
| 166 | refresh_token = '1/0/a.df219fjls0' |
| 167 | token_expiry = datetime.datetime.utcnow() |
| 168 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
| 169 | user_agent = 'refresh_checker/1.0' |
| 170 | client_id = 'some_client_id' |
| 171 | |
| 172 | credentials = OAuth2Credentials( |
| 173 | access_token, client_id, client_secret, |
| 174 | refresh_token, token_expiry, token_uri, |
| 175 | user_agent) |
| 176 | |
| 177 | open(FILENAME, 'a+b').close() |
| 178 | os.chmod(FILENAME, 0400) |
| 179 | |
| 180 | store = multistore_file.get_credential_storage( |
| 181 | FILENAME, |
| 182 | credentials.client_id, |
| 183 | credentials.user_agent, |
| 184 | ['some-scope', 'some-other-scope']) |
| 185 | |
| 186 | store.put(credentials) |
| 187 | if os.name == 'posix': |
| 188 | self.assertTrue(store._multistore._read_only) |
| 189 | os.chmod(FILENAME, 0600) |
| 190 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 191 | |
| 192 | def test_multistore_non_existent_file(self): |
| 193 | store = multistore_file.get_credential_storage( |
| 194 | FILENAME, |
| 195 | 'some_client_id', |
| 196 | 'user-agent/1.0', |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 197 | ['some-scope', 'some-other-scope']) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 198 | |
| 199 | credentials = store.get() |
| 200 | self.assertEquals(None, credentials) |
| 201 | |
| 202 | def test_multistore_file(self): |
| 203 | access_token = 'foo' |
| 204 | client_secret = 'cOuDdkfjxxnv+' |
| 205 | refresh_token = '1/0/a.df219fjls0' |
| 206 | token_expiry = datetime.datetime.utcnow() |
| 207 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
| 208 | user_agent = 'refresh_checker/1.0' |
| 209 | client_id = 'some_client_id' |
| 210 | |
| 211 | credentials = OAuth2Credentials( |
| 212 | access_token, client_id, client_secret, |
| 213 | refresh_token, token_expiry, token_uri, |
| 214 | user_agent) |
| 215 | |
| 216 | store = multistore_file.get_credential_storage( |
| 217 | FILENAME, |
| 218 | credentials.client_id, |
| 219 | credentials.user_agent, |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 220 | ['some-scope', 'some-other-scope']) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 221 | |
| 222 | store.put(credentials) |
| 223 | credentials = store.get() |
| 224 | |
| 225 | self.assertNotEquals(None, credentials) |
| 226 | self.assertEquals('foo', credentials.access_token) |
| 227 | |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame^] | 228 | store.delete() |
| 229 | credentials = store.get() |
| 230 | |
| 231 | self.assertEquals(None, credentials) |
| 232 | |
Joe Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 233 | if os.name == 'posix': |
| 234 | self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode))) |
| 235 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 236 | if __name__ == '__main__': |
| 237 | unittest.main() |