test_engine.py 10 KB
Newer Older
Yusei Tahara's avatar
Yusei Tahara committed
1 2 3

from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase

4
from zope.interface import implementer
Yusei Tahara's avatar
Yusei Tahara committed
5
from Products.PortalTransforms.utils import TransformException
6
from Products.PortalTransforms.interfaces import ITransform
Yusei Tahara's avatar
Yusei Tahara committed
7 8 9 10 11 12 13 14 15 16
from Products.PortalTransforms.chain import chain

import urllib
import re

class BaseTransform:
    def name(self):
        return getattr(self, '__name__', self.__class__.__name__)


17
@implementer(ITransform)
Yusei Tahara's avatar
Yusei Tahara committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
class HtmlToText(BaseTransform):
    inputs = ('text/html',)
    output = 'text/plain'

    def __call__(self, orig, **kwargs):
        orig = re.sub('<[^>]*>(?i)(?m)', '', orig)
        return urllib.unquote(re.sub('\n+', '\n', orig)).strip()

    def convert(self, orig, data, **kwargs):
        orig = self.__call__(orig)
        data.setData(orig)
        return data

class HtmlToTextWithEncoding(HtmlToText):
    output_encoding = 'ascii'

34
@implementer(ITransform)
Yusei Tahara's avatar
Yusei Tahara committed
35 36 37 38 39 40 41 42 43 44 45 46 47
class FooToBar(BaseTransform):
    inputs = ('text/*',)
    output = 'text/plain'

    def __call__(self, orig, **kwargs):
        orig = re.sub('foo', 'bar', orig)
        return urllib.unquote(re.sub('\n+', '\n', orig)).strip()

    def convert(self, orig, data, **kwargs):
        orig = self.__call__(orig)
        data.setData(orig)
        return data

48
@implementer(ITransform)
49 50 51 52 53 54 55 56 57
class DummyHtmlFilter1(BaseTransform):
    __name__ = 'dummy_html_filter1'
    inputs = ('text/html',)
    output = 'text/html'

    def convert(self, orig, data, **kwargs):
        data.setData("<span class='dummy'>%s</span>" % orig)
        return data

58
@implementer(ITransform)
59 60 61 62 63 64 65 66
class DummyHtmlFilter2(BaseTransform):
    __name__ = 'dummy_html_filter2'
    inputs = ('text/html',)
    output = 'text/html'

    def convert(self, orig, data, **kwargs):
        data.setData("<div class='dummy'>%s</div>" % orig)
        return data
Yusei Tahara's avatar
Yusei Tahara committed
67

68 69 70 71 72 73 74 75 76

class QuxToVHost(DummyHtmlFilter1):
    __name__ = 'qux_to_vhost'

    def convert(self, orig, data, context, **kwargs):
        data.setData(re.sub('qux', context.REQUEST['SERVER_URL'], orig))
        return data


77
@implementer(ITransform)
Yusei Tahara's avatar
Yusei Tahara committed
78
class TransformNoIO(BaseTransform):
79
    pass
Yusei Tahara's avatar
Yusei Tahara committed
80 81

class BadTransformMissingImplements(BaseTransform):
82
    #__implements__ = None
Yusei Tahara's avatar
Yusei Tahara committed
83 84 85
    inputs = ('text/*',)
    output = 'text/plain'

86
@implementer(ITransform)
Yusei Tahara's avatar
Yusei Tahara committed
87 88 89 90
class BadTransformBadMIMEType1(BaseTransform):
    inputs = ('truc/muche',)
    output = 'text/plain'

91
@implementer(ITransform)
Yusei Tahara's avatar
Yusei Tahara committed
92 93 94 95
class BadTransformBadMIMEType2(BaseTransform):
    inputs = ('text/plain',)
    output = 'truc/muche'

96
@implementer(ITransform)
Yusei Tahara's avatar
Yusei Tahara committed
97 98 99 100
class BadTransformNoInput(BaseTransform):
    inputs = ()
    output = 'text/plain'

101
@implementer(ITransform)
Yusei Tahara's avatar
Yusei Tahara committed
102 103 104
class BadTransformWildcardOutput(BaseTransform):
    inputs = ('text/plain',)
    output = 'text/*'
105

