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

zopefoundation / Products.CMFCore / 6246931310

20 Sep 2023 09:54AM UTC coverage: 86.008% (-0.3%) from 86.266%
6246931310

Pull #131

github

mauritsvanrees
gha: don't need setup-python on 27 as we use the 27 container.
Pull Request #131: Make decodeFolderFilter and encodeFolderFilter non-public.

2466 of 3689 branches covered (0.0%)

Branch coverage included in aggregate %.

6 of 6 new or added lines in 1 file covered. (100.0%)

17297 of 19289 relevant lines covered (89.67%)

0.9 hits per line

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

81.93
/src/Products/CMFCore/tests/test_ActionProviderBase.py
1
##############################################################################
2
#
3
# Copyright (c) 2002 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE.
11
#
12
##############################################################################
13
""" Unit tests for ActionProviderBase module.
1✔
14
"""
15

16
import unittest
1✔
17
import warnings
1✔
18

19
from zope.component import getSiteManager
1✔
20
from zope.interface.verify import verifyClass
1✔
21
from zope.testing.cleanup import cleanUp
1✔
22

23
#   We have to import these here to make the "ugly sharing" test case go.
24
from ..ActionInformation import ActionInformation
1✔
25
from ..ActionProviderBase import ActionProviderBase
1✔
26
from ..interfaces import IMembershipTool
1✔
27
from ..interfaces import IURLTool
1✔
28
from .base.dummy import DummySite
1✔
29
from .base.dummy import DummyTool
1✔
30
from .base.testcase import SecurityTest
1✔
31

32

33
class DummyProvider(ActionProviderBase, DummyTool):
1✔
34

35
    _actions = (ActionInformation(id='an_id',
1✔
36
                                  title='A Title',
37
                                  action='',
38
                                  condition='',
39
                                  permissions=(),
40
                                  category='',
41
                                  visible=False),)
42

43

44
class DummyAction:
1✔
45

46
    def __init__(self, value):
1✔
47
        self.value = value
1✔
48

49
    def clone(self):
1✔
50
        return self.__class__(self.value)
1✔
51

52
    def __eq__(self, other):
1✔
53
        return (
1✔
54
            type(self) is type(other) and
55
            self.__class__ == other.__class__ and
56
            self.value == other.value
57
        )
58

59

60
class ActionProviderBaseTests(SecurityTest):
1✔
61

62
    def setUp(self):
1✔
63
        SecurityTest.setUp(self)
1✔
64
        self.site = DummySite('site').__of__(self.app)
1✔
65
        sm = getSiteManager()
1✔
66
        sm.registerUtility(DummyTool(), IMembershipTool)
1✔
67
        sm.registerUtility(DummyTool().__of__(self.site), IURLTool)
1✔
68

69
    def tearDown(self):
1✔
70
        cleanUp()
1✔
71
        SecurityTest.tearDown(self)
1✔
72

73
    def _makeProvider(self, dummy=0):
1✔
74

75
        klass = dummy and DummyProvider or ActionProviderBase
1✔
76
        return klass()
1✔
77

78
    def test_interfaces(self):
1✔
79
        from ..interfaces import IActionProvider
1✔
80

81
        verifyClass(IActionProvider, ActionProviderBase)
1✔
82

83
    def test_addAction(self):
1✔
84
        apb = self._makeProvider()
1✔
85
        self.assertFalse(apb._actions)
1✔
86
        old_actions = apb._actions
1✔
87
        apb.addAction(id='foo',
1✔
88
                      name='foo_action',
89
                      action='',
90
                      condition='',
91
                      permission='',
92
                      category='')
93
        self.assertTrue(apb._actions)
1✔
94
        self.assertFalse(apb._actions is old_actions)
1✔
95

96
    def test_addActionBlankPermission(self):
1✔
97
        # make sure a blank permission gets stored as an empty tuple
98
        # '' and () and ('',) should mean no permission.
99

100
        apb = self._makeProvider()
