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): |
| 101 | # Write a file with a pickled OAuth2Credentials. |
| 102 | access_token = 'foo' |
| 103 | client_id = 'some_client_id' |
| 104 | client_secret = 'cOuDdkfjxxnv+' |
| 105 | refresh_token = '1/0/a.df219fjls0' |
| 106 | token_expiry = datetime.datetime.utcnow() |
| 107 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
| 108 | user_agent = 'refresh_checker/1.0' |
| 109 | |
| 110 | credentials = OAuth2Credentials( |
| 111 | access_token, client_id, client_secret, |
| 112 | refresh_token, token_expiry, token_uri, |
| 113 | user_agent) |
| 114 | |
| 115 | s = Storage(FILENAME) |
| 116 | s.put(credentials) |
| 117 | credentials = s.get() |
| 118 | new_cred = copy.copy(credentials) |
| 119 | new_cred.access_token = 'bar' |
| 120 | s.put(new_cred) |
| 121 | |
| 122 | credentials._refresh(lambda x: x) |
| 123 | self.assertEquals(credentials.access_token, 'bar') |
| 124 | |
| 125 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 126 | def test_access_token_credentials(self): |
| 127 | access_token = 'foo' |
| 128 | user_agent = 'refresh_checker/1.0' |
| 129 | |
| 130 | credentials = AccessTokenCredentials(access_token, user_agent) |
| 131 | |
| 132 | s = Storage(FILENAME) |
| 133 | credentials = s.put(credentials) |
| 134 | credentials = s.get() |
| 135 | |
| 136 | self.assertNotEquals(None, credentials) |
| 137 | self.assertEquals('foo', credentials.access_token) |
Joe Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 138 | mode = os.stat(FILENAME).st_mode |
| 139 | |
| 140 | if os.name == 'posix': |
| 141 | self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode))) |
| 142 | |
| 143 | def test_read_only_file_fail_lock(self): |
| 144 | access_token = 'foo' |
| 145 | client_secret = 'cOuDdkfjxxnv+' |
| 146 | refresh_token = '1/0/a.df219fjls0' |
| 147 | token_expiry = datetime.datetime.utcnow() |
| 148 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
| 149 | user_agent = 'refresh_checker/1.0' |
| 150 | client_id = 'some_client_id' |
| 151 | |
| 152 | credentials = OAuth2Credentials( |
| 153 | access_token, client_id, client_secret, |
| 154 | refresh_token, token_expiry, token_uri, |
| 155 | user_agent) |
| 156 | |
| 157 | open(FILENAME, 'a+b').close() |
| 158 | os.chmod(FILENAME, 0400) |
| 159 | |
| 160 | store = multistore_file.get_credential_storage( |
| 161 | FILENAME, |
| 162 | credentials.client_id, |
| 163 | credentials.user_agent, |
| 164 | ['some-scope', 'some-other-scope']) |
| 165 | |
| 166 | store.put(credentials) |
| 167 | if os.name == 'posix': |
| 168 | self.assertTrue(store._multistore._read_only) |
| 169 | os.chmod(FILENAME, 0600) |
| 170 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 171 | |
| 172 | def test_multistore_non_existent_file(self): |
| 173 | store = multistore_file.get_credential_storage( |
| 174 | FILENAME, |
| 175 | 'some_client_id', |
| 176 | 'user-agent/1.0', |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 177 | ['some-scope', 'some-other-scope']) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 178 | |
| 179 | credentials = store.get() |
| 180 | self.assertEquals(None, credentials) |
| 181 | |
| 182 | def test_multistore_file(self): |
| 183 | access_token = 'foo' |
| 184 | client_secret = 'cOuDdkfjxxnv+' |
| 185 | refresh_token = '1/0/a.df219fjls0' |
| 186 | token_expiry = datetime.datetime.utcnow() |
| 187 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
| 188 | user_agent = 'refresh_checker/1.0' |
| 189 | client_id = 'some_client_id' |
| 190 | |
| 191 | credentials = OAuth2Credentials( |
| 192 | access_token, client_id, client_secret, |
| 193 | refresh_token, token_expiry, token_uri, |
| 194 | user_agent) |
| 195 | |
| 196 | store = multistore_file.get_credential_storage( |
| 197 | FILENAME, |
| 198 | credentials.client_id, |
| 199 | credentials.user_agent, |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 200 | ['some-scope', 'some-other-scope']) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 201 | |
| 202 | store.put(credentials) |
| 203 | credentials = store.get() |
| 204 | |
| 205 | self.assertNotEquals(None, credentials) |
| 206 | self.assertEquals('foo', credentials.access_token) |
| 207 | |
Joe Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 208 | if os.name == 'posix': |
| 209 | self.assertEquals('0600', oct(stat.S_IMODE(os.stat(FILENAME).st_mode))) |
| 210 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 211 | if __name__ == '__main__': |
| 212 | unittest.main() |