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 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import os
  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 ProductImageFactory, create_order
  6. from oscar.test.utils import EmailsMixin, remove_image_folders
  7. OrderDispatcher = get_class('order.utils', 'OrderDispatcher')
  8. class TestConcreteEmailsSending(EmailsMixin, TestCase):
  9. def setUp(self):
  10. super().setUp()
  11. self.dispatcher = OrderDispatcher()
  12. def test_send_order_placed_email_for_user(self):
  13. order_number = 'SOME-NUM00042'
  14. order = create_order(number=order_number, user=self.user)
  15. extra_context = {
  16. 'order': order,
  17. 'lines': order.lines.all()
  18. }
  19. self.dispatcher.send_order_placed_email_for_user(order, extra_context)
  20. self._test_common_part()
  21. expected_subject = 'Confirmation of order {}'.format(order_number)
  22. assert expected_subject == mail.outbox[0].subject
  23. assert 'Your order contains:' in mail.outbox[0].body
  24. product_title = order.lines.first().title
  25. assert product_title in mail.outbox[0].body
  26. def test_send_order_placed_email_with_attachments_for_user(self):
  27. remove_image_folders()
  28. order_number = 'SOME-NUM00042'
  29. order = create_order(number=order_number, user=self.user)
  30. extra_context = {
  31. 'order': order,
  32. 'lines': order.lines.all()
  33. }
  34. line = order.lines.first()
  35. product_image = ProductImageFactory(product=line.product)
  36. attachments = [
  37. ['fake_file.html', b'file_content', 'text/html'],
  38. ['fake_image.png', b'file_content', 'image/png'],
  39. product_image.original.path, # To test sending file from `FileField` based field
  40. ]
  41. self.dispatcher.send_order_placed_email_for_user(order, extra_context, attachments)
  42. # All attachments were sent with email
  43. assert len(mail.outbox[0].attachments) == 3
  44. expected_attachments = ['fake_file.html', 'fake_image.png', 'test_image.jpg']
  45. assert [attachment[0] for attachment in mail.outbox[0].attachments] == expected_attachments
  46. # Remove test file
  47. os.remove(product_image.original.path)