blob: f1aaf4d1220777c5b2e31b3c47deb1ae813b2ce8 [file] [log] [blame]
Joe Gregoriof08a4982011-10-07 13:11:16 -04001# 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
20import os
21import unittest
22import StringIO
23
24
Joe Gregorioce2d2042011-10-07 14:35:14 -040025from oauth2client import clientsecrets
Joe Gregoriof08a4982011-10-07 13:11:16 -040026
27
28class OAuth2CredentialsTests(unittest.TestCase):
29
30 def setUp(self):
31 pass
32
33 def tearDown(self):
34 pass
35
36 def test_validate_error(self):
37 ERRORS = [
Joe Gregoriof08a4982011-10-07 13:11:16 -040038 ('{}', 'Invalid'),
39 ('{"foo": {}}', 'Unknown'),
40 ('{"web": {}}', 'Missing'),
41 ('{"web": {"client_id": "dkkd"}}', 'Missing'),
42 ("""{
43 "web": {
44 "client_id": "[[CLIENT ID REQUIRED]]",
45 "client_secret": "[[CLIENT SECRET REQUIRED]]",
46 "redirect_uris": ["http://localhost:8080/oauth2callback"],
47 "auth_uri": "",
48 "token_uri": ""
49 }
50 }
51 """, 'Property'),
52 ]
53 for src, match in ERRORS:
54 # Test load(s)
55 try:
56 clientsecrets.loads(src)
57 self.fail(src + ' should not be a valid client_secrets file.')
58 except clientsecrets.InvalidClientSecretsError, e:
59 self.assertTrue(str(e).startswith(match))
60
61 # Test loads(fp)
62 try:
63 fp = StringIO.StringIO(src)
64 clientsecrets.load(fp)
65 self.fail(src + ' should not be a valid client_secrets file.')
66 except clientsecrets.InvalidClientSecretsError, e:
67 self.assertTrue(str(e).startswith(match))
68
69 def test_load_by_filename(self):
70 try:
71 clientsecrets.loadfile(os.path.join(__file__, '..',
72 'afilethatisntthere.json'))
73 self.fail('should fail to load a missing client_secrets file.')
74 except clientsecrets.InvalidClientSecretsError, e:
75 self.assertTrue(str(e).startswith('File'))
76
77
78if __name__ == '__main__':
79 unittest.main()