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.

103 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Camptocamp
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
  4. from odoo.tests.common import SavepointCase
  5. from odoo import exceptions
  6. import json
  7. class TestMultiToken(SavepointCase):
  8. post_install = True
  9. at_install = False
  10. @classmethod
  11. def setUpClass(cls):
  12. super(TestMultiToken, cls).setUpClass()
  13. cls.token_model = cls.env['auth.oauth.multi.token']
  14. cls.provider_google = cls.env.ref('auth_oauth.provider_google')
  15. cls.user_model = cls.env['res.users'].with_context({
  16. 'tracking_disable': True,
  17. 'no_reset_password': True,
  18. })
  19. cls.user = cls.user_model.create({
  20. 'name': 'John Doe',
  21. 'login': 'johndoe',
  22. 'oauth_uid': 'oauth_uid_johndoe',
  23. 'oauth_provider_id': cls.provider_google.id,
  24. })
  25. def _fake_params(self, **kw):
  26. params = {
  27. 'state': json.dumps({'t': 'FAKE_TOKEN'}),
  28. 'access_token': 'FAKE_ACCESS_TOKEN',
  29. }
  30. params.update(kw)
  31. return params
  32. def test_no_provider_no_access(self):
  33. validation = {
  34. 'user_id': 'oauth_uid_no_one',
  35. }
  36. params = self._fake_params()
  37. with self.assertRaises(exceptions.AccessDenied):
  38. self.user_model._auth_oauth_signin(
  39. self.provider_google.id, validation, params
  40. )
  41. def _test_one_token(self):
  42. validation = {
  43. 'user_id': 'oauth_uid_johndoe',
  44. }
  45. params = self._fake_params()
  46. login = self.user_model._auth_oauth_signin(
  47. self.provider_google.id, validation, params
  48. )
  49. self.assertEqual(login, 'johndoe')
  50. def test_access_one_token(self):
  51. # no token yet
  52. self.assertFalse(self.user.oauth_access_token_ids)
  53. self._test_one_token()
  54. token_count = 1
  55. self.assertEqual(
  56. len(self.user.oauth_access_token_ids),
  57. token_count)
  58. self.assertEqual(
  59. len(self.token_model._oauth_user_tokens(self.user.id)),
  60. token_count)
  61. def test_access_multi_token(self):
  62. # no token yet
  63. self.assertFalse(self.user.oauth_access_token_ids)
  64. # use as many token as max allowed
  65. for token_count in range(1, self.user.oauth_access_max_token + 1):
  66. self._test_one_token()
  67. self.assertEqual(
  68. len(self.user.oauth_access_token_ids),
  69. token_count)
  70. self.assertEqual(
  71. len(self.token_model._oauth_user_tokens(self.user.id)),
  72. token_count)
  73. # exceed the number
  74. self._test_one_token()
  75. # token count match max number + 1
  76. self.assertEqual(
  77. len(self.user.oauth_access_token_ids),
  78. self.user.oauth_access_max_token + 1)
  79. # but active tokens don't
  80. self.assertEqual(
  81. len(self.token_model._oauth_user_tokens(self.user.id)),
  82. self.user.oauth_access_max_token)
  83. def test_remove_oauth_access_token(self):
  84. res = self.user._get_session_token_fields()
  85. self.assertFalse('oauth_access_token' in res)
  86. self.assertTrue('oauth_master_uuid' in res)
  87. def test_action_oauth_clear_token(self):
  88. self.user.action_oauth_clear_token()
  89. active_token = self.user.oauth_access_token_ids.filtered(
  90. lambda x: x.active_token)
  91. self.assertEqual(len(active_token), 0)