Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 1 | # Copyright 2011 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Unit tests for oauth2client.clientsecrets.""" |
| 16 | |
| 17 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 18 | |
| 19 | |
| 20 | import os |
| 21 | import unittest |
| 22 | import StringIO |
| 23 | |
Joe Gregorio | 39defdc | 2011-10-07 15:14:43 -0400 | [diff] [blame] | 24 | import httplib2 |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 25 | |
Joe Gregorio | ce2d204 | 2011-10-07 14:35:14 -0400 | [diff] [blame] | 26 | from oauth2client import clientsecrets |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 27 | |
| 28 | |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame^] | 29 | DATA_DIR = os.path.join(os.path.dirname(__file__), 'data') |
| 30 | VALID_FILE = os.path.join(DATA_DIR, 'client_secrets.json') |
| 31 | INVALID_FILE = os.path.join(DATA_DIR, 'unfilled_client_secrets.json') |
| 32 | NONEXISTENT_FILE = os.path.join(__file__, '..', 'afilethatisntthere.json') |
| 33 | |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 34 | class OAuth2CredentialsTests(unittest.TestCase): |
| 35 | |
| 36 | def setUp(self): |
| 37 | pass |
| 38 | |
| 39 | def tearDown(self): |
| 40 | pass |
| 41 | |
| 42 | def test_validate_error(self): |
| 43 | ERRORS = [ |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 44 | ('{}', 'Invalid'), |
| 45 | ('{"foo": {}}', 'Unknown'), |
| 46 | ('{"web": {}}', 'Missing'), |
| 47 | ('{"web": {"client_id": "dkkd"}}', 'Missing'), |
| 48 | ("""{ |
| 49 | "web": { |
| 50 | "client_id": "[[CLIENT ID REQUIRED]]", |
| 51 | "client_secret": "[[CLIENT SECRET REQUIRED]]", |
| 52 | "redirect_uris": ["http://localhost:8080/oauth2callback"], |
| 53 | "auth_uri": "", |
| 54 | "token_uri": "" |
| 55 | } |
| 56 | } |
| 57 | """, 'Property'), |
| 58 | ] |
| 59 | for src, match in ERRORS: |
| 60 | # Test load(s) |
| 61 | try: |
| 62 | clientsecrets.loads(src) |
| 63 | self.fail(src + ' should not be a valid client_secrets file.') |
| 64 | except clientsecrets.InvalidClientSecretsError, e: |
| 65 | self.assertTrue(str(e).startswith(match)) |
| 66 | |
| 67 | # Test loads(fp) |
| 68 | try: |
| 69 | fp = StringIO.StringIO(src) |
| 70 | clientsecrets.load(fp) |
| 71 | self.fail(src + ' should not be a valid client_secrets file.') |
| 72 | except clientsecrets.InvalidClientSecretsError, e: |
| 73 | self.assertTrue(str(e).startswith(match)) |
| 74 | |
| 75 | def test_load_by_filename(self): |
| 76 | try: |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame^] | 77 | clientsecrets._loadfile(NONEXISTENT_FILE) |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 78 | self.fail('should fail to load a missing client_secrets file.') |
| 79 | except clientsecrets.InvalidClientSecretsError, e: |
| 80 | self.assertTrue(str(e).startswith('File')) |
| 81 | |
| 82 | |
Joe Gregorio | c29aaa9 | 2012-07-16 16:16:31 -0400 | [diff] [blame^] | 83 | class CachedClientsecretsTests(unittest.TestCase): |
| 84 | |
| 85 | class CacheMock(object): |
| 86 | def __init__(self): |
| 87 | self.cache = {} |
| 88 | self.last_get_ns = None |
| 89 | self.last_set_ns = None |
| 90 | |
| 91 | def get(self, key, namespace=''): |
| 92 | # ignoring namespace for easier testing |
| 93 | self.last_get_ns = namespace |
| 94 | return self.cache.get(key, None) |
| 95 | |
| 96 | def set(self, key, value, namespace=''): |
| 97 | # ignoring namespace for easier testing |
| 98 | self.last_set_ns = namespace |
| 99 | self.cache[key] = value |
| 100 | |
| 101 | def setUp(self): |
| 102 | self.cache_mock = self.CacheMock() |
| 103 | |
| 104 | def test_cache_miss(self): |
| 105 | client_type, client_info = clientsecrets.loadfile( |
| 106 | VALID_FILE, cache=self.cache_mock) |
| 107 | self.assertEquals('web', client_type) |
| 108 | self.assertEquals('foo_client_secret', client_info['client_secret']) |
| 109 | |
| 110 | cached = self.cache_mock.cache[VALID_FILE] |
| 111 | self.assertEquals({client_type: client_info}, cached) |
| 112 | |
| 113 | # make sure we're using non-empty namespace |
| 114 | ns = self.cache_mock.last_set_ns |
| 115 | self.assertTrue(bool(ns)) |
| 116 | # make sure they're equal |
| 117 | self.assertEquals(ns, self.cache_mock.last_get_ns) |
| 118 | |
| 119 | def test_cache_hit(self): |
| 120 | self.cache_mock.cache[NONEXISTENT_FILE] = { 'web': 'secret info' } |
| 121 | |
| 122 | client_type, client_info = clientsecrets.loadfile( |
| 123 | NONEXISTENT_FILE, cache=self.cache_mock) |
| 124 | self.assertEquals('web', client_type) |
| 125 | self.assertEquals('secret info', client_info) |
| 126 | # make sure we didn't do any set() RPCs |
| 127 | self.assertEqual(None, self.cache_mock.last_set_ns) |
| 128 | |
| 129 | def test_validation(self): |
| 130 | try: |
| 131 | clientsecrets.loadfile(INVALID_FILE, cache=self.cache_mock) |
| 132 | self.fail('Expected InvalidClientSecretsError to be raised ' |
| 133 | 'while loading %s' % INVALID_FILE) |
| 134 | except clientsecrets.InvalidClientSecretsError: |
| 135 | pass |
| 136 | |
| 137 | def test_without_cache(self): |
| 138 | # this also ensures loadfile() is backward compatible |
| 139 | client_type, client_info = clientsecrets.loadfile(VALID_FILE) |
| 140 | self.assertEquals('web', client_type) |
| 141 | self.assertEquals('foo_client_secret', client_info['client_secret']) |
| 142 | |
| 143 | |
Joe Gregorio | f08a498 | 2011-10-07 13:11:16 -0400 | [diff] [blame] | 144 | if __name__ == '__main__': |
| 145 | unittest.main() |