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.

172 lines
5.7 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
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Therp BV <http://therp.nl>
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from openerp.tests.common import TransactionCase
  5. class TestAuditlog(object):
  6. def test_LogCreation(self):
  7. """First test, caching some data."""
  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. auditlog_log = self.env['auditlog.log']
  32. testgroup2 = self.env['res.groups'].create({
  33. 'name': 'testgroup2',
  34. })
  35. self.assertTrue(auditlog_log.search([
  36. ('model_id', '=', self.groups_model_id),
  37. ('method', '=', 'create'),
  38. ('res_id', '=', testgroup2.id),
  39. ]).ensure_one())
  40. def test_LogCreation3(self):
  41. """Third test, two groups, the latter being the parent of the former.
  42. Then we remove it right after (with (2, X) tuple) to test the creation
  43. of a 'write' log with a deleted resource (so with no text
  44. representation).
  45. """
  46. auditlog_log = self.env['auditlog.log']
  47. testgroup3 = testgroup3 = self.env['res.groups'].create({
  48. 'name': 'testgroup3',
  49. })
  50. testgroup4 = self.env['res.groups'].create({
  51. 'name': 'testgroup4',
  52. 'implied_ids': [(4, testgroup3.id)],
  53. })
  54. testgroup4.write({'implied_ids': [(2, testgroup3.id)]})
  55. self.assertTrue(auditlog_log.search([
  56. ('model_id', '=', self.groups_model_id),
  57. ('method', '=', 'create'),
  58. ('res_id', '=', testgroup3.id),
  59. ]).ensure_one())
  60. self.assertTrue(auditlog_log.search([
  61. ('model_id', '=', self.groups_model_id),
  62. ('method', '=', 'create'),
  63. ('res_id', '=', testgroup4.id),
  64. ]).ensure_one())
  65. self.assertTrue(auditlog_log.search([
  66. ('model_id', '=', self.groups_model_id),
  67. ('method', '=', 'write'),
  68. ('res_id', '=', testgroup4.id),
  69. ]).ensure_one())
  70. class TestAuditlogFull(TransactionCase, TestAuditlog):
  71. def setUp(self):
  72. super(TestAuditlogFull, self).setUp()
  73. self.groups_model_id = self.env.ref('base.model_res_groups').id
  74. self.groups_rule = self.env['auditlog.rule'].create({
  75. 'name': 'testrule for groups',
  76. 'model_id': self.groups_model_id,
  77. 'log_read': True,
  78. 'log_create': True,
  79. 'log_write': True,
  80. 'log_unlink': True,
  81. 'state': 'subscribed',
  82. 'log_type': 'full',
  83. })
  84. def tearDown(self):
  85. self.groups_rule.unlink()
  86. super(TestAuditlogFull, self).tearDown()
  87. class TestAuditlogFast(TransactionCase, TestAuditlog):
  88. def setUp(self):
  89. super(TestAuditlogFast, self).setUp()
  90. self.groups_model_id = self.env.ref('base.model_res_groups').id
  91. self.groups_rule = self.env['auditlog.rule'].create({
  92. 'name': 'testrule for groups',
  93. 'model_id': self.groups_model_id,
  94. 'log_read': True,
  95. 'log_create': True,
  96. 'log_write': True,
  97. 'log_unlink': True,
  98. 'state': 'subscribed',
  99. 'log_type': 'fast',
  100. })
  101. def tearDown(self):
  102. self.groups_rule.unlink()
  103. super(TestAuditlogFast, self).tearDown()
  104. class TestMethods(TransactionCase):
  105. def setUp(self):
  106. super(TestMethods, self).setUp()
  107. # Clear all existing logging lines
  108. existing_audit_logs = self.env['auditlog.log'].search([])
  109. existing_audit_logs.unlink()
  110. # Create account period to test
  111. self.partner = self.env['res.partner'].create({
  112. 'name': 'Test User'
  113. })
  114. self.partner_model = self.env['ir.model'].search([
  115. ('model', '=', 'res.partner')])
  116. # Setup rule for model "account.period.close" and method "data_save"
  117. self.auditlog_rule = self.env['auditlog.rule'].create({
  118. 'name': 'res.partner',
  119. 'model_id': self.partner_model.id,
  120. 'log_type': 'fast',
  121. 'log_read': False,
  122. 'log_create': False,
  123. 'log_write': False,
  124. 'log_unlink': False,
  125. 'log_custom': True,
  126. 'custom_method_ids': [(0, 0, {
  127. 'name': 'copy',
  128. 'message': 'execute_copy',
  129. })]
  130. })
  131. self.auditlog_rule.subscribe()
  132. def tearDown(self):
  133. self.auditlog_rule.unsubscribe()
  134. super(TestMethods, self).tearDown()
  135. def test_01_subscribe_unsubscribe(self):
  136. """The test is subscribed by default, so let's try both"""
  137. self.auditlog_rule.unsubscribe()
  138. self.auditlog_rule.subscribe()
  139. def test_02_copy_res_partner_logging(self):
  140. self.partner.copy()
  141. logs = self.env['auditlog.log'].search([
  142. ('res_id', '=', self.partner.id),
  143. ('model_id', '=', self.partner_model.id),
  144. ('method', '=', 'execute_copy')
  145. ])
  146. self.assertEqual(len(logs), 1)