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.

97 lines
3.5 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 openerp.tests.common import TransactionCase
  6. model = 'openerp.addons.mass_mailing_custom_unsubscribe.models.mail_mail'
  7. class EndTestException(Exception):
  8. pass
  9. class TestMailMail(TransactionCase):
  10. def setUp(self):
  11. super(TestMailMail, self).setUp()
  12. self.Model = self.env['mail.mail']
  13. param_obj = self.env['ir.config_parameter']
  14. self.base_url = param_obj.get_param('web.base.url')
  15. self.config_msg = param_obj.get_param(
  16. 'mass_mailing.unsubscribe.label'
  17. )
  18. @mock.patch('%s.urlparse' % model)
  19. @mock.patch('%s.urllib' % model)
  20. def test_get_unsubscribe_url_proper_url(self, urllib, urlparse):
  21. """ It should join the URL w/ proper args """
  22. urlparse.urljoin.side_effect = EndTestException
  23. expect = mock.MagicMock(), 'email', 'msg'
  24. with self.assertRaises(EndTestException):
  25. self.Model._get_unsubscribe_url(*expect)
  26. urlparse.urljoin.assert_called_once_with(
  27. self.base_url,
  28. 'mail/mailing/%(mailing_id)s/unsubscribe?%(params)s' % {
  29. 'mailing_id': expect[0].mailing_id.id,
  30. 'params': urllib.urlencode(),
  31. }
  32. )
  33. @mock.patch('%s.urlparse' % model)
  34. @mock.patch('%s.urllib' % model)
  35. def test_get_unsubscribe_url_correct_params(self, urllib, urlparse):
  36. """ It should create URL params w/ proper data """
  37. urlparse.urljoin.side_effect = EndTestException
  38. expect = mock.MagicMock(), 'email', 'msg'
  39. with self.assertRaises(EndTestException):
  40. self.Model._get_unsubscribe_url(*expect)
  41. urllib.urlencode.assert_called_once_with(dict(
  42. db=self.env.cr.dbname,
  43. res_id=expect[0].res_id,
  44. email=expect[1],
  45. token=self.env['mail.mass_mailing'].hash_create(
  46. expect[0].mailing_id.id,
  47. expect[0].res_id,
  48. expect[1],
  49. )
  50. ))
  51. @mock.patch('%s.urlparse' % model)
  52. @mock.patch('%s.urllib' % model)
  53. def test_get_unsubscribe_url_false_config_msg(self, urllib, urlparse):
  54. """ It should return default config msg when none supplied """
  55. expects = ['uri', False]
  56. urlparse.urljoin.return_value = expects[0]
  57. with mock.patch.object(self.Model, 'env') as env:
  58. env['ir.config_paramater'].get_param.side_effect = expects
  59. res = self.Model._get_unsubscribe_url(
  60. mock.MagicMock(), 'email', 'msg'
  61. )
  62. self.assertIn(
  63. expects[0], res,
  64. 'Did not include URI in default message'
  65. )
  66. self.assertIn(
  67. 'msg', res,
  68. 'Did not include input msg in default message'
  69. )
  70. @mock.patch('%s.urlparse' % model)
  71. @mock.patch('%s.urllib' % model)
  72. def test_get_unsubscribe_url_with_config_msg(self, urllib, urlparse):
  73. """ It should return config message w/ URL formatted """
  74. expects = ['uri', 'test %(url)s']
  75. urlparse.urljoin.return_value = expects[0]
  76. with mock.patch.object(self.Model, 'env') as env:
  77. env['ir.config_paramater'].get_param.side_effect = expects
  78. res = self.Model._get_unsubscribe_url(
  79. mock.MagicMock(), 'email', 'msg'
  80. )
  81. self.assertEqual(
  82. expects[1] % {'url': expects[0]}, res,
  83. 'Did not return proper config message'
  84. )