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