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.

test_emails.py 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. from django.contrib.sites.models import Site
  2. from django.core import mail
  3. from django.core.exceptions import ImproperlyConfigured
  4. from django.test import TestCase, override_settings
  5. from oscar.core.loading import get_class
  6. from oscar.test.factories import (
  7. ProductAlertFactory, UserFactory, create_product)
  8. from oscar.test.utils import EmailsMixin
  9. CustomerDispatcher = get_class('customer.utils', 'CustomerDispatcher')
  10. AlertsDispatcher = get_class('customer.alerts.utils', 'AlertsDispatcher')
  11. class TestCustomerConcreteEmailsSending(EmailsMixin, TestCase):
  12. def setUp(self):
  13. super().setUp()
  14. self.dispatcher = CustomerDispatcher()
  15. def test_send_registration_email_for_user(self, additional_context=None):
  16. extra_context = {'user': self.user}
  17. if additional_context:
  18. extra_context.update(additional_context)
  19. self.dispatcher.send_registration_email_for_user(self.user, extra_context)
  20. self._test_common_part()
  21. self.assertEqual('Thank you for registering.', mail.outbox[0].subject)
  22. self.assertIn('Thank you for registering.', mail.outbox[0].body)
  23. @override_settings(SITE_ID=None, ALLOWED_HOSTS=["example.com"])
  24. def test_send_registration_email_for_user_multisite(self):
  25. with self.assertRaises(ImproperlyConfigured, msg=self.DJANGO_IMPROPERLY_CONFIGURED_MSG):
  26. self.test_send_registration_email_for_user()
  27. additional_context = {"request": self.request}
  28. self.test_send_registration_email_for_user(additional_context=additional_context)
  29. def test_send_password_reset_email_for_user(self, additional_context=None):
  30. extra_context = {
  31. 'user': self.user,
  32. 'reset_url': '/django-oscar/django-oscar',
  33. }
  34. request = None
  35. if additional_context:
  36. request = additional_context.get("request")
  37. extra_context.update(additional_context)
  38. self.dispatcher.send_password_reset_email_for_user(self.user, extra_context)
  39. self._test_common_part()
  40. expected_subject = 'Resetting your password at {}.'.format(Site.objects.get_current(request))
  41. self.assertEqual(expected_subject, mail.outbox[0].subject)
  42. self.assertIn('Please go to the following page and choose a new password:', mail.outbox[0].body)
  43. self.assertIn('http://example.com/django-oscar/django-oscar', mail.outbox[0].body)
  44. @override_settings(SITE_ID=None, ALLOWED_HOSTS=["example.com"])
  45. def test_send_password_reset_email_for_user_multisite(self):
  46. with self.assertRaises(ImproperlyConfigured, msg=self.DJANGO_IMPROPERLY_CONFIGURED_MSG):
  47. self.test_send_password_reset_email_for_user()
  48. additional_context = {"request": self.request}
  49. self.test_send_password_reset_email_for_user(additional_context=additional_context)
  50. def test_send_password_changed_email_for_user(self, additional_context=None):
  51. extra_context = {
  52. 'user': self.user,
  53. 'reset_url': '/django-oscar/django-oscar',
  54. }
  55. request = None
  56. if additional_context:
  57. request = additional_context.get("request")
  58. extra_context.update(additional_context)
  59. self.dispatcher.send_password_changed_email_for_user(self.user, extra_context)
  60. self._test_common_part()
  61. expected_subject = 'Your password changed at {}.'.format(Site.objects.get_current(request))
  62. self.assertEqual(expected_subject, mail.outbox[0].subject)
  63. self.assertIn('your password has been changed', mail.outbox[0].body)
  64. self.assertIn('http://example.com/django-oscar/django-oscar', mail.outbox[0].body)
  65. @override_settings(SITE_ID=None, ALLOWED_HOSTS=["example.com"])
  66. def test_send_password_changed_email_for_user_multisite(self):
  67. with self.assertRaises(ImproperlyConfigured, msg=self.DJANGO_IMPROPERLY_CONFIGURED_MSG):
  68. self.test_send_password_changed_email_for_user()
  69. additional_context = {"request": self.request}
  70. self.test_send_password_changed_email_for_user(additional_context=additional_context)
  71. def test_send_email_changed_email_for_user(self, additional_context=None):
  72. extra_context = {
  73. 'user': self.user,
  74. 'reset_url': '/django-oscar/django-oscar',
  75. 'new_email': 'some_new@mail.com',
  76. }
  77. request = None
  78. if additional_context:
  79. request = additional_context.get("request")
  80. extra_context.update(additional_context)
  81. self.dispatcher.send_email_changed_email_for_user(self.user, extra_context)
  82. self._test_common_part()
  83. expected_subject = 'Your email address has changed at {}.'.format(Site.objects.get_current(request))
  84. self.assertEqual(expected_subject, mail.outbox[0].subject)
  85. self.assertIn('your email address has been changed', mail.outbox[0].body)
  86. self.assertIn('http://example.com/django-oscar/django-oscar', mail.outbox[0].body)
  87. self.assertIn('some_new@mail.com', mail.outbox[0].body)
  88. @override_settings(SITE_ID=None, ALLOWED_HOSTS=["example.com"])
  89. def test_send_email_changed_email_for_user_multisite(self):
  90. with self.assertRaises(ImproperlyConfigured, msg=self.DJANGO_IMPROPERLY_CONFIGURED_MSG):
  91. self.test_send_email_changed_email_for_user()
  92. additional_context = {"request": self.request}
  93. self.test_send_email_changed_email_for_user(additional_context=additional_context)
  94. class TestAlertsConcreteEmailsSending(EmailsMixin, TestCase):
  95. def setUp(self):
  96. super().setUp()
  97. self.dispatcher = AlertsDispatcher()
  98. def test_send_product_alert_email_for_user(self):
  99. product = create_product(num_in_stock=5)
  100. ProductAlertFactory(product=product, user=self.user)
  101. self.dispatcher.send_product_alert_email_for_user(product)
  102. self._test_common_part()
  103. expected_subject = '{} is back in stock'.format(product.title)
  104. self.assertEqual(expected_subject, mail.outbox[0].subject)
  105. self.assertIn('We are happy to inform you that our product', mail.outbox[0].body)
  106. # No `hurry_mode`
  107. self.assertNotIn('Beware that the amount of items in stock is limited.', mail.outbox[0].body)
  108. def test_send_product_alert_email_for_user_with_hurry_mode(self):
  109. another_user = UserFactory(email='another_user@mail.com')
  110. product = create_product(num_in_stock=1)
  111. ProductAlertFactory(product=product, user=self.user, email=self.user.email)
  112. ProductAlertFactory(product=product, user=another_user, email=another_user.email)
  113. self.dispatcher.send_product_alert_email_for_user(product)
  114. self.assertEqual(len(mail.outbox), 2) # Separate email for each user
  115. expected_subject = '{} is back in stock'.format(product.title)
  116. self.assertEqual(expected_subject, mail.outbox[0].subject)
  117. for outboxed_email in mail.outbox:
  118. self.assertEqual(expected_subject, outboxed_email.subject)
  119. self.assertIn('We are happy to inform you that our product', outboxed_email.body)
  120. self.assertIn('Beware that the amount of items in stock is limited.', outboxed_email.body)
  121. def test_send_product_alert_confirmation_email_for_user(self):
  122. product = create_product(num_in_stock=5)
  123. alert = ProductAlertFactory(product=product, user=self.user, email=self.user.email, key='key00042')
  124. self.dispatcher.send_product_alert_confirmation_email_for_user(alert)
  125. self._test_common_part()
  126. self.assertEqual('Confirmation required for stock alert', mail.outbox[0].subject)
  127. self.assertIn('Somebody (hopefully you) has requested an email alert', mail.outbox[0].body)
  128. self.assertIn(alert.get_confirm_url(), mail.outbox[0].body)
  129. self.assertIn(alert.get_cancel_url(), mail.outbox[0].body)