def test_schedule_without_send_at_with_check_url_false_not_sent_delete( self, mock_requests, mock_Response): EmailKind.objects.create(name='my-test-email', language='es', template='Hello, world!', plain_template='Hello, world! soy antiguo', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**') schedule( 'my-test-email', 'es', {'check_url': 'http://service.qdqmedia.com/cansend/34324/blah'}) mock_Response.configure_mock(status_code=200) mock_Response.configure_mock(text=json.dumps({ 'allowed': False, 'delete': True })) mock_requests.get.return_value = mock_Response sent_count = send_entries() self.assertEqual(0, sent_count) entries = EmailEntry.objects.filter(deleted=True) self.assertEqual(1, len(entries)) clean_entries() entries = EmailEntry.objects.filter(deleted=True) self.assertEqual(0, len(entries))
def test_send_with_specific_backend_success(self): EmailKind.objects.create(name='my-test-email', language='es', template='Hello, world!', plain_template='Hello, world! soy antiguo', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**') schedule('my-test-email', 'es', {'backend': 'mynewbackend'}) sent_count = send_entries() self.assertEqual(1, sent_count)
def test_schedule_with_send_at_future_without_check_url_not_sent(self): EmailKind.objects.create(name='my-test-email', language='es', template='Hello, world!', plain_template='Hello, world! soy antiguo', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**') now_ts = int(time.mktime(timezone.now().timetuple())) future_ts = now_ts + 300 schedule('my-test-email', 'es', {'send_at': future_ts}) sent_count = send_entries() self.assertEqual(0, sent_count)
def test_send_embedded_images_some_unused(self): ekind = EmailKind.objects.create( name='my-test-email', language='es', template=('<h1>Hola, {{ first_name }}</h1>' '<img src="cid:logo-solocal">'), plain_template='Hola, {{ first_name }}', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**', default_context={'first_name': 'Luisa'}) upload_image1 = create_upload_image() upload_image2 = create_upload_image() EmbeddedImage.objects.create(kind=ekind, placeholder_name='logo-solocal', image=upload_image1) EmbeddedImage.objects.create(kind=ekind, placeholder_name='logo-qdq', image=upload_image2) entry_params = {} entry = schedule('my-test-email', 'es', entry_params) email = send(entry) self.assertTrue(entry.sent) self.assertIsNotNone(entry.datetime_sent) self.assertEqual(1, len(email.attachments)) image1 = EmbeddedImage.objects.get(kind=ekind, placeholder_name='logo-solocal') self.assertEqual(image1.content_id[1:-1], email.attachments[0].get('Content-ID'))
def test_send_attachs_success(self): EmailKind.objects.create(name='my-test-email', language='es', template='Hello, world!', plain_template='Hello, world! soy antiguo', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**') image_content = get_jpg_content() entry_params = { 'attachs': [{ 'filename': 'test.jpg', 'content_type': 'image/jpeg', 'content': base64.b64encode(image_content) }] } entry = schedule('my-test-email', 'es', entry_params) email = send(entry) self.assertTrue(entry.sent) self.assertEqual('348dj38dj28do5jd82', entry.thirdparty_id) self.assertIsNotNone(entry.datetime_sent) self.assertEqual('Hello, world!', entry.rendered_template) self.assertEqual('Hello, world! soy antiguo', entry.rendered_plain_template) att = Attachment.objects.get(entry=entry) self.assertEqual('test.jpg', att.name) self.assertEqual('image/jpeg', att.content_type) self.assertEqual(email.attachments[0][0], 'test.jpg') self.assertEqual(email.attachments[0][1], image_content) self.assertEqual(email.attachments[0][2], 'image/jpeg')
def test_send_no_attachs_success(self): EmailKind.objects.create(name='my-test-email', language='es', template='Hello, world!', plain_template='Hello, world! soy antiguo', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**') entry_params = {} entry = schedule('my-test-email', 'es', entry_params) email = send(entry) entry = EmailEntry.objects.get(id=entry.id) self.assertTrue(entry.sent) self.assertEqual('348dj38dj28do5jd82', entry.thirdparty_id) self.assertEqual('Hello, world!', entry.rendered_template) self.assertEqual('Hello, world! soy antiguo', entry.rendered_plain_template) self.assertEqual('Hello, world! soy antiguo', email.body) self.assertEqual('Hello, world!', email.alternatives[0][0]) self.assertEqual('text/html', email.alternatives[0][1]) self.assertEqual('348dj38dj28do5jd82', email.response_content[0]['id']) self.assertEqual(entry.recipients.split(','), email.to) self.assertEqual(entry.sender, email.from_email) self.assertEqual(entry.subject, email.subject) self.assertEqual(entry.reply_to.split(','), email.reply_to) self.assertIsNotNone(entry.datetime_sent)
def on_enqueue_email(channel, method_frame, header_frame, body): """Callback to email enqueue. Sends ack to Rabbit on success""" increment(settings.METRIC['CONSUMER_RECEIVED']) sched_logger.info('[scheduler] Starting enqueue: {}'.format(body)) try: parsed_body = json.loads(body.decode('utf8')) schedule(name=parsed_body['name'], language=parsed_body['language'], params=parsed_body) increment(settings.METRIC['SCHEDULED_OK']) sched_logger.info('[scheduler] Finished enqueue: successful') except Exception: logger.exception('Exception ocurred during enqueuing') sched_logger.info('[scheduler] Finished enqueue: failure') increment(settings.METRIC['SCHEDULED_FAIL']) finally: channel.basic_ack(delivery_tag=method_frame.delivery_tag) increment(settings.METRIC['CONSUMER_ACK'])
def test_send_with_specific_backend_rejected(self): EmailKind.objects.create(name='my-test-email', language='es', template='Hello, world!', plain_template='Hello, world! soy antiguo', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**') entry = schedule('my-test-email', 'es', {'backend': 'myrejectbackend'}) sent_count = send_entries() entry = EmailEntry.objects.get(id=entry.id) self.assertEqual(0, sent_count) self.assertFalse(entry.sent) self.assertEqual(entry.thirdparty_reject, 'potato')
def test_schedule_with_metadata(self): EmailKind.objects.create(name='my-test-email', language='es', template='Hello, world!', plain_template='Hello, world! soy antiguo', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**') meta_fields = {'meta1': 'meta1value', 'meta2': 'meta2value'} entry = schedule('my-test-email', 'es', {'meta_fields': meta_fields}) email = send(entry) self.assertTrue(entry.sent) self.assertEqual(meta_fields, entry.metadata) self.assertEqual('meta1value', email.metadata['meta1']) self.assertIn('meta2value', email.metadata['meta2'])
def test_send_emailkind_no_active(self): EmailKind.objects.create(name='my-test-email', language='es', template='Hello, world!', plain_template='Hello, world! soy antiguo', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**', active=False) entry_params = {} entry = schedule('my-test-email', 'es', entry_params) send_entries() entry = EmailEntry.objects.get(id=entry.id) self.assertFalse(entry.sent) self.assertIsNone(entry.datetime_sent)
def test_send_no_attachs_failure(self): EmailKind.objects.create(name='my-test-email', language='es', template='Hello, world!', plain_template='Hello, world! soy antiguo', default_sender='*****@*****.**', default_recipients='*****@*****.**', default_subject='Email Test', default_reply_to='*****@*****.**') entry_params = {} entry = schedule('my-test-email', 'es', entry_params) send(entry) entry = EmailEntry.objects.get(id=entry.id) self.assertFalse(entry.sent) self.assertEqual('', entry.thirdparty_id) self.assertEqual('', entry.rendered_template) self.assertEqual('', entry.rendered_plain_template) self.assertIsNone(entry.datetime_sent)