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

zopefoundation / Zope / 3956162881

pending completion
3956162881

push

github

Michael Howitz
Update to deprecation warning free releases.

4401 of 7036 branches covered (62.55%)

Branch coverage included in aggregate %.

27161 of 31488 relevant lines covered (86.26%)

0.86 hits per line

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

98.75
/src/ZPublisher/tests/test_xmlrpc.py
1
import unittest
1✔
2
import xmlrpc.client
1✔
3

4
from DateTime import DateTime
1✔
5

6

7
class FauxResponse:
1✔
8

9
    def __init__(self):
1✔
10
        self._headers = {}
1✔
11
        self._body = None
1✔
12

13
    def setBody(self, body):
1✔
14
        self._body = body
1✔
15

16
    def setHeader(self, name, value):
1✔
17
        self._headers[name] = value
1✔
18

19
    def setStatus(self, status):
1✔
20
        self._status = status
×
21

22

23
class FauxInstance:
1✔
24
    def __init__(self, **kw):
1✔
25
        self.__dict__.update(kw)
1✔
26

27

28
class XMLRPCResponseTests(unittest.TestCase):
1✔
29

30
    def _getTargetClass(self):
1✔
31
        from ZPublisher.xmlrpc import Response
1✔
32
        return Response
1✔
33

34
    def _makeOne(self, *args, **kw):
1✔
35
        return self._getTargetClass()(*args, **kw)
1✔
36

37
    def test_setBody(self):
1✔
38
        body = FauxInstance(_secret='abc', public='def')
1✔
39
        faux = FauxResponse()
1✔
40
        response = self._makeOne(faux)
1✔
41

42
        response.setBody(body)
1✔
43

44
        body_str = faux._body
1✔
45
        self.assertEqual(type(body_str), type(''))
1✔
46

47
        as_set, method = xmlrpc.client.loads(body_str)
1✔
48
        as_set = as_set[0]
1✔
49

50
        self.assertIsNone(method)
1✔
51
        self.assertNotIn('_secret', as_set.keys())
1✔
52
        self.assertIn('public', as_set.keys())
1✔
53
        self.assertEqual(as_set['public'], 'def')
1✔
54

55
    def test_nil(self):
1✔
56
        body = FauxInstance(public=None)
1✔
57
        faux = FauxResponse()
1✔
58
        response = self._makeOne(faux)
1✔
59
        response.setBody(body)
1✔
60
        data, method = xmlrpc.client.loads(faux._body)
1✔
61
        self.assertIs(data[0]['public'], None)
1✔
62

63
    def test_instance(self):
1✔
64
        # Instances are turned into dicts with their private
65
        # attributes removed.
66
        body = FauxInstance(_secret='abc', public='def')
1✔
67
        faux = FauxResponse()
1✔
68
        response = self._makeOne(faux)
1✔
69
        response.setBody(body)
1✔
70
        data, method = xmlrpc.client.loads(faux._body)
1✔
71
        data = data[0]
1✔
72
        self.assertEqual(data, {'public': 'def'})
1✔
73

74
    def test_instanceattribute(self):
1✔
75
        # While the removal of private ('_') attributes works fine for the
76
        # top-level instance, how about attributes that are themselves
77
        # instances?
78
        body = FauxInstance(public=FauxInstance(_secret='abc', public='def'))
1✔
79
        faux = FauxResponse()
1✔
80
        response = self._makeOne(faux)
1✔
81
        response.setBody(body)
1✔
82
        data, method = xmlrpc.client.loads(faux._body)
1✔
83
        data = data[0]['public']
1✔
84
        self.assertEqual(data, {'public': 'def'})
1✔
85

86
    def test_instanceattribute_recursive(self):
1✔
87
        # Instance "flattening" should work recursively, ad infinitum
88
        body = FauxInstance(public=FauxInstance(public=FauxInstance(
1✔
89
            _secret='abc', public='def')))
90
        faux = FauxResponse()
1✔
91
        response = self._makeOne(faux)
1✔
92
        response.setBody(body)
1✔
93
        data, method = xmlrpc.client.loads(faux._body)
1✔
94
        data = data[0]['public']['public']
1✔
95
        self.assertEqual(data, {'public': 'def'})
1✔
96

97
    def test_instance_in_list(self):
1✔
98
        # Instances are turned into dicts with their private
99
        # attributes removed, even when embedded in another
100
        # data structure.
101
        body = [FauxInstance(_secret='abc', public='def')]
1✔
102
        faux = FauxResponse()
1✔
103
        response = self._makeOne(faux)
1✔
104
        response.setBody(body)
1✔
105
        data, method = xmlrpc.client.loads(faux._body)
1✔
106
        data = data[0][0]
1✔
107
        self.assertEqual(data, {'public': 'def'})
1✔
108

109
    def test_instance_in_dict(self):
1✔
110
        # Instances are turned into dicts with their private
111
        # attributes removed, even when embedded in another
112
        # data structure.
113
        body = {'faux': FauxInstance(_secret='abc', public='def')}
1✔
114
        faux = FauxResponse()
1✔
115
        response = self._makeOne(faux)
1✔
116
        response.setBody(body)
1✔
117
        data, method = xmlrpc.client.loads(faux._body)
1✔
118
        data = data[0]['faux']
1✔
119
        self.assertEqual(data, {'public': 'def'})
