'This test tests that courses with exorbitantly large display names
can still send emails, since it appears that 320 appears to be the
character length limit of from emails for Amazon SES.'

@override_settings(BULK_EMAIL_DEFAULT_FROM_EMAIL='no-reply@courseupdates.edx.org')
def test_long_course_display_name(self):

    test_email = {'action': 'Send    email', 'send_to': '["myself",    "staff",    "learners"]', 'subject': 'test    subject    for    self', 'message': 'test    message    for    self'}
    long_name = u'\u0424\u0438\u043d\u0430\u043d\u0441\u043e\u0432\u043e\u0435    \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435    \u0438    \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0430,    \u0447\u0430\u0441\u0442\u044c    1:    \u043c\u0430\u043a\u0440\u043e\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435    \u0441\u0447\u0435\u0442\u0430    \u0438    \u0430\u043d\u0430\u043b\u0438\u0437'
    course = CourseFactory.create(display_name=long_name, org='IMF', number='FPP.1x', run='2016')
    instructor = InstructorFactory(course_key=course.id)
    unexpected_from_addr = _get_source_address(course.id, course.display_name, course_language=None, truncate=False)
    (__, encoded_unexpected_from_addr) = forbid_multi_line_headers('from', unexpected_from_addr, 'utf-8')
    escaped_encoded_unexpected_from_addr = escape(encoded_unexpected_from_addr)
    self.assertEqual(len(encoded_unexpected_from_addr), 318)
    self.assertEqual(len(escaped_encoded_unexpected_from_addr), 324)
    self.assertEqual(len(unexpected_from_addr), 137)
    self.login_as_user(instructor)
    send_mail_url = reverse('send_email', kwargs={'course_id': unicode(course.id)})
    response = self.client.post(send_mail_url, test_email)
    self.assertTrue(json.loads(response.content)['success'])
    self.assertEqual(len(mail.outbox), 1)
    from_email = mail.outbox[0].from_email
    expected_from_addr = u'"{course_name}"    Course    Staff    <{course_name}-no-reply@courseupdates.edx.org>'.format(course_name=course.id.course)
    self.assertEqual(from_email, expected_from_addr)
    self.assertEqual(len(from_email), 61)
