test_access_token.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 AccessTokenEndpoint
  5. from tests.unittest import TestCase
  6. class AccessTokenEndpointTest(TestCase):
  7. def setUp(self):
  8. self.validator = MagicMock(wraps=RequestValidator())
  9. self.validator.check_client_key.return_value = True
  10. self.validator.check_request_token.return_value = True
  11. self.validator.check_verifier.return_value = True
  12. self.validator.allowed_signature_methods = ['HMAC-SHA1']
  13. self.validator.get_client_secret.return_value = 'bar'
  14. self.validator.get_request_token_secret.return_value = 'secret'
  15. self.validator.get_realms.return_value = ['foo']
  16. self.validator.timestamp_lifetime = 600
  17. self.validator.validate_client_key.return_value = True
  18. self.validator.validate_request_token.return_value = True
  19. self.validator.validate_verifier.return_value = True
  20. self.validator.validate_timestamp_and_nonce.return_value = True
  21. self.validator.invalidate_request_token.return_value = True
  22. self.validator.dummy_client = 'dummy'
  23. self.validator.dummy_secret = 'dummy'
  24. self.validator.dummy_request_token = 'dummy'
  25. self.validator.save_access_token = MagicMock()
  26. self.endpoint = AccessTokenEndpoint(self.validator)
  27. self.client = Client('foo',
  28. client_secret='bar',
  29. resource_owner_key='token',
  30. resource_owner_secret='secret',
  31. verifier='verfier')
  32. self.uri, self.headers, self.body = self.client.sign(
  33. 'https://i.b/access_token')
  34. def test_check_request_token(self):
  35. self.validator.check_request_token.return_value = False
  36. h, b, s = self.endpoint.create_access_token_response(
  37. self.uri, headers=self.headers)
  38. self.assertEqual(s, 400)
  39. self.assertIn('invalid_request', b)
  40. def test_check_verifier(self):
  41. self.validator.check_verifier.return_value = False
  42. h, b, s = self.endpoint.create_access_token_response(
  43. self.uri, headers=self.headers)
  44. self.assertEqual(s, 400)
  45. self.assertIn('invalid_request', b)
  46. def test_validate_client_key(self):
  47. self.validator.validate_client_key.return_value = False
  48. h, b, s = self.endpoint.create_access_token_response(
  49. self.uri, headers=self.headers)
  50. self.assertEqual(s, 401)
  51. def test_validate_request_token(self):
  52. self.validator.validate_request_token.return_value = False
  53. h, b, s = self.endpoint.create_access_token_response(
  54. self.uri, headers=self.headers)
  55. self.assertEqual(s, 401)
  56. def test_validate_verifier(self):
  57. self.validator.validate_verifier.return_value = False
  58. h, b, s = self.endpoint.create_access_token_response(
  59. self.uri, headers=self.headers)
  60. self.assertEqual(s, 401)
  61. def test_validate_signature(self):
  62. client = Client('foo',
  63. resource_owner_key='token',
  64. resource_owner_secret='secret',
  65. verifier='verfier')
  66. _, headers, _ = client.sign(self.uri + '/extra')
  67. h, b, s = self.endpoint.create_access_token_response(
  68. self.uri, headers=headers)
  69. self.assertEqual(s, 401)
  70. def test_valid_request(self):
  71. h, b, s = self.endpoint.create_access_token_response(
  72. self.uri, headers=self.headers)
  73. self.assertEqual(s, 200)
  74. self.assertIn('oauth_token', b)
  75. self.validator.validate_timestamp_and_nonce.assert_called_once_with(
  76. self.client.client_key, ANY, ANY, ANY,
  77. request_token=self.client.resource_owner_key)
  78. self.validator.invalidate_request_token.assert_called_once_with(
  79. self.client.client_key, self.client.resource_owner_key, ANY)