def test_http_auth(self, post_mock): with safe_change_mjml_settings(): for server_conf in mjml_settings.MJML_HTTPSERVERS: server_conf['HTTP_AUTH'] = ('testuser', 'testpassword') response = requests.Response() response.status_code = 200 response._content = force_bytes( json.dumps({ 'errors': [], 'html': 'html_string', 'mjml': 'mjml_string', 'mjml_version': '4.5.1', })) response.encoding = 'utf-8' response.headers['Content-Type'] = 'text/html; charset=utf-8' response.headers['Content-Length'] = len(response._content) post_mock.return_value = response render_tpl(self.TPLS['simple']) self.assertTrue(post_mock.called) self.assertIn('auth', post_mock.call_args[1]) self.assertIsInstance(post_mock.call_args[1]['auth'], requests.auth.HTTPBasicAuth) self.assertEqual(post_mock.call_args[1]['auth'].username, 'testuser') self.assertEqual(post_mock.call_args[1]['auth'].password, 'testpassword')
def test_public_api(self): with safe_change_mjml_settings(): mjml_settings.MJML_HTTPSERVERS = ({ 'URL': 'https://api.mjml.io/v1/render', 'HTTP_AUTH': ('****', '****'), }, ) html = render_tpl( self.TPLS['with_text_context_and_unicode'], { 'text': self.TEXTS['unicode'] + ' [START]' + ('1 2 3 4 5 6 7 8 9 0 ' * 1024) + '[END]', }) self.assertIn('<html ', html) self.assertIn('<body', html) self.assertIn(u'Український текст', html) self.assertIn(self.TEXTS['unicode'], html) self.assertIn(u'©', html) self.assertIn('[START]', html) self.assertIn('[END]', html) with self.assertRaises(RuntimeError) as cm: render_tpl(""" {% mjml %} <mjml> <mj-body> <mj-button> </mj-body> </mjml> {% endmjml %} """) self.assertIn(' Tag: mj-button Message: mj-button ', str(cm.exception))
def test_http_server_error(self): with self.assertRaises(RuntimeError) as cm: render_tpl(""" {% mjml %} <mjml> <mj-body> <mj-button> </mj-body> </mjml> {% endmjml %} """) self.assertIn(' Tag: mj-button Message: mj-button ', str(cm.exception))
def test_simple(self): html = render_tpl(self.TPLS['simple']) self.assertIn('<html ', html) self.assertIn('<body', html) self.assertIn('20px ', html) self.assertIn('Test title', html) self.assertIn('Test button', html) with self.assertRaises(RuntimeError): render_tpl(""" {% mjml %} 123 {% endmjml %} """)
def test_error(self): with self.assertRaises(TemplateSyntaxError): render_tpl(""" {% mjml "var"%} <mjml><mj-body><mj-container></mj-container></mj-body></mjml> {% endmjml %} """) with self.assertRaises(TemplateSyntaxError): render_tpl(""" {% mjml var %} <mjml><mj-body><mj-container></mj-container></mj-body></mjml> {% endmjml %} """, {'var': 'test'})
def test_simple(self): html = render_tpl(self.TPLS['simple']) self.assertIn('<html ', html) self.assertIn('<body', html) self.assertIn('20px ', html) self.assertIn('Test title', html) self.assertIn('Test button', html)
def test_with_tags(self): items = ['test one', 'test two', 'test three'] context = { 'items': items, } html = render_tpl(""" {% mjml %} <mjml> <mj-body> <mj-container> <mj-section> <mj-column> <mj-image src="img/test.png"></mj-image> <mj-text font-size="20px" align="center">Test title</mj-text> </mj-column> </mj-section> <mj-section> <mj-column> {# test_comment $} {% for item in items %} <mj-text align="center">{{ item }}</mj-text> {% endfor %} <mj-button background-color="#ffcc00" font-size="15px">Test button</mj-button> </mj-column> </mj-section> </mj-container> </mj-body> </mjml> {% endmjml %} """, context) self.assertIn('<html ', html) self.assertIn('<body', html) for item in items: self.assertIn(item, html) self.assertNotIn('test_comment', html)
def test_with_vars(self): context = { 'title': 'Test title', 'title_size': '20px', 'btn_label': 'Test button', 'btn_color': '#ffcc00' } html = render_tpl(""" {% mjml %} <mjml> <mj-body> <mj-container> <mj-section> <mj-column> <mj-image src="img/test.png"></mj-image> <mj-text font-size="{{ title_size }}" align="center">{{ title }}</mj-text> </mj-column> </mj-section> <mj-section> <mj-column> <mj-button background-color="{{ btn_color }}" font-size="15px">{{ btn_label }}</mj-button> </mj-column> </mj-section> </mj-container> </mj-body> </mjml> {% endmjml %} """, context) self.assertIn('<html ', html) self.assertIn('<body', html) for val in context.values(): self.assertIn(val, html)
def test_unicode(self): html = render_tpl(self.TPLS['with_text_context_and_unicode'], {'text': self.TEXTS['unicode']}) self.assertIn('<html ', html) self.assertIn('<body', html) self.assertIn(u'Український текст', html) self.assertIn(self.TEXTS['unicode'], html) self.assertIn(u'©', html)
def test_large_tpl(self): html = render_tpl(self.TPLS['with_text_context'], { 'text': '[START]' + ('1 2 3 4 5 6 7 8 9 0 ' * 410 * 1024) + '[END]', }) self.assertIn('<html ', html) self.assertIn('<body', html) self.assertIn('[START]', html) self.assertIn('[END]', html)
def test_big_email(self): big_text = '[START]' + ('Big text. ' * 820 * 1024) + '[END]' html = render_tpl(self.TPLS['with_text_context'], {'text': big_text}) self.assertIn('<html ', html) self.assertIn('<body', html) self.assertIn('Big text. ', html) self.assertIn('[START]', html) self.assertIn('[END]', html) self.assertIn('</body>', html) self.assertIn('</html>', html)
def test_unicode(self): smile = u'\u263a' checkmark = u'\u2713' candy = u'\U0001f36d' # b'\xf0\x9f\x8d\xad'.decode('utf-8') unicode_text = smile + checkmark + candy html = render_tpl(self.TPLS['with_text_context_and_unicode'], {'text': unicode_text}) self.assertIn('<html ', html) self.assertIn('<body', html) self.assertIn(unicode_text, html) self.assertIn(u'©', html)