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 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 33 | from apiclient.http import HttpMockSequence |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 34 | from oauth2client import multistore_file |
| 35 | from oauth2client.anyjson import simplejson |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 36 | from oauth2client.client import AccessTokenCredentials |
| 37 | from oauth2client.client import AssertionCredentials |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 38 | from oauth2client.client import OAuth2Credentials |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 39 | from oauth2client.file import Storage |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 40 | |
| 41 | |
Joe Beda | 17311fb | 2011-09-20 14:43:08 -0700 | [diff] [blame] | 42 | FILENAME = tempfile.mktemp('oauth2client_test.data') |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 43 | |
| 44 | |
| 45 | class OAuth2ClientFileTests(unittest.TestCase): |
| 46 | |
| 47 | def tearDown(self): |
| 48 | try: |
| 49 | os.unlink(FILENAME) |
| 50 | except OSError: |
| 51 | pass |
| 52 | |
| 53 | def setUp(self): |
| 54 | try: |
| 55 | os.unlink(FILENAME) |
| 56 | except OSError: |
| 57 | pass |
| 58 | |
| 59 | def test_non_existent_file_storage(self): |
| 60 | s = Storage(FILENAME) |
| 61 | credentials = s.get() |
| 62 | self.assertEquals(None, credentials) |
| 63 | |
| 64 | def test_pickle_and_json_interop(self): |
| 65 | # Write a file with a pickled OAuth2Credentials. |
| 66 | access_token = 'foo' |
| 67 | client_id = 'some_client_id' |
| 68 | client_secret = 'cOuDdkfjxxnv+' |
| 69 | refresh_token = '1/0/a.df219fjls0' |
| 70 | token_expiry = datetime.datetime.utcnow() |
| 71 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
| 72 | user_agent = 'refresh_checker/1.0' |
| 73 | |
| 74 | credentials = OAuth2Credentials( |
| 75 | access_token, client_id, client_secret, |
| 76 | refresh_token, token_expiry, token_uri, |
| 77 | user_agent) |
| 78 | |
| 79 | f = open(FILENAME, 'w') |
| 80 | pickle.dump(credentials, f) |
| 81 | f.close() |
| 82 | |
Joe Gregorio | ec55584 | 2011-10-27 11:10:39 -0400 | [diff] [blame] | 83 | # Storage should be not be able to read that object, as the capability to |
| 84 | # read and write credentials as pickled objects has been removed. |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 85 | s = Storage(FILENAME) |
Joe Gregorio | ec55584 | 2011-10-27 11:10:39 -0400 | [diff] [blame] | 86 | read_credentials = s.get() |
| 87 | self.assertEquals(None, read_credentials) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 88 | |
| 89 | # Now write it back out and confirm it has been rewritten as JSON |
| 90 | s.put(credentials) |
| 91 | f = file(FILENAME) |
| 92 | data = simplejson.load(f) |
| 93 | f.close() |
| 94 | |
| 95 | self.assertEquals(data['access_token'], 'foo') |
| 96 | self.assertEquals(data['_class'], 'OAuth2Credentials') |
Joe Beda | 17311fb | 2011-09-20 14:43:08 -0700 | [diff] [blame] | 97 | self.assertEquals(data['_module'], OAuth2Credentials.__module__) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 98 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 99 | def test_token_refresh(self): |
| 100 | # Write a file with a pickled OAuth2Credentials. |
| 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 | |
| 124 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 125 | def test_access_token_credentials(self): |
| 126 | access_token = 'foo' |
| 127 | user_agent = 'refresh_checker/1.0' |
| 128 | |
| 129 | credentials = AccessTokenCredentials(access_token, user_agent) |
| 130 | |
| 131 | s = Storage(FILENAME) |
| 132 | credentials = s.put(credentials) |
| 133 | credentials = s.get() |
| 134 | |
| 135 | self.assertNotEquals(None, credentials) |
| 136 | self.assertEquals('foo', credentials.access_token) |
| 137 | |
| 138 | def test_multistore_non_existent_file(self): |
| 139 | store = multistore_file.get_credential_storage( |
| 140 | FILENAME, |
| 141 | 'some_client_id', |
| 142 | 'user-agent/1.0', |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 143 | ['some-scope', 'some-other-scope']) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 144 | |
| 145 | credentials = store.get() |
| 146 | self.assertEquals(None, credentials) |
| 147 | |
| 148 | def test_multistore_file(self): |
| 149 | access_token = 'foo' |
| 150 | client_secret = 'cOuDdkfjxxnv+' |
| 151 | refresh_token = '1/0/a.df219fjls0' |
| 152 | token_expiry = datetime.datetime.utcnow() |
| 153 | token_uri = 'https://www.google.com/accounts/o8/oauth2/token' |
| 154 | user_agent = 'refresh_checker/1.0' |
| 155 | client_id = 'some_client_id' |
| 156 | |
| 157 | credentials = OAuth2Credentials( |
| 158 | access_token, client_id, client_secret, |
| 159 | refresh_token, token_expiry, token_uri, |
| 160 | user_agent) |
| 161 | |
| 162 | store = multistore_file.get_credential_storage( |
| 163 | FILENAME, |
| 164 | credentials.client_id, |
| 165 | credentials.user_agent, |
Joe Gregorio | f2f8a5a | 2011-10-14 15:11:29 -0400 | [diff] [blame] | 166 | ['some-scope', 'some-other-scope']) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 167 | |
| 168 | store.put(credentials) |
| 169 | credentials = store.get() |
| 170 | |
| 171 | self.assertNotEquals(None, credentials) |
| 172 | self.assertEquals('foo', credentials.access_token) |
| 173 | |
| 174 | if __name__ == '__main__': |
| 175 | unittest.main() |