From d1d2e1edd5aeb967a36a48375e06a3b04c54bd2d Mon Sep 17 00:00:00 2001 From: lars Date: Thu, 26 Oct 2006 12:32:44 +0000 Subject: [PATCH] base unittest class for the web interface added unittests for builtin pages seperated from plugin unittests plugin unittest framework added --- pythonrewrite/bin/WebInterfaceTestClass.py | 77 ++++++++++ .../bin/unittests.CryptoBoxWebserver.py | 140 ------------------ pythonrewrite/bin/unittests.Plugins.py | 33 +++++ pythonrewrite/bin/unittests.WebSites.py | 55 +++++++ 4 files changed, 165 insertions(+), 140 deletions(-) create mode 100644 pythonrewrite/bin/WebInterfaceTestClass.py delete mode 100755 pythonrewrite/bin/unittests.CryptoBoxWebserver.py create mode 100755 pythonrewrite/bin/unittests.Plugins.py create mode 100755 pythonrewrite/bin/unittests.WebSites.py diff --git a/pythonrewrite/bin/WebInterfaceTestClass.py b/pythonrewrite/bin/WebInterfaceTestClass.py new file mode 100644 index 0000000..c210a0f --- /dev/null +++ b/pythonrewrite/bin/WebInterfaceTestClass.py @@ -0,0 +1,77 @@ +""" +super class of all web interface unittests for the cryptobox + +just inherit this class and add some test functions +""" + +import unittest +import twill +import cherrypy +import WebInterfaceSites + +## we do the following, for easy surfing +## e.g. use: cbx.go(your_url) +## commands api: http://twill.idyll.org/commands.html +CBXHOST="localhost" +CBXPORT=8081 +CBX_URL="http://%s:%d/" % (CBXHOST, CBXPORT) +LOG_FILE="/tmp/twill.log" + +class WebInterfaceTestClass(unittest.TestCase): + '''this class checks the webserver, using "twill" + + the tests in this class are from the browsers point of view, so not + really unittests. + fetch twill from: http://twill.idyll.org + one way to manually run twill code is through the python + interpreter commandline e.g.: + + import twill + twill.shell.main() + go http://localhost:8080 + find "my very special html content" + help + ''' + + def setUp(self): + '''configures the cherrypy server that it works nice with twill + ''' + cherrypy.config.update({ + 'server.logToScreen' : False, + 'autoreload.on': False, + 'server.threadPool': 1, + 'server.environment': 'production', + }) + cherrypy.root = WebInterfaceSites.WebInterfaceSites() + cherrypy.server.start(initOnly=True, serverClass=None) + + from cherrypy._cpwsgi import wsgiApp + twill.add_wsgi_intercept(CBXHOST, CBXPORT, lambda: wsgiApp) + + # grab the output of twill commands + self.output = open(LOG_FILE,"a") + twill.set_output(self.output) + self.cmd = twill.commands + self.URL = CBX_URL + self.cbox = cherrypy.root.cbox + self.globals, self.locals = twill.namespaces.get_twill_glocals() + + + def tearDown(self): + '''clean up the room when leaving''' + # remove intercept. + twill.remove_wsgi_intercept(CBXHOST, CBXPORT) + # shut down the cherrypy server. + cherrypy.server.stop() + self.output.close() + + + def __get_soup(): + browser = twill.commands.get_browser() + soup = BeautifulSoup(browser.get_html()) + return soup + + + def register_auth(self, url, user="admin", password="admin"): + self.cmd.add_auth("CryptoBox", url, user, password) + diff --git a/pythonrewrite/bin/unittests.CryptoBoxWebserver.py b/pythonrewrite/bin/unittests.CryptoBoxWebserver.py deleted file mode 100755 index 665fdd5..0000000 --- a/pythonrewrite/bin/unittests.CryptoBoxWebserver.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python2.4 - -import unittest -import twill -import cherrypy -import WebInterfaceSites -from twill import get_browser - -## this makes assertRaises shorter -from twill.errors import * -from mechanize import BrowserStateError, LinkNotFoundError - -## we do the following, for easy surfing -## e.g. use: cbx.go(your_url) -## commands api: http://twill.idyll.org/commands.html -cbx = twill.commands -CBXHOST="localhost" -CBXPORT=8081 -CBX="http://"+CBXHOST+":"+str(CBXPORT) - -class WebserverTests(unittest.TestCase): - '''this class checks the webserver, using "twill" - - the tests in this class are from the browsers point of view, so not - really unittests. - fetch twill from: http://twill.idyll.org - one way to manually run twill code is through the python - interpreter commandline e.g.: - - import twill - twill.shell.main() - go http://localhost:8080 - find "my very special html content" - help - ''' - - def setUp(self): - '''configures the cherrypy server that it works nice with twill - ''' - cherrypy.config.update({ - 'server.logToScreen' : False, - 'autoreload.on': False, - 'server.threadPool': 1, - 'server.environment': 'production', - }) - cherrypy.root = WebInterfaceSites.WebInterfaceSites() - cherrypy.server.start(initOnly=True, serverClass=None) - - from cherrypy._cpwsgi import wsgiApp - twill.add_wsgi_intercept(CBXHOST, CBXPORT, lambda: wsgiApp) - - # grab the output of twill commands - self.output = open("/tmp/twill.log","a") - twill.set_output(self.output) - - - def tearDown(self): - '''clean up the room when leaving''' - # remove intercept. - twill.remove_wsgi_intercept(CBXHOST, CBXPORT) - # shut down the cherrypy server. - cherrypy.server.stop() - self.output.close() - - def __get_soup(): - from twill.commands import get_browser - browser = get_browser() - soup = BeautifulSoup(browser.get_html()) - return soup - - ######################################## - ######## the tests start here ########## - - def test_is_server_running(self): - '''the server should run under given name and port''' - cbx.go(CBX) - # wrong port should fail - self.assertRaises(BrowserStateError, cbx.go, "http://"+CBXHOST+":"+str(CBXPORT+1)) - # wrong hostname too - self.assertRaises(BrowserStateError, cbx.go, "http://localhorst:"+str(CBXPORT)) - - def test_helppages(self): - '''helpsites should be availbale in different languages''' - cbx.go(CBX) - cbx.go("doc") - cbx.find("Table of Contents") - - cbx.go("doc?weblang=en") - cbx.find("Table of Contents") - cbx.find("Getting started") - self.assertRaises(TwillAssertionError, cbx.notfind, "Table of Contents") - - cbx.go("doc?weblang=de") - cbx.find("Table of Contents") - cbx.find("Wie geht es los") - - cbx.go("doc?weblang=si") - self.assertRaises(TwillAssertionError, cbx.notfind, "Table of Contents") - - def test_goto_status(self): - '''display all active devices''' - cbx.go(CBX) - cbx.go("status") - cbx.find("Status") - - def test_goto_system(self): - cbx.go(CBX) - cbx.go("system") - cbx.find("System") - cbx.notfind("Sstem") - cbx.show() - self.assertRaises(TwillAssertionError, cbx.find, "Sstem") - self.assertRaises(TwillAssertionError, cbx.notfind, "System") - - def test_plugin_network(self): - '''change of network address''' - import socket - # actIP assumes working nameservice - actIP = socket.gethostbyname(socket.gethostname()) - wrongIP = "192.168.123.321" - cbx.go(CBX) - cbx.go("plugins/network") - self.assertRaises(TwillAssertionError, cbx.notfind, str("Data.Status.Plugins.network=" + actIP)) - self.assertRaises(TwillAssertionError, cbx.find, str("Data.Status.Plugins.network= ")) - self.assertRaises(TwillAssertionError, cbx.find, str("Data.Status.Plugins.network=" + wrongIP)) - #TODO: change the address and test again - #print cbx.show() - print cbx.showhistory() - print cbx.showforms() - print "1" - cbx.formvalue('1','ip4',"123") - print "a" - cbx.submit() - print "b" - cbx.code(200) - - -if __name__ == "__main__": - unittest.main() - diff --git a/pythonrewrite/bin/unittests.Plugins.py b/pythonrewrite/bin/unittests.Plugins.py new file mode 100755 index 0000000..929c9de --- /dev/null +++ b/pythonrewrite/bin/unittests.Plugins.py @@ -0,0 +1,33 @@ +#!/usr/bin/python2.4 + +import unittest +import Plugins + +class CheckForUndefinedTestCases(unittest.TestCase): + """here we will add failing test functions for every non-existing testcase""" + + +def create_testcases(): + + plugins = Plugins.PluginManager(None, "../plugins").getPlugins() + glob_dict = globals() + loc_dict = locals() + for pl in plugins: + test_class = pl.getTestClass() + if test_class: + ## add the testclass to the global dictionary + glob_dict["unittest" + pl.getName()] = test_class + else: + subname = "test_existence_%s" % pl.getName() + def test_existence(self): + """check if the plugin (%s) contains tests""" % pl.getName() + self.fail("no tests defined for plugin: %s" % pl.getName()) + ## add this function to the class above + setattr(CheckForUndefinedTestCases, subname, test_existence) + #FIXME: the failure output always contains the same name for all plugins + + +create_testcases() + +if __name__ == "__main__": + unittest.main() diff --git a/pythonrewrite/bin/unittests.WebSites.py b/pythonrewrite/bin/unittests.WebSites.py new file mode 100755 index 0000000..c9c653e --- /dev/null +++ b/pythonrewrite/bin/unittests.WebSites.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python2.4 + +import unittest + +## this makes assertRaises shorter +from twill.errors import * +from mechanize import BrowserStateError, LinkNotFoundError + +## import the module of the common super class of all web interface test classes +import WebInterfaceTestClass + + + +class WebServer(WebInterfaceTestClass.WebInterfaceTestClass): + + def test_is_server_running(self): + '''the server should run under given name and port''' + self.cmd.go(self.URL) + ## other URLs must not be checked, as we do not know, if they are valid + + +class BuiltinPages(WebInterfaceTestClass.WebInterfaceTestClass): + + def test_help_pages(self): + '''help pages should be available in different languages''' + + ## check english help pages + self.cmd.go(self.URL + "doc?weblang=en") + self.cmd.find("Table of Contents") + self.cmd.find("Getting started") + + self.cmd.go(self.URL + "doc?weblang=de") + self.cmd.find("Table of Contents") + self.cmd.find("Wie geht es los") + + self.cmd.go(self.URL + "doc?weblang=si") + self.assertRaises(TwillAssertionError, self.cmd.notfind, "Table of Contents") + #TODO: add a slovene text here, as soon as the help is translated + + + def test_goto_status(self): + '''display all active devices''' + self.cmd.go(self.URL + "status") + self.cmd.find("Status") + + + def test_goto_system(self): + self.cmd.go(self.URL + "system") + self.cmd.find("System") + + + +if __name__ == "__main__": + unittest.main() +