diff options
author | Pat Ferate <pferate+github@gmail.com> | 2016-08-01 13:17:14 -0700 |
---|---|---|
committer | Pat Ferate <pferate+github@gmail.com> | 2016-08-04 14:11:25 -0700 |
commit | 2f5c53b1fa6a2325427da0a3b63d4dc7ddc4261e (patch) | |
tree | 43b2f5f72197c16f391a8ad26c607a3ef73040ce /tests/test__helpers.py | |
parent | 0dc30bc03375f7dd4525b95f4f641417e947f28b (diff) | |
download | oauth2client-2f5c53b1fa6a2325427da0a3b63d4dc7ddc4261e.tar.gz |
Merge util.py and _helpers.py
A new file, `_helpers.py`, was created without realizing that
`utils.py` existed for the same purpose.
Moving all to `_helpers.py`.
Diffstat (limited to 'tests/test__helpers.py')
-rw-r--r-- | tests/test__helpers.py | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/tests/test__helpers.py b/tests/test__helpers.py index cd54186..8ac8890 100644 --- a/tests/test__helpers.py +++ b/tests/test__helpers.py @@ -11,13 +11,133 @@ # 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 mock import unittest2 from oauth2client import _helpers +__author__ = 'jcgregorio@google.com (Joe Gregorio)' + + +class PositionalTests(unittest2.TestCase): + + def test_usage(self): + _helpers.positional_parameters_enforcement = ( + _helpers.POSITIONAL_EXCEPTION) + + # 1 positional arg, 1 keyword-only arg. + @_helpers.positional(1) + def function(pos, kwonly=None): + return True + + self.assertTrue(function(1)) + self.assertTrue(function(1, kwonly=2)) + with self.assertRaises(TypeError): + function(1, 2) + + # No positional, but a required keyword arg. + @_helpers.positional(0) + def function2(required_kw): + return True + + self.assertTrue(function2(required_kw=1)) + with self.assertRaises(TypeError): + function2(1) + + # Unspecified positional, should automatically figure out 1 positional + # 1 keyword-only (same as first case above). + @_helpers.positional + def function3(pos, kwonly=None): + return True + + self.assertTrue(function3(1)) + self.assertTrue(function3(1, kwonly=2)) + with self.assertRaises(TypeError): + function3(1, 2) + + @mock.patch('oauth2client._helpers.logger') + def test_enforcement_warning(self, mock_logger): + _helpers.positional_parameters_enforcement = ( + _helpers.POSITIONAL_WARNING) + + @_helpers.positional(1) + def function(pos, kwonly=None): + return True + + self.assertTrue(function(1, 2)) + self.assertTrue(mock_logger.warning.called) + + @mock.patch('oauth2client._helpers.logger') + def test_enforcement_ignore(self, mock_logger): + _helpers.positional_parameters_enforcement = _helpers.POSITIONAL_IGNORE + + @_helpers.positional(1) + def function(pos, kwonly=None): + return True + + self.assertTrue(function(1, 2)) + self.assertFalse(mock_logger.warning.called) + + +class ScopeToStringTests(unittest2.TestCase): + + def test_iterables(self): + cases = [ + ('', ''), + ('', ()), + ('', []), + ('', ('',)), + ('', ['', ]), + ('a', ('a',)), + ('b', ['b', ]), + ('a b', ['a', 'b']), + ('a b', ('a', 'b')), + ('a b', 'a b'), + ('a b', (s for s in ['a', 'b'])), + ] + for expected, case in cases: + self.assertEqual(expected, _helpers.scopes_to_string(case)) + + +class StringToScopeTests(unittest2.TestCase): + + def test_conversion(self): + cases = [ + (['a', 'b'], ['a', 'b']), + ('', []), + ('a', ['a']), + ('a b c d e f', ['a', 'b', 'c', 'd', 'e', 'f']), + ] + + for case, expected in cases: + self.assertEqual(expected, _helpers.string_to_scopes(case)) + + +class AddQueryParameterTests(unittest2.TestCase): + + def test__add_query_parameter(self): + self.assertEqual( + _helpers._add_query_parameter('/action', 'a', None), + '/action') + self.assertEqual( + _helpers._add_query_parameter('/action', 'a', 'b'), + '/action?a=b') + self.assertEqual( + _helpers._add_query_parameter('/action?a=b', 'a', 'c'), + '/action?a=c') + # Order is non-deterministic. + self.assertIn( + _helpers._add_query_parameter('/action?a=b', 'c', 'd'), + ['/action?a=b&c=d', '/action?c=d&a=b']) + self.assertEqual( + _helpers._add_query_parameter('/action', 'a', ' ='), + '/action?a=+%3D') + + class Test__parse_pem_key(unittest2.TestCase): def test_valid_input(self): |