vendor/Five/1.2b-r20590

view tests/testing/restricted.py @ 0:3673ed425f80

Vendor import of Five 1.2b+ (r20590)
author fguillaume
date Fri, 02 Dec 2005 20:25:42 +0000
parents
children
line source
1 ##############################################################################
2 #
3 # Copyright (c) 2004, 2005 Zope Corporation and Contributors.
4 # All Rights Reserved.
5 #
6 # This software is subject to the provisions of the Zope Public License,
7 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
8 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11 # FOR A PARTICULAR PURPOSE.
12 #
13 ##############################################################################
14 """Restricted python test helpers
16 Based on Plone's RestrictedPythonTestCase, with kind permission by the
17 Plone developers.
19 $Id: restricted.py 14473 2005-07-10 13:02:21Z philikon $
20 """
21 from AccessControl import Unauthorized
22 from Testing.ZopeTestCase import ZopeTestCase
24 def addPythonScript(folder, id, params='', body=''):
25 """Add a PythonScript to folder."""
26 # clean up any 'ps' that's already here..
27 try:
28 folder._getOb(id)
29 folder.manage_delObjects([id])
30 except AttributeError:
31 pass # it's okay, no 'ps' exists yet
32 factory = folder.manage_addProduct['PythonScripts']
33 factory.manage_addPythonScript(id)
34 folder[id].ZPythonScript_edit(params, body)
36 def checkRestricted(folder, psbody):
37 """Perform a check by running restricted Python code."""
38 addPythonScript(folder, 'ps', body=psbody)
39 try:
40 folder.ps()
41 except Unauthorized, e:
42 raise AssertionError, e
44 def checkUnauthorized(folder, psbody):
45 """Perform a check by running restricted Python code. Expect to
46 encounter an Unauthorized exception."""
47 addPythonScript(folder, 'ps', body=psbody)
48 try:
49 folder.ps()
50 except Unauthorized:
51 pass
52 else:
53 raise AssertionError, "Authorized but shouldn't be"
55 class RestrictedPythonTestCase(ZopeTestCase):
56 """Javiotic test case for restricted code."""
58 def addPS(self, id, params='', body=''):
59 addPythonScript(self.folder, id, params, body)
61 def check(self, psbody):
62 checkRestricted(self.folder, psbody)
64 def checkUnauthorized(self, psbody):
65 checkUnauthorized(self.folder, psbody)