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_dispatcher.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from django.core import mail
  2. from django.test import TestCase
  3. from oscar.apps.customer.models import CommunicationEventType, Email
  4. from oscar.apps.customer.utils import Dispatcher, get_password_reset_url
  5. from oscar.apps.order.models import CommunicationEvent
  6. from oscar.core.compat import get_user_model
  7. from oscar.test.factories import SiteFactory, create_order
  8. User = get_user_model()
  9. class TestDispatcher(TestCase):
  10. def _dispatch_order_messages(self, order_number, order, email=None):
  11. et = CommunicationEventType.objects.create(code="ORDER_PLACED",
  12. name="Order Placed",
  13. category=CommunicationEventType.ORDER_RELATED)
  14. messages = et.get_messages({
  15. 'order': order,
  16. 'lines': order.lines.all()
  17. })
  18. self.assertIn(order_number, messages['body'])
  19. self.assertIn(order_number, messages['html'])
  20. dispatcher = Dispatcher()
  21. dispatcher.dispatch_order_messages(order, messages, et)
  22. self.assertEqual(CommunicationEvent.objects.filter(order=order, event_type=et).count(), 1)
  23. self.assertEqual(len(mail.outbox), 1)
  24. message = mail.outbox[0]
  25. self.assertIn(order_number, message.body)
  26. # test sending messages to emails without account and text body
  27. messages['body'] = ''
  28. dispatcher.dispatch_direct_messages(email, messages)
  29. self.assertEqual(len(mail.outbox), 2)
  30. def _dispatch_user_messages(self, user, event_code, ctx, subject):
  31. msgs = CommunicationEventType.objects.get_and_render(
  32. code=event_code, context=ctx)
  33. Dispatcher().dispatch_user_messages(user, msgs)
  34. self.assertEqual(len(mail.outbox), 1)
  35. self.assertEqual(mail.outbox[0].subject, subject)
  36. self.assertEqual(Email.objects.count(), 1)
  37. email = Email.objects.last()
  38. self.assertEqual(email.user.id, user.id)
  39. self.assertEqual(email.email, 'testuser@example.com')
  40. def test_dispatch_order_messages(self):
  41. email = 'testuser@example.com'
  42. user = User.objects.create_user('testuser', email,
  43. 'somesimplepassword')
  44. order = create_order(number='12345', user=user)
  45. self.assertFalse(order.is_anonymous)
  46. self._dispatch_order_messages(order_number='12345', order=order, email=email)
  47. def test_dispatch_anonymous_order_messages(self):
  48. order = create_order(number='12346', guest_email='testguest@example.com')
  49. self.assertTrue(order.is_anonymous)
  50. self._dispatch_order_messages(order_number='12346', order=order, email='testguest@example.com', )
  51. def test_dispatch_email_changed_user_message(self):
  52. user = User.objects.create_user('testuser', 'testuser@example.com', 'somesimplepassword')
  53. CommunicationEventType.objects.create(code='EMAIL_CHANGED',
  54. name='Email Changed',
  55. category=CommunicationEventType.USER_RELATED)
  56. ctx = {
  57. 'user': user,
  58. 'site': SiteFactory(name='Test Site'),
  59. 'reset_url': get_password_reset_url(user),
  60. 'new_email': 'newtestuser@example.com',
  61. }
  62. self._dispatch_user_messages(
  63. user, 'EMAIL_CHANGED', ctx, 'Your email address has changed at Test Site.'
  64. )
  65. def test_dispatch_registration_email_message(self):
  66. user = User.objects.create_user('testuser', 'testuser@example.com', 'somesimplepassword')
  67. CommunicationEventType.objects.create(code='REGISTRATION',
  68. name='Registration',
  69. category=CommunicationEventType.USER_RELATED)
  70. ctx = {'user': user,
  71. 'site': SiteFactory()}
  72. self._dispatch_user_messages(user, 'REGISTRATION', ctx, 'Thank you for registering.')
  73. def test_dispatcher_uses_email_connection(self):
  74. connection = mail.get_connection()
  75. disp = Dispatcher(mail_connection=connection)
  76. disp.dispatch_direct_messages('test@example.com', {
  77. 'subject': 'Test',
  78. 'body': 'This is a test.',
  79. 'html': '',
  80. })
  81. self.assertEqual(len(mail.outbox), 1)