1✔
120

121
    def test_instance_security(self):
1✔
122
        # Make sure instances' Zope permission settings are respected
123
        from AccessControl.Permissions import view
1✔
124
        from OFS.Folder import Folder
1✔
125
        from OFS.Image import manage_addFile
1✔
126

127
        folder = Folder('folder')
1✔
128
        manage_addFile(folder, 'file1')
1✔
129
        folder.file1.manage_permission(view, ('Manager',), 0)
1✔
130
        manage_addFile(folder, 'file2')
1✔
131
        folder.file2.manage_permission(view, ('Manager', 'Anonymous'), 0)
1✔
132

133
        faux = FauxResponse()
1✔
134
        response = self._makeOne(faux)
1✔
135
        response.setBody(folder)
1✔
136
        data, method = xmlrpc.client.loads(faux._body)
1✔
137

138
        self.assertFalse('file1' in data[0].keys())
1✔
139
        self.assertTrue('file2' in data[0].keys())
1✔
140

141
    def test_zopedatetimeinstance(self):
1✔
142
        # DateTime instance at top-level
143
        body = DateTime('2006-05-24 07:00:00 GMT+0')
1✔
144
        faux = FauxResponse()
1✔
145
        response = self._makeOne(faux)
1✔
146
        response.setBody(body)
1✔
147
        data, method = xmlrpc.client.loads(faux._body)
1✔
148
        data = data[0]
1✔
149
        self.assertTrue(isinstance(data, xmlrpc.client.DateTime))
1✔
150
        self.assertEqual(data.value, '2006-05-24T07:00:00+00:00')
1✔
151

152
    def test_zopedatetimeattribute(self):
1✔
153
        # DateTime instance as attribute
154
        body = FauxInstance(public=DateTime('2006-05-24 07:00:00 GMT+0'))
1✔
155
        faux = FauxResponse()
1✔
156
        response = self._makeOne(faux)
1✔
157
        response.setBody(body)
1✔
158
        data, method = xmlrpc.client.loads(faux._body)
1✔
159
        data = data[0]['public']
1✔
160
        self.assertTrue(isinstance(data, xmlrpc.client.DateTime))
1✔
161
        self.assertEqual(data.value, '2006-05-24T07:00:00+00:00')
1✔
162

163
    def test_zopedatetimeattribute_recursive(self):
1✔
164
        # DateTime encoding should work recursively
165
        body = FauxInstance(public=FauxInstance(
1✔
166
            public=DateTime('2006-05-24 07:00:00 GMT+0')))
167
        faux = FauxResponse()
1✔
168
        response = self._makeOne(faux)
1✔
169
        response.setBody(body)
1✔
170
        data, method = xmlrpc.client.loads(faux._body)
1✔
171
        data = data[0]['public']['public']
1✔
172
        self.assertTrue(isinstance(data, xmlrpc.client.DateTime))
1✔
173
        self.assertEqual(data.value, '2006-05-24T07:00:00+00:00')
1✔
174

175
    def test_zopedatetimeinstance_in_list(self):
1✔
176
        # DateTime instance embedded in a list
177
        body = [DateTime('2006-05-24 07:00:00 GMT+0')]
1✔
178
        faux = FauxResponse()
1✔
179
        response = self._makeOne(faux)
1✔
180
        response.setBody(body)
1✔
181
        data, method = xmlrpc.client.loads(faux._body)
1✔
182
        data = data[0][0]
1✔
183
        self.assertTrue(isinstance(data, xmlrpc.client.DateTime))
1✔
184
        self.assertEqual(data.value, '2006-05-24T07:00:00+00:00')
1✔
185

186
    def test_zopedatetimeinstance_in_dict(self):
1✔
187
        # DateTime instance embedded in a dict
188
        body = {'date': DateTime('2006-05-24 07:00:00 GMT+0')}
1✔
189
        faux = FauxResponse()
1✔
190
        response = self._makeOne(faux)
1✔
191
        response.setBody(body)
1✔
192
        data, method = xmlrpc.client.loads(faux._body)
1✔
193
        data = data[0]['date']
1✔
194
        self.assertTrue(isinstance(data, xmlrpc.client.DateTime))
1✔
195
        self.assertEqual(data.value, '2006-05-24T07:00:00+00:00')
1✔
196

197
    def test_functionattribute(self):
1✔
198
        # Cannot marshal functions or methods, obviously
199

200
        def foo():
1✔
201
            pass
×
202

203
        body = FauxInstance(public=foo)
1✔
204
        faux = FauxResponse()
1✔
205
        response = self._makeOne(faux)
1✔
206
        response.setBody(body)
1✔
207
        func = xmlrpc.client.loads(faux._body)
1✔
208
        self.assertEqual(func, (({'public': {}},), None))
1✔
209

210
    def test_emptystringattribute(self):
1✔
211
        # Test an edge case: attribute name '' is possible,
212
        # at least in theory.
213
        body = FauxInstance(_secret='abc')
1✔
214
        setattr(body, '', True)
1✔
215
        faux = FauxResponse()
1✔
216
        response = self._makeOne(faux)
1✔
217
        response.setBody(body)
1✔
218
        data, method = xmlrpc.client.loads(faux._body)
1✔
219
        data = data[0]
1✔
220
        self.assertEqual(data, {'': True})
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