def test_is_valid_positive_case(self): email = '*****@*****.**' subject = 'Test email' service = BaseEmailService(recipient_email_list=[email]) service.subject = subject service.template_name = 'test_email.html' self.assertTrue(service.is_valid())
def test_is_valid_no_template(self): email = '*****@*****.**' subject = 'Test email' service = BaseEmailService(recipient_email_list=[email]) service.subject = subject with self.assertRaises(EmailServiceConfigError): service.is_valid()
def test_has_errors_negative_case(self): email = '*****@*****.**' subject = 'Test email' service = BaseEmailService(recipient_email_list=[email]) service.subject = subject service.template_name = 'test_email.html' self.assertFalse(service.has_errors())
def test_process_regular(self): email = '*****@*****.**' subject = 'Test email' service = BaseEmailService(recipient_email_list=[email]) service.subject = subject service.template_name = 'test_email.html' self.assertTrue(service.process())
def test_build_mail_object_translation_works(self): service = BaseEmailService(recipient_email_list='*****@*****.**') service.template_name = 'test_email.html' msg_obj = service._build_mail_object() # Assertions self.assertIsInstance(msg_obj, EmailMultiAlternatives) self.assertIn('vrijdag', msg_obj.body) self.assertIn('vrijdag', msg_obj.alternatives[0][0])
def test_build_mail_object_regular(self): from_email = '*****@*****.**' reply_to_email = '*****@*****.**' email = '*****@*****.**' subject = 'Test email' my_var = 'Lorem ipsum dolor!' service = BaseEmailService(recipient_email_list=[email], context_data={'my_var': my_var}) service.FROM_EMAIL = from_email service.REPLY_TO_ADDRESS = reply_to_email service.subject = subject service.template_name = 'test_email.html' msg_obj = service._build_mail_object() # Assertions self.assertIsInstance(msg_obj, EmailMultiAlternatives) self.assertEqual(msg_obj.subject, subject) self.assertEqual(msg_obj.from_email, from_email) self.assertEqual(msg_obj.to, [email]) self.assertEqual(msg_obj.reply_to, [reply_to_email]) self.assertIn('Friday', msg_obj.body) self.assertIn(my_var, msg_obj.body) self.assertIn('Friday', msg_obj.alternatives[0][0]) self.assertIn(my_var, msg_obj.alternatives[0][0])
def test_process_with_error(self): subject = 'Test email' service = BaseEmailService() service.subject = subject service.template_name = 'test_email.html' with self.assertRaises(EmailServiceConfigError): service.process()
def test_add_attachments_path_wrong_dict_data(self): # Setup service = BaseEmailService() service.template_name = 'test_email.html' msg_obj = service._build_mail_object() filename = 'awesome_file.txt' service.attachment_list = [{'filename': filename}] with self.assertRaises(EmailServiceAttachmentError): service._add_attachments(msg_obj)
def test_get_subject_with_prefix(self): prefix = 'Ai: Core' subject = 'I am a subject!' service = BaseEmailService() service.SUBJECT_PREFIX = prefix service.subject = subject self.assertIn(prefix, service.get_subject()) self.assertIn(subject, service.get_subject())
def test_add_attachments_path_as_str(self): # Setup service = BaseEmailService() service.template_name = 'test_email.html' msg_obj = service._build_mail_object() file_path = settings.BASE_PATH / 'tests/files/testfile.txt' service.attachment_list = [file_path] msg_obj = service._add_attachments(msg_obj) self.assertEqual(len(msg_obj.attachments), 1) self.assertEqual(msg_obj.attachments[0][0], basename(file_path))
def test_setting_txt_templates_works(self): my_var = 'Lorem ipsum dolor!' service = BaseEmailService( recipient_email_list=['*****@*****.**'], context_data={'my_var': my_var}) service.FROM_EMAIL = '*****@*****.**' service.subject = 'Test mail' service.template_name = 'test_email.html' service.template_txt_name = 'test_email.txt' msg_obj = service._build_mail_object() # Assertions self.assertIsInstance(msg_obj, EmailMultiAlternatives) self.assertIn('I am a different content', msg_obj.body) self.assertNotIn('I am a different content', msg_obj.alternatives[0][0]) self.assertIn(my_var, msg_obj.body)
def test_add_attachments_path_as_dict(self): # Setup service = BaseEmailService() service.template_name = 'test_email.html' msg_obj = service._build_mail_object() filename = 'awesome_file.txt' file_path = settings.BASE_PATH / 'tests/files/testfile.txt' service.attachment_list = [{ 'filename': filename, 'file': file_path, 'mimetype': 'text/text' }] msg_obj = service._add_attachments(msg_obj) self.assertEqual(len(msg_obj.attachments), 1) self.assertEqual(msg_obj.attachments[0][0], filename)
def test_build_mail_object_with_attachments(self): from_email = '*****@*****.**' reply_to_email = '*****@*****.**' email = '*****@*****.**' subject = 'Test email' my_var = 'Lorem ipsum dolor!' file_path = settings.BASE_PATH / 'tests/files/testfile.txt' service = BaseEmailService(recipient_email_list=[email], context_data={'my_var': my_var}, attachment_list=[file_path]) service.FROM_EMAIL = from_email service.REPLY_TO_ADDRESS = reply_to_email service.subject = subject service.template_name = 'test_email.html' msg_obj = service._build_mail_object() # Assertions self.assertIsInstance(msg_obj, EmailMultiAlternatives) self.assertEqual(len(msg_obj.attachments), 1) self.assertEqual(msg_obj.attachments[0][0], basename(file_path))
def test_get_translation_regular_english(self): service = BaseEmailService() self.assertEqual(service.get_translation(), 'en')
def test_get_from_email_from_settings(self): service = BaseEmailService() self.assertEqual(service.get_from_email(), '*****@*****.**')
def test_has_errors_positive_case(self): service = BaseEmailService() service.is_valid(raise_exception=False) self.assertTrue(service.has_errors())
def test_is_valid_no_exception_raised(self): service = BaseEmailService() service.is_valid(raise_exception=False) self.assertEqual(len(service.errors), 3)
def test_is_valid_no_recipient(self): subject = 'Test email' service = BaseEmailService() service.subject = subject with self.assertRaises(EmailServiceConfigError): service.is_valid()
def test_get_from_email_from_class(self): email = '*****@*****.**' service = BaseEmailService() service.FROM_EMAIL = email self.assertEqual(service.get_from_email(), email)
def test_is_valid_no_subject(self): email = '*****@*****.**' service = BaseEmailService(recipient_email_list=[email]) service.template_name = 'test_email.html' with self.assertRaises(EmailServiceConfigError): service.is_valid()
def test_get_reply_to_email_is_set(self): email = '*****@*****.**' service = BaseEmailService() service.REPLY_TO_ADDRESS = email self.assertEqual(service.get_reply_to_emails(), [email])
def test_get_bcc_email_not_set(self): service = BaseEmailService() self.assertEqual(service.get_bcc_emails(), [])
def test_get_translation_settings_short(self): service = BaseEmailService() self.assertEqual(service.get_translation(), 'de')
def test_get_bcc_email_is_set(self): email = '*****@*****.**' service = BaseEmailService() service.bcc_email_list = [email] self.assertEqual(service.get_bcc_emails(), [email])
def test_get_translation_settings_invalid_value(self): service = BaseEmailService() self.assertEqual(service.get_translation(), None)
def test_get_translation_regular_german(self): service = BaseEmailService() self.assertEqual(service.get_translation(), 'de')
def test_get_attachments_regular(self): file_path = 'usr/albertus/myfile.csv' service = BaseEmailService(attachment_list=[file_path]) self.assertEqual(service.get_attachments(), [file_path])
def test_get_attachments_empty(self): service = BaseEmailService() self.assertEqual(service.get_attachments(), [])
def test_init_recipient_as_str_is_wrapped_to_list(self): email = '*****@*****.**' service = BaseEmailService(email) self.assertEqual(service.recipient_email_list, [email])
def test_get_subject_no_prefix(self): subject = 'I am a subject!' service = BaseEmailService() service.subject = subject self.assertEqual(service.get_subject(), subject)