Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

test_emails.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from django.contrib.sites.models import Site
  2. from django.core import mail
  3. from django.test import TestCase
  4. from oscar.core.loading import get_class
  5. from oscar.test.factories import (
  6. ProductAlertFactory, UserFactory, create_product)
  7. from oscar.test.utils import EmailsMixin
  8. CustomerDispatcher = get_class('customer.utils', 'CustomerDispatcher')
  9. AlertsDispatcher = get_class('customer.alerts.utils', 'AlertsDispatcher')
  10. class TestCustomerConcreteEmailsSending(EmailsMixin, TestCase):
  11. def setUp(self):
  12. super().setUp()
  13. self.dispatcher = CustomerDispatcher()
  14. def test_send_registration_email_for_user(self):
  15. extra_context = {'user': self.user}
  16. self.dispatcher.send_registration_email_for_user(self.user, extra_context)
  17. self._test_common_part()
  18. self.assertEqual('Thank you for registering.', mail.outbox[0].subject)
  19. self.assertIn('Thank you for registering.', mail.outbox[0].body)
  20. def test_send_password_reset_email_for_user(self):
  21. extra_context = {
  22. 'user': self.user,
  23. 'reset_url': '/django-oscar/django-oscar',
  24. }
  25. self.dispatcher.send_password_reset_email_for_user(self.user, extra_context)
  26. self._test_common_part()
  27. expected_subject = 'Resetting your password at {}.'.format(Site.objects.get_current())
  28. self.assertEqual(expected_subject, mail.outbox[0].subject)
  29. self.assertIn('Please go to the following page and choose a new password:', mail.outbox[0].body)
  30. self.assertIn('http://example.com/django-oscar/django-oscar', mail.outbox[0].body)
  31. def test_send_password_changed_email_for_user(self):
  32. extra_context = {
  33. 'user': self.user,
  34. 'reset_url': '/django-oscar/django-oscar',
  35. }
  36. self.dispatcher.send_password_changed_email_for_user(self.user, extra_context)
  37. self._test_common_part()
  38. expected_subject = 'Your password changed at {}.'.format(Site.objects.get_current())
  39. self.assertEqual(expected_subject, mail.outbox[0].subject)
  40. self.assertIn('your password has been changed', mail.outbox[0].body)
  41. self.assertIn('http://example.com/django-oscar/django-oscar', mail.outbox[0].body)
  42. def test_send_email_changed_email_for_user(self):
  43. extra_context = {
  44. 'user': self.user,
  45. 'reset_url': '/django-oscar/django-oscar',
  46. 'new_email': 'some_new@mail.com',
  47. }
  48. self.dispatcher.send_email_changed_email_for_user(self.user, extra_context)
  49. self._test_common_part()
  50. expected_subject = 'Your email address has changed at {}.'.format(Site.objects.get_current())
  51. self.assertEqual(expected_subject, mail.outbox[0].subject)
  52. self.assertIn('your email address has been changed', mail.outbox[0].body)
  53. self.assertIn('http://example.com/django-oscar/django-oscar', mail.outbox[0].body)
  54. self.assertIn('some_new@mail.com', mail.outbox[0].body)
  55. class TestAlertsConcreteEmailsSending(EmailsMixin, TestCase):
  56. def setUp(self):
  57. super().setUp()
  58. self.dispatcher = AlertsDispatcher()
  59. def test_send_product_alert_email_for_user(self):
  60. product = create_product(num_in_stock=5)
  61. ProductAlertFactory(product=product, user=self.user)
  62. self.dispatcher.send_product_alert_email_for_user(product)
  63. self._test_common_part()
  64. expected_subject = '{} is back in stock'.format(product.title)
  65. self.assertEqual(expected_subject, mail.outbox[0].subject)
  66. self.assertIn('We are happy to inform you that our product', mail.outbox[0].body)
  67. # No `hurry_mode`
  68. self.assertNotIn('Beware that the amount of items in stock is limited.', mail.outbox[0].body)
  69. def test_send_product_alert_email_for_user_with_hurry_mode(self):
  70. another_user = UserFactory(email='another_user@mail.com')
  71. product = create_product(num_in_stock=1)
  72. ProductAlertFactory(product=product, user=self.user, email=self.user.email)
  73. ProductAlertFactory(product=product, user=another_user, email=another_user.email)
  74. self.dispatcher.send_product_alert_email_for_user(product)
  75. self.assertEqual(len(mail.outbox), 2) # Separate email for each user
  76. expected_subject = '{} is back in stock'.format(product.title)
  77. self.assertEqual(expected_subject, mail.outbox[0].subject)
  78. for outboxed_email in mail.outbox:
  79. self.assertEqual(expected_subject, outboxed_email.subject)
  80. self.assertIn('We are happy to inform you that our product', outboxed_email.body)
  81. self.assertIn('Beware that the amount of items in stock is limited.', outboxed_email.body)
  82. def test_send_product_alert_confirmation_email_for_user(self):
  83. product = create_product(num_in_stock=5)
  84. alert = ProductAlertFactory(product=product, user=self.user, email=self.user.email, key='key00042')
  85. self.dispatcher.send_product_alert_confirmation_email_for_user(alert)
  86. self._test_common_part()
  87. self.assertEqual('Confirmation required for stock alert', mail.outbox[0].subject)
  88. self.assertIn('Somebody (hopefully you) has requested an email alert', mail.outbox[0].body)
  89. self.assertIn(alert.get_confirm_url(), mail.outbox[0].body)
  90. self.assertIn(alert.get_cancel_url(), mail.outbox[0].body)