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_order_dispatcher.py 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from django.core import mail
  2. from django.test import TestCase
  3. from oscar.core.compat import get_user_model
  4. from oscar.core.loading import get_class, get_model
  5. from oscar.test.factories import create_order
  6. User = get_user_model()
  7. CommunicationEventType = get_model('communication', 'CommunicationEventType')
  8. CommunicationEvent = get_model('order', 'CommunicationEvent')
  9. Email = get_model('communication', 'Email')
  10. Dispatcher = get_class('communication.utils', 'Dispatcher')
  11. OrderDispatcher = get_class('order.utils', 'OrderDispatcher')
  12. class TestDispatcher(TestCase):
  13. def _dispatch_order_messages(self, order_number, order, email=None):
  14. event_code = OrderDispatcher.ORDER_PLACED_EVENT_CODE
  15. et = CommunicationEventType.objects.create(
  16. code=event_code,
  17. name="Order Placed",
  18. category=CommunicationEventType.ORDER_RELATED,
  19. )
  20. messages = et.get_messages({
  21. 'order': order,
  22. 'lines': order.lines.all()
  23. })
  24. assert order_number in messages['body']
  25. assert order_number in messages['html']
  26. order_dispatcher = OrderDispatcher()
  27. order_dispatcher.dispatch_order_messages(order, messages, event_code)
  28. assert CommunicationEvent.objects.filter(order=order, event_type=et).count() == 1
  29. assert len(mail.outbox) == 1
  30. message = mail.outbox[0]
  31. assert order_number in message.body
  32. # Test sending messages to emails without account and text body
  33. messages['body'] = ''
  34. dispatcher = Dispatcher()
  35. dispatcher.dispatch_direct_messages(email, messages)
  36. assert len(mail.outbox) == 2
  37. def test_dispatch_order_messages(self):
  38. email = 'testuser@example.com'
  39. user = User.objects.create_user('testuser', email, 'somesimplepassword')
  40. order = create_order(number='12345', user=user)
  41. assert not order.is_anonymous
  42. self._dispatch_order_messages(order_number='12345', order=order, email=email)
  43. def test_dispatch_anonymous_order_messages(self):
  44. order = create_order(number='12346', guest_email='testguest@example.com')
  45. assert order.is_anonymous
  46. self._dispatch_order_messages(order_number='12346', order=order, email='testguest@example.com', )