示例#1
0
 def setUp(self):
     self.env = EnvironmentStub(enable=[TextileRenderer])
     self.env.config.set('wiki', 'safe_schemes', 'https, http, data')
     self.renderer = TextileRenderer(self.env)
     self.req = MockRequest(self.env)
     self.context = web_context(self.req)
示例#2
0
class TextileRendererTestCase(unittest.TestCase):
    def setUp(self):
        self.env = EnvironmentStub(enable=[TextileRenderer])
        self.env.config.set('wiki', 'safe_schemes', 'https, http, data')
        self.renderer = TextileRenderer(self.env)
        self.req = MockRequest(self.env)
        self.context = web_context(self.req)

    def tearDown(self):
        self.env.reset_db()

    def _render(self, text):
        result = self.renderer.render(self.context, 'textile', text)
        self.assertEqual(Markup, type(result))
        return result

    def _parse_xml(self, source):
        return ElementTree.fromstring(source.encode('utf-8'))

    def test_system_info(self):
        for name, version in self.renderer.get_system_info():
            self.assertEqual('Textile', name)

    def test_image(self):
        result = self._render(
            textwrap.dedent(u"""\
            !https://example.org/foo.png! uníćode
            !//example.net/foo.png!       uníćode
            !/path/to/foo.png!            uníćode
            !foo.png!                     uníćode
            !data:image/png,foo!          uníćode
            """))
        tree = self._parse_xml(result)
        elements = tree.findall('img')
        self.assertEqual(elements[0].get('src'), 'https://example.org/foo.png')
        self.assertEqual(elements[0].get('crossorigin'), 'anonymous')
        self.assertEqual(elements[1].get('src'), '//example.net/foo.png')
        self.assertEqual(elements[1].get('crossorigin'), 'anonymous')
        self.assertEqual(elements[2].get('src'), '/path/to/foo.png')
        self.assertEqual(elements[2].get('crossorigin'), None)
        self.assertEqual(elements[3].get('src'), 'foo.png')
        self.assertEqual(elements[3].get('crossorigin'), None)
        self.assertIn(elements[4].get('src'), ['data:image/png,foo', '#'])
        self.assertEqual(elements[4].get('crossorigin'), None)

    def test_style(self):
        result = self._render(
            textwrap.dedent(u"""\
            *{background:url(https://example.org/foo.png)}uníćode*
            *{background:url(//example.net/foo.png)      }uníćode*
            *{background:url(/path/to/foo.png)           }uníćode*
            *{background:url(./foo.png)                  }uníćode*
            *{background:url(foo.png)                    }uníćode*
            *{background:url(data:image/png,foo)         }uníćode*
            """))
        self.assertNotIn('url(https://example.org/foo.png)', result)
        self.assertNotIn('url(//example.net/foo.png)', result)
        self.assertIn('url(/path/to/foo.png)', result)
        self.assertIn('url(./foo.png)', result)
        self.assertIn('url(foo.png)', result)
        self.assertIn('url(data:image/png,foo)', result)

    def test_html(self):
        result = self._render(
            textwrap.dedent(u"""\
            <a href="ftp://example.org/">unsafe</a>
            <img src="//example.org/foo.png" />
            <span style="background-image:url(http://example.org/foo.png)">unsafe</span>
            """))
        self.assertNotIn('href="ftp://', result)
        self.assertIn(
            '<img src="//example.org/foo.png" crossorigin="anonymous"', result)
        self.assertNotIn('url(http://example.org/foo.png)', result)
        self.assertIn('<span>unsafe</span>', result)