test_request_token.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from unittest.mock import ANY, MagicMock
  2. from oauthlib.oauth1 import RequestValidator
  3. from oauthlib.oauth1.rfc5849 import Client
  4. from oauthlib.oauth1.rfc5849.endpoints import RequestTokenEndpoint
  5. from tests.unittest import TestCase
  6. class RequestTokenEndpointTest(TestCase):
  7. def setUp(self):
  8. self.validator = MagicMock(wraps=RequestValidator())
  9. self.validator.check_client_key.return_value = True
  10. self.validator.allowed_signature_methods = ['HMAC-SHA1']
  11. self.validator.get_client_secret.return_value = 'bar'
  12. self.validator.get_default_realms.return_value = ['foo']
  13. self.validator.timestamp_lifetime = 600
  14. self.validator.check_realms.return_value = True
  15. self.validator.validate_client_key.return_value = True
  16. self.validator.validate_requested_realms.return_value = True
  17. self.validator.validate_redirect_uri.return_value = True
  18. self.validator.validate_timestamp_and_nonce.return_value = True
  19. self.validator.dummy_client = 'dummy'
  20. self.validator.dummy_secret = 'dummy'
  21. self.validator.save_request_token = MagicMock()
  22. self.endpoint = RequestTokenEndpoint(self.validator)
  23. self.client = Client('foo', client_secret='bar', realm='foo',
  24. callback_uri='https://c.b/cb')
  25. self.uri, self.headers, self.body = self.client.sign(
  26. 'https://i.b/request_token')
  27. def test_check_redirect_uri(self):
  28. client = Client('foo')
  29. uri, headers, _ = client.sign(self.uri)
  30. h, b, s = self.endpoint.create_request_token_response(
  31. uri, headers=headers)
  32. self.assertEqual(s, 400)
  33. self.assertIn('invalid_request', b)
  34. def test_check_realms(self):
  35. self.validator.check_realms.return_value = False
  36. h, b, s = self.endpoint.create_request_token_response(
  37. self.uri, headers=self.headers)
  38. self.assertEqual(s, 400)
  39. self.assertIn('invalid_request', b)
  40. def test_validate_client_key(self):
  41. self.validator.validate_client_key.return_value = False
  42. h, b, s = self.endpoint.create_request_token_response(
  43. self.uri, headers=self.headers)
  44. self.assertEqual(s, 401)
  45. def test_validate_realms(self):
  46. self.validator.validate_requested_realms.return_value = False
  47. h, b, s = self.endpoint.create_request_token_response(
  48. self.uri, headers=self.headers)
  49. self.assertEqual(s, 401)
  50. def test_validate_redirect_uri(self):
  51. self.validator.validate_redirect_uri.return_value = False
  52. h, b, s = self.endpoint.create_request_token_response(
  53. self.uri, headers=self.headers)
  54. self.assertEqual(s, 401)
  55. def test_validate_signature(self):
  56. client = Client('foo', callback_uri='https://c.b/cb')
  57. _, headers, _ = client.sign(self.uri + '/extra')
  58. h, b, s = self.endpoint.create_request_token_response(
  59. self.uri, headers=headers)
  60. self.assertEqual(s, 401)
  61. def test_valid_request(self):
  62. h, b, s = self.endpoint.create_request_token_response(
  63. self.uri, headers=self.headers)
  64. self.assertEqual(s, 200)
  65. self.assertIn('oauth_token', b)
  66. self.validator.validate_timestamp_and_nonce.assert_called_once_with(
  67. self.client.client_key, ANY, ANY, ANY,
  68. request_token=self.client.resource_owner_key)
  69. def test_uri_provided_realm(self):
  70. client = Client('foo', callback_uri='https://c.b/cb',
  71. client_secret='bar')
  72. uri = self.uri + '?realm=foo'
  73. _, headers, _ = client.sign(uri)
  74. h, b, s = self.endpoint.create_request_token_response(
  75. uri, headers=headers)
  76. self.assertEqual(s, 200)
  77. self.assertIn('oauth_token', b)