summaryrefslogtreecommitdiff
path: root/src/zope/traversing/tests/test_vhosting.py
blob: ed393e6c669e6157558c07ebad6682c6458be0a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
##############################################################################
#
# Copyright (c) 2003 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Functional tests for virtual hosting.
"""
import os
import unittest
from io import StringIO

import transaction

import zope.component
import zope.interface
from zope.browserresource.resource import Resource
from zope.configuration import xmlconfig
from zope.location.interfaces import IRoot
from zope.publisher.browser import BrowserRequest, BrowserView
from zope.publisher.interfaces import NotFound
from zope.publisher.interfaces.browser import IBrowserPublisher
from zope.publisher.publish import publish
from zope.publisher.skinnable import setDefaultSkin
from zope.security.checker import defineChecker, NamesChecker, NoProxy
from zope.security.checker import _checkers, undefineChecker
from zope.tales import expressions
from zope.tales.tales import ExpressionEngine
from zope.testing.cleanup import cleanUp

from zope.traversing.adapters import traversePathElement
from zope.traversing.api import traverse
from zope.traversing.testing import browserResource, Contained


class MyObj(Contained):
    def __getitem__(self, key):
        return traverse(self, '/foo/bar/' + key)

class IFolder(zope.interface.Interface):
    pass

@zope.interface.implementer(IFolder, IBrowserPublisher)
class Folder(Contained, dict):
    def __init__(self):
        dict.__init__(self, {})

    def __setitem__(self, name, value):
        value.__parent__ = self
        value.__name__ = name
        dict.__setitem__(self, name, value)

    def publishTraverse(self, request, name):
        subob = self.get(name, None)
        if subob is None:
            raise NotFound(self.context, name, request) # pragma: no cover
        return subob


@zope.interface.implementer(IRoot)
class RootFolder(Folder):
    pass


# Copy some code from zope.pagetemplate to avoid the depedency (break circle)
class ZopeTraverser(object):

    def __call__(self, object, path_items, econtext):
        request = econtext._vars_stack[0].get('request', None)
        path_items = list(path_items)
        path_items.reverse()

        while path_items:
            name = path_items.pop()
            assert getattr(object, '__class__', None) != dict
            object = traversePathElement(object, name, path_items,
                                         request=request)
        return object
zopeTraverser = ZopeTraverser()

class PathExpr(expressions.PathExpr):

    def __init__(self, name, expr, engine):
        super(PathExpr, self).__init__(name, expr, engine, zopeTraverser)

def Engine():
    e = ExpressionEngine()
    for pt in PathExpr._default_type_names:
        e.registerType(pt, PathExpr)
    return e

Engine = Engine()

class MyTalesPage(object):

    def __init__(self, source):
        self.source = source

    def pt_getContext(self, instance, request, **_kw):
        # instance is a View component
        namespace = {}
        namespace['template'] = self
        namespace['request'] = request
        namespace['container'] = namespace['context'] = instance
        return namespace

    def render(self, instance, request, *args, **kw):
        context = self.pt_getContext(instance, request)
        code = Engine.compile(self.source)
        return str(code(Engine.getContext(context)))


class MyPageEval(BrowserView):

    def __call__(self, **kw):
        """Call a Page Template"""
        template = self.context
        request = self.request
        return template.render(template.__parent__, request, **kw)

    index = __call__

class MyFolderPage(BrowserView):

    def __call__(self, **kw):
        """My folder page"""
        self.request.response.redirect('index.html')
        return ''

    index = __call__


class TestVirtualHosting(unittest.TestCase):

    def setUp(self):
        f = os.path.join(os.path.split(__file__)[0], 'ftesting.zcml')
        xmlconfig.file(f)
        defineChecker(MyObj, NoProxy)
        defineChecker(RootFolder, NoProxy)
        defineChecker(Folder, NoProxy)
        self.app = RootFolder()

    def tearDown(self):
        undefineChecker(MyObj)
        undefineChecker(RootFolder)
        undefineChecker(Folder)
        cleanUp()

    def makeRequest(self, path=''):
        env = {"HTTP_HOST": 'localhost',
               "HTTP_REFERER": 'localhost'}
        p = path.split('?')
        if len(p) == 1:
            env['PATH_INFO'] = p[0]

        request = BrowserRequest(StringIO(u''), env)
        request.setPublication(DummyPublication(self.app))
        setDefaultSkin(request)
        return request

    def publish(self, path):
        return publish(self.makeRequest(path)).response

    def test_request_url(self):
        self.addPage('/pt', u'request/URL')
        self.verify('/pt', 'http://localhost/pt/index.html')
        self.verify('/++vh++/++/pt',
                    'http://localhost/pt/index.html')
        self.verify('/++vh++https:localhost:443/++/pt',
                    'https://localhost/pt/index.html')
        self.verify('/++vh++https:localhost:443/fake/folders/++/pt',
                    'https://localhost/fake/folders/pt/index.html')

        self.addPage('/foo/bar/pt', u'request/URL')
        self.verify('/foo/bar/pt', 'http://localhost/foo/bar/pt/index.html')
        self.verify('/foo/bar/++vh++/++/pt',
                    'http://localhost/pt/index.html')
        self.verify('/foo/bar/++vh++https:localhost:443/++/pt',
                    'https://localhost/pt/index.html')
        self.verify('/foo/++vh++https:localhost:443/fake/folders/++/bar/pt',
                    'https://localhost/fake/folders/bar/pt/index.html')

    def test_request_redirect(self):
        self.addPage('/foo/index.html', u'Spam')
        self.verifyRedirect('/foo', 'http://localhost/foo/index.html')
        self.verifyRedirect('/++vh++https:localhost:443/++/foo',
                            'https://localhost/foo/index.html')
        self.verifyRedirect('/foo/++vh++https:localhost:443/bar/++',
                            'https://localhost/bar/index.html')

    def test_absolute_url(self):
        self.addPage('/pt', u'context/@@absolute_url')
        self.verify('/pt', 'http://localhost')
        self.verify('/++vh++/++/pt',
                    'http://localhost')
        self.verify('/++vh++https:localhost:443/++/pt',
                    'https://localhost')
        self.verify('/++vh++https:localhost:443/fake/folders/++/pt',
                    'https://localhost/fake/folders')

        self.addPage('/foo/bar/pt',
                     u'context/@@absolute_url')
        self.verify('/foo/bar/pt', 'http://localhost/foo/bar')
        self.verify('/foo/bar/++vh++/++/pt',
                    'http://localhost')
        self.verify('/foo/bar/++vh++https:localhost:443/++/pt',
                    'https://localhost')
        self.verify('/foo/++vh++https:localhost:443/fake/folders/++/bar/pt',
                    'https://localhost/fake/folders/bar')

    def test_absolute_url_absolute_traverse(self):
        self.createObject('/foo/bar/obj', MyObj())
        self.addPage('/foo/bar/pt',
                     u'container/obj/pt/@@absolute_url')
        self.verify('/foo/bar/pt', 'http://localhost/foo/bar/pt')
        self.verify('/foo/++vh++https:localhost:443/++/bar/pt',
                    'https://localhost/bar/pt')

    def test_resources(self):
        browserResource('quux', Resource)
        # Only register the checker once, so that multiple test runs pass.
        if Resource not in _checkers:
            defineChecker(Resource, NamesChecker(['__call__']))
        self.addPage(u'/foo/bar/pt',
                     u'context/++resource++quux')
        self.verify(u'/foo/bar/pt', u'http://localhost/@@/quux')
        self.verify(u'/foo/++vh++https:localhost:443/fake/folders/++/bar/pt',
                    u'https://localhost/fake/folders/@@/quux')

    def createFolders(self, path):
        """addFolders('/a/b/c/d') would traverse and/or create three nested
        folders (a, b, c) and return a tuple (c, 'd') where c is a Folder
        instance at /a/b/c."""
        folder = self.app  #self.connection.root()['Application']
        if path[0] == '/':
            path = path[1:]
        path = path.split('/')
        for id in path[:-1]:
            try:
                folder = folder[id]
            except KeyError:
                folder[id] = Folder()
                folder = folder[id]
        return folder, path[-1]

    def createObject(self, path, obj):
        folder, id = self.createFolders(path)
        folder[id] = obj
        transaction.commit()

    def addPage(self, path, content):
        page = MyTalesPage(content)
        self.createObject(path, page)

    def verify(self, path, content):
        result = self.publish(path)
        self.assertEqual(result.getStatus(), 200)
        self.assertEqual(result.consumeBody().decode(), content)

    def verifyRedirect(self, path, location):
        result = self.publish(path)
        self.assertEqual(result.getStatus(), 302)
        self.assertEqual(result.getHeader('Location'), location)


class DummyPublication(object):

    def __init__(self, app):
        self.app = app

    def beforeTraversal(self, request):
        """Pre-traversal hook.

        This is called *once* before any traversal has been done.
        """

    def getApplication(self, request):
        """Returns the object where traversal should commence.
        """
        return self.app

    def callTraversalHooks(self, request, ob):
        """Invokes any traversal hooks associated with the object.

        This is called before traversing each object.  The ob argument
        is the object that is about to be traversed.
        """

    def traverseName(self, request, ob, name):
        """Traverses to the next object.

        Name must be an ASCII string or Unicode object."""
        if name == 'index.html':
            from zope.component import getMultiAdapter
            view = getMultiAdapter((ob, request), name=name)
            return view

        from zope.traversing.publicationtraverse import PublicationTraverserWithoutProxy
        t = PublicationTraverserWithoutProxy()
        return t.traverseName(request, ob, name)

    def afterTraversal(self, request, ob):
        """Post-traversal hook.

        This is called after all traversal.
        """

    def callObject(self, request, ob):
        """Call the object, returning the result.

        For GET/POST this means calling it, but for other methods
        (including those of WebDAV and FTP) this might mean invoking
        a method of an adapter.
        """
        from zope.publisher.publish import mapply
        return mapply(ob, request.getPositionalArguments(), request)

    def afterCall(self, request, ob):
        """Post-callObject hook (if it was successful).
        """

    def handleException(self, ob, request, exc_info, retry_allowed=1): # pragma: no cover
        """Handle an exception

        Either:
        - sets the body of the response, request.response, or
        - raises a Retry exception, or
        - throws another exception, which is a Bad Thing.
        """
        import traceback
        traceback.print_exception(*exc_info)

    def endRequest(self, request, ob):
        """Do any end-of-request cleanup
        """

    def getDefaultTraversal(self, request, ob):
        if hasattr(ob, 'index'):
            return ob, ()
        return ob, ('index.html',)