1✔
101
        apb.addAction(id='foo',
1✔
102
                      name='foo_action',
103
                      action='',
104
                      condition='',
105
                      permission='',
106
                      category='',
107
                      )
108
        self.assertEqual(apb._actions[0].permissions, ())
1✔
109

110
        apb.addAction(id='foo',
1✔
111
                      name='foo_action',
112
                      action='',
113
                      condition='',
114
                      permission=('',),
115
                      category='',
116
                      )
117
        self.assertEqual(apb._actions[1].permissions, ())
1✔
118

119
        apb.addAction(id='foo',
1✔
120
                      name='foo_action',
121
                      action='',
122
                      condition='',
123
                      permission=(),
124
                      category='',
125
                      )
126
        self.assertEqual(apb._actions[2].permissions, ())
1✔
127

128
    def test_extractActionBlankPermission(self):
1✔
129
        # make sure a blank permission gets stored as an empty tuple
130
        # both () and ('',) should mean no permission.
131

132
        apb = self._makeProvider()
1✔
133

134
        index = 5
1✔
135
        properties = {
1✔
136
            'id_5': 'foo',
137
            'name_5': 'foo_action',
138
            'permission_5': (),
139
            }
140
        action = apb._extractAction(properties, index)
1✔
141
        self.assertEqual(action.permissions, ())
1✔
142

143
        index = 2
1✔
144
        properties = {
1✔
145
            'id_2': 'foo',
146
            'name_2': 'foo_action',
147
            'permission_2': ('',),
148
            }
149
        action = apb._extractAction(properties, index)
1✔
150
        self.assertEqual(action.permissions, ())
1✔
151

152
    def test_changeActions(self):
1✔
153
        apb = DummyTool()
1✔
154
        old_actions = list(apb._actions)
1✔
155

156
        keys = [('id_%d', None),
1✔
157
                ('name_%d', None),
158
                ('action_%d', ''),
159
                ('condition_%d', ''),
160
                ('permission_%d', None),
161
                ('category_%d', None),
162
                ('visible_%d', 0)]
163

164
        properties = {}
1✔
165
        for i in range(len(old_actions)):
1!
166
            for key, value in keys:
×
167
                token = key % i
×
168
                if value is None:
×
169
                    value = token
×
170
                properties[token] = value
×
171

172
        apb.changeActions(properties=properties)
1✔
173

174
        marker = []
1✔
175
        for i in range(len(apb._actions)):
1!
176

177
            for key, value in keys:
×
178
                attr = key[:-3]
×
179

180
                if value is None:
×
181
                    value = key % i
×
182

183
                if attr == 'name':    # WAAAA
×
184
                    attr = 'title'
×
185

186
                if attr == 'permission':    # WAAAA
×
187
                    attr = 'permissions'
×
188
                    value = (value,)
×
189

190
                attr_value = getattr(apb._actions[i], attr, marker)
×
191
                self.assertEqual(attr_value, value, '%s, %s != %s, %s'
×
192
                                 % (attr, attr_value, key, value))
193
        self.assertFalse(apb._actions is old_actions)
1✔
194

195
    def test_deleteActions(self):
1✔
196

197
        apb = self._makeProvider()
1✔
198
        apb._actions = tuple(map(DummyAction, ['0', '1', '2']))
1✔
199
        highander_action = apb._actions[1]  # There can be only one
1✔
200
        apb.deleteActions(selections=(0, 2))
1✔
201
        self.assertEqual(len(apb._actions), 1)
1✔
202
        self.assertTrue(highander_action in apb._actions)
1✔
203

204
    def test_DietersNastySharingBug(self):
1✔
205

206
        one = self._makeProvider(dummy=1)
1✔
207
        another = self._makeProvider(dummy=1)
1✔
208

209
        def idify(x):
1✔
210
            return id(x)
1✔
211

212
        with warnings.catch_warnings():
1✔
213
            warnings.simplefilter('ignore')
