test_signature_only.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 SignatureOnlyEndpoint
  5. from tests.unittest import TestCase
  6. class SignatureOnlyEndpointTest(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.timestamp_lifetime = 600
  13. self.validator.validate_client_key.return_value = True
  14. self.validator.validate_timestamp_and_nonce.return_value = True
  15. self.validator.dummy_client = 'dummy'
  16. self.validator.dummy_secret = 'dummy'
  17. self.endpoint = SignatureOnlyEndpoint(self.validator)
  18. self.client = Client('foo', client_secret='bar')
  19. self.uri, self.headers, self.body = self.client.sign(
  20. 'https://i.b/protected_resource')
  21. def test_missing_parameters(self):
  22. v, r = self.endpoint.validate_request(
  23. self.uri)
  24. self.assertFalse(v)
  25. def test_validate_client_key(self):
  26. self.validator.validate_client_key.return_value = False
  27. v, r = self.endpoint.validate_request(
  28. self.uri, headers=self.headers)
  29. self.assertFalse(v)
  30. def test_validate_signature(self):
  31. client = Client('foo')
  32. _, headers, _ = client.sign(self.uri + '/extra')
  33. v, r = self.endpoint.validate_request(
  34. self.uri, headers=headers)
  35. self.assertFalse(v)
  36. def test_valid_request(self):
  37. v, r = self.endpoint.validate_request(
  38. self.uri, headers=self.headers)
  39. self.assertTrue(v)
  40. self.validator.validate_timestamp_and_nonce.assert_called_once_with(
  41. self.client.client_key, ANY, ANY, ANY)