Yusei Tahara's avatar
Yusei Tahara committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
class TestEngine(ATSiteTestCase):

    def afterSetUp(self):
        ATSiteTestCase.afterSetUp(self)
        self.engine = self.portal.portal_transforms
        self.data = '<b>foo</b>'

    def register(self):
        #A default set of transforms to prove the interfaces work
        self.engine.registerTransform(HtmlToText())
        self.engine.registerTransform(FooToBar())

    def testRegister(self):
        self.register()

    def testFailRegister(self):
        register = self.engine.registerTransform
        self.assertRaises(TransformException, register, TransformNoIO())
        self.assertRaises(TransformException, register, BadTransformMissingImplements())
        self.assertRaises(TransformException, register, BadTransformBadMIMEType1())
        self.assertRaises(TransformException, register, BadTransformBadMIMEType2())
        self.assertRaises(TransformException, register, BadTransformNoInput())
        self.assertRaises(TransformException, register, BadTransformWildcardOutput())

    def testCall(self):
        self.register()
        data = self.engine('HtmlToText', self.data)
133
        self.assertEqual(data, "foo")
Yusei Tahara's avatar
Yusei Tahara committed
134 135

        data = self.engine('FooToBar', self.data)
136
        self.assertEqual(data, "<b>bar</b>")
Yusei Tahara's avatar
Yusei Tahara committed
137 138 139 140 141

    def testConvert(self):
        self.register()

        data = self.engine.convert('HtmlToText', self.data)
142 143 144 145
        self.assertEqual(data.getData(), "foo")
        self.assertEqual(data.getMetadata()['mimetype'], 'text/plain')
        self.assertEqual(data.getMetadata().get('encoding'), None)
        self.assertEqual(data.name(), "HtmlToText")
Yusei Tahara's avatar
Yusei Tahara committed
146 147 148

        self.engine.registerTransform(HtmlToTextWithEncoding())
        data = self.engine.convert('HtmlToTextWithEncoding', self.data)
149 150 151
        self.assertEqual(data.getMetadata()['mimetype'], 'text/plain')
        self.assertEqual(data.getMetadata()['encoding'], 'ascii')
        self.assertEqual(data.name(), "HtmlToTextWithEncoding")
Yusei Tahara's avatar
Yusei Tahara committed
152 153 154 155 156

    def testConvertTo(self):
        self.register()

        data = self.engine.convertTo('text/plain', self.data, mimetype="text/html")
157 158 159 160
        self.assertEqual(data.getData(), "foo")
        self.assertEqual(data.getMetadata()['mimetype'], 'text/plain')
        self.assertEqual(data.getMetadata().get('encoding'), None)
        self.assertEqual(data.name(), "text/plain")
Yusei Tahara's avatar
Yusei Tahara committed
161 162 163 164 165

        self.engine.unregisterTransform('HtmlToText')
        self.engine.unregisterTransform('FooToBar')
        self.engine.registerTransform(HtmlToTextWithEncoding())
        data = self.engine.convertTo('text/plain', self.data, mimetype="text/html")
166
        self.assertEqual(data.getMetadata()['mimetype'], 'text/plain')
167
        # HtmlToTextWithEncoding. Now None is the right
168
        #self.assertEqual(data.getMetadata()['encoding'], 'ascii')
169
        # XXX the new algorithm is choosing html_to_text instead of
170 171
        self.assertEqual(data.getMetadata()['encoding'], None)
        self.assertEqual(data.name(), "text/plain")
Yusei Tahara's avatar
Yusei Tahara committed
172 173 174 175 176 177 178 179 180

    def testChain(self):
        self.register()
        hb = chain('hbar')
        hb.registerTransform(HtmlToText())
        hb.registerTransform(FooToBar())

        self.engine.registerTransform(hb)
        cache = self.engine.convert('hbar', self.data)
181 182
        self.assertEqual(cache.getData(), "bar")
        self.assertEqual(cache.name(), "hbar")
Yusei Tahara's avatar
Yusei Tahara committed
183

184 185 186
    def testPolicy(self):
        mt = 'text/x-html-safe'
        data = '<script>this_is_unsafe();</script><p>this is safe</p>'
187

188
        cache = self.engine.convertTo(mt, data, mimetype='text/html')
189
        self.assertEqual(cache.getData(), '<p>this is safe</p>')
