You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
5.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 SYLEAM
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import logging
  5. from openerp.tests.common import TransactionCase
  6. from ..oauth2.validator import OdooValidator
  7. _logger = logging.getLogger(__name__)
  8. try:
  9. from oauthlib import oauth2
  10. except ImportError:
  11. _logger.debug('Cannot `import oauthlib`.')
  12. class TestOAuthProviderClient(TransactionCase):
  13. def setUp(self):
  14. super(TestOAuthProviderClient, self).setUp()
  15. self.client_vals = {
  16. 'name': 'Client',
  17. 'identifier': 'client',
  18. }
  19. def new_client(self, vals=None):
  20. values = self.client_vals.copy()
  21. if vals is not None:
  22. values.update(vals)
  23. return self.env['oauth.provider.client'].create(values)
  24. def test_grant_response_type_default(self):
  25. """ Check the value of the grant_type and response_type fields """
  26. # Default : Web Application
  27. client = self.new_client({'identifier': 'default'})
  28. self.assertEqual(client.grant_type, 'authorization_code')
  29. self.assertEqual(client.response_type, 'code')
  30. def test_grant_response_type_web_application(self):
  31. """ Check the value of the grant_type and response_type fields """
  32. # Web Application
  33. client = self.new_client(vals={'application_type': 'web application'})
  34. self.assertEqual(client.grant_type, 'authorization_code')
  35. self.assertEqual(client.response_type, 'code')
  36. def test_grant_response_type_mobile_application(self):
  37. """ Check the value of the grant_type and response_type fields """
  38. # Mobile Application
  39. client = self.new_client(
  40. vals={'application_type': 'mobile application'})
  41. self.assertEqual(client.grant_type, 'implicit')
  42. self.assertEqual(client.response_type, 'token')
  43. def test_grant_response_type_legacy_application(self):
  44. """ Check the value of the grant_type and response_type fields """
  45. # Legacy Application
  46. client = self.new_client(
  47. vals={'application_type': 'legacy application'})
  48. self.assertEqual(client.grant_type, 'password')
  49. self.assertEqual(client.response_type, 'none')
  50. def test_grant_response_type_backend_application(self):
  51. """ Check the value of the grant_type and response_type fields """
  52. # Backend Application
  53. client = self.new_client(
  54. vals={'application_type': 'backend application'})
  55. self.assertEqual(client.grant_type, 'client_credentials')
  56. self.assertEqual(client.response_type, 'none')
  57. def test_get_oauth2_server_default(self):
  58. """ Check the returned server, depending on the application type """
  59. # Default : Web Application
  60. client = self.new_client({'identifier': 'default'})
  61. self.assertTrue(
  62. isinstance(client.get_oauth2_server(),
  63. oauth2.WebApplicationServer))
  64. def test_get_oauth2_server_web_application(self):
  65. """ Check the returned server, depending on the application type """
  66. # Web Application
  67. client = self.new_client(vals={'application_type': 'web application'})
  68. self.assertTrue(
  69. isinstance(client.get_oauth2_server(),
  70. oauth2.WebApplicationServer))
  71. def test_get_oauth2_server_mobile_application(self):
  72. """ Check the returned server, depending on the application type """
  73. # Mobile Application
  74. client = self.new_client(
  75. vals={'application_type': 'mobile application'})
  76. self.assertTrue(
  77. isinstance(client.get_oauth2_server(),
  78. oauth2.MobileApplicationServer))
  79. def test_get_oauth2_server_legacy_applicaton(self):
  80. """ Check the returned server, depending on the application type """
  81. # Legacy Application
  82. client = self.new_client(
  83. vals={'application_type': 'legacy application'})
  84. self.assertTrue(
  85. isinstance(client.get_oauth2_server(),
  86. oauth2.LegacyApplicationServer))
  87. def test_get_oauth2_server_backend_application(self):
  88. """ Check the returned server, depending on the application type """
  89. # Backend Application
  90. client = self.new_client(
  91. vals={'application_type': 'backend application'})
  92. self.assertTrue(
  93. isinstance(client.get_oauth2_server(),
  94. oauth2.BackendApplicationServer))
  95. def test_get_oauth2_server_validator(self):
  96. """ Check the validator of the returned server """
  97. client = self.new_client()
  98. # No defined validator: Check that an OdooValidator instance is created
  99. self.assertTrue(
  100. isinstance(client.get_oauth2_server().request_validator,
  101. OdooValidator))
  102. def test_get_oauth2_server_validator_custom(self):
  103. """ Check the validator of the returned server """
  104. client = self.new_client()
  105. # Passed validator : Check that the validator instance is used
  106. validator = OdooValidator()
  107. self.assertEqual(
  108. client.get_oauth2_server(validator).request_validator, validator)