2006-10-11 17:51:28 +02:00
|
|
|
#!/usr/bin/env python2.4
|
|
|
|
|
2006-08-24 02:06:19 +02:00
|
|
|
import unittest
|
2006-09-09 18:18:50 +02:00
|
|
|
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"
|
2006-10-25 12:03:06 +02:00
|
|
|
CBXPORT=8081
|
2006-09-09 18:18:50 +02:00
|
|
|
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)
|
|
|
|
|
2006-08-24 02:06:19 +02:00
|
|
|
|
2006-09-09 18:18:50 +02:00
|
|
|
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()
|
|
|
|
|
2006-10-25 12:03:06 +02:00
|
|
|
def __get_soup():
|
|
|
|
from twill.commands import get_browser
|
|
|
|
browser = get_browser()
|
|
|
|
soup = BeautifulSoup(browser.get_html())
|
|
|
|
return soup
|
2006-09-09 18:18:50 +02:00
|
|
|
|
|
|
|
########################################
|
|
|
|
######## 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):
|
2006-10-25 12:03:06 +02:00
|
|
|
'''display all active devices'''
|
2006-09-09 18:18:50 +02:00
|
|
|
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")
|
2006-10-25 12:03:06 +02:00
|
|
|
cbx.show()
|
2006-09-09 18:18:50 +02:00
|
|
|
self.assertRaises(TwillAssertionError, cbx.find, "Sstem")
|
|
|
|
self.assertRaises(TwillAssertionError, cbx.notfind, "System")
|
2006-08-24 02:06:19 +02:00
|
|
|
|
2006-10-25 12:03:06 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2006-08-24 02:06:19 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|
2006-09-09 18:18:50 +02:00
|
|
|
|