• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / grokcore.formlib / 16861221675

18 Jun 2025 06:33AM UTC coverage: 96.005%. Remained the same
16861221675

push

github

icemac
Back to development: 5.1

92 of 106 branches covered (86.79%)

Branch coverage included in aggregate %.

701 of 720 relevant lines covered (97.36%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.37
/src/grokcore/formlib/testing.py
1
##############################################################################
2
#
3
# Copyright (c) 2007 Zope Foundation 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
"""Grok test helpers
15
"""
16
import grokcore.security
1✔
17
import martian
1✔
18
from grokcore.component import Context
1✔
19
from grokcore.component import zcml
1✔
20
from grokcore.security.util import protect_getattr
1✔
21
from grokcore.security.util import protect_setattr
1✔
22
from martian.error import GrokError
1✔
23
from zope.configuration.config import ConfigurationMachine
1✔
24

25

26
# Below is a simple grokker + directives that allow you to protect
27
# attributes of a class with Zope 3 security checkers.  This is needed
28
# to run the ftests of this package with the standard Zope publication.
29
# We may hope that something like this eventually makes it into
30
# grokcore.security.
31

32

33
class Model(Context):
1✔
34
    pass
1✔
35

36

37
class protect_get(grokcore.security.require):
1✔
38
    store = martian.MULTIPLE
1✔
39
    default = []
1✔
40

41
    def validate(self, permission, *attrs):
1✔
42
        super().validate(permission)
1✔
43
        for name in attrs:
1✔
44
            # TODO We should probably check whether 'name' is a valid
45
            # Python identifier
46
            martian.validateText(self, name)
1✔
47

48
    def factory(self, permission, *attrs):
1✔
49
        permission = super().factory(permission)
1✔
50
        return (permission, attrs)
1✔
51

52
    # Override baseclass's __call__.  This directive can't be used as
53
    # a decorator at the same time like grok.require() can.
54
    def __call__(self, *args, **kw):
1✔
55
        raise NotImplementedError
56

57

58
class protect_set(protect_get):
1✔
59
    pass
1✔
60

61

62
class ModelSecurityGrokker(martian.ClassGrokker):
1✔
63
    martian.component(Model)
1✔
64
    martian.directive(protect_get)
1✔
65
    martian.directive(protect_set)
1✔
66

67
    def execute(self, factory, config, protect_get, protect_set, **kw):
1✔
68
        get_names = {}
1✔
69
        for permission, attrs in protect_get:
1✔
70
            for name in attrs:
1✔
71
                if name in get_names:
1!
72
                    raise GrokError("Duplicate read protection for %r "
×
73
                                    "attribute on %r." % (name, factory),
74
                                    factory)
75
                get_names[name] = permission
1✔
76

77
        set_names = {}
1✔
78
        for permission, attrs in protect_set:
1✔
79
            for name in attrs:
1✔
80
                if name in set_names:
1!
81
                    raise GrokError("Duplicate write protection for %r "
×
82
                                    "attribute on %r." % (name, factory),
83
                                    factory)
84
                set_names[name] = permission
1✔
85

86
        for name, permission in get_names.items():
1✔
87
            config.action(
1✔
88
                discriminator=('protectName', factory, name),
89
                callable=protect_getattr,
90
                args=(factory, name, permission),
91
            )
92
        for name, permission in set_names.items():
1✔
93
            config.action(
1✔
94
                discriminator=('protectSetAttribute', factory, name),
95
                callable=protect_setattr,
96
                args=(factory, name, permission),
97
            )
98
        return True
1✔
99

100

101
def grok(module_name):
1✔
102
    config = ConfigurationMachine()
1✔
103
    zcml.do_grok('grokcore.component.meta', config)
1✔
104
    zcml.do_grok('grokcore.security.meta', config)
1✔
105
    zcml.do_grok('grokcore.view.meta', config)
1✔
106
    zcml.do_grok('grokcore.formlib.meta', config)
1✔
107
    zcml.do_grok(module_name, config)
1✔
108
    config.execute_actions()
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc