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.

63 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # This module copyright (C) 2015 Therp BV (<http://therp.nl>).
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. from openerp import exceptions
  21. from openerp.tools import mute_logger
  22. from openerp.tests.common import TransactionCase
  23. from ..base_suspend_security import BaseSuspendSecurityUid
  24. class TestBaseSuspendSecurity(TransactionCase):
  25. def test_base_suspend_security(self):
  26. # tests are called before register_hook
  27. self.env['ir.rule']._register_hook()
  28. user_id = self.env.ref('base.user_demo').id
  29. other_company = self.env['res.company'].create({
  30. 'name': 'other company',
  31. # without this, a partner is created and mail's constraint on
  32. # notify_email kicks in
  33. 'partner_id': self.env.ref('base.partner_demo').id,
  34. })
  35. # be sure what we try is forbidden
  36. with self.assertRaises(exceptions.AccessError):
  37. with mute_logger('openerp.addons.base.ir.ir_model'):
  38. self.env.ref('base.user_root').sudo(user_id).name = 'test'
  39. with self.assertRaises(exceptions.AccessError):
  40. with mute_logger('openerp.addons.base.ir.ir_model'):
  41. other_company.sudo(user_id).name = 'test'
  42. # this tests ir.model.access
  43. self.env.ref('base.user_root').sudo(user_id).suspend_security().write({
  44. 'name': 'test'})
  45. self.assertEqual(self.env.ref('base.user_root').name, 'test')
  46. self.assertEqual(self.env.ref('base.user_root').write_uid.id, user_id)
  47. # this tests ir.rule
  48. other_company.sudo(user_id).suspend_security().write({'name': 'test'})
  49. self.assertEqual(other_company.name, 'test')
  50. self.assertEqual(other_company.write_uid.id, user_id)
  51. # this tests if _normalize_args conversion works
  52. self.env['res.users'].browse(
  53. self.env['res.users'].suspend_security().env.uid)
  54. # check equality, that's relevant for picking the right environment
  55. self.assertNotEqual(BaseSuspendSecurityUid(42), 42)
  56. self.assertNotEqual(
  57. BaseSuspendSecurityUid(42), BaseSuspendSecurityUid(43),
  58. )
  59. self.assertEqual(
  60. BaseSuspendSecurityUid(42), BaseSuspendSecurityUid(42),
  61. )