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.

95 lines
3.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2018 Akretion (Florian da Costa)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from datetime import date, timedelta
  5. from openerp import api, exceptions
  6. from openerp.tests import common
  7. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
  8. class TestMessageVacuumRule(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': 'mail.channel',
  15. 'res_id': self.env.ref('mail.channel_all_employees').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(TestMessageVacuumRule, self).tearDown()
  23. def setUp(self):
  24. super(TestMessageVacuumRule, self).setUp()
  25. self.registry.enter_test_mode()
  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.channel_model = self.env.ref('mail.model_mail_channel')
  31. today = date.today()
  32. before_400_days = today - timedelta(days=400)
  33. self.before_400_days = before_400_days.strftime(
  34. DEFAULT_SERVER_DATE_FORMAT)
  35. def test_mail_vacuum_rules(self):
  36. rule_vals = {
  37. 'name': 'Subtype Model',
  38. 'retention_time': 399,
  39. 'message_type': 'email',
  40. 'model_ids': [(6, 0, [self.channel_model.id])],
  41. 'message_subtype_ids': [(6, 0, [self.subtype.id])],
  42. }
  43. rule = self.env['message.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_mail_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 wront 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_mail_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_mail_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_mail_message()
  72. message = self.message_obj.search(
  73. [('id', 'in', message_ids)])
  74. self.assertEqual(len(message),
  75. 0)
  76. def test_retention_time_constraint(self):
  77. rule_vals = {
  78. 'name': 'Subtype Model',
  79. 'retention_time': 0,
  80. 'message_type': 'email',
  81. }
  82. with self.assertRaises(exceptions.ValidationError):
  83. self.env['message.vacuum.rule'].create(rule_vals)