1# Copyright 2015 Google Inc. All rights reserved. 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"""Unit tests for oauth2client._helpers.""" 15 16import unittest2 17 18from oauth2client import _helpers 19 20 21class Test__parse_pem_key(unittest2.TestCase): 22 23 def test_valid_input(self): 24 test_string = b'1234-----BEGIN FOO BAR BAZ' 25 result = _helpers._parse_pem_key(test_string) 26 self.assertEqual(result, test_string[4:]) 27 28 def test_bad_input(self): 29 test_string = b'DOES NOT HAVE DASHES' 30 result = _helpers._parse_pem_key(test_string) 31 self.assertEqual(result, None) 32 33 34class Test__json_encode(unittest2.TestCase): 35 36 def test_dictionary_input(self): 37 # Use only a single key since dictionary hash order 38 # is non-deterministic. 39 data = {u'foo': 10} 40 result = _helpers._json_encode(data) 41 self.assertEqual(result, '{"foo":10}') 42 43 def test_list_input(self): 44 data = [42, 1337] 45 result = _helpers._json_encode(data) 46 self.assertEqual(result, '[42,1337]') 47 48 49class Test__to_bytes(unittest2.TestCase): 50 51 def test_with_bytes(self): 52 value = b'bytes-val' 53 self.assertEqual(_helpers._to_bytes(value), value) 54 55 def test_with_unicode(self): 56 value = u'string-val' 57 encoded_value = b'string-val' 58 self.assertEqual(_helpers._to_bytes(value), encoded_value) 59 60 def test_with_nonstring_type(self): 61 value = object() 62 with self.assertRaises(ValueError): 63 _helpers._to_bytes(value) 64 65 66class Test__from_bytes(unittest2.TestCase): 67 68 def test_with_unicode(self): 69 value = u'bytes-val' 70 self.assertEqual(_helpers._from_bytes(value), value) 71 72 def test_with_bytes(self): 73 value = b'string-val' 74 decoded_value = u'string-val' 75 self.assertEqual(_helpers._from_bytes(value), decoded_value) 76 77 def test_with_nonstring_type(self): 78 value = object() 79 with self.assertRaises(ValueError): 80 _helpers._from_bytes(value) 81 82 83class Test__urlsafe_b64encode(unittest2.TestCase): 84 85 DEADBEEF_ENCODED = b'ZGVhZGJlZWY' 86 87 def test_valid_input_bytes(self): 88 test_string = b'deadbeef' 89 result = _helpers._urlsafe_b64encode(test_string) 90 self.assertEqual(result, self.DEADBEEF_ENCODED) 91 92 def test_valid_input_unicode(self): 93 test_string = u'deadbeef' 94 result = _helpers._urlsafe_b64encode(test_string) 95 self.assertEqual(result, self.DEADBEEF_ENCODED) 96 97 98class Test__urlsafe_b64decode(unittest2.TestCase): 99 100 def test_valid_input_bytes(self): 101 test_string = b'ZGVhZGJlZWY' 102 result = _helpers._urlsafe_b64decode(test_string) 103 self.assertEqual(result, b'deadbeef') 104 105 def test_valid_input_unicode(self): 106 test_string = b'ZGVhZGJlZWY' 107 result = _helpers._urlsafe_b64decode(test_string) 108 self.assertEqual(result, b'deadbeef') 109 110 def test_bad_input(self): 111 import binascii 112 bad_string = b'+' 113 with self.assertRaises((TypeError, binascii.Error)): 114 _helpers._urlsafe_b64decode(bad_string) 115