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.

148 lines
5.0 KiB

7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # Copyright 2015 Therp BV <https://therp.nl>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo.tests.common import TransactionCase
  4. class AuditlogCommon(object):
  5. def test_LogCreation(self):
  6. """First test, caching some data."""
  7. self.groups_rule.subscribe()
  8. auditlog_log = self.env['auditlog.log']
  9. group = self.env['res.groups'].create({
  10. 'name': 'testgroup1',
  11. })
  12. self.assertTrue(auditlog_log.search([
  13. ('model_id', '=', self.groups_model_id),
  14. ('method', '=', 'create'),
  15. ('res_id', '=', group.id),
  16. ]).ensure_one())
  17. group.write({'name': 'Testgroup1'})
  18. self.assertTrue(auditlog_log.search([
  19. ('model_id', '=', self.groups_model_id),
  20. ('method', '=', 'write'),
  21. ('res_id', '=', group.id),
  22. ]).ensure_one())
  23. group.unlink()
  24. self.assertTrue(auditlog_log.search([
  25. ('model_id', '=', self.groups_model_id),
  26. ('method', '=', 'unlink'),
  27. ('res_id', '=', group.id),
  28. ]).ensure_one())
  29. def test_LogCreation2(self):
  30. """Second test, using cached data of the first one."""
  31. self.groups_rule.subscribe()
  32. auditlog_log = self.env['auditlog.log']
  33. testgroup2 = self.env['res.groups'].create({
  34. 'name': 'testgroup2',
  35. })
  36. self.assertTrue(auditlog_log.search([
  37. ('model_id', '=', self.groups_model_id),
  38. ('method', '=', 'create'),
  39. ('res_id', '=', testgroup2.id),
  40. ]).ensure_one())
  41. def test_LogCreation3(self):
  42. """Third test, two groups, the latter being the parent of the former.
  43. Then we remove it right after (with (2, X) tuple) to test the creation
  44. of a 'write' log with a deleted resource (so with no text
  45. representation).
  46. """
  47. self.groups_rule.subscribe()
  48. auditlog_log = self.env['auditlog.log']
  49. testgroup3 = testgroup3 = self.env['res.groups'].create({
  50. 'name': 'testgroup3',
  51. })
  52. testgroup4 = self.env['res.groups'].create({
  53. 'name': 'testgroup4',
  54. 'implied_ids': [(4, testgroup3.id)],
  55. })
  56. testgroup4.write({'implied_ids': [(2, testgroup3.id)]})
  57. self.assertTrue(auditlog_log.search([
  58. ('model_id', '=', self.groups_model_id),
  59. ('method', '=', 'create'),
  60. ('res_id', '=', testgroup3.id),
  61. ]).ensure_one())
  62. self.assertTrue(auditlog_log.search([
  63. ('model_id', '=', self.groups_model_id),
  64. ('method', '=', 'create'),
  65. ('res_id', '=', testgroup4.id),
  66. ]).ensure_one())
  67. self.assertTrue(auditlog_log.search([
  68. ('model_id', '=', self.groups_model_id),
  69. ('method', '=', 'write'),
  70. ('res_id', '=', testgroup4.id),
  71. ]).ensure_one())
  72. def test_LogCreation4(self):
  73. """Fourth test, create several records at once (with create multi
  74. feature starting from Odoo 12) and check that the same number of logs
  75. has been generated.
  76. """
  77. self.groups_rule.subscribe()
  78. auditlog_log = self.env['auditlog.log']
  79. groups_vals = [
  80. {'name': 'testgroup1'},
  81. {'name': 'testgroup3'},
  82. {'name': 'testgroup2'},
  83. ]
  84. groups = self.env['res.groups'].create(groups_vals)
  85. # Ensure that the recordset returns is in the same order
  86. # than list of vals
  87. expected_names = ['testgroup1', 'testgroup3', 'testgroup2']
  88. self.assertEqual(groups.mapped('name'), expected_names)
  89. logs = auditlog_log.search([
  90. ('model_id', '=', self.groups_model_id),
  91. ('method', '=', 'create'),
  92. ('res_id', 'in', groups.ids),
  93. ])
  94. self.assertEqual(len(logs), len(groups))
  95. class TestAuditlogFull(TransactionCase, AuditlogCommon):
  96. def setUp(self):
  97. super(TestAuditlogFull, self).setUp()
  98. self.groups_model_id = self.env.ref('base.model_res_groups').id
  99. self.groups_rule = self.env['auditlog.rule'].create({
  100. 'name': 'testrule for groups',
  101. 'model_id': self.groups_model_id,
  102. 'log_read': True,
  103. 'log_create': True,
  104. 'log_write': True,
  105. 'log_unlink': True,
  106. 'log_type': 'full',
  107. })
  108. def tearDown(self):
  109. self.groups_rule.unlink()
  110. super(TestAuditlogFull, self).tearDown()
  111. class TestAuditlogFast(TransactionCase, AuditlogCommon):
  112. def setUp(self):
  113. super(TestAuditlogFast, self).setUp()
  114. self.groups_model_id = self.env.ref('base.model_res_groups').id
  115. self.groups_rule = self.env['auditlog.rule'].create({
  116. 'name': 'testrule for groups',
  117. 'model_id': self.groups_model_id,
  118. 'log_read': True,
  119. 'log_create': True,
  120. 'log_write': True,
  121. 'log_unlink': True,
  122. 'log_type': 'fast',
  123. })
  124. def tearDown(self):
  125. self.groups_rule.unlink()
  126. super(TestAuditlogFast, self).tearDown()