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.

221 lines
7.7 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. def test_LogCreation5(self):
  96. """Fifth test, create a record and check that the same number of logs
  97. has been generated. And then delete it, check that it has created log
  98. with 0 fields updated.
  99. """
  100. self.groups_rule.subscribe()
  101. auditlog_log = self.env['auditlog.log']
  102. testgroup5 = self.env['res.groups'].create({
  103. 'name': 'testgroup5',
  104. })
  105. self.assertTrue(auditlog_log.search([
  106. ('model_id', '=', self.groups_model_id),
  107. ('method', '=', 'create'),
  108. ('res_id', '=', testgroup5.id),
  109. ]).ensure_one())
  110. testgroup5.unlink()
  111. log_record = auditlog_log.search([
  112. ('model_id', '=', self.groups_model_id),
  113. ('method', '=', 'unlink'),
  114. ('res_id', '=', testgroup5.id),
  115. ]).ensure_one()
  116. self.assertTrue(log_record)
  117. if not self.groups_rule.capture_record:
  118. self.assertEqual(len(log_record.line_ids), 0)
  119. def test_LogCreation6(self):
  120. """Six test, create a record and check that the same number of logs
  121. has been generated. And then delete it, check that it has created log
  122. with x fields updated as per rule
  123. """
  124. self.groups_rule.subscribe()
  125. auditlog_log = self.env['auditlog.log']
  126. testgroup6 = self.env['res.groups'].create({
  127. 'name': 'testgroup6',
  128. })
  129. self.assertTrue(auditlog_log.search([
  130. ('model_id', '=', self.groups_model_id),
  131. ('method', '=', 'create'),
  132. ('res_id', '=', testgroup6.id),
  133. ]).ensure_one())
  134. testgroup6.unlink()
  135. log_record = auditlog_log.search([
  136. ('model_id', '=', self.groups_model_id),
  137. ('method', '=', 'unlink'),
  138. ('res_id', '=', testgroup6.id),
  139. ]).ensure_one()
  140. self.assertTrue(log_record)
  141. if self.groups_rule.capture_record:
  142. self.assertTrue(len(log_record.line_ids) > 0)
  143. class TestAuditlogFull(TransactionCase, AuditlogCommon):
  144. def setUp(self):
  145. super(TestAuditlogFull, self).setUp()
  146. self.groups_model_id = self.env.ref('base.model_res_groups').id
  147. self.groups_rule = self.env['auditlog.rule'].create({
  148. 'name': 'testrule for groups',
  149. 'model_id': self.groups_model_id,
  150. 'log_read': True,
  151. 'log_create': True,
  152. 'log_write': True,
  153. 'log_unlink': True,
  154. 'log_type': 'full',
  155. })
  156. def tearDown(self):
  157. self.groups_rule.unlink()
  158. super(TestAuditlogFull, self).tearDown()
  159. class TestAuditlogFast(TransactionCase, AuditlogCommon):
  160. def setUp(self):
  161. super(TestAuditlogFast, self).setUp()
  162. self.groups_model_id = self.env.ref('base.model_res_groups').id
  163. self.groups_rule = self.env['auditlog.rule'].create({
  164. 'name': 'testrule for groups',
  165. 'model_id': self.groups_model_id,
  166. 'log_read': True,
  167. 'log_create': True,
  168. 'log_write': True,
  169. 'log_unlink': True,
  170. 'log_type': 'fast',
  171. })
  172. def tearDown(self):
  173. self.groups_rule.unlink()
  174. super(TestAuditlogFast, self).tearDown()
  175. class TestAuditlogFullCaptureRecord(TransactionCase, AuditlogCommon):
  176. def setUp(self):
  177. super(TestAuditlogFullCaptureRecord, self).setUp()
  178. self.groups_model_id = self.env.ref('base.model_res_groups').id
  179. self.groups_rule = self.env['auditlog.rule'].create({
  180. 'name': 'testrule for groups with capture unlink record',
  181. 'model_id': self.groups_model_id,
  182. 'log_read': True,
  183. 'log_create': True,
  184. 'log_write': True,
  185. 'log_unlink': True,
  186. 'log_type': 'full',
  187. 'capture_record': True,
  188. })
  189. def tearDown(self):
  190. self.groups_rule.unlink()
  191. super(TestAuditlogFullCaptureRecord, self).tearDown()