1 #
2 # Copyright (c) 2001 Richard Jones
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # This module is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 #
11 # $Id: test_htmltemplate.py,v 1.21 2002-09-10 00:19:54 richard Exp $
13 import unittest, cgi, time, os, shutil
15 from roundup import date, password
16 from roundup.htmltemplate import IndexTemplate, ItemTemplate
17 from roundup import template_funcs
18 tf = template_funcs
19 from roundup.i18n import _
20 from roundup.hyperdb import String, Password, Date, Interval, Link, \
21 Multilink, Boolean, Number
23 class TestClass:
24 def get(self, nodeid, attribute, default=None):
25 if attribute == 'string':
26 return 'Node %s: I am a string'%nodeid
27 elif attribute == 'filename':
28 return 'file.foo'
29 elif attribute == 'date':
30 return date.Date('2000-01-01')
31 elif attribute == 'boolean':
32 return 0
33 elif attribute == 'number':
34 return 1234
35 elif attribute == 'reldate':
36 return date.Date() + date.Interval('- 2y 1m')
37 elif attribute == 'interval':
38 return date.Interval('-3d')
39 elif attribute == 'link':
40 return '1'
41 elif attribute == 'multilink':
42 return ['1', '2']
43 elif attribute == 'password':
44 return password.Password('sekrit')
45 elif attribute == 'key':
46 return 'the key'+nodeid
47 elif attribute == 'html':
48 return '<html>hello, I am HTML</html>'
49 elif attribute == 'multiline':
50 return 'hello\nworld'
51 elif attribute == 'email':
52 return 'test@foo.domain.example'
53 def list(self):
54 return ['1', '2']
55 def filter(self, search_matches, filterspec, sort, group):
56 return ['1', '2']
57 def getprops(self):
58 return {'string': String(), 'date': Date(), 'interval': Interval(),
59 'link': Link('other'), 'multilink': Multilink('other'),
60 'password': Password(), 'html': String(), 'key': String(),
61 'novalue': String(), 'filename': String(), 'multiline': String(),
62 'reldate': Date(), 'email': String(), 'boolean': Boolean(),
63 'number': Number()}
64 def labelprop(self, default_to_id=0):
65 return 'key'
67 class TestDatabase:
68 classes = {'other': TestClass()}
69 def getclass(self, name):
70 return TestClass()
71 def __getattr(self, name):
72 return Class()
74 class TestClient:
75 def __init__(self):
76 self.db = None
77 self.form = None
78 self.write = None
80 class FunctionCase(unittest.TestCase):
81 def setUp(self):
82 ''' Set up the harness for calling the individual tests
83 '''
84 client = TestClient()
85 client.db = TestDatabase()
86 cl = TestClass()
87 self.args = (client, 'test_class', cl, cl.getprops(), '1', None)
89 def call(self, func, *args, **kws):
90 args = self.args + args
91 return func(*args, **kws)
93 # def do_plain(self, property, escape=0):
94 def testPlain_string(self):
95 s = 'Node 1: I am a string'
96 self.assertEqual(self.call(tf.do_plain, 'string'), s)
98 def testPlain_password(self):
99 self.assertEqual(self.call(tf.do_plain, 'password'), '*encrypted*')
101 def testPlain_html(self):
102 s = '<html>hello, I am HTML</html>'
103 self.assertEqual(self.call(tf.do_plain, 'html', escape=0), s)
104 s = cgi.escape(s)
105 self.assertEqual(self.call(tf.do_plain, 'html', escape=1), s)
107 def testPlain_date(self):
108 self.assertEqual(self.call(tf.do_plain, 'date'), '2000-01-01.00:00:00')
110 def testPlain_interval(self):
111 self.assertEqual(self.call(tf.do_plain, 'interval'), '- 3d')
113 def testPlain_link(self):
114 self.assertEqual(self.call(tf.do_plain, 'link'), 'the key1')
116 def testPlain_multilink(self):
117 self.assertEqual(self.call(tf.do_plain, 'multilink'), 'the key1, the key2')
119 def testPlain_boolean(self):
120 self.assertEqual(self.call(tf.do_plain, 'boolean'), 'No')
122 def testPlain_number(self):
123 self.assertEqual(self.call(tf.do_plain,'number'), '1234')
125 # def do_field(self, property, size=None, showid=0):
126 def testField_string(self):
127 self.assertEqual(self.call(tf.do_field, 'string'),
128 '<input name="string" value="Node 1: I am a string" size="30">')
129 self.assertEqual(self.call(tf.do_field, 'string', size=10),
130 '<input name="string" value="Node 1: I am a string" size="10">')
132 def testField_password(self):
133 self.assertEqual(self.call(tf.do_field, 'password'),
134 '<input type="password" name="password" size="30">')
135 self.assertEqual(self.call(tf.do_field,'password', size=10),
136 '<input type="password" name="password" size="10">')
138 def testField_html(self):
139 self.assertEqual(self.call(tf.do_field, 'html'), '<input name="html" '
140 'value="<html>hello, I am HTML</html>" size="30">')
141 self.assertEqual(self.call(tf.do_field, 'html', size=10),
142 '<input name="html" value="<html>hello, I am '
143 'HTML</html>" size="10">')
145 def testField_date(self):
146 self.assertEqual(self.call(tf.do_field, 'date'),
147 '<input name="date" value="2000-01-01.00:00:00" size="30">')
148 self.assertEqual(self.call(tf.do_field, 'date', size=10),
149 '<input name="date" value="2000-01-01.00:00:00" size="10">')
151 def testField_interval(self):
152 self.assertEqual(self.call(tf.do_field,'interval'),
153 '<input name="interval" value="- 3d" size="30">')
154 self.assertEqual(self.call(tf.do_field, 'interval', size=10),
155 '<input name="interval" value="- 3d" size="10">')
157 def testField_link(self):
158 self.assertEqual(self.call(tf.do_field, 'link'), '''<select name="link">
159 <option value="-1">- no selection -</option>
160 <option selected value="1">the key1</option>
161 <option value="2">the key2</option>
162 </select>''')
164 def testField_multilink(self):
165 self.assertEqual(self.call(tf.do_field,'multilink'),
166 '<input name="multilink" size="30" value="the key1,the key2">')
167 self.assertEqual(self.call(tf.do_field, 'multilink', size=10),
168 '<input name="multilink" size="10" value="the key1,the key2">')
170 def testField_boolean(self):
171 self.assertEqual(self.call(tf.do_field, 'boolean'),
172 '<input type="radio" name="boolean" value="yes" >Yes<input type="radio" name="boolean" value="no" checked>No')
174 def testField_number(self):
175 self.assertEqual(self.call(tf.do_field, 'number'),
176 '<input name="number" value="1234" size="30">')
177 self.assertEqual(self.call(tf.do_field, 'number', size=10),
178 '<input name="number" value="1234" size="10">')
180 # def do_multiline(self, property, rows=5, cols=40)
181 def testMultiline_string(self):
182 self.assertEqual(self.call(tf.do_multiline, 'multiline'),
183 '<textarea name="multiline" rows="5" cols="40">'
184 'hello\nworld</textarea>')
185 self.assertEqual(self.call(tf.do_multiline, 'multiline', rows=10),
186 '<textarea name="multiline" rows="10" cols="40">'
187 'hello\nworld</textarea>')
188 self.assertEqual(self.call(tf.do_multiline, 'multiline', cols=10),
189 '<textarea name="multiline" rows="5" cols="10">'
190 'hello\nworld</textarea>')
192 def testMultiline_nonstring(self):
193 s = _('[Multiline: not a string]')
194 self.assertEqual(self.call(tf.do_multiline, 'date'), s)
195 self.assertEqual(self.call(tf.do_multiline, 'interval'), s)
196 self.assertEqual(self.call(tf.do_multiline, 'password'), s)
197 self.assertEqual(self.call(tf.do_multiline, 'link'), s)
198 self.assertEqual(self.call(tf.do_multiline, 'multilink'), s)
199 self.assertEqual(self.call(tf.do_multiline, 'boolean'), s)
200 self.assertEqual(self.call(tf.do_multiline, 'number'), s)
202 # def do_menu(self, property, size=None, height=None, showid=0):
203 def testMenu_nonlinks(self):
204 s = _('[Menu: not a link]')
205 self.assertEqual(self.call(tf.do_menu, 'string'), s)
206 self.assertEqual(self.call(tf.do_menu, 'date'), s)
207 self.assertEqual(self.call(tf.do_menu, 'interval'), s)
208 self.assertEqual(self.call(tf.do_menu, 'password'), s)
209 self.assertEqual(self.call(tf.do_menu, 'boolean'), s)
210 self.assertEqual(self.call(tf.do_menu, 'number'), s)
212 def testMenu_link(self):
213 self.assertEqual(self.call(tf.do_menu, 'link'), '''<select name="link">
214 <option value="-1">- no selection -</option>
215 <option selected value="1">the key1</option>
216 <option value="2">the key2</option>
217 </select>''')
218 self.assertEqual(self.call(tf.do_menu, 'link', size=6),
219 '''<select name="link">
220 <option value="-1">- no selection -</option>
221 <option selected value="1">the...</option>
222 <option value="2">the...</option>
223 </select>''')
224 self.assertEqual(self.call(tf.do_menu, 'link', showid=1),
225 '''<select name="link">
226 <option value="-1">- no selection -</option>
227 <option selected value="1">other1: the key1</option>
228 <option value="2">other2: the key2</option>
229 </select>''')
231 def testMenu_multilink(self):
232 self.assertEqual(self.call(tf.do_menu, 'multilink', height=10),
233 '''<select multiple name="multilink" size="10">
234 <option selected value="1">the key1</option>
235 <option selected value="2">the key2</option>
236 </select>''')
237 self.assertEqual(self.call(tf.do_menu, 'multilink', size=6, height=10),
238 '''<select multiple name="multilink" size="10">
239 <option selected value="1">the...</option>
240 <option selected value="2">the...</option>
241 </select>''')
242 self.assertEqual(self.call(tf.do_menu, 'multilink', showid=1),
243 '''<select multiple name="multilink" size="2">
244 <option selected value="1">other1: the key1</option>
245 <option selected value="2">other2: the key2</option>
246 </select>''')
248 # def do_link(self, property=None, is_download=0):
249 def testLink_novalue(self):
250 self.assertEqual(self.call(tf.do_link, 'novalue'),
251 _('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
253 def testLink_string(self):
254 self.assertEqual(self.call(tf.do_link, 'string'),
255 '<a href="test_class1">Node 1: I am a string</a>')
257 def testLink_file(self):
258 self.assertEqual(self.call(tf.do_link, 'filename', is_download=1),
259 '<a href="test_class1/file.foo">file.foo</a>')
261 def testLink_date(self):
262 self.assertEqual(self.call(tf.do_link, 'date'),
263 '<a href="test_class1">2000-01-01.00:00:00</a>')
265 def testLink_interval(self):
266 self.assertEqual(self.call(tf.do_link, 'interval'),
267 '<a href="test_class1">- 3d</a>')
269 def testLink_link(self):
270 self.assertEqual(self.call(tf.do_link, 'link'),
271 '<a href="other1">the key1</a>')
273 def testLink_link_id(self):
274 self.assertEqual(self.call(tf.do_link, 'link', showid=1),
275 '<a href="other1" title="the key1">1</a>')
277 def testLink_multilink(self):
278 self.assertEqual(self.call(tf.do_link, 'multilink'),
279 '<a href="other1">the key1</a>, <a href="other2">the key2</a>')
281 def testLink_multilink_id(self):
282 self.assertEqual(self.call(tf.do_link, 'multilink', showid=1),
283 '<a href="other1" title="the key1">1</a>, <a href="other2" title="the key2">2</a>')
285 def testLink_boolean(self):
286 self.assertEqual(self.call(tf.do_link, 'boolean'),
287 '<a href="test_class1">No</a>')
289 def testLink_number(self):
290 self.assertEqual(self.call(tf.do_link, 'number'),
291 '<a href="test_class1">1234</a>')
293 # def do_count(self, property, **args):
294 def testCount_nonlinks(self):
295 s = _('[Count: not a Multilink]')
296 self.assertEqual(self.call(tf.do_count, 'string'), s)
297 self.assertEqual(self.call(tf.do_count, 'date'), s)
298 self.assertEqual(self.call(tf.do_count, 'interval'), s)
299 self.assertEqual(self.call(tf.do_count, 'password'), s)
300 self.assertEqual(self.call(tf.do_count, 'link'), s)
301 self.assertEqual(self.call(tf.do_count, 'boolean'), s)
302 self.assertEqual(self.call(tf.do_count, 'number'), s)
304 def testCount_multilink(self):
305 self.assertEqual(self.call(tf.do_count, 'multilink'), '2')
307 # def do_reldate(self, property, pretty=0):
308 def testReldate_nondate(self):
309 s = _('[Reldate: not a Date]')
310 self.assertEqual(self.call(tf.do_reldate, 'string'), s)
311 self.assertEqual(self.call(tf.do_reldate, 'interval'), s)
312 self.assertEqual(self.call(tf.do_reldate, 'password'), s)
313 self.assertEqual(self.call(tf.do_reldate, 'link'), s)
314 self.assertEqual(self.call(tf.do_reldate, 'multilink'), s)
315 self.assertEqual(self.call(tf.do_reldate, 'boolean'), s)
316 self.assertEqual(self.call(tf.do_reldate, 'number'), s)
318 def testReldate_date(self):
319 self.assertEqual(self.call(tf.do_reldate, 'reldate'), '- 2y 1m')
320 interval = date.Interval('- 2y 1m')
321 self.assertEqual(self.call(tf.do_reldate, 'reldate', pretty=1),
322 interval.pretty())
324 # def do_download(self, property):
325 def testDownload_novalue(self):
326 self.assertEqual(self.call(tf.do_download, 'novalue'),
327 _('[no %(propname)s]')%{'propname':'novalue'.capitalize()})
329 def testDownload_string(self):
330 self.assertEqual(self.call(tf.do_download, 'string'),
331 '<a href="test_class1/Node 1: I am a string">Node 1: '
332 'I am a string</a>')
334 def testDownload_file(self):
335 self.assertEqual(self.call(tf.do_download, 'filename', is_download=1),
336 '<a href="test_class1/file.foo">file.foo</a>')
338 def testDownload_date(self):
339 self.assertEqual(self.call(tf.do_download, 'date'),
340 '<a href="test_class1/2000-01-01.00:00:00">2000-01-01.00:00:00</a>')
342 def testDownload_interval(self):
343 self.assertEqual(self.call(tf.do_download, 'interval'),
344 '<a href="test_class1/- 3d">- 3d</a>')
346 def testDownload_link(self):
347 self.assertEqual(self.call(tf.do_download, 'link'),
348 '<a href="other1/the key1">the key1</a>')
350 def testDownload_multilink(self):
351 self.assertEqual(self.call(tf.do_download, 'multilink'),
352 '<a href="other1/the key1">the key1</a>, '
353 '<a href="other2/the key2">the key2</a>')
355 def testDownload_boolean(self):
356 self.assertEqual(self.call(tf.do_download, 'boolean'),
357 '<a href="test_class1/No">No</a>')
359 def testDownload_number(self):
360 self.assertEqual(self.call(tf.do_download, 'number'),
361 '<a href="test_class1/1234">1234</a>')
363 # def do_checklist(self, property, reverse=0):
364 def testChecklist_nonlinks(self):
365 s = _('[Checklist: not a link]')
366 self.assertEqual(self.call(tf.do_checklist, 'string'), s)
367 self.assertEqual(self.call(tf.do_checklist, 'date'), s)
368 self.assertEqual(self.call(tf.do_checklist, 'interval'), s)
369 self.assertEqual(self.call(tf.do_checklist, 'password'), s)
370 self.assertEqual(self.call(tf.do_checklist, 'boolean'), s)
371 self.assertEqual(self.call(tf.do_checklist, 'number'), s)
373 def testChecklstk_link(self):
374 self.assertEqual(self.call(tf.do_checklist, 'link'),
375 '''the key1:<input type="checkbox" checked name="link" value="the key1">
376 the key2:<input type="checkbox" name="link" value="the key2">
377 [unselected]:<input type="checkbox" name="link" value="-1">''')
379 def testChecklink_multilink(self):
380 self.assertEqual(self.call(tf.do_checklist, 'multilink'),
381 '''the key1:<input type="checkbox" checked name="multilink" value="the key1">
382 the key2:<input type="checkbox" checked name="multilink" value="the key2">''')
384 # def do_note(self, rows=5, cols=80):
385 def testNote(self):
386 self.assertEqual(self.call(tf.do_note), '<textarea name="__note" '
387 'wrap="hard" rows=5 cols=80></textarea>')
389 # def do_list(self, property, reverse=0):
390 def testList_nonlinks(self):
391 s = _('[List: not a Multilink]')
392 self.assertEqual(self.call(tf.do_list, 'string'), s)
393 self.assertEqual(self.call(tf.do_list, 'date'), s)
394 self.assertEqual(self.call(tf.do_list, 'interval'), s)
395 self.assertEqual(self.call(tf.do_list, 'password'), s)
396 self.assertEqual(self.call(tf.do_list, 'link'), s)
397 self.assertEqual(self.call(tf.do_list, 'boolean'), s)
398 self.assertEqual(self.call(tf.do_list, 'number'), s)
400 def testList_multilink(self):
401 # TODO: test this (needs to have lots and lots of support!
402 #self.assertEqual(self.tf.do_list('multilink'),'')
403 pass
405 def testClasshelp(self):
406 self.assertEqual(self.call(tf.do_classhelp, 'theclass', 'prop1,prop2'),
407 '<a href="javascript:help_window(\'classhelp?classname=theclass'
408 '&properties=prop1,prop2\', \'400\', \'400\')"><b>(?)</b></a>')
410 # def do_email(self, property, rows=5, cols=40)
411 def testEmail_string(self):
412 self.assertEqual(self.call(tf.do_email, 'email'), 'test at foo domain example')
414 def testEmail_nonstring(self):
415 s = _('[Email: not a string]')
416 self.assertEqual(self.call(tf.do_email, 'date'), s)
417 self.assertEqual(self.call(tf.do_email, 'interval'), s)
418 self.assertEqual(self.call(tf.do_email, 'password'), s)
419 self.assertEqual(self.call(tf.do_email, 'link'), s)
420 self.assertEqual(self.call(tf.do_email, 'multilink'), s)
421 self.assertEqual(self.call(tf.do_email, 'boolean'), s)
422 self.assertEqual(self.call(tf.do_email, 'number'), s)
425 from test_db import setupSchema, MyTestCase, config
427 class Client:
428 user = 'admin'
430 class IndexTemplateCase(unittest.TestCase):
431 def setUp(self):
432 from roundup.backends import anydbm
433 # remove previous test, ignore errors
434 if os.path.exists(config.DATABASE):
435 shutil.rmtree(config.DATABASE)
436 os.makedirs(config.DATABASE + '/files')
437 self.db = anydbm.Database(config, 'test')
438 setupSchema(self.db, 1, anydbm)
440 client = Client()
441 client.db = self.db
442 client.instance = None
443 self.tf = tf = IndexTemplate(client, '', 'issue')
444 tf.props = ['title']
446 # admin user
447 self.db.user.create(username="admin", roles='Admin')
448 self.db.user.create(username="anonymous", roles='User')
450 def testBasic(self):
451 self.assertEqual(self.tf.execute_template('hello'), 'hello')
453 def testValue(self):
454 self.tf.nodeid = self.db.issue.create(title="spam", status='1')
455 self.assertEqual(self.tf.execute_template('<display call="plain(\'title\')">'), 'spam')
457 def testColumnSelection(self):
458 self.tf.nodeid = self.db.issue.create(title="spam", status='1')
459 self.assertEqual(self.tf.execute_template('<property name="title">'
460 '<display call="plain(\'title\')"></property>'
461 '<property name="bar">hello</property>'), 'spam')
462 self.tf.props = ['bar']
463 self.assertEqual(self.tf.execute_template('<property name="title">'
464 '<display call="plain(\'title\')"></property>'
465 '<property name="bar">hello</property>'), 'hello')
467 def testSecurityPass(self):
468 self.assertEqual(self.tf.execute_template(
469 '<require permission="Edit">hello<else>foo</require>'), 'hello')
471 def testSecurityPassValue(self):
472 self.tf.nodeid = self.db.issue.create(title="spam", status='1')
473 self.assertEqual(self.tf.execute_template(
474 '<require permission="Edit">'
475 '<display call="plain(\'title\')">'
476 '<else>not allowed</require>'), 'spam')
478 def testSecurityFail(self):
479 self.tf.client.user = 'anonymous'
480 self.assertEqual(self.tf.execute_template(
481 '<require permission="Edit">hello<else>foo</require>'), 'foo')
483 def testSecurityFailValue(self):
484 self.tf.nodeid = self.db.issue.create(title="spam", status='1')
485 self.tf.client.user = 'anonymous'
486 self.assertEqual(self.tf.execute_template(
487 '<require permission="Edit">allowed<else>'
488 '<display call="plain(\'title\')"></require>'), 'spam')
490 def tearDown(self):
491 if os.path.exists('_test_dir'):
492 shutil.rmtree('_test_dir')
495 class ItemTemplateCase(unittest.TestCase):
496 def setUp(self):
497 ''' Set up the harness for calling the individual tests
498 '''
499 from roundup.backends import anydbm
500 # remove previous test, ignore errors
501 if os.path.exists(config.DATABASE):
502 shutil.rmtree(config.DATABASE)
503 os.makedirs(config.DATABASE + '/files')
504 self.db = anydbm.Database(config, 'test')
505 setupSchema(self.db, 1, anydbm)
507 client = Client()
508 client.db = self.db
509 client.instance = None
510 self.tf = tf = IndexTemplate(client, '', 'issue')
511 tf.nodeid = self.db.issue.create(title="spam", status='1')
513 # admin user
514 self.db.user.create(username="admin", roles='Admin')
515 self.db.user.create(username="anonymous", roles='User')
517 def testBasic(self):
518 self.assertEqual(self.tf.execute_template('hello'), 'hello')
520 def testValue(self):
521 self.assertEqual(self.tf.execute_template('<display call="plain(\'title\')">'), 'spam')
523 def testSecurityPass(self):
524 self.assertEqual(self.tf.execute_template(
525 '<require permission="Edit">hello<else>foo</require>'), 'hello')
527 def testSecurityPassValue(self):
528 self.assertEqual(self.tf.execute_template(
529 '<require permission="Edit">'
530 '<display call="plain(\'title\')">'
531 '<else>not allowed</require>'), 'spam')
533 def testSecurityFail(self):
534 self.tf.client.user = 'anonymous'
535 self.assertEqual(self.tf.execute_template(
536 '<require permission="Edit">hello<else>foo</require>'), 'foo')
538 def testSecurityFailValue(self):
539 self.tf.client.user = 'anonymous'
540 self.assertEqual(self.tf.execute_template(
541 '<require permission="Edit">allowed<else>'
542 '<display call="plain(\'title\')"></require>'), 'spam')
544 def tearDown(self):
545 if os.path.exists('_test_dir'):
546 shutil.rmtree('_test_dir')
548 def suite():
549 return unittest.TestSuite([
550 unittest.makeSuite(FunctionCase, 'test'),
551 #unittest.makeSuite(IndexTemplateCase, 'test'),
552 #unittest.makeSuite(ItemTemplateCase, 'test'),
553 ])
556 # vim: set filetype=python ts=4 sw=4 et si