diff test/test_actions.py @ 2004:1782fe36e7b8

Move out parts of client.py to new modules: * actions.py - the xxxAction and xxxPermission functions refactored into Action classes * exceptions.py - all exceptions * form_parser.py - parsePropsFromForm & extractFormList in a FormParser class Also added some new tests for the Actions.
author Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
date Wed, 11 Feb 2004 21:34:31 +0000
parents
children 366d3bbce982
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/test_actions.py	Wed Feb 11 21:34:31 2004 +0000
@@ -0,0 +1,144 @@
+import unittest
+from cgi import FieldStorage, MiniFieldStorage
+
+from roundup import hyperdb
+from roundup.cgi.actions import *
+from roundup.cgi.exceptions import Redirect, Unauthorised
+
+class MockNull:
+    def __init__(self, **kwargs):
+        for key, value in kwargs.items():
+            setattr(self, key, value)
+
+    def __call__(self, *args, **kwargs): return MockNull()
+    def __getattr__(self, name):
+        # This allows assignments which assume all intermediate steps are Null
+        # objects if they don't exist yet.
+        #
+        # For example (with just 'client' defined):
+        #
+        # client.db.config.TRACKER_WEB = 'BASE/'        
+        setattr(self, name, MockNull())
+        return getattr(self, name)
+
+    def __getitem__(self, key): return self    
+    def __nonzero__(self): return 0
+    def __str__(self): return ''
+
+def true(*args, **kwargs):
+    return 1
+
+class ActionTestCase(unittest.TestCase):
+    def setUp(self):
+        self.form = FieldStorage()
+        self.client = MockNull()
+        self.client.form = self.form
+    
+class ShowActionTestCase(ActionTestCase):
+    def assertRaisesMessage(self, exception, callable, message, *args, **kwargs):
+        try:
+            callable(*args, **kwargs)
+        except exception, msg:
+            self.assertEqual(str(msg), message)
+        else:
+            if hasattr(excClass,'__name__'): excName = excClass.__name__
+            else: excName = str(excClass)
+            raise self.failureException, excName
+    
+    def testShowAction(self):
+        self.client.db.config.TRACKER_WEB = 'BASE/'
+
+        action = ShowAction(self.client)
+        self.assertRaises(ValueError, action.handle)
+
+        self.form.value.append(MiniFieldStorage('@type', 'issue'))
+        self.assertRaisesMessage(Redirect, action.handle, 'BASE/issue')
+        
+        self.form.value.append(MiniFieldStorage('@number', '1'))
+        self.assertRaisesMessage(Redirect, action.handle, 'BASE/issue1')
+
+class RetireActionTestCase(ActionTestCase):
+    def testRetireAction(self):
+        self.client.db.security.hasPermission = true
+        self.client.ok_message = []
+        RetireAction(self.client).handle()
+        self.assert_(len(self.client.ok_message) == 1)
+
+    def testNoPermission(self):
+        self.assertRaises(Unauthorised, RetireAction(self.client).handle)
+
+    def testDontRetireAdminOrAnonymous(self):
+        self.client.db.security.hasPermission=true
+        self.client.classname = 'user'        
+        self.client.db.user.get = lambda a,b: 'admin'
+        self.assertRaises(ValueError, RetireAction(self.client).handle)
+
+class SearchActionTestCase(ActionTestCase):
+    def setUp(self):
+        ActionTestCase.setUp(self)
+        self.action = SearchAction(self.client)
+
+class StandardSearchActionTestCase(SearchActionTestCase):
+    def testNoPermission(self):
+        self.assertRaises(Unauthorised, self.action.handle)
+    
+    def testQueryName(self):
+        self.assertEqual(self.action.getQueryName(), '')
+
+        self.form.value.append(MiniFieldStorage('@queryname', 'foo'))
+        self.assertEqual(self.action.getQueryName(), 'foo')
+
+class FakeFilterVarsTestCase(SearchActionTestCase):
+    def setUp(self):
+        SearchActionTestCase.setUp(self)
+        self.client.db.classes.getprops = lambda: {'foo': hyperdb.Multilink('foo')}
+
+    def assertFilterEquals(self, expected):
+        self.action.fakeFilterVars()
+        self.assertEqual(self.form.getvalue('@filter'), expected)
+        
+    def testEmptyMultilink(self):
+        self.form.value.append(MiniFieldStorage('foo', ''))
+        self.form.value.append(MiniFieldStorage('foo', ''))
+
+        self.assertFilterEquals(None)
+
+    def testNonEmptyMultilink(self):
+        self.form.value.append(MiniFieldStorage('foo', ''))
+        self.form.value.append(MiniFieldStorage('foo', '1'))
+
+        self.assertFilterEquals('foo')
+
+    def testEmptyKey(self):
+        self.form.value.append(MiniFieldStorage('foo', ''))
+        self.assertFilterEquals(None)
+
+    def testStandardKey(self):
+        self.form.value.append(MiniFieldStorage('foo', '1'))
+        self.assertFilterEquals('foo')
+
+    def testStringKey(self):
+        self.client.db.classes.getprops = lambda: {'foo': hyperdb.String()}
+        self.form.value.append(MiniFieldStorage('foo', 'hello'))
+        self.assertFilterEquals('foo')
+
+    def testTokenizedStringKey(self):
+        self.client.db.classes.getprops = lambda: {'foo': hyperdb.String()}
+        self.form.value.append(MiniFieldStorage('foo', 'hello world'))
+        
+        self.assertFilterEquals('foo')
+
+        # The single value gets replaced with the tokenized list.
+        self.assertEqual([x.value for x in self.form['foo']], ['hello', 'world'])
+        
+def test_suite():
+    suite = unittest.TestSuite()
+    suite.addTest(unittest.makeSuite(RetireActionTestCase))
+    suite.addTest(unittest.makeSuite(StandardSearchActionTestCase))
+    suite.addTest(unittest.makeSuite(FakeFilterVarsTestCase))
+    suite.addTest(unittest.makeSuite(ShowActionTestCase))    
+    return suite
+
+if __name__ == '__main__':
+    runner = unittest.TextTestRunner()
+    unittest.main(testRunner=runner)

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