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.

113 lines
4.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2015 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.tests.common import TransactionCase
  22. class TestAuditlog(TransactionCase):
  23. def test_LogCreation(self):
  24. """First test, caching some data."""
  25. auditlog_log = self.env['auditlog.log']
  26. groups_model_id = self.env.ref('base.model_res_groups').id
  27. self.env['auditlog.rule'].create({
  28. 'name': 'testrule for groups',
  29. 'model_id': groups_model_id,
  30. 'log_read': True,
  31. 'log_create': True,
  32. 'log_write': True,
  33. 'log_unlink': True,
  34. 'state': 'subscribed',
  35. })
  36. group = self.env['res.groups'].create({
  37. 'name': 'testgroup1',
  38. })
  39. self.assertTrue(auditlog_log.search([
  40. ('model_id', '=', groups_model_id),
  41. ('method', '=', 'create'),
  42. ('res_id', '=', group.id),
  43. ]))
  44. group.write({'name': 'Testgroup1'})
  45. self.assertTrue(auditlog_log.search([
  46. ('model_id', '=', groups_model_id),
  47. ('method', '=', 'write'),
  48. ('res_id', '=', group.id),
  49. ]))
  50. group.unlink()
  51. self.assertTrue(auditlog_log.search([
  52. ('model_id', '=', groups_model_id),
  53. ('method', '=', 'unlink'),
  54. ('res_id', '=', group.id),
  55. ]))
  56. def test_LogCreation2(self):
  57. """Second test, using cached data of the first one."""
  58. self.env['res.groups'].create({
  59. 'name': 'testgroup2',
  60. })
  61. def test_LogCreation3(self):
  62. """Third test, two groups, the latter being the parent of the former.
  63. Then we remove it right after (with (2, X) tuple) to test the creation
  64. of a 'write' log with a deleted resource (so with no text
  65. representation).
  66. """
  67. testgroup3 = self.env['res.groups'].create({
  68. 'name': 'testgroup3',
  69. })
  70. testgroup4 = self.env['res.groups'].create({
  71. 'name': 'testgroup4',
  72. 'implied_ids': [(4, testgroup3.id)],
  73. })
  74. testgroup4.write({'implied_ids': [(2, testgroup3.id)]})
  75. def test_LogInheritedField(self):
  76. """Check the log works well when updating an inherited field
  77. (e.g. field 'lang' on 'res.users' inherited from 'res.partner').
  78. """
  79. auditlog_log = self.env['auditlog.log']
  80. users_model_id = self.env.ref('base.model_res_users').id
  81. self.env['auditlog.rule'].create({
  82. 'name': 'testrule for users',
  83. 'model_id': users_model_id,
  84. 'log_read': True,
  85. 'log_create': True,
  86. 'log_write': True,
  87. 'log_unlink': True,
  88. 'state': 'subscribed',
  89. })
  90. # Log 'create'
  91. user = self.env['res.users'].create({
  92. 'name': 'testuser_inheritedfield',
  93. 'login': 'testuser.inheritedfield@company.com',
  94. 'lang': 'en_US', # field inherited from 'res.partner'
  95. })
  96. self.assertTrue(auditlog_log.search([
  97. ('model_id', '=', users_model_id),
  98. ('method', '=', 'create'),
  99. ('res_id', '=', user.id),
  100. ]))
  101. # Log 'read'
  102. data = user.read()[0]
  103. self.assertIn('lang', data)
  104. self.assertTrue(auditlog_log.search([
  105. ('model_id', '=', users_model_id),
  106. ('method', '=', 'read'),
  107. ('res_id', '=', user.id),
  108. ]))