comparison test/test_htmltemplate.py @ 902:b0d3d3535998

Bugger it. Here's the current shape of the new security implementation. Still to do: . call the security funcs from cgi and mailgw . change shipped templates to include correct initialisation and remove the old config vars ... that seems like a lot. The bulk of the work has been done though. Honest :)
author Richard Jones <richard@users.sourceforge.net>
date Thu, 25 Jul 2002 07:14:06 +0000
parents a568596dbea7
children 502a5ae11cc5
comparison
equal deleted inserted replaced
901:31a62bcb9c80 902:b0d3d3535998
6 # 6 #
7 # This module is distributed in the hope that it will be useful, 7 # This module is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of 8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # 10 #
11 # $Id: test_htmltemplate.py,v 1.17 2002-07-18 23:07:07 richard Exp $ 11 # $Id: test_htmltemplate.py,v 1.18 2002-07-25 07:14:06 richard Exp $
12 12
13 import unittest, cgi, time 13 import unittest, cgi, time, os, shutil
14 14
15 from roundup import date, password 15 from roundup import date, password
16 from roundup.htmltemplate import TemplateFunctions 16 from roundup.htmltemplate import TemplateFunctions, IndexTemplate, ItemTemplate
17 from roundup.i18n import _ 17 from roundup.i18n import _
18 from roundup.hyperdb import String, Password, Date, Interval, Link, \ 18 from roundup.hyperdb import String, Password, Date, Interval, Link, \
19 Multilink, Boolean, Number 19 Multilink, Boolean, Number
20 20
21 class Class: 21 class TestClass:
22 def get(self, nodeid, attribute, default=None): 22 def get(self, nodeid, attribute, default=None):
23 if attribute == 'string': 23 if attribute == 'string':
24 return 'Node %s: I am a string'%nodeid 24 return 'Node %s: I am a string'%nodeid
25 elif attribute == 'filename': 25 elif attribute == 'filename':
26 return 'file.foo' 26 return 'file.foo'
60 'reldate': Date(), 'email': String(), 'boolean': Boolean(), 60 'reldate': Date(), 'email': String(), 'boolean': Boolean(),
61 'number': Number()} 61 'number': Number()}
62 def labelprop(self, default_to_id=0): 62 def labelprop(self, default_to_id=0):
63 return 'key' 63 return 'key'
64 64
65 class Database: 65 class TestDatabase:
66 classes = {'other': Class()} 66 classes = {'other': TestClass()}
67 def getclass(self, name): 67 def getclass(self, name):
68 return Class() 68 return Class()
69 def __getattr(self, name): 69 def __getattr(self, name):
70 return Class() 70 return Class()
71 71
72 class Client: 72 class FunctionCase(unittest.TestCase):
73 write = None
74
75 class NodeCase(unittest.TestCase):
76 def setUp(self): 73 def setUp(self):
77 ''' Set up the harness for calling the individual tests 74 ''' Set up the harness for calling the individual tests
78 ''' 75 '''
79 self.tf = tf = TemplateFunctions() 76 self.tf = tf = TemplateFunctions()
80 tf.nodeid = '1' 77 tf.nodeid = '1'
81 tf.cl = Class() 78 tf.cl = TestClass()
82 tf.classname = 'test_class' 79 tf.classname = 'test_class'
83 tf.properties = tf.cl.getprops() 80 tf.properties = tf.cl.getprops()
84 tf.db = Database() 81 tf.db = TestDatabase()
85 82
86 # def do_plain(self, property, escape=0): 83 # def do_plain(self, property, escape=0):
87 def testPlain_string(self): 84 def testPlain_string(self):
88 s = 'Node 1: I am a string' 85 s = 'Node 1: I am a string'
89 self.assertEqual(self.tf.do_plain('string'), s) 86 self.assertEqual(self.tf.do_plain('string'), s)
398 def testClasshelp(self): 395 def testClasshelp(self):
399 self.assertEqual(self.tf.do_classhelp('theclass', 'prop1,prop2'), 396 self.assertEqual(self.tf.do_classhelp('theclass', 'prop1,prop2'),
400 '<a href="javascript:help_window(\'classhelp?classname=theclass' 397 '<a href="javascript:help_window(\'classhelp?classname=theclass'
401 '&properties=prop1,prop2\', \'400\', \'400\')"><b>(?)</b></a>') 398 '&properties=prop1,prop2\', \'400\', \'400\')"><b>(?)</b></a>')
402 399
403 # def do_multiline(self, property, rows=5, cols=40) 400 # def do_email(self, property, rows=5, cols=40)
404 def testEmail_string(self): 401 def testEmail_string(self):
405 self.assertEqual(self.tf.do_email('email'), 'test at foo domain example') 402 self.assertEqual(self.tf.do_email('email'), 'test at foo domain example')
406 403
407 def testEmail_nonstring(self): 404 def testEmail_nonstring(self):
408 s = _('[Email: not a string]') 405 s = _('[Email: not a string]')
412 self.assertEqual(self.tf.do_email('link'), s) 409 self.assertEqual(self.tf.do_email('link'), s)
413 self.assertEqual(self.tf.do_email('multilink'), s) 410 self.assertEqual(self.tf.do_email('multilink'), s)
414 self.assertEqual(self.tf.do_email('boolean'), s) 411 self.assertEqual(self.tf.do_email('boolean'), s)
415 self.assertEqual(self.tf.do_email('number'), s) 412 self.assertEqual(self.tf.do_email('number'), s)
416 413
414
415 from test_db import setupSchema, MyTestCase, config
416
417 class Client:
418 user = 'admin'
419
420 class IndexTemplateCase(unittest.TestCase):
421 def setUp(self):
422 from roundup.backends import anydbm
423 # remove previous test, ignore errors
424 if os.path.exists(config.DATABASE):
425 shutil.rmtree(config.DATABASE)
426 os.makedirs(config.DATABASE + '/files')
427 self.db = anydbm.Database(config, 'test')
428 setupSchema(self.db, 1, anydbm)
429
430 client = Client()
431 client.db = self.db
432 client.instance = None
433 self.tf = tf = IndexTemplate(client, '', 'issue')
434 tf.props = ['title']
435
436 # admin user
437 r = str(self.db.role.lookup('Admin'))
438 self.db.user.create(username="admin", roles=[r])
439 r = str(self.db.role.lookup('User'))
440 self.db.user.create(username="anonymous", roles=[r])
441
442 def testBasic(self):
443 self.assertEqual(self.tf.execute_template('hello'), 'hello')
444
445 def testValue(self):
446 self.tf.nodeid = self.db.issue.create(title="spam", status='1')
447 self.assertEqual(self.tf.execute_template('<display call="plain(\'title\')">'), 'spam')
448
449 def testColumnSelection(self):
450 self.tf.nodeid = self.db.issue.create(title="spam", status='1')
451 self.assertEqual(self.tf.execute_template('<property name="title">'
452 '<display call="plain(\'title\')"></property>'
453 '<property name="bar">hello</property>'), 'spam')
454 self.tf.props = ['bar']
455 self.assertEqual(self.tf.execute_template('<property name="title">'
456 '<display call="plain(\'title\')"></property>'
457 '<property name="bar">hello</property>'), 'hello')
458
459 def testSecurityPass(self):
460 self.assertEqual(self.tf.execute_template(
461 '<require permission="Edit">hello<else>foo</require>'), 'hello')
462
463 def testSecurityPassValue(self):
464 self.tf.nodeid = self.db.issue.create(title="spam", status='1')
465 self.assertEqual(self.tf.execute_template(
466 '<require permission="Edit">'
467 '<display call="plain(\'title\')">'
468 '<else>not allowed</require>'), 'spam')
469
470 def testSecurityFail(self):
471 self.tf.client.user = 'anonymous'
472 self.assertEqual(self.tf.execute_template(
473 '<require permission="Edit">hello<else>foo</require>'), 'foo')
474
475 def testSecurityFailValue(self):
476 self.tf.nodeid = self.db.issue.create(title="spam", status='1')
477 self.tf.client.user = 'anonymous'
478 self.assertEqual(self.tf.execute_template(
479 '<require permission="Edit">allowed<else>'
480 '<display call="plain(\'title\')"></require>'), 'spam')
481
482 def tearDown(self):
483 if os.path.exists('_test_dir'):
484 shutil.rmtree('_test_dir')
485
486
487 class ItemTemplateCase(unittest.TestCase):
488 def setUp(self):
489 ''' Set up the harness for calling the individual tests
490 '''
491 from roundup.backends import anydbm
492 # remove previous test, ignore errors
493 if os.path.exists(config.DATABASE):
494 shutil.rmtree(config.DATABASE)
495 os.makedirs(config.DATABASE + '/files')
496 self.db = anydbm.Database(config, 'test')
497 setupSchema(self.db, 1, anydbm)
498
499 client = Client()
500 client.db = self.db
501 client.instance = None
502 self.tf = tf = IndexTemplate(client, '', 'issue')
503 tf.nodeid = self.db.issue.create(title="spam", status='1')
504
505 # admin user
506 r = str(self.db.role.lookup('Admin'))
507 self.db.user.create(username="admin", roles=[r])
508 r = str(self.db.role.lookup('User'))
509 self.db.user.create(username="anonymous", roles=[r])
510
511 def testBasic(self):
512 self.assertEqual(self.tf.execute_template('hello'), 'hello')
513
514 def testValue(self):
515 self.assertEqual(self.tf.execute_template('<display call="plain(\'title\')">'), 'spam')
516
517 def testSecurityPass(self):
518 self.assertEqual(self.tf.execute_template(
519 '<require permission="Edit">hello<else>foo</require>'), 'hello')
520
521 def testSecurityPassValue(self):
522 self.assertEqual(self.tf.execute_template(
523 '<require permission="Edit">'
524 '<display call="plain(\'title\')">'
525 '<else>not allowed</require>'), 'spam')
526
527 def testSecurityFail(self):
528 self.tf.client.user = 'anonymous'
529 self.assertEqual(self.tf.execute_template(
530 '<require permission="Edit">hello<else>foo</require>'), 'foo')
531
532 def testSecurityFailValue(self):
533 self.tf.client.user = 'anonymous'
534 self.assertEqual(self.tf.execute_template(
535 '<require permission="Edit">allowed<else>'
536 '<display call="plain(\'title\')"></require>'), 'spam')
537
538 def tearDown(self):
539 if os.path.exists('_test_dir'):
540 shutil.rmtree('_test_dir')
541
417 def suite(): 542 def suite():
418 return unittest.makeSuite(NodeCase, 'test') 543 return unittest.TestSuite([
544 unittest.makeSuite(FunctionCase, 'test'),
545 unittest.makeSuite(IndexTemplateCase, 'test'),
546 unittest.makeSuite(ItemTemplateCase, 'test'),
547 ])
419 548
420 549
421 # 550 #
422 # $Log: not supported by cvs2svn $ 551 # $Log: not supported by cvs2svn $
552 # Revision 1.17 2002/07/18 23:07:07 richard
553 # Unit tests and a few fixes.
554 #
423 # Revision 1.16 2002/07/09 05:20:09 richard 555 # Revision 1.16 2002/07/09 05:20:09 richard
424 # . added email display function - mangles email addrs so they're not so easily 556 # . added email display function - mangles email addrs so they're not so easily
425 # scraped from the web 557 # scraped from the web
426 # 558 #
427 # Revision 1.15 2002/07/08 06:39:00 richard 559 # Revision 1.15 2002/07/08 06:39:00 richard

Roundup Issue Tracker: http://roundup-tracker.org/