aboutsummaryrefslogtreecommitdiff
path: root/tests/test__helpers.py
diff options
context:
space:
mode:
authorDanny Hermes <daniel.j.hermes@gmail.com>2015-07-06 20:46:36 -0700
committerDanny Hermes <daniel.j.hermes@gmail.com>2015-07-13 13:46:53 -0700
commit3b3aeb65d704c0eec3471f12eae32d07dceaf288 (patch)
tree7494b5c2e13da10c480dc8be18d4cece79d1c6e9 /tests/test__helpers.py
parent76b3c40068db9923930db0e6047a9e5124ae68b9 (diff)
downloadoauth2client-3b3aeb65d704c0eec3471f12eae32d07dceaf288.tar.gz
Factoring out conditional code from `crypt.py`.
Until now, code that depended on PyCrypto or OpenSSL was defined conditionally (e.g. indented) in `crypt.py`. Rather than grouping all these together, we factor out the library specific behavior into standalone modules (but make the modules private / protected). In addition, added a `_helpers.py` module with common behavior that was previously defined in multiple places. Finally, beefed up some test cases so that the three newly added modules had 100% test coverage. Towards #212.
Diffstat (limited to 'tests/test__helpers.py')
-rw-r--r--tests/test__helpers.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/test__helpers.py b/tests/test__helpers.py
new file mode 100644
index 0000000..f476a13
--- /dev/null
+++ b/tests/test__helpers.py
@@ -0,0 +1,81 @@
+# Copyright 2015 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Unit tests for oauth2client._helpers."""
+
+import unittest
+
+from oauth2client._helpers import _json_encode
+from oauth2client._helpers import _parse_pem_key
+from oauth2client._helpers import _urlsafe_b64decode
+from oauth2client._helpers import _urlsafe_b64encode
+
+
+class Test__parse_pem_key(unittest.TestCase):
+
+ def test_valid_input(self):
+ test_string = b'1234-----BEGIN FOO BAR BAZ'
+ result = _parse_pem_key(test_string)
+ self.assertEqual(result, test_string[4:])
+
+ def test_bad_input(self):
+ test_string = b'DOES NOT HAVE DASHES'
+ result = _parse_pem_key(test_string)
+ self.assertEqual(result, None)
+
+
+class Test__json_encode(unittest.TestCase):
+
+ def test_dictionary_input(self):
+ # Use only a single key since dictionary hash order
+ # is non-deterministic.
+ data = {u'foo': 10}
+ result = _json_encode(data)
+ self.assertEqual(result, """{"foo":10}""")
+
+ def test_list_input(self):
+ data = [42, 1337]
+ result = _json_encode(data)
+ self.assertEqual(result, """[42,1337]""")
+
+
+class Test__urlsafe_b64encode(unittest.TestCase):
+
+ def test_valid_input_bytes(self):
+ test_string = b'deadbeef'
+ result = _urlsafe_b64encode(test_string)
+ self.assertEqual(result, u'ZGVhZGJlZWY')
+
+ def test_valid_input_unicode(self):
+ test_string = u'deadbeef'
+ result = _urlsafe_b64encode(test_string)
+ self.assertEqual(result, u'ZGVhZGJlZWY')
+
+
+class Test__urlsafe_b64decode(unittest.TestCase):
+
+ def test_valid_input_bytes(self):
+ test_string = b'ZGVhZGJlZWY'
+ result = _urlsafe_b64decode(test_string)
+ self.assertEqual(result, b'deadbeef')
+
+ def test_valid_input_unicode(self):
+ test_string = b'ZGVhZGJlZWY'
+ result = _urlsafe_b64decode(test_string)
+ self.assertEqual(result, b'deadbeef')
+
+ def test_bad_input(self):
+ import binascii
+ bad_string = b'+'
+ self.assertRaises((TypeError, binascii.Error),
+ _urlsafe_b64decode, bad_string)