unittests added to some plugins

added "name" tag to all form elements
This commit is contained in:
lars 2006-10-26 12:25:12 +00:00
parent 0a8fc07556
commit 1dc0da0382
23 changed files with 199 additions and 28 deletions

View file

@ -6,7 +6,7 @@
<h1><?cs var:html_escape(Lang.Plugins.network.Title.Network) ?></h1>
<?cs call:print_form_header("plugins/network") ?>
<?cs call:print_form_header("set_ip", "plugins/network") ?>
<p><label for="ip"><?cs var:html_escape(Lang.Plugins.network.Text.IP) ?>: </label>
<input class="ipnum" type="text" tabindex="1" name="ip1" size="3" id="ip"

View file

@ -2,9 +2,10 @@ import subprocess
import os
import CryptoBoxPlugin
## specify (in seconds), how long we should wait before redirecting and ip change
REDIRECT_DELAY=20
CHANGE_IP_DELAY=5
CHANGE_IP_DELAY=1
class network(CryptoBoxPlugin.CryptoBoxPlugin):
@ -14,10 +15,13 @@ class network(CryptoBoxPlugin.CryptoBoxPlugin):
def doAction(self, store=None, redirected="", ip1="", ip2="", ip3="", ip4=""):
## if we were redirected, then we should display the default page
self.cbox.log.debug("executing network plugin")
if redirected == "1":
self.cbox.log.debug("network plugin: redirected")
return None
## request for IP change?
if store:
self.cbox.log.debug("network plugin: changing IP")
try:
for ip_in in (ip1, ip2, ip3, ip4):
if (int(ip_in) < 0) or (int(ip_in) > 255):
@ -39,6 +43,7 @@ class network(CryptoBoxPlugin.CryptoBoxPlugin):
self.__prepareFormData()
return "form_network"
else:
self.cbox.log.debug("network plugin: show form")
## just show the form
self.__prepareFormData()
return "form_network"
@ -48,11 +53,11 @@ class network(CryptoBoxPlugin.CryptoBoxPlugin):
return "%d.%d.%d.%d" % self.__getCurrentIP()
def __getRedirectDestionation(self, ip):
def __getRedirectDestination(self, ip):
import cherrypy
req = cherrypy.request
dest = ip
base_parts = req.base.split(":")
dest = "%s:%s" % (base_parts[0], ip)
if len(base_parts) == 3:
dest += ":%s" % base_parts[2]
return dest
@ -97,6 +102,7 @@ class network(CryptoBoxPlugin.CryptoBoxPlugin):
time.sleep(CHANGE_IP_DELAY)
proc = subprocess.Popen(
shell = False,
stderr = subprocess.PIPE,
args = [
self.cbox.prefs["Programs"]["super"],
self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
@ -104,6 +110,9 @@ class network(CryptoBoxPlugin.CryptoBoxPlugin):
os.path.join(self.pluginDir, "root_action.py"),
ip])
proc.communicate()
if proc.returncode != 0:
self.cbox.log.warn("failed to change IP address: %s" % ip)
self.cbox.log.warn("error output: %s" % str(proc.stderr.read()))
return
thread = threading.Thread()
thread.run = delayedIPchange
@ -112,3 +121,5 @@ class network(CryptoBoxPlugin.CryptoBoxPlugin):
# TODO: how could we guess, if it failed?
return True

View file

@ -0,0 +1,45 @@
import WebInterfaceTestClass
from network import CHANGE_IP_DELAY
class unittests(WebInterfaceTestClass.WebInterfaceTestClass):
def atest_ip_change(self):
'''change of network address'''
## the time module is necessary for the CHANGE_IP_DELAY
import time
self.register_auth(self.URL + "plugins/network")
self.cmd.go(self.URL + "plugins/network")
## extract the current IP from the network plugin output
def getCurrentIP():
self.cmd.go(self.URL + "plugins/network")
self.cmd.show()
self.cmd.find(u'Data.Status.Plugins.network=([0-9\.]*)$', "m")
return self.locals["__match__"]
origIPtext = getCurrentIP()
origIPocts = origIPtext.split(".")
## check, if the original IP is valid (contains four octets)
self.assertEquals(4, len(origIPocts))
self.cmd.echo(origIPocts)
wrongIP = "192.168.123.321"
def setIP((ip1, ip2, ip3, ip4)):
self.cmd.go(self.URL + "plugins/network")
self.cmd.formvalue("set_ip", "ip1", str(ip1))
self.cmd.formvalue("set_ip", "ip2", str(ip2))
self.cmd.formvalue("set_ip", "ip3", str(ip3))
self.cmd.formvalue("set_ip", "ip4", str(ip4))
self.cmd.submit()
## sleep a little bit longer than the delay necessary for ip-change
time.sleep(CHANGE_IP_DELAY + 0.2)
setIP([1,-2,0,1])
self.assertEquals(origIPtext, getCurrentIP())
setIP([1,0,0,256])
self.assertEquals(origIPtext, getCurrentIP())
setIP([1,"foo",0,1])
self.assertEquals(origIPtext, getCurrentIP())
setIP([10,12,0,2])
self.assertEquals("10.12.0.2", getCurrentIP())
setIP(origIPocts)
self.assertEquals(origIPtext, getCurrentIP())