~ubuntu-branches/ubuntu/trusty/zope.app.form/trusty

« back to all changes in this revision

Viewing changes to src/zope/app/form/tests/utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Gediminas Paulauskas
  • Date: 2010-12-02 01:37:03 UTC
  • Revision ID: james.westby@ubuntu.com-20101202013703-fvbpiae1ok9v73we
Tags: upstream-4.0.2
ImportĀ upstreamĀ versionĀ 4.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2001, 2002 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
"""Utilities for testing form machinery
 
15
 
 
16
$Id: utils.py 29405 2005-03-07 18:22:16Z poster $
 
17
"""
 
18
from zope.interface.interfaces import IMethod
 
19
from zope.security.interfaces import ForbiddenAttribute, Unauthorized
 
20
import zope.security.checker
 
21
from zope.schema import getFieldsInOrder
 
22
 
 
23
class DummyChecker(object):
 
24
    """a checker for testing that requires explicit declarations
 
25
    
 
26
    requires explicit declaration of what is and is not authorized; does not
 
27
    require testing machinery to set up an interaction or a request.
 
28
    
 
29
    To instantiate, pass two dictionaries, the first for get access attribute
 
30
    protection, and the second for set access attribute protection.  keys
 
31
    should be the attribute names, and values should be boolean True and
 
32
    False, where True indicates authorized and False, unauthorized.  Any
 
33
    attributes that are not explicitly set and, in the case of get protection,
 
34
    are not in the zope.security.checker._available_by_default list,
 
35
    will cause ForbiddenAttribute to be raised when the name is checked, as
 
36
    with the real zope.security checkers.
 
37
    """
 
38
    def __init__(self, getnames, setnames):
 
39
        self.getnames = getnames
 
40
        self.setnames = setnames
 
41
    def check_getattr(self, obj, name):
 
42
        if name not in zope.security.checker._available_by_default:
 
43
            try:
 
44
                val = self.getnames[name]
 
45
            except KeyError:
 
46
                raise ForbiddenAttribute
 
47
            else:
 
48
                if not val:
 
49
                    raise Unauthorized
 
50
    check = check_getattr
 
51
    def check_setattr(self, obj, name):
 
52
        try:
 
53
            val = self.setnames[name]
 
54
        except KeyError:
 
55
            raise ForbiddenAttribute
 
56
        else:
 
57
            if not val:
 
58
                raise Unauthorized
 
59
    def proxy(self, value):
 
60
        return value
 
61
 
 
62
def SchemaChecker(schema, readonly=False):
 
63
    """returns a checker that allows read and write access to fields in schema.
 
64
    """
 
65
    get = {}
 
66
    set = {}
 
67
    for name, field in getFieldsInOrder(schema):
 
68
        get[name] = True
 
69
        if not field.readonly:
 
70
            if IMethod.providedBy(field):
 
71
                get[field.writer.__name__] = True
 
72
            else:
 
73
                set[name] = True
 
74
    if readonly:
 
75
        for nm in set:
 
76
            set[nm] = False
 
77
    return DummyChecker(get, set)
 
78
 
 
79
def securityWrap(ob, schema, readonly=False):
 
80
    return zope.security.checker.Proxy(ob, SchemaChecker(schema, readonly))
 
81