summaryrefslogtreecommitdiff
path: root/repoze/bfg/tests/test_zcml.py
blob: 6d79606ad8c1a1271d7ffab4a2b756ca93d6ea76 (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import unittest

from zope.component.testing import PlacelessSetup

class TestViewDirective(unittest.TestCase, PlacelessSetup):
    def setUp(self):
        PlacelessSetup.setUp(self)

    def tearDown(self):
        PlacelessSetup.tearDown(self)

    def _getFUT(self):
        from repoze.bfg.zcml import view
        return view

    def test_no_view(self):
        f = self._getFUT()
        from zope.configuration.exceptions import ConfigurationError
        context = DummyContext()
        self.assertRaises(ConfigurationError, f, context, 'repoze.view', None)

    def test_only_view(self):
        f = self._getFUT()
        context = DummyContext()
        class IFoo:
            pass
        def view(context, request):
            pass
        f(context, 'repoze.view', IFoo, view=view)
        actions = context.actions
        from repoze.bfg.interfaces import IRequest
        from repoze.bfg.interfaces import IView
        from repoze.bfg.interfaces import IViewPermission
        from repoze.bfg.security import ViewPermissionFactory
        from repoze.bfg.zcml import handler
        from zope.component.interface import provideInterface

        self.assertEqual(len(actions), 3)

        provide = actions[0]
        self.assertEqual(provide['discriminator'], None)
        self.assertEqual(provide['callable'], provideInterface)
        self.assertEqual(provide['args'][0], '')
        self.assertEqual(provide['args'][1], IFoo)

        permission = actions[1]
        permission_discriminator = ('permission', IFoo, '', IRequest,
                                    IViewPermission)
        self.assertEqual(permission['discriminator'], permission_discriminator)
        self.assertEqual(permission['callable'], handler)
        self.assertEqual(permission['args'][0], 'registerAdapter')
        self.failUnless(isinstance(permission['args'][1],ViewPermissionFactory))
        self.assertEqual(permission['args'][1].permission_name, 'repoze.view')
        self.assertEqual(permission['args'][2], (IFoo, IRequest))
        self.assertEqual(permission['args'][3], IViewPermission)
        self.assertEqual(permission['args'][4], '')
        self.assertEqual(permission['args'][5], None)
        
        regadapt = actions[2]
        regadapt_discriminator = ('view', IFoo, '', IRequest, IView, True)
        self.assertEqual(regadapt['discriminator'], regadapt_discriminator)
        self.assertEqual(regadapt['callable'], handler)
        self.assertEqual(regadapt['args'][0], 'registerAdapter')
        self.assertEqual(regadapt['args'][1], view)
        self.assertEqual(regadapt['args'][2], (IFoo, IRequest))
        self.assertEqual(regadapt['args'][3], IView)
        self.assertEqual(regadapt['args'][4], '')
        self.assertEqual(regadapt['args'][5], None)

    def test_request_type(self):
        f = self._getFUT()
        context = DummyContext()
        class IFoo:
            pass
        def view(context, request):
            pass
        f(context, 'repoze.view', IFoo, view=view, request_type=IDummy)
        actions = context.actions
        from repoze.bfg.interfaces import IView
        from repoze.bfg.interfaces import IViewPermission
        from repoze.bfg.security import ViewPermissionFactory
        from repoze.bfg.zcml import handler
        from zope.component.interface import provideInterface

        self.assertEqual(len(actions), 3)

        provide = actions[0]
        self.assertEqual(provide['discriminator'], None)
        self.assertEqual(provide['callable'], provideInterface)
        self.assertEqual(provide['args'][0], '')
        self.assertEqual(provide['args'][1], IFoo)

        permission = actions[1]
        permission_discriminator = ('permission', IFoo, '', IDummy,
                                    IViewPermission)
        self.assertEqual(permission['discriminator'], permission_discriminator)
        self.assertEqual(permission['callable'], handler)
        self.assertEqual(permission['args'][0], 'registerAdapter')
        self.failUnless(isinstance(permission['args'][1],ViewPermissionFactory))
        self.assertEqual(permission['args'][1].permission_name, 'repoze.view')
        self.assertEqual(permission['args'][2], (IFoo, IDummy))
        self.assertEqual(permission['args'][3], IViewPermission)
        self.assertEqual(permission['args'][4], '')
        self.assertEqual(permission['args'][5], None)
        
        regadapt = actions[2]
        regadapt_discriminator = ('view', IFoo, '', IDummy, IView, True)
        self.assertEqual(regadapt['discriminator'], regadapt_discriminator)
        self.assertEqual(regadapt['callable'], handler)
        self.assertEqual(regadapt['args'][0], 'registerAdapter')
        self.assertEqual(regadapt['args'][1], view)
        self.assertEqual(regadapt['args'][2], (IFoo, IDummy))
        self.assertEqual(regadapt['args'][3], IView)
        self.assertEqual(regadapt['args'][4], '')
        self.assertEqual(regadapt['args'][5], None)

    def test_uncacheable(self):
        f = self._getFUT()
        context = DummyContext()
        class IFoo:
            pass
        def view(context, request):
            pass
        f(context, 'repoze.view', IFoo, view=view, request_type=IDummy,
          cacheable=False)
        actions = context.actions
        from repoze.bfg.interfaces import IView
        from repoze.bfg.zcml import handler
        from repoze.bfg.zcml import Uncacheable

        self.assertEqual(len(actions), 3)

        regadapt = actions[2]
        regadapt_discriminator = ('view', IFoo, '', IDummy, IView, Uncacheable)
        self.assertEqual(regadapt['discriminator'], regadapt_discriminator)
        self.assertEqual(regadapt['callable'], handler)
        self.assertEqual(regadapt['args'][0], 'registerAdapter')
        self.assertEqual(regadapt['args'][1], view)
        self.assertEqual(regadapt['args'][2], (IFoo, IDummy))
        self.assertEqual(regadapt['args'][3], IView)
        self.assertEqual(regadapt['args'][4], '')
        self.assertEqual(regadapt['args'][5], None)

class TestFixtureApp(unittest.TestCase, PlacelessSetup):
    def setUp(self):
        PlacelessSetup.setUp(self)

    def tearDown(self):
        PlacelessSetup.tearDown(self)

    def test_registry_actions_can_be_pickled_and_unpickled(self):
        import repoze.bfg.tests.fixtureapp as package
        from zope.configuration import config
        from zope.configuration import xmlconfig
        context = config.ConfigurationMachine()
        xmlconfig.registerCommonDirectives(context)
        context.package = package
        xmlconfig.include(context, 'configure.zcml', package)
        context.execute_actions(clear=False)
        actions = context.actions
        import cPickle
        dumped = cPickle.dumps(actions, -1)
        new = cPickle.loads(dumped)
        self.assertEqual(len(actions), len(new))

class TestZCMLPickling(unittest.TestCase, PlacelessSetup):
    i = 0
    def setUp(self):
        self.tempdir = None
        PlacelessSetup.setUp(self)
        import sys
        import os
        import tempfile
        from repoze.bfg.path import package_path
        from repoze.bfg.tests import fixtureapp as package
        import shutil
        tempdir = tempfile.mkdtemp()
        modname = 'myfixture%s' % self.i
        self.i += 1
        self.packagepath = os.path.join(tempdir, modname)
        fixturedir = package_path(package)
        pckname = os.path.join(fixturedir, 'configure.zcml.cache')
        if os.path.isfile(pckname):
            os.remove(pckname)
        shutil.copytree(fixturedir, self.packagepath)
        sys.path.insert(0, tempdir)
        self.module = __import__(modname)
        self.tempdir = tempdir

    def tearDown(self):
        PlacelessSetup.tearDown(self)
        import sys
        import shutil
        if self.module is not None:
            del sys.modules[self.module.__name__]
        if self.tempdir is not None:
            sys.path.pop(0)
            shutil.rmtree(self.tempdir)

    def test_file_configure(self):
        import os
        import cPickle
        from repoze.bfg.zcml import file_configure
        self.assertEqual(False, file_configure('configure.zcml', self.module))
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        self.failUnless(os.path.exists(picklename))
        actions = cPickle.load(open(picklename, 'rb'))
        self.failUnless(actions)

    def test_file_configure_uncacheable_removes_cache(self):
        import os
        from repoze.bfg.zcml import file_configure
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        f = open(picklename, 'w')
        f.write('imhere')
        self.failUnless(os.path.exists(picklename))

        import repoze.bfg.zcml
        keep_view = repoze.bfg.zcml.view

        def wrap_view(*arg, **kw):
            kw['cacheable'] = False
            return keep_view(*arg, **kw)

        try:
            repoze.bfg.zcml.view = wrap_view
            file_configure('configure.zcml', self.module)
            self.failIf(os.path.exists(picklename)) # should be deleted
        finally:
            repoze.bfg.zcml.view = keep_view

    def test_file_configure_nonexistent_configure_dot_zcml(self):
        import os
        from repoze.bfg.zcml import file_configure
        os.remove(os.path.join(self.packagepath, 'configure.zcml'))
        self.assertRaises(IOError, file_configure, 'configure.zcml',
                          self.module)

    def test_file_configure_pickling_error(self):
        import os
        from repoze.bfg.zcml import file_configure
        def dumpfail(actions, f):
            raise IOError
        self.assertEqual(False,
                      file_configure('configure.zcml', self.module, dumpfail))
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        self.failIf(os.path.exists(picklename))

    def test_zcml_configure_writes_pickle_when_none_exists(self):
        import os
        import cPickle
        from repoze.bfg.zcml import zcml_configure
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        self.failUnless(os.path.exists(picklename))
        actions = cPickle.load(open(picklename, 'rb'))
        self.failUnless(actions)

    def test_zcml_configure_uses_file_configure_with_bad_pickle1(self):
        import os
        import cPickle
        from repoze.bfg.zcml import zcml_configure
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        f = open(picklename, 'wb')
        cPickle.dump((), f)
        f.close()
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))

    def test_zcml_configure_uses_file_configure_with_bad_pickle2(self):
        import os
        from repoze.bfg.zcml import zcml_configure
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        f = open(picklename, 'wb')
        f.write('garbage')
        f.close()
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))

    def test_zcml_configure_uses_file_configure_with_outofdate_pickle1(self):
        import os
        import cPickle
        import time
        from repoze.bfg.zcml import zcml_configure
        basename = os.path.join(self.packagepath, 'configure.zcml')
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))
        self.failUnless(os.path.exists(picklename))
        actions = cPickle.load(open(picklename, 'rb'))
        self.failUnless(actions)
        os.utime(basename, (-1, time.time() + 100))
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))

    def test_zcml_configure_uses_file_configure_with_outofdate_pickle2(self):
        import os
        import cPickle
        import time
        from repoze.bfg.zcml import zcml_configure
        basename = os.path.join(self.packagepath, 'another.zcml')
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))
        self.failUnless(os.path.exists(picklename))
        actions = cPickle.load(open(picklename, 'rb'))
        self.failUnless(actions)
        os.utime(basename, (-1, time.time() + 100))
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))

    def test_zcml_configure_uses_file_configure_with_missing_dependent(self):
        import os
        import cPickle
        from repoze.bfg.zcml import zcml_configure
        from zope.configuration.xmlconfig import ZopeXMLConfigurationError
        basename = os.path.join(self.packagepath, 'another.zcml')
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))
        self.failUnless(os.path.exists(picklename))
        actions = cPickle.load(open(picklename, 'rb'))
        self.failUnless(actions)
        os.remove(basename)
        self.assertRaises(ZopeXMLConfigurationError, zcml_configure,
                          'configure.zcml', self.module)

    def test_zcml_configure_uses_file_configure_with_bad_version(self):
        import os
        from repoze.bfg.zcml import zcml_configure
        from repoze.bfg.zcml import PVERSION
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        f = open(picklename, 'wb')
        import cPickle
        data = (PVERSION+1, 0, [])
        cPickle.dump(data, open(picklename, 'wb'))
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))

    def test_zcml_configure_uses_file_configure_with_bad_time(self):
        import os
        from repoze.bfg.zcml import zcml_configure
        from repoze.bfg.zcml import PVERSION
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        f = open(picklename, 'wb')
        import cPickle
        data = (PVERSION, None, [])
        cPickle.dump(data, open(picklename, 'wb'))
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))

    def test_zcml_configure_uses_file_configure_with_bad_actions(self):
        import os
        from repoze.bfg.zcml import zcml_configure
        from repoze.bfg.zcml import PVERSION
        import time
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        f = open(picklename, 'wb')
        import cPickle
        data = (PVERSION, time.time()+500, None)
        cPickle.dump(data, open(picklename, 'wb'))
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))

    def test_zcml_configure_uses_good_pickle(self):
        import os
        import cPickle
        import time
        from repoze.bfg.zcml import zcml_configure
        from repoze.bfg.zcml import PVERSION
        basename = os.path.join(self.packagepath, 'another.zcml')
        picklename = os.path.join(self.packagepath, 'configure.zcml.cache')
        self.assertEqual(False, zcml_configure('configure.zcml', self.module))
        self.failUnless(os.path.exists(picklename))
        actions = cPickle.load(open(picklename, 'rb'))
        self.failUnless(actions)
        actions = (PVERSION, time.time()+100, actions[2])
        cPickle.dump(actions, open(picklename, 'wb'))
        self.assertEqual(True, zcml_configure('configure.zcml', self.module))

class Dummy:
    pass

class DummyContext:
    def __init__(self):
        self.actions = []
        self.info = None

    def path(self, name):
        import os
        here = os.path.dirname(__file__)
        fixtures = os.path.join(here, 'fixtures')
        return os.path.join(fixtures, name)

    def action(self, discriminator, callable, args):
        self.actions.append(
            {'discriminator':discriminator,
             'callable':callable,
             'args':args}
            )

from zope.interface import Interface
class IDummy(Interface):
    pass