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.

54 lines
2.6 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. class TestBaseSuspendSecurity(TransactionCase):
  24. def test_base_suspend_security(self):
  25. # tests are called before register_hook
  26. self.env['ir.rule']._register_hook()
  27. user_id = self.env.ref('base.user_demo').id
  28. other_company = self.env['res.company'].create({
  29. 'name': 'other company',
  30. # without this, a partner is created and mail's constraint on
  31. # notify_email kicks in
  32. 'partner_id': self.env.ref('base.partner_demo').id,
  33. })
  34. # be sure what we try is forbidden
  35. with self.assertRaises(exceptions.AccessError):
  36. with mute_logger('openerp.addons.base.ir.ir_model'):
  37. self.env.ref('base.user_root').sudo(user_id).name = 'test'
  38. with self.assertRaises(exceptions.AccessError):
  39. with mute_logger('openerp.addons.base.ir.ir_model'):
  40. other_company.sudo(user_id).name = 'test'
  41. # this tests ir.model.access
  42. self.env.ref('base.user_root').sudo(user_id).suspend_security().write({
  43. 'name': 'test'})
  44. self.assertEqual(self.env.ref('base.user_root').name, 'test')
  45. self.assertEqual(self.env.ref('base.user_root').write_uid.id, user_id)
  46. # this tests ir.rule
  47. other_company.sudo(user_id).suspend_security().write({'name': 'test'})
  48. self.assertEqual(other_company.name, 'test')
  49. self.assertEqual(other_company.write_uid.id, user_id)
  50. # this tests if _normalize_args conversion works
  51. self.env['res.users'].browse(
  52. self.env['res.users'].suspend_security().env.uid)