test_resource.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 ResourceEndpoint
  5. from tests.unittest import TestCase
  6. class ResourceEndpointTest(TestCase):
  7. def setUp(self):
  8. self.validator = MagicMock(wraps=RequestValidator())
  9. self.validator.check_client_key.return_value = True
  10. self.validator.check_access_token.return_value = True
  11. self.validator.allowed_signature_methods = ['HMAC-SHA1']
  12. self.validator.get_client_secret.return_value = 'bar'
  13. self.validator.get_access_token_secret.return_value = 'secret'
  14. self.validator.timestamp_lifetime = 600
  15. self.validator.validate_client_key.return_value = True
  16. self.validator.validate_access_token.return_value = True
  17. self.validator.validate_timestamp_and_nonce.return_value = True
  18. self.validator.validate_realms.return_value = True
  19. self.validator.dummy_client = 'dummy'
  20. self.validator.dummy_secret = 'dummy'
  21. self.validator.dummy_access_token = 'dummy'
  22. self.endpoint = ResourceEndpoint(self.validator)
  23. self.client = Client('foo',
  24. client_secret='bar',
  25. resource_owner_key='token',
  26. resource_owner_secret='secret')
  27. self.uri, self.headers, self.body = self.client.sign(
  28. 'https://i.b/protected_resource')
  29. def test_missing_parameters(self):
  30. self.validator.check_access_token.return_value = False
  31. v, r = self.endpoint.validate_protected_resource_request(
  32. self.uri)
  33. self.assertFalse(v)
  34. def test_check_access_token(self):
  35. self.validator.check_access_token.return_value = False
  36. v, r = self.endpoint.validate_protected_resource_request(
  37. self.uri, headers=self.headers)
  38. self.assertFalse(v)
  39. def test_validate_client_key(self):
  40. self.validator.validate_client_key.return_value = False
  41. v, r = self.endpoint.validate_protected_resource_request(
  42. self.uri, headers=self.headers)
  43. self.assertFalse(v)
  44. # the validator log should have `False` values
  45. self.assertFalse(r.validator_log['client'])
  46. self.assertTrue(r.validator_log['realm'])
  47. self.assertTrue(r.validator_log['resource_owner'])
  48. self.assertTrue(r.validator_log['signature'])
  49. def test_validate_access_token(self):
  50. self.validator.validate_access_token.return_value = False
  51. v, r = self.endpoint.validate_protected_resource_request(
  52. self.uri, headers=self.headers)
  53. self.assertFalse(v)
  54. # the validator log should have `False` values
  55. self.assertTrue(r.validator_log['client'])
  56. self.assertTrue(r.validator_log['realm'])
  57. self.assertFalse(r.validator_log['resource_owner'])
  58. self.assertTrue(r.validator_log['signature'])
  59. def test_validate_realms(self):
  60. self.validator.validate_realms.return_value = False
  61. v, r = self.endpoint.validate_protected_resource_request(
  62. self.uri, headers=self.headers)
  63. self.assertFalse(v)
  64. # the validator log should have `False` values
  65. self.assertTrue(r.validator_log['client'])
  66. self.assertFalse(r.validator_log['realm'])
  67. self.assertTrue(r.validator_log['resource_owner'])
  68. self.assertTrue(r.validator_log['signature'])
  69. def test_validate_signature(self):
  70. client = Client('foo',
  71. resource_owner_key='token',
  72. resource_owner_secret='secret')
  73. _, headers, _ = client.sign(self.uri + '/extra')
  74. v, r = self.endpoint.validate_protected_resource_request(
  75. self.uri, headers=headers)
  76. self.assertFalse(v)
  77. # the validator log should have `False` values
  78. self.assertTrue(r.validator_log['client'])
  79. self.assertTrue(r.validator_log['realm'])
  80. self.assertTrue(r.validator_log['resource_owner'])
  81. self.assertFalse(r.validator_log['signature'])
  82. def test_valid_request(self):
  83. v, r = self.endpoint.validate_protected_resource_request(
  84. self.uri, headers=self.headers)
  85. self.assertTrue(v)
  86. self.validator.validate_timestamp_and_nonce.assert_called_once_with(
  87. self.client.client_key, ANY, ANY, ANY,
  88. access_token=self.client.resource_owner_key)
  89. # everything in the validator_log should be `True`
  90. self.assertTrue(all(r.validator_log.items()))