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

test_dispatcher.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from django.core import mail
  2. from django.test import TestCase
  3. from oscar.apps.customer.utils import get_password_reset_url
  4. from oscar.core.compat import get_user_model
  5. from oscar.core.loading import get_class, get_model
  6. from oscar.test.factories import SiteFactory
  7. User = get_user_model()
  8. CommunicationEventType = get_model('communication', 'CommunicationEventType')
  9. Email = get_model('communication', 'Email')
  10. Dispatcher = get_class('communication.utils', 'Dispatcher')
  11. CustomerDispatcher = get_class('customer.utils', 'CustomerDispatcher')
  12. class TestDispatcher(TestCase):
  13. def _dispatch_user_messages(self, user, event_code, ctx, subject):
  14. msgs = CommunicationEventType.objects.get_and_render(code=event_code, context=ctx)
  15. Dispatcher().dispatch_user_messages(user, msgs)
  16. assert len(mail.outbox) == 1
  17. assert mail.outbox[0].subject == subject
  18. assert Email.objects.count() == 1
  19. email = Email.objects.last()
  20. assert email.user.id == user.id
  21. assert email.email == 'testuser@example.com'
  22. def test_dispatch_email_changed_user_message(self):
  23. user = User.objects.create_user('testuser', 'testuser@example.com', 'somesimplepassword')
  24. event_code = CustomerDispatcher.EMAIL_CHANGED_EVENT_CODE
  25. CommunicationEventType.objects.create(
  26. code=event_code,
  27. name='Email Changed',
  28. category=CommunicationEventType.USER_RELATED,
  29. )
  30. ctx = {
  31. 'user': user,
  32. 'site': SiteFactory(name='Test Site'),
  33. 'reset_url': get_password_reset_url(user),
  34. 'new_email': 'newtestuser@example.com',
  35. }
  36. self._dispatch_user_messages(
  37. user, event_code, ctx, 'Your email address has changed at Test Site.'
  38. )
  39. def test_dispatch_registration_email_message(self):
  40. user = User.objects.create_user('testuser', 'testuser@example.com', 'somesimplepassword')
  41. event_code = CustomerDispatcher.REGISTRATION_EVENT_CODE
  42. CommunicationEventType.objects.create(
  43. code=event_code,
  44. name='Registration',
  45. category=CommunicationEventType.USER_RELATED,
  46. )
  47. ctx = {'user': user,
  48. 'site': SiteFactory()}
  49. self._dispatch_user_messages(user, event_code, ctx, 'Thank you for registering.')
  50. def test_dispatcher_uses_email_connection(self):
  51. connection = mail.get_connection()
  52. disp = Dispatcher(mail_connection=connection)
  53. disp.dispatch_direct_messages('test@example.com', {
  54. 'subject': 'Test',
  55. 'body': 'This is a test.',
  56. 'html': '',
  57. })
  58. assert len(mail.outbox) == 1