test_utils.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. from oauthlib.oauth1.rfc5849.utils import *
  3. from tests.unittest import TestCase
  4. class UtilsTests(TestCase):
  5. sample_params_list = [
  6. ("notoauth", "shouldnotbehere"),
  7. ("oauth_consumer_key", "9djdj82h48djs9d2"),
  8. ("oauth_token", "kkk9d7dh3k39sjv7"),
  9. ("notoautheither", "shouldnotbehere")
  10. ]
  11. sample_params_dict = {
  12. "notoauth": "shouldnotbehere",
  13. "oauth_consumer_key": "9djdj82h48djs9d2",
  14. "oauth_token": "kkk9d7dh3k39sjv7",
  15. "notoautheither": "shouldnotbehere"
  16. }
  17. sample_params_unicode_list = [
  18. ("notoauth", "shouldnotbehere"),
  19. ("oauth_consumer_key", "9djdj82h48djs9d2"),
  20. ("oauth_token", "kkk9d7dh3k39sjv7"),
  21. ("notoautheither", "shouldnotbehere")
  22. ]
  23. sample_params_unicode_dict = {
  24. "notoauth": "shouldnotbehere",
  25. "oauth_consumer_key": "9djdj82h48djs9d2",
  26. "oauth_token": "kkk9d7dh3k39sjv7",
  27. "notoautheither": "shouldnotbehere"
  28. }
  29. authorization_header = """OAuth realm="Example",
  30. oauth_consumer_key="9djdj82h48djs9d2",
  31. oauth_token="kkk9d7dh3k39sjv7",
  32. oauth_signature_method="HMAC-SHA1",
  33. oauth_timestamp="137131201",
  34. oauth_nonce="7d8f3e4a",
  35. oauth_signature="djosJKDKJSD8743243%2Fjdk33klY%3D" """.strip()
  36. bad_authorization_headers = (
  37. "OAuth",
  38. "OAuth oauth_nonce=",
  39. "Negotiate b2F1dGhsaWI=",
  40. "OA",
  41. )
  42. def test_filter_params(self):
  43. # The following is an isolated test function used to test the filter_params decorator.
  44. @filter_params
  45. def special_test_function(params, realm=None):
  46. """ I am a special test function """
  47. return 'OAuth ' + ','.join(['='.join([k, v]) for k, v in params])
  48. # check that the docstring got through
  49. self.assertEqual(special_test_function.__doc__, " I am a special test function ")
  50. # Check that the decorator filtering works as per design.
  51. # Any param that does not start with 'oauth'
  52. # should not be present in the filtered params
  53. filtered_params = special_test_function(self.sample_params_list)
  54. self.assertNotIn("notoauth", filtered_params)
  55. self.assertIn("oauth_consumer_key", filtered_params)
  56. self.assertIn("oauth_token", filtered_params)
  57. self.assertNotIn("notoautheither", filtered_params)
  58. def test_filter_oauth_params(self):
  59. # try with list
  60. # try with list
  61. # try with list
  62. self.assertEqual(len(self.sample_params_list), 4)
  63. # Any param that does not start with 'oauth'
  64. # should not be present in the filtered params
  65. filtered_params = filter_oauth_params(self.sample_params_list)
  66. self.assertEqual(len(filtered_params), 2)
  67. self.assertTrue(filtered_params[0][0].startswith('oauth'))
  68. self.assertTrue(filtered_params[1][0].startswith('oauth'))
  69. # try with dict
  70. # try with dict
  71. # try with dict
  72. self.assertEqual(len(self.sample_params_dict), 4)
  73. # Any param that does not start with 'oauth'
  74. # should not be present in the filtered params
  75. filtered_params = filter_oauth_params(self.sample_params_dict)
  76. self.assertEqual(len(filtered_params), 2)
  77. self.assertTrue(filtered_params[0][0].startswith('oauth'))
  78. self.assertTrue(filtered_params[1][0].startswith('oauth'))
  79. def test_escape(self):
  80. self.assertRaises(ValueError, escape, b"I am a string type. Not a unicode type.")
  81. self.assertEqual(escape("I am a unicode type."), "I%20am%20a%20unicode%20type.")
  82. self.assertIsInstance(escape("I am a unicode type."), str)
  83. def test_unescape(self):
  84. self.assertRaises(ValueError, unescape, b"I am a string type. Not a unicode type.")
  85. self.assertEqual(unescape("I%20am%20a%20unicode%20type."), 'I am a unicode type.')
  86. self.assertIsInstance(unescape("I%20am%20a%20unicode%20type."), str)
  87. def test_parse_authorization_header(self):
  88. # make us some headers
  89. authorization_headers = parse_authorization_header(self.authorization_header)
  90. # is it a list?
  91. self.assertIsInstance(authorization_headers, list)
  92. # are the internal items tuples?
  93. for header in authorization_headers:
  94. self.assertIsInstance(header, tuple)
  95. # are the internal components of each tuple unicode?
  96. for k, v in authorization_headers:
  97. self.assertIsInstance(k, str)
  98. self.assertIsInstance(v, str)
  99. # let's check the parsed headers created
  100. correct_headers = [
  101. ("oauth_nonce", "7d8f3e4a"),
  102. ("oauth_timestamp", "137131201"),
  103. ("oauth_consumer_key", "9djdj82h48djs9d2"),
  104. ('oauth_signature', 'djosJKDKJSD8743243%2Fjdk33klY%3D'),
  105. ('oauth_signature_method', 'HMAC-SHA1'),
  106. ('oauth_token', 'kkk9d7dh3k39sjv7'),
  107. ('realm', 'Example')]
  108. self.assertEqual(sorted(authorization_headers), sorted(correct_headers))
  109. # Check against malformed headers.
  110. for header in self.bad_authorization_headers:
  111. self.assertRaises(ValueError, parse_authorization_header, header)