[Zope3-checkins] SVN: Zope3/branches/hdima-password-managers/src/zope/app/server/ Added more tests

Dmitry Vasiliev dima at hlabs.spb.ru
Thu Oct 27 08:21:45 EDT 2005


Log message for revision 39669:
  Added more tests
  

Changed:
  A   Zope3/branches/hdima-password-managers/src/zope/app/server/tests/site.zcml
  U   Zope3/branches/hdima-password-managers/src/zope/app/server/tests/test_zpasswd.py
  U   Zope3/branches/hdima-password-managers/src/zope/app/server/zpasswd.py

-=-
Added: Zope3/branches/hdima-password-managers/src/zope/app/server/tests/site.zcml
===================================================================
--- Zope3/branches/hdima-password-managers/src/zope/app/server/tests/site.zcml	2005-10-27 11:05:58 UTC (rev 39668)
+++ Zope3/branches/hdima-password-managers/src/zope/app/server/tests/site.zcml	2005-10-27 12:21:45 UTC (rev 39669)
@@ -0,0 +1 @@
+<configure xmlns="http://namespaces.zope.org/zope"/>

Modified: Zope3/branches/hdima-password-managers/src/zope/app/server/tests/test_zpasswd.py
===================================================================
--- Zope3/branches/hdima-password-managers/src/zope/app/server/tests/test_zpasswd.py	2005-10-27 11:05:58 UTC (rev 39668)
+++ Zope3/branches/hdima-password-managers/src/zope/app/server/tests/test_zpasswd.py	2005-10-27 12:21:45 UTC (rev 39669)
@@ -15,14 +15,127 @@
 
 $Id$
 """
+
+import os
 import unittest
+
 from zope.testing.doctestunit import DocTestSuite
 
+from zope.app.authentication import password
+from zope.app.server.tests.test_mkzopeinstance import TestBase
 
+from zope.app.server import zpasswd
+
+
+class ArgumentParsingTestCase(TestBase):
+
+    config = os.path.join(os.path.dirname(__file__), "site.zcml")
+
+    def parse_args(self, args):
+        argv = ["foo/bar.py"] + args
+        options = zpasswd.parse_args(argv)
+        self.assertEqual(options.program, "bar.py")
+        self.assert_(options.version)
+        return options
+
+    def check_stdout_content(self, args):
+        try:
+            options = self.parse_args(args)
+        except SystemExit, e:
+            self.assertEqual(e.code, 0)
+            self.assert_(self.stdout.getvalue())
+            self.failIf(self.stderr.getvalue())
+        else:
+            self.fail("expected SystemExit")
+
+    def test_no_arguments(self):
+        options = self.parse_args([])
+        self.assert_(options.managers)
+        self.assert_(not options.destination)
+
+    def test_version_long(self):
+        self.check_stdout_content(["--version"])
+
+    def test_help_long(self):
+        self.check_stdout_content(["--help"])
+
+    def test_help_short(self):
+        self.check_stdout_content(["-h"])
+
+    def test_destination_short(self):
+        options = self.parse_args(["-o", "filename"])
+        self.assertEqual(options.destination, "filename")
+
+    def test_destination_long(self):
+        options = self.parse_args(["--output", "filename"])
+        self.assertEqual(options.destination, "filename")
+
+    def test_config_short(self):
+        options = self.parse_args(["-c", self.config])
+        self.assert_(options.managers)
+
+    def test_config_long(self):
+        options = self.parse_args(["--config", self.config])
+        self.assert_(options.managers)
+
+class ControlledInputApplication(zpasswd.Application):
+
+    def __init__(self, options, input_lines):
+        super(ControlledInputApplication, self).__init__(options)
+        self.__input = input_lines
+
+    def read_input_line(self, prompt):
+        return self.__input.pop(0)
+
+    read_password = read_input_line
+
+    def all_input_consumed(self):
+        return not self.__input
+
+class Options(object):
+
+    config = None
+    destination = None
+    version = "[test-version]"
+    program = "[test-program]"
+    managers = password.managers
+
+class InputCollectionTestCase(TestBase):
+
+    def createOptions(self):
+        return Options()
+
+    def check_principal(self, expected):
+        output = self.stdout.getvalue()
+        self.failUnless(output)
+
+        principal_lines = output.splitlines()[-(len(expected) + 1):-1]
+        for line, expline in zip(principal_lines, expected):
+            self.failUnlessEqual(line.strip(), expline)
+
+    def test_principal_information(self):
+        options = self.createOptions()
+        app = ControlledInputApplication(options,
+            ["id", "title", "login", "1", "passwd", "passwd", "description"])
+        app.process()
+        self.failUnless(not self.stderr.getvalue())
+        self.failUnless(app.all_input_consumed())
+        self.check_principal([
+            '<principal',
+            'id="id"',
+            'title="title"',
+            'login="login"',
+            'password="passwd"',
+            'description="description"',
+            '/>'
+            ])
+
+
 def test_suite():
-    return unittest.TestSuite((
-        DocTestSuite('zope.app.server.zpasswd'),
-        ))
+    suite = DocTestSuite('zope.app.server.zpasswd')
+    suite.addTest(unittest.makeSuite(ArgumentParsingTestCase))
+    suite.addTest(unittest.makeSuite(InputCollectionTestCase))
+    return suite
 
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')

Modified: Zope3/branches/hdima-password-managers/src/zope/app/server/zpasswd.py
===================================================================
--- Zope3/branches/hdima-password-managers/src/zope/app/server/zpasswd.py	2005-10-27 11:05:58 UTC (rev 39668)
+++ Zope3/branches/hdima-password-managers/src/zope/app/server/zpasswd.py	2005-10-27 12:21:45 UTC (rev 39669)
@@ -253,6 +253,8 @@
                 managers.insert(0, (name, manager))
             else:
                 managers.append((name, manager))
+        if not managers:
+            from zope.app.authentication.password import managers
     return managers
 
 def parse_args(argv):



More information about the Zope3-Checkins mailing list