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