test_parameters.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. from oauthlib.common import urlencode
  3. from oauthlib.oauth1.rfc5849.parameters import (
  4. _append_params, prepare_form_encoded_body, prepare_headers,
  5. prepare_request_uri_query,
  6. )
  7. from tests.unittest import TestCase
  8. class ParameterTests(TestCase):
  9. auth_only_params = [
  10. ('oauth_consumer_key', "9djdj82h48djs9d2"),
  11. ('oauth_token', "kkk9d7dh3k39sjv7"),
  12. ('oauth_signature_method', "HMAC-SHA1"),
  13. ('oauth_timestamp', "137131201"),
  14. ('oauth_nonce', "7d8f3e4a"),
  15. ('oauth_signature', "bYT5CMsGcbgUdFHObYMEfcx6bsw=")
  16. ]
  17. auth_and_data = list(auth_only_params)
  18. auth_and_data.append(('data_param_foo', 'foo'))
  19. auth_and_data.append(('data_param_1', '1'))
  20. realm = 'testrealm'
  21. norealm_authorization_header = ' '.join((
  22. 'OAuth',
  23. 'oauth_consumer_key="9djdj82h48djs9d2",',
  24. 'oauth_token="kkk9d7dh3k39sjv7",',
  25. 'oauth_signature_method="HMAC-SHA1",',
  26. 'oauth_timestamp="137131201",',
  27. 'oauth_nonce="7d8f3e4a",',
  28. 'oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D"',
  29. ))
  30. withrealm_authorization_header = ' '.join((
  31. 'OAuth',
  32. 'realm="testrealm",',
  33. 'oauth_consumer_key="9djdj82h48djs9d2",',
  34. 'oauth_token="kkk9d7dh3k39sjv7",',
  35. 'oauth_signature_method="HMAC-SHA1",',
  36. 'oauth_timestamp="137131201",',
  37. 'oauth_nonce="7d8f3e4a",',
  38. 'oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D"',
  39. ))
  40. def test_append_params(self):
  41. unordered_1 = [
  42. ('oauth_foo', 'foo'),
  43. ('lala', 123),
  44. ('oauth_baz', 'baz'),
  45. ('oauth_bar', 'bar'), ]
  46. unordered_2 = [
  47. ('teehee', 456),
  48. ('oauth_quux', 'quux'), ]
  49. expected = [
  50. ('teehee', 456),
  51. ('lala', 123),
  52. ('oauth_quux', 'quux'),
  53. ('oauth_foo', 'foo'),
  54. ('oauth_baz', 'baz'),
  55. ('oauth_bar', 'bar'), ]
  56. self.assertEqual(_append_params(unordered_1, unordered_2), expected)
  57. def test_prepare_headers(self):
  58. self.assertEqual(
  59. prepare_headers(self.auth_only_params, {}),
  60. {'Authorization': self.norealm_authorization_header})
  61. self.assertEqual(
  62. prepare_headers(self.auth_only_params, {}, realm=self.realm),
  63. {'Authorization': self.withrealm_authorization_header})
  64. def test_prepare_headers_ignore_data(self):
  65. self.assertEqual(
  66. prepare_headers(self.auth_and_data, {}),
  67. {'Authorization': self.norealm_authorization_header})
  68. self.assertEqual(
  69. prepare_headers(self.auth_and_data, {}, realm=self.realm),
  70. {'Authorization': self.withrealm_authorization_header})
  71. def test_prepare_form_encoded_body(self):
  72. existing_body = ''
  73. form_encoded_body = 'data_param_foo=foo&data_param_1=1&oauth_consumer_key=9djdj82h48djs9d2&oauth_token=kkk9d7dh3k39sjv7&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_nonce=7d8f3e4a&oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%3D'
  74. self.assertEqual(
  75. urlencode(prepare_form_encoded_body(self.auth_and_data, existing_body)),
  76. form_encoded_body)
  77. def test_prepare_request_uri_query(self):
  78. url = 'http://notarealdomain.com/foo/bar/baz?some=args&go=here'
  79. request_uri_query = 'http://notarealdomain.com/foo/bar/baz?some=args&go=here&data_param_foo=foo&data_param_1=1&oauth_consumer_key=9djdj82h48djs9d2&oauth_token=kkk9d7dh3k39sjv7&oauth_signature_method=HMAC-SHA1&oauth_timestamp=137131201&oauth_nonce=7d8f3e4a&oauth_signature=bYT5CMsGcbgUdFHObYMEfcx6bsw%3D'
  80. self.assertEqual(
  81. prepare_request_uri_query(self.auth_and_data, url),
  82. request_uri_query)