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.

169 lines
6.5 KiB

  1. # © 2018 Akretion (Florian da Costa)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from datetime import date, timedelta
  4. from odoo import api, exceptions
  5. from odoo.tests import common
  6. from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
  7. import base64
  8. class TestVacuumRule(common.TransactionCase):
  9. def create_mail_message(self, message_type, subtype):
  10. vals = {
  11. 'message_type': message_type,
  12. 'subtype_id': subtype and subtype.id or False,
  13. 'date': self.before_400_days,
  14. 'model': 'res.partner',
  15. 'res_id': self.env.ref('base.partner_root').id,
  16. 'subject': 'Test',
  17. 'body': 'Body Test',
  18. }
  19. return self.message_obj.create(vals)
  20. def tearDown(self):
  21. self.registry.leave_test_mode()
  22. super(TestVacuumRule, self).tearDown()
  23. def setUp(self):
  24. super(TestVacuumRule, self).setUp()
  25. self.registry.enter_test_mode(self.env.cr)
  26. self.env = api.Environment(self.registry.test_cr, self.env.uid,
  27. self.env.context)
  28. self.subtype = self.env.ref('mail.mt_comment')
  29. self.message_obj = self.env['mail.message']
  30. self.attachment_obj = self.env['ir.attachment']
  31. self.partner_model = self.env.ref('base.model_res_partner')
  32. today = date.today()
  33. self.before_400_days = today - timedelta(days=400)
  34. def test_mail_vacuum_rules(self):
  35. rule_vals = {
  36. 'name': 'Subtype Model',
  37. 'ttype': 'message',
  38. 'retention_time': 399,
  39. 'message_type': 'email',
  40. 'model_ids': [(6, 0, [self.env.ref('base.model_res_partner').id])],
  41. 'message_subtype_ids': [(6, 0, [self.subtype.id])],
  42. }
  43. rule = self.env['vacuum.rule'].create(rule_vals)
  44. m1 = self.create_mail_message('notification', self.subtype)
  45. m2 = self.create_mail_message('email', self.env.ref('mail.mt_note'))
  46. m3 = self.create_mail_message('email', False)
  47. message_ids = [m1.id, m2.id, m3.id]
  48. self.message_obj.autovacuum(ttype='message')
  49. message = self.message_obj.search(
  50. [('id', 'in', message_ids)])
  51. # no message deleted because either message_type is wrong or subtype
  52. # is wrong or subtype is empty
  53. self.assertEqual(len(message),
  54. 3)
  55. rule.write({'message_type': 'notification', 'retention_time': 405})
  56. self.message_obj.autovacuum(ttype='message')
  57. message = self.message_obj.search(
  58. [('id', 'in', message_ids)])
  59. # no message deleted because of retention time
  60. self.assertEqual(len(message),
  61. 3)
  62. rule.write({'retention_time': 399})
  63. self.message_obj.autovacuum(ttype='message')
  64. message = self.message_obj.search(
  65. [('id', 'in', message_ids)])
  66. self.assertEqual(len(message),
  67. 2)
  68. rule.write({'message_type': 'email',
  69. 'message_subtype_ids': [(6, 0, [])],
  70. 'empty_subtype': True})
  71. self.message_obj.autovacuum(ttype='message')
  72. message = self.message_obj.search(
  73. [('id', 'in', message_ids)])
  74. self.assertEqual(len(message),
  75. 0)
  76. def create_attachment(self, name):
  77. vals = {
  78. 'name': name,
  79. 'datas': base64.b64encode(b'Content'),
  80. 'datas_fname': name,
  81. 'res_id': self.env.ref('base.partner_root').id,
  82. 'res_model': 'res.partner',
  83. }
  84. return self.env['ir.attachment'].create(vals)
  85. def test_attachment_vacuum_rule(self):
  86. rule_vals = {
  87. 'name': 'Partner Attachments',
  88. 'ttype': 'attachment',
  89. 'retention_time': 100,
  90. 'model_ids': [(6, 0, [self.partner_model.id])],
  91. 'filename_pattern': 'test',
  92. }
  93. self.env['vacuum.rule'].create(rule_vals)
  94. a1 = self.create_attachment('Test-dummy')
  95. a2 = self.create_attachment('test24')
  96. # Force create date to old date to test deletion with 100 days
  97. # retention time
  98. before_102_days = date.today() - timedelta(days=102)
  99. before_102_days_str = before_102_days.strftime(
  100. DEFAULT_SERVER_DATE_FORMAT)
  101. self.env.cr.execute("""
  102. UPDATE ir_attachment SET create_date = '%s'
  103. WHERE id = %s
  104. """ % (before_102_days_str, a2.id))
  105. a2.write({'create_date': date.today() - timedelta(days=102)})
  106. a3 = self.create_attachment('other')
  107. self.env.cr.execute("""
  108. UPDATE ir_attachment SET create_date = '%s'
  109. WHERE id = %s
  110. """ % (before_102_days_str, a3.id))
  111. attachment_ids = [a1.id, a2.id, a3.id]
  112. self.attachment_obj.autovacuum(ttype='attachment')
  113. attachments = self.attachment_obj.search(
  114. [('id', 'in', attachment_ids)])
  115. # Only one message deleted because other 2 are with bad name or to
  116. # recent.
  117. self.assertEqual(len(attachments),
  118. 2)
  119. def test_retention_time_constraint(self):
  120. rule_vals = {
  121. 'name': 'Subtype Model',
  122. 'ttype': 'message',
  123. 'retention_time': 0,
  124. 'message_type': 'email',
  125. }
  126. with self.assertRaises(exceptions.ValidationError):
  127. self.env['vacuum.rule'].create(rule_vals)
  128. def test_res_model_domain(self):
  129. partner = self.env['res.partner'].create({'name': 'Test Partner'})
  130. # automatic creation message
  131. self.assertEqual(len(partner.message_ids), 1)
  132. # change date message to simulate it is an old one
  133. partner.message_ids.write({'date': '2017-01-01'})
  134. partner_model = self.env.ref('base.model_res_partner')
  135. rule_vals = {
  136. 'name': 'Partners',
  137. 'ttype': 'message',
  138. 'retention_time': 399,
  139. 'message_type': 'all',
  140. 'model_ids': [(6, 0, [partner_model.id])],
  141. 'model_filter_domain': "[['name', '=', 'Dummy']]",
  142. 'empty_subtype': True,
  143. }
  144. rule = self.env['vacuum.rule'].create(rule_vals)
  145. self.message_obj.autovacuum(ttype='message')
  146. # no message deleted as the filter does not match
  147. self.assertEqual(len(partner.message_ids), 1)
  148. rule.write({
  149. 'model_filter_domain': "[['name', '=', 'Test Partner']]"
  150. })
  151. self.message_obj.autovacuum(ttype='message')
  152. self.assertEqual(len(partner.message_ids), 0)