test_auditlog.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. # Get partner to test
  111. self.partner = self.env['res.partner'].search([], limit=1)
  112. self.partner_model = self.env['ir.model'].search([
  113. ('model', '=', 'res.partner')])
  114. # Setup auditlog rules
  115. self.auditlog_rule = self.env['auditlog.rule'].create({
  116. 'name': 'res.partner',
  117. 'model_id': self.partner_model.id,
  118. 'log_type': 'fast',
  119. 'log_read': False,
  120. 'log_create': False,
  121. 'log_write': False,
  122. 'log_unlink': False,
  123. 'log_custom_method': True,
  124. 'custom_method_ids': [(0, 0, {
  125. 'name': 'onchange_type',
  126. 'message': 'onchange_type',
  127. })]
  128. })
  129. self.auditlog_rule.subscribe()
  130. def tearDown(self):
  131. self.auditlog_rule.unsubscribe()
  132. super(TestMethods, self).tearDown()
  133. def test_01_subscribe_unsubscribe(self):
  134. """The test is subscribed by default, so let's try both"""
  135. self.auditlog_rule.unsubscribe()
  136. self.auditlog_rule.subscribe()
  137. def test_02_copy_res_partner_logging(self):
  138. """ Copy partner and see if the action gets logged """
  139. self.partner.onchange_type(False)
  140. logs = self.env['auditlog.log'].search([
  141. ('res_id', '=', self.partner.id),
  142. ('model_id', '=', self.partner_model.id),
  143. ('method', '=', 'onchange_type')
  144. ])
  145. self.assertEqual(len(logs), 1)
  146. def test_03_copy_res_partner_logging_old_api(self):
  147. """ Perform the same test as 02 but with the old API """
  148. self.registry('res.partner').onchange_type(
  149. self.cr, self.uid, self.partner.id, False)
  150. logs = self.env['auditlog.log'].search([
  151. ('res_id', '=', self.partner.id),
  152. ('model_id', '=', self.partner_model.id),
  153. ('method', '=', 'onchange_type')
  154. ])
  155. self.assertEqual(len(logs), 1)