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.

272 lines
9.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 SYLEAM
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp.tests.common import TransactionCase
  5. class TestOAuthProviderScope(TransactionCase):
  6. def setUp(self):
  7. super(TestOAuthProviderScope, self).setUp()
  8. self.filter = self.env['ir.filters'].create({
  9. 'name': 'Current user',
  10. 'model_id': 'res.users',
  11. 'domain': "[('id', '=', uid)]",
  12. })
  13. self.scope_vals = {
  14. 'name': 'Scope',
  15. 'code': 'scope',
  16. 'description': 'Description of the scope',
  17. 'model_id': self.env.ref('base.model_res_users').id,
  18. 'filter_id': self.filter.id,
  19. 'field_ids': [
  20. (6, 0, [self.env.ref('base.field_res_users_email').id]),
  21. ],
  22. }
  23. def new_scope(self, vals=None):
  24. values = self.scope_vals
  25. if vals is not None:
  26. values.update(vals)
  27. return self.env['oauth.provider.scope'].create(values)
  28. def test_get_data_from_model_without_filter(self):
  29. """ Check the values returned by the get_data_for_model method when no
  30. filter is defined
  31. """
  32. scope = self.new_scope({'filter_id': False})
  33. # Check a simple call with the right model
  34. data = scope.get_data_for_model('res.users')
  35. # Check that we have multiple users (otherwise this test is useless)
  36. self.assertTrue(len(self.env['res.users'].search([]).ids) > 1)
  37. self.assertEqual(
  38. set(data.keys()), set(self.env['res.users'].search([]).ids))
  39. def test_get_data_from_model_without_filter_wrong_model(self):
  40. """ Check the values returned by the get_data_for_model method when no
  41. filter is defined
  42. """
  43. scope = self.new_scope({'filter_id': False})
  44. # Check a simple call with a wrong model
  45. data = scope.get_data_for_model('res.partner')
  46. self.assertEqual(data, {})
  47. def test_get_data_from_model_with_filter(self):
  48. """ Check the values returned by the get_data_for_model method when no
  49. res_id is supplied
  50. """
  51. scope = self.new_scope()
  52. # Check a simple call with the right model
  53. data = scope.get_data_for_model('res.users')
  54. self.assertEqual(data, {
  55. self.env.user.id: {
  56. 'id': self.env.user.id,
  57. 'email': self.env.user.email,
  58. },
  59. })
  60. def test_get_data_from_model_with_filter_wrong_model(self):
  61. """ Check the values returned by the get_data_for_model method when no
  62. res_id is supplied
  63. """
  64. scope = self.new_scope()
  65. # Check a simple call with a wrong model
  66. data = scope.get_data_for_model('res.partner')
  67. self.assertEqual(data, {})
  68. def test_get_data_from_model_with_res_id_and_no_filter(self):
  69. """ Check the values returned by the get_data_for_model method when a
  70. res_id is supplied
  71. """
  72. scope = self.new_scope({'filter_id': False})
  73. # Check a simple call with the right model
  74. data = scope.get_data_for_model('res.users', res_id=self.env.user.id)
  75. self.assertEqual(data, {
  76. 'id': self.env.user.id,
  77. 'email': self.env.user.email,
  78. })
  79. def test_get_data_from_model_with_res_id_and_no_filter_wrong_model(self):
  80. """ Check the values returned by the get_data_for_model method when a
  81. res_id is supplied
  82. """
  83. scope = self.new_scope({'filter_id': False})
  84. # Check a simple call with a wrong model
  85. data = scope.get_data_for_model(
  86. 'res.partner', res_id=self.env.user.id + 1)
  87. self.assertEqual(data, {})
  88. def test_get_data_from_model_with_res_id(self):
  89. """ Check the values returned by the get_data_for_model method when a
  90. res_id is supplied
  91. """
  92. scope = self.new_scope()
  93. # Check a simple call with the right model
  94. data = scope.get_data_for_model('res.users', res_id=self.env.user.id)
  95. self.assertEqual(data, {
  96. 'id': self.env.user.id,
  97. 'email': self.env.user.email,
  98. })
  99. def test_get_data_from_model_with_res_id_wrong_model(self):
  100. """ Check the values returned by the get_data_for_model method when a
  101. res_id is supplied
  102. """
  103. scope = self.new_scope()
  104. # Check a simple call with a wrong model
  105. data = scope.get_data_for_model(
  106. 'res.partner', res_id=self.env.user.id + 1)
  107. self.assertEqual(data, {})
  108. def _generate_multiple_scopes(self):
  109. scopes = self.new_scope()
  110. scopes += self.new_scope({
  111. 'code': 'Profile',
  112. 'field_ids': [(6, 0, [
  113. self.env.ref('base.field_res_users_name').id,
  114. self.env.ref('base.field_res_users_city').id,
  115. self.env.ref('base.field_res_users_country_id').id,
  116. ])],
  117. })
  118. scopes += self.new_scope({
  119. 'model_id': self.env.ref('base.model_res_groups').id,
  120. 'code': 'All groups',
  121. 'filter_id': False,
  122. 'field_ids': [
  123. (6, 0, [self.env.ref('base.field_res_groups_name').id]),
  124. ],
  125. })
  126. return scopes
  127. def test_get_data_from_model_with_multiple_scopes_empty_fields(self):
  128. """ Check the values returned by the get_data_for_model method when
  129. calling on multiple scopes
  130. """
  131. scopes = self._generate_multiple_scopes()
  132. # Check a simple call with the right model with empty fields
  133. self.env.user.city = False
  134. self.env.user.country_id = False
  135. data = scopes.get_data_for_model('res.users')
  136. self.assertEqual(data, {self.env.user.id: {
  137. 'id': 1,
  138. 'email': self.env.user.email,
  139. 'name': self.env.user.name,
  140. 'city': False,
  141. 'country_id': False,
  142. }})
  143. def test_get_data_from_model_with_multiple_scopesfirst_model(self):
  144. """ Check the values returned by the get_data_for_model method when
  145. calling on multiple scopes
  146. """
  147. scopes = self._generate_multiple_scopes()
  148. # Check a simple call with the right model without empty fields
  149. country = self.env.ref('base.fr')
  150. self.env.user.city = 'Paris'
  151. self.env.user.country_id = country
  152. data = scopes.get_data_for_model('res.users')
  153. self.assertEqual(data, {self.env.user.id: {
  154. 'id': 1,
  155. 'email': self.env.user.email,
  156. 'name': self.env.user.name,
  157. 'city': self.env.user.city,
  158. 'country_id': country.name,
  159. }})
  160. def test_get_data_from_model_with_multiple_scopes_second_model(self):
  161. """ Check the values returned by the get_data_for_model method when
  162. calling on multiple scopes
  163. """
  164. scopes = self._generate_multiple_scopes()
  165. # Check a simple call with another right model
  166. data = scopes.get_data_for_model('res.groups')
  167. self.assertEqual(
  168. set(data.keys()), set(self.env['res.groups'].search([]).ids))
  169. def test_get_data_from_model_with_multiple_scopes_wrong_model(self):
  170. """ Check the values returned by the get_data_for_model method when
  171. calling on multiple scopes
  172. """
  173. scopes = self._generate_multiple_scopes()
  174. # Check a simple call with a wrong model
  175. data = scopes.get_data_for_model('res.partner')
  176. self.assertEqual(data, {})
  177. def _generate_multiple_scopes_match(self):
  178. scopes = self.new_scope()
  179. scopes += self.new_scope({
  180. 'code': 'All users',
  181. 'filter_id': False,
  182. })
  183. scopes += self.new_scope({
  184. 'model_id': self.env.ref('base.model_res_groups').id,
  185. 'code': 'All groups',
  186. 'filter_id': False,
  187. 'field_ids': [
  188. (6, 0, [self.env.ref('base.field_res_groups_name').id]),
  189. ],
  190. })
  191. return scopes
  192. def test_get_data_from_model_with_all_scopes_match(self):
  193. """ Check the values returned by the get_data_for_model method when all
  194. scopes are required to match
  195. """
  196. scopes = self._generate_multiple_scopes_match()
  197. # Check a simple call with the right model with any scope match
  198. # returned records
  199. data = scopes.get_data_for_model('res.users')
  200. self.assertEqual(
  201. set(data.keys()), set(self.env['res.users'].search([]).ids))
  202. def test_get_data_from_model_with_all_scopes_match_first_model(self):
  203. """ Check the values returned by the get_data_for_model method when all
  204. scopes are required to match
  205. """
  206. scopes = self._generate_multiple_scopes_match()
  207. # Check a simple call with the right model with all scopes required to
  208. # match returned records
  209. data = scopes.get_data_for_model('res.users', all_scopes_match=True)
  210. self.assertEqual(data, {self.env.user.id: {
  211. 'id': 1,
  212. 'email': self.env.user.email,
  213. }})
  214. def test_get_data_from_model_with_all_scopes_match_second_model(self):
  215. """ Check the values returned by the get_data_for_model method when all
  216. scopes are required to match
  217. """
  218. scopes = self._generate_multiple_scopes_match()
  219. # Check a simple call with another right model
  220. data = scopes.get_data_for_model('res.groups')
  221. self.assertEqual(
  222. set(data.keys()), set(self.env['res.groups'].search([]).ids))
  223. def test_get_data_from_model_with_all_scopes_match_wrong_model(self):
  224. """ Check the values returned by the get_data_for_model method when all
  225. scopes are required to match
  226. """
  227. scopes = self._generate_multiple_scopes_match()
  228. # Check a simple call with a wrong model
  229. data = scopes.get_data_for_model('res.partner')
  230. self.assertEqual(data, {})