1✔
214
            old_ids = one_ids = list(map(idify, one.listActions()))
1✔
215
            another_ids = list(map(idify, another.listActions()))
1✔
216

217
            self.assertEqual(one_ids, another_ids)
1✔
218

219
            one.changeActions({'id_0': 'different_id',
1✔
220
                               'name_0': 'A Different Title',
221
                               'action_0': 'arise_shine',
222
                               'condition_0': 'always',
223
                               'permissions_0': 'granted',
224
                               'category_0': 'quality',
225
                               'visible_0': 1})
226

227
            one_ids = list(map(idify, one.listActions()))
1✔
228
            another_ids = list(map(idify, another.listActions()))
1✔
229
        self.assertFalse(one_ids == another_ids)
1✔
230
        self.assertEqual(old_ids, another_ids)
1✔
231

232
    def test_listActionInfos(self):
1✔
233
        wanted = [{'id': 'an_id', 'title': 'A Title', 'description': '',
1✔
234
                   'url': '', 'category': 'object', 'visible': False,
235
                   'available': True, 'allowed': True, 'link_target': None,
236
                   'icon': ''}]
237

238
        apb = self.site._setObject('portal_apb', self._makeProvider(1))
1✔
239
        with warnings.catch_warnings():
1✔
240
            warnings.simplefilter('ignore')
1✔
241
            rval = apb.listActionInfos()
1✔
242
            self.assertEqual(rval, [])
1✔
243
            rval = apb.listActionInfos(check_visibility=0)
1✔
244
            self.assertEqual(rval, wanted)
1✔
245
            rval = apb.listActionInfos('foo/another_id', check_visibility=0)
1✔
246
            self.assertEqual(rval, [])
1✔
247

248
    def test_getActionObject(self):
1✔
249
        apb = self.site._setObject('portal_apb', self._makeProvider(1))
1✔
250
        with warnings.catch_warnings():
1✔
251
            warnings.simplefilter('ignore')
1✔
252
            rval = apb.getActionObject('object/an_id')
1✔
253
            self.assertEqual(rval, apb._actions[0])
1✔
254
            rval = apb.getActionObject('object/not_existing_id')
1✔
255
            self.assertEqual(rval, None)
1✔
256
            self.assertRaises(ValueError, apb.getActionObject, 'wrong_format')
1✔
257

258
    def test_getActionInfo(self):
1✔
259
        wanted = {'id': 'an_id', 'title': 'A Title', 'description': '',
1✔
260
                  'url': '', 'category': 'object', 'visible': False,
261
                  'available': True, 'allowed': True, 'link_target': None,
262
                  'icon': ''}
263

264
        apb = self.site._setObject('portal_apb', self._makeProvider(1))
1✔
265
        with warnings.catch_warnings():
1✔
266
            warnings.simplefilter('ignore')
1✔
267
            rval = apb.getActionInfo(('object/an_id',))
1✔
268
            self.assertEqual(rval, wanted)
1✔
269
            rval = apb.getActionInfo('object/an_id')
1✔
270
            self.assertEqual(rval, wanted)
1✔
271
            self.assertRaises(ValueError, apb.getActionInfo, 'object/an_id',
1✔
272
                              check_visibility=1)
273

274
            # The following is nasty, but I want to make sure the ValueError
275
            # carries some useful information
276
            INVALID_ID = 'invalid_id'
1✔
277
            try:
1✔
278
                rval = apb.getActionInfo('object/%s' % INVALID_ID)
1✔
279
            except ValueError as e:
1✔
280
                message = e.args[0]
1✔
281
                detail = '"%s" does not offer action "%s"' % (message,
1✔
282
                                                              INVALID_ID)
283
                self.assertTrue(message.find(INVALID_ID) != -1, detail)
1✔
284

285

286
def test_suite():
1✔
287
    return unittest.TestSuite((
1✔
288
        unittest.makeSuite(ActionProviderBaseTests),
289
        ))
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