test_extra_credentials.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """Ensure extra credentials can be supplied for inclusion in tokens.
  2. """
  3. from unittest import mock
  4. from oauthlib.oauth2 import (
  5. BackendApplicationServer, LegacyApplicationServer, MobileApplicationServer,
  6. RequestValidator, WebApplicationServer,
  7. )
  8. from tests.unittest import TestCase
  9. class ExtraCredentialsTest(TestCase):
  10. def set_client(self, request):
  11. request.client = mock.MagicMock()
  12. request.client.client_id = 'mocked'
  13. return True
  14. def setUp(self):
  15. self.validator = mock.MagicMock(spec=RequestValidator)
  16. self.validator.get_default_redirect_uri.return_value = 'https://i.b/cb'
  17. self.web = WebApplicationServer(self.validator)
  18. self.mobile = MobileApplicationServer(self.validator)
  19. self.legacy = LegacyApplicationServer(self.validator)
  20. self.backend = BackendApplicationServer(self.validator)
  21. def test_post_authorization_request(self):
  22. def save_code(client_id, token, request):
  23. self.assertEqual('creds', request.extra)
  24. def save_token(token, request):
  25. self.assertEqual('creds', request.extra)
  26. # Authorization code grant
  27. self.validator.save_authorization_code.side_effect = save_code
  28. self.web.create_authorization_response(
  29. 'https://i.b/auth?client_id=foo&response_type=code',
  30. scopes=['foo'],
  31. credentials={'extra': 'creds'})
  32. # Implicit grant
  33. self.validator.save_bearer_token.side_effect = save_token
  34. self.mobile.create_authorization_response(
  35. 'https://i.b/auth?client_id=foo&response_type=token',
  36. scopes=['foo'],
  37. credentials={'extra': 'creds'})
  38. def test_token_request(self):
  39. def save_token(token, request):
  40. self.assertIn('extra', token)
  41. self.validator.save_bearer_token.side_effect = save_token
  42. self.validator.authenticate_client.side_effect = self.set_client
  43. # Authorization code grant
  44. self.web.create_token_response('https://i.b/token',
  45. body='grant_type=authorization_code&code=foo',
  46. credentials={'extra': 'creds'})
  47. # Password credentials grant
  48. self.legacy.create_token_response('https://i.b/token',
  49. body='grant_type=password&username=foo&password=bar',
  50. credentials={'extra': 'creds'})
  51. # Client credentials grant
  52. self.backend.create_token_response('https://i.b/token',
  53. body='grant_type=client_credentials',
  54. credentials={'extra': 'creds'})