190 191 192 193 194 195 196 197

        self.engine.registerTransform(DummyHtmlFilter1())
        self.engine.registerTransform(DummyHtmlFilter2())
        required = ['dummy_html_filter1', 'dummy_html_filter2']

        self.engine.manage_addPolicy(mt, required)
        expected_policy = [('text/x-html-safe',
                            ('dummy_html_filter1', 'dummy_html_filter2'))]
198
        self.assertEqual(self.engine.listPolicies(), expected_policy)
199 200

        cache = self.engine.convertTo(mt, data, mimetype='text/html')
201
        self.assertEqual(cache.getData(), '<div class="dummy"><span class="dummy"><p>this is safe</p></span></div>')
202

203 204
        self.assertEqual(cache.getMetadata()['mimetype'], mt)
        self.assertEqual(cache.name(), mt)
205 206

        path = self.engine._findPath('text/html', mt, required)
207
        self.assertEqual(str(path),
208 209 210 211
                             "[<Transform at dummy_html_filter1>, "
                             "<Transform at dummy_html_filter2>, "
                             "<Transform at safe_html>]")

Yusei Tahara's avatar
Yusei Tahara committed
212 213 214 215
    def testSame(self):
        data = "This is a test"
        mt = "text/plain"
        out = self.engine.convertTo('text/plain', data, mimetype=mt)
216 217
        self.assertEqual(out.getData(), data)
        self.assertEqual(out.getMetadata()['mimetype'], 'text/plain')
Yusei Tahara's avatar
Yusei Tahara committed
218 219 220 221 222 223 224

    def testCache(self):
        data = "This is a test"
        other_data = 'some different data'
        mt = "text/plain"
        self.engine.max_sec_in_cache = 20
        out = self.engine.convertTo(mt, data, mimetype=mt, object=self)
225
        self.assertEqual(out.getData(), data, out.getData())
Yusei Tahara's avatar
Yusei Tahara committed
226
        out = self.engine.convertTo(mt, other_data, mimetype=mt, object=self)
227
        self.assertEqual(out.getData(), data, out.getData())
Yusei Tahara's avatar
Yusei Tahara committed
228 229
        self.engine.max_sec_in_cache = -1
        out = self.engine.convertTo(mt, data, mimetype=mt, object=self)
230
        self.assertEqual(out.getData(), data, out.getData())
Yusei Tahara's avatar
Yusei Tahara committed
231
        out = self.engine.convertTo(mt, other_data, mimetype=mt, object=self)
232
        self.assertEqual(out.getData(), other_data, out.getData())
Yusei Tahara's avatar
Yusei Tahara committed
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    def testCacheWithVHost(self):
        """Ensure that the transform cache key includes virtual
        hosting so that transforms which are dependent on the virtual
        hosting don't get invalid data from the cache.  This happens,
        for example, in the resolve UID functionality used by visual
        editors."""
        mt = 'text/x-html-safe'
        self.engine.registerTransform(QuxToVHost())
        required = ['qux_to_vhost']
        self.engine.manage_addPolicy(mt, required)

        data = '<a href="qux">vhost link</a>'

        out = self.engine.convertTo(
            mt, data, mimetype='text/html', object=self.folder,
            context=self.folder)
250
        self.assertEqual(
251 252 253 254 255 256 257
            out.getData(), '<a href="http://nohost">vhost link</a>',
            out.getData())

        # Test when object is not a context
        out = self.engine.convertTo(
            mt, data, mimetype='text/html', object=self,
            context=self.folder)
258
        self.assertEqual(
259 260 261 262 263 264 265 266 267
            out.getData(), '<a href="http://nohost">vhost link</a>',
            out.getData())

        # Change the virtual hosting
        self.folder.REQUEST['SERVER_URL'] = 'http://otherhost'

        out = self.engine.convertTo(
            mt, data, mimetype='text/html', object=self.folder,
            context=self.folder)
268
        self.assertEqual(
269 270 271 272 273 274 275
            out.getData(), '<a href="http://otherhost">vhost link</a>',
            out.getData())

        # Test when object is not a context
        out = self.engine.convertTo(
            mt, data, mimetype='text/html', object=self,
            context=self.folder)
276
        self.assertEqual(
277 278 279
            out.getData(), '<a href="http://otherhost">vhost link</a>',
            out.getData())

Yusei Tahara's avatar
Yusei Tahara committed
280 281 282 283 284 285

def test_suite():
    from unittest import TestSuite, makeSuite
    suite = TestSuite()
    suite.addTest(makeSuite(TestEngine))
    return suite