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.

111 lines
3.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 LasLabs Inc.
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import mock
  5. from contextlib import contextmanager
  6. from openerp.tests.common import TransactionCase
  7. from openerp.addons.mass_mailing_custom_unsubscribe.controllers.main import (
  8. CustomUnsubscribe
  9. )
  10. model = 'openerp.addons.mass_mailing_custom_unsubscribe.controllers.main'
  11. @contextmanager
  12. def mock_assets():
  13. """ Mock & yield controller assets """
  14. with mock.patch('%s.request' % model) as request:
  15. yield {
  16. 'request': request,
  17. }
  18. class EndTestException(Exception):
  19. pass
  20. class TestController(TransactionCase):
  21. def setUp(self):
  22. super(TestController, self).setUp()
  23. self.controller = CustomUnsubscribe()
  24. def _default_domain(self):
  25. return [
  26. ('opt_out', '=', False),
  27. ('list_id.not_cross_unsubscriptable', '=', False),
  28. ]
  29. def test_mailing_list_contacts_by_email_search(self):
  30. """ It should search for contacts """
  31. expect = 'email'
  32. with mock_assets() as mk:
  33. self.controller._mailing_list_contacts_by_email(expect)
  34. model_obj = mk['request'].env['mail.mass_mailing.contact'].sudo()
  35. model_obj.search.assert_called_once_with(
  36. [('email', '=', expect)] + self._default_domain()
  37. )
  38. def test_mailing_list_contacts_by_email_return(self):
  39. """ It should return result of search """
  40. expect = 'email'
  41. with mock_assets() as mk:
  42. res = self.controller._mailing_list_contacts_by_email(expect)
  43. model_obj = mk['request'].env['mail.mass_mailing.contact'].sudo()
  44. self.assertEqual(
  45. model_obj.search(), res,
  46. )
  47. def test_unsubscription_reason_gets_context(self):
  48. """ It should retrieve unsub qcontext """
  49. expect = 'mailing_id', 'email', 'res_id', 'token'
  50. with mock_assets():
  51. with mock.patch.object(
  52. self.controller, 'unsubscription_qcontext'
  53. ) as unsub:
  54. unsub.side_effect = EndTestException
  55. with self.assertRaises(EndTestException):
  56. self.controller.unsubscription_reason(*expect)
  57. unsub.assert_called_once_with(*expect)
  58. def test_unsubscription_updates_with_extra_context(self):
  59. """ It should update qcontext with provided vals """
  60. expect = 'mailing_id', 'email', 'res_id', 'token'
  61. qcontext = {'context': 'test'}
  62. with mock_assets():
  63. with mock.patch.object(
  64. self.controller, 'unsubscription_qcontext'
  65. ) as unsub:
  66. self.controller.unsubscription_reason(
  67. *expect, qcontext_extra=qcontext
  68. )
  69. unsub().update.assert_called_once_with(qcontext)
  70. def test_unsubscription_updates_rendered_correctly(self):
  71. """ It should correctly render website """
  72. expect = 'mailing_id', 'email', 'res_id', 'token'
  73. with mock_assets() as mk:
  74. with mock.patch.object(
  75. self.controller, 'unsubscription_qcontext'
  76. ) as unsub:
  77. self.controller.unsubscription_reason(*expect)
  78. mk['request'].website.render.assert_called_once_with(
  79. "mass_mailing_custom_unsubscribe.reason_form",
  80. unsub(),
  81. )
  82. def test_unsubscription_updates_returns_site(self):
  83. """ It should return website """
  84. expect = 'mailing_id', 'email', 'res_id', 'token'
  85. with mock_assets() as mk:
  86. with mock.patch.object(
  87. self.controller, 'unsubscription_qcontext'
  88. ):
  89. res = self.controller.unsubscription_reason(*expect)
  90. self.assertEqual(
  91. mk['request'].website.render(), res
  92. )