vendor/Five/1.2b-r20590

view browser/tests/pages.txt @ 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 Test browser pages
2 ==================
4 Let's register a quite large amount of test pages:
6 >>> import Products.Five.browser.tests
7 >>> from Products.Five import zcml
8 >>> zcml.load_config("configure.zcml", Products.Five)
9 >>> zcml.load_config('pages.zcml', package=Products.Five.browser.tests)
11 Let's add a test object that we view most of the pages off of:
13 >>> from Products.Five.tests.testing.simplecontent import manage_addSimpleContent
14 >>> manage_addSimpleContent(self.folder, 'testoid', 'Testoid')
16 We also need to create a stub user account and login; otherwise we
17 wouldn't have all the rights to do traversal etc.:
19 >>> uf = self.folder.acl_users
20 >>> uf._doAddUser('manager', 'r00t', ['Manager'], [])
21 >>> self.login('manager')
23 Now for some actual testing...
26 Simple pages
27 ------------
29 A browser page that is a view class's attribute (method):
31 >>> view = self.folder.unrestrictedTraverse('testoid/eagle.txt')
32 >>> view is not None
33 True
34 >>> from Products.Five.browser.tests.pages import SimpleView
35 >>> isinstance(view, SimpleView)
36 True
37 >>> view()
38 'The eagle has landed'
40 A browser page that is a Page Template.
42 >>> view = self.folder.unrestrictedTraverse('testoid/owl.html')
43 >>> view()
44 '<p>2</p>\n'
46 A browser page that is a PageTemplate plus a view class:
48 >>> view = self.folder.unrestrictedTraverse('testoid/falcon.html')
49 >>> isinstance(view, SimpleView)
50 True
51 >>> view()
52 '<p>The falcon has taken flight</p>\n'
54 Test pages that have been registered through the cumulative
55 <browser:pages> directive:
57 >>> view = self.folder.unrestrictedTraverse('testoid/eagle-page.txt')
58 >>> isinstance(view, SimpleView)
59 True
60 >>> view()
61 'The eagle has landed'
63 >>> view = self.folder.unrestrictedTraverse('testoid/mouse-page.txt')
64 >>> isinstance(view, SimpleView)
65 True
66 >>> view()
67 'The mouse has been eaten by the eagle'
69 Zope 2 objects always need a docstring in order to be published. Five
70 adds a docstring automatically if a view method doesn't have it, but
71 it shouldn't modify existing ones:
73 >>> view = self.folder.unrestrictedTraverse('testoid/eagle.txt')
74 >>> view.eagle.__doc__ == SimpleView.eagle.__doc__
75 True
77 Test whether new-style classes are ignored when registering browser
78 pages with view classes. When traversing for a non-existing view, we
79 should get an AttributeError:
81 >>> self.folder.unrestrictedTraverse('testoid/@@new_style_class')
82 Traceback (most recent call last):
83 ...
84 AttributeError: @@new_style_class
87 ZPT-based browser pages
88 -----------------------
90 Test access to ``context`` from ZPTs:
92 >>> view = self.folder.unrestrictedTraverse('testoid/flamingo.html')
93 >>> print view()
94 <p>Hello world</p>
95 <p>Hello world</p>
97 Test macro access from ZPT pages:
99 >>> view = self.folder.unrestrictedTraverse('testoid/seagull.html')
100 >>> view()
101 '<html><head><title>bird macro</title></head><body>Color: gray</body></html>\n'
103 Test whether old-style direct traversal still works with a
104 five:traversable class:
106 >>> old_view = self.folder.unrestrictedTraverse('testoid/direct')
107 >>> old_view()
108 'Direct traversal worked'
110 test_zpt_things:
112 >>> view = self.folder.unrestrictedTraverse('testoid/condor.html')
113 >>> print view()
114 <p>Hello world</p>
115 <p>The eagle has landed</p>
116 <p>Hello world</p>
117 <p>Hello world</p>
119 Make sure that tal:repeat works in ZPT browser pages:
121 >>> view = self.folder.unrestrictedTraverse('testoid/ostrich.html')
122 >>> print view()
123 <ul>
124 <li>Alpha</li>
125 <li>Beta</li>
126 <li>Gamma</li>
127 </ul>
128 <ul>
129 <li>0</li>
130 <li>1</li>
131 <li>2</li>
132 </ul>
134 Test TALES traversal in ZPT pages:
136 >>> view = self.folder.unrestrictedTraverse('testoid/tales_traversal.html')
137 >>> print view()
138 <p>testoid</p>
139 <p>test_folder_1_</p>
141 Make sure that global template variables in ZPT pages are correct:
143 >>> view = self.folder.unrestrictedTraverse('testoid/template_variables.html')
144 >>> print view()
145 View is a view: True
146 Context is testoid: True
147 Contaxt.aq_parent is test_folder_1_: True
148 Container is context: True
149 Here is context: True
150 Nothing is None: True
151 Default works: True
152 Root is the application: True
153 Template is a template: True
154 Traverse_subpath exists and is empty: True
155 Request is a request: True
156 User is manager: True
157 Options exist: True
158 Attrs exist: True
159 Repeat exists: True
160 Loop exists: True
161 Modules exists: True
163 Make sure that ZPT's aren't a security-less zone. Let's logout and
164 try to access some protected stuff. Let's not forgot to login again,
165 of course:
167 >>> from AccessControl import allow_module
168 >>> allow_module('smtpd')
169 >>> self.logout()
170 >>> view = self.folder.unrestrictedTraverse('testoid/security.html')
171 >>> print view()
172 <div>NoneType</div>
173 <div>smtpd</div>
174 >>> self.login('manager')
176 Test pages registered through the <five:pagesFromDirectory /> directive:
178 >>> view = self.folder.unrestrictedTraverse('testoid/dirpage1')
179 >>> print view()
180 <html>
181 <p>This is page 1</p>
182 </html>
184 >>> view = self.folder.unrestrictedTraverse('testoid/dirpage2')
185 >>> print view()
186 <html>
187 <p>This is page 2</p>
188 </html>
191 Low-level security
192 ------------------
194 This tests security on a low level (functional pages test has
195 high-level security tests). Let's manually look up a protected view:
197 >>> from Products.Five.traversable import FakeRequest
198 >>> from zope.app import zapi
199 >>> request = FakeRequest()
200 >>> view = zapi.getView(self.folder.testoid, 'eagle.txt', request)
202 It's protecting the object with the permission, and not the attribute,
203 so we get ('',) instead of ('eagle',):
205 >>> getattr(view, '__ac_permissions__')
206 (('View management screens', ('',)),)
208 Wrap into an acquisition so that imPermissionRole objects can be
209 evaluated. __roles__ is a imPermissionRole object:
211 >>> view = view.__of__(self.folder.testoid)
212 >>> view_roles = getattr(view, '__roles__', None)
213 >>> view_roles
214 ('Manager',)
216 Check to see if view's context properly acquires its true
217 parent
219 >>> from Acquisition import aq_parent, aq_base, aq_inner
220 >>> context = getattr(view, 'context')
222 Check the wrapper type
224 >>> from Acquisition import ImplicitAcquisitionWrapper
225 >>> type(context) == ImplicitAcquisitionWrapper
226 True
228 The acquired parent is the view. This isn't
229 usually what you want.
231 >>> aq_parent(context) == view
232 True
234 To get what you usually want, do this
236 >>> context.aq_inner.aq_parent
237 <Folder at /test_folder_1_>
239 C methods work the same
241 >>> aq_parent(aq_inner(context))
242 <Folder at /test_folder_1_>
244 High-level security
245 -------------------
247 >>> protected_view_names = [
248 ... 'eagle.txt', 'falcon.html', 'owl.html', 'flamingo.html',
249 ... 'condor.html', 'protectededitform.html']
250 >>>
251 >>> public_view_names = [
252 ... 'public_attribute_page',
253 ... 'public_template_page',
254 ... 'public_template_class_page',
255 ... 'nodoc-method', 'nodoc-function', 'nodoc-object',
256 ... 'dirpage1', 'dirpage2']
258 >>> from Products.Five.tests.testing.restricted import checkRestricted
259 >>> from Products.Five.tests.testing.restricted import checkUnauthorized
261 As long as we're not authenticated, we should get Unauthorized for
262 protected views, but we should be able to view the public ones:
264 >>> self.logout()
265 >>> for view_name in protected_view_names:
266 ... checkUnauthorized(
267 ... self.folder,
268 ... 'context.restrictedTraverse("testoid/%s")()' % view_name)
270 >>> for view_name in public_view_names:
271 ... checkRestricted(
272 ... self.folder,
273 ... 'context.restrictedTraverse("testoid/%s")()' % view_name)
274 >>> self.login('manager')
276 Being logged in as a manager again, we find that the protected pages
277 are not accessible to us:
279 >>> for view_name in protected_view_names:
280 ... checkRestricted(
281 ... self.folder,
282 ... 'context.restrictedTraverse("testoid/%s")()' % view_name)
284 >>> checkRestricted(
285 ... self.folder,
286 ... 'context.restrictedTraverse("testoid/eagle.method").eagle()')
289 Other
290 -----
292 Make sure that browser pages can be overridden:
294 >>> zcml.load_string('''
295 ... <includeOverrides
296 ... package="Products.Five.browser.tests"
297 ... file="overrides.zcml" />
298 ... ''')
299 >>> view = self.folder.unrestrictedTraverse('testoid/overridden_view')
300 >>> view()
301 'The mouse has been eaten by the eagle'
303 Test traversal to resources from within ZPT pages:
305 >>> zcml.load_config('resource.zcml', package=Products.Five.browser.tests)
306 >>> view = self.folder.unrestrictedTraverse('testoid/parakeet.html')
307 >>> print view()
308 <html><body><img alt=""
309 src="http://nohost/test_folder_1_/testoid/++resource++pattern.png" /></body></html>
312 Clean up
313 --------
315 >>> from zope.app.tests.placelesssetup import tearDown
316 >>> tearDown()