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))
示例#3
0
    def test_check_mjml_command(self):
        with safe_change_mjml_settings():
            mjml_settings.MJML_EXEC_CMD = '/no_mjml_exec_test'
            with self.assertRaises(ImproperlyConfigured):
                check_mjml_command()

            mjml_settings.MJML_EXEC_CMD = ['python', '-c', 'print("wrong result for testing")', '-']
            with self.assertRaises(ImproperlyConfigured):
                check_mjml_command()
 def setUpClass(cls):
     cls._settings_manager = safe_change_mjml_settings()
     cls._settings_manager.__enter__()
     mjml_settings.MJML_BACKEND_MODE = cls.SERVER_TYPE
     super(TestMJMLHTTPServer, cls).setUpClass()