@ -1,14 +1,12 @@
import logging
import logging
import sys
import sys
import unittest
import unittest
from StringIO import StringIO
import json
import json
import os
import os
import copy
import copy
import thread
import threading
import time
import time
import requests
import requests
import StringIO
import re
import re
@ -51,22 +49,24 @@ class FormConfigTestCase(unittest.TestCase):
fc = sf . get_form_config ( )
fc = sf . get_form_config ( )
fd = fc . get_form_def ( ' test_store ' )
fd = fc . get_form_def ( ' test_store ' )
res = runscript . run_script ( fd , { } , { } )
res = runscript . run_script ( fd , { } , { } )
self . assertEquals ( res [ ' exitcode ' ] , 33 )
self . assertEqual ( res [ ' exitcode ' ] , 33 )
self . assertTrue ( ' stdout ' in res [ ' stdout ' ] )
self . assertTrue ( b ' stdout ' in res [ ' stdout ' ] )
self . assertTrue ( ' stderr ' in res [ ' stderr ' ] )
self . assertTrue ( b ' stderr ' in res [ ' stderr ' ] )
def testCallbackRaw ( self ) :
def testCallbackRaw ( self ) :
""" Test a callback that returns raw output """
""" Test a callback that returns raw output """
sf = scriptform . ScriptForm ( ' test_formconfig_callback.json ' )
sf = scriptform . ScriptForm ( ' test_formconfig_callback.json ' )
fc = sf . get_form_config ( )
fc = sf . get_form_config ( )
fd = fc . get_form_def ( ' test_raw ' )
fd = fc . get_form_def ( ' test_raw ' )
stdout = file ( ' tmp_stdout ' , ' w+ ' ) # can't use StringIO
stdout = open ( ' tmp_stdout ' , ' w+ ' ) # can't use StringIO
stderr = file ( ' tmp_stderr ' , ' w+ ' )
stderr = open ( ' tmp_stderr ' , ' w+ ' )
exitcode = runscript . run_script ( fd , { } , { } , stdout , stderr )
exitcode = runscript . run_script ( fd , { } , { } , stdout , stderr )
stdout . seek ( 0 )
stdout . seek ( 0 )
stderr . seek ( 0 )
stderr . seek ( 0 )
self . assertTrue ( exitcode == 33 )
self . assertTrue ( exitcode == 33 )
self . assertTrue ( ' stdout ' in stdout . read ( ) )
self . assertTrue ( ' stdout ' in stdout . read ( ) )
stdout . close ( )
stderr . close ( )
def testCallbackMissingParams ( self ) :
def testCallbackMissingParams ( self ) :
"""
"""
@ -115,7 +115,7 @@ class FormDefinitionTest(unittest.TestCase):
form_values = { " val_string " : " 1234 " }
form_values = { " val_string " : " 1234 " }
errors , values = fd . validate ( form_values )
errors , values = fd . validate ( form_values )
self . assertNotIn ( ' val_string ' , errors )
self . assertNotIn ( ' val_string ' , errors )
self . assertEquals ( values [ ' val_string ' ] , " 1234 " )
self . assertEqual ( values [ ' val_string ' ] , " 1234 " )
def testValidateIntegerInvalid ( self ) :
def testValidateIntegerInvalid ( self ) :
fd = self . fc . get_form_def ( ' test_val_integer ' )
fd = self . fc . get_form_def ( ' test_val_integer ' )
@ -143,7 +143,7 @@ class FormDefinitionTest(unittest.TestCase):
form_values = { " val_integer " : 6 }
form_values = { " val_integer " : 6 }
errors , values = fd . validate ( form_values )
errors , values = fd . validate ( form_values )
self . assertNotIn ( ' val_integer ' , errors )
self . assertNotIn ( ' val_integer ' , errors )
self . assertEquals ( values [ ' val_integer ' ] , 6 )
self . assertEqual ( values [ ' val_integer ' ] , 6 )
def testValidateFloatInvalid ( self ) :
def testValidateFloatInvalid ( self ) :
fd = self . fc . get_form_def ( ' test_val_float ' )
fd = self . fc . get_form_def ( ' test_val_float ' )
@ -171,7 +171,7 @@ class FormDefinitionTest(unittest.TestCase):
form_values = { " val_float " : 2.29 }
form_values = { " val_float " : 2.29 }
errors , values = fd . validate ( form_values )
errors , values = fd . validate ( form_values )
self . assertNotIn ( ' val_float ' , errors )
self . assertNotIn ( ' val_float ' , errors )
self . assertEquals ( values [ ' val_float ' ] , 2.29 )
self . assertEqual ( values [ ' val_float ' ] , 2.29 )
def testValidateDateInvalid ( self ) :
def testValidateDateInvalid ( self ) :
fd = self . fc . get_form_def ( ' test_val_date ' )
fd = self . fc . get_form_def ( ' test_val_date ' )
@ -200,14 +200,14 @@ class FormDefinitionTest(unittest.TestCase):
form_values = { " val_date " : ' 2015-03-03 ' }
form_values = { " val_date " : ' 2015-03-03 ' }
errors , values = fd . validate ( form_values )
errors , values = fd . validate ( form_values )
self . assertNotIn ( ' val_date ' , errors )
self . assertNotIn ( ' val_date ' , errors )
self . assertEquals ( values [ ' val_date ' ] , datetime . date ( 2015 , 3 , 3 ) )
self . assertEqual ( values [ ' val_date ' ] , datetime . date ( 2015 , 3 , 3 ) )
def testValidateSelectValue ( self ) :
def testValidateSelectValue ( self ) :
fd = self . fc . get_form_def ( ' test_val_select ' )
fd = self . fc . get_form_def ( ' test_val_select ' )
form_values = { " val_select " : ' option_a ' }
form_values = { " val_select " : ' option_a ' }
errors , values = fd . validate ( form_values )
errors , values = fd . validate ( form_values )
self . assertNotIn ( ' val_select ' , errors )
self . assertNotIn ( ' val_select ' , errors )
self . assertEquals ( values [ ' val_select ' ] , ' option_a ' )
self . assertEqual ( values [ ' val_select ' ] , ' option_a ' )
def testValidateSelectInvalid ( self ) :
def testValidateSelectInvalid ( self ) :
fd = self . fc . get_form_def ( ' test_val_select ' )
fd = self . fc . get_form_def ( ' test_val_select ' )
@ -221,14 +221,14 @@ class FormDefinitionTest(unittest.TestCase):
form_values = { " val_checkbox " : ' on ' }
form_values = { " val_checkbox " : ' on ' }
errors , values = fd . validate ( form_values )
errors , values = fd . validate ( form_values )
self . assertNotIn ( ' val_checkbox ' , errors )
self . assertNotIn ( ' val_checkbox ' , errors )
self . assertEquals ( values [ ' val_checkbox ' ] , ' on ' )
self . assertEqual ( values [ ' val_checkbox ' ] , ' on ' )
def testValidateCheckboxDefaultOn ( self ) :
def testValidateCheckboxDefaultOn ( self ) :
fd = self . fc . get_form_def ( ' test_val_checkbox_on ' )
fd = self . fc . get_form_def ( ' test_val_checkbox_on ' )
form_values = { " val_checkbox_on " : ' off ' }
form_values = { " val_checkbox_on " : ' off ' }
errors , values = fd . validate ( form_values )
errors , values = fd . validate ( form_values )
self . assertNotIn ( ' val_checkbox_on ' , errors )
self . assertNotIn ( ' val_checkbox_on ' , errors )
self . assertEquals ( values [ ' val_checkbox_on ' ] , ' off ' )
self . assertEqual ( values [ ' val_checkbox_on ' ] , ' off ' )
def testValidateCheckboxInvalid ( self ) :
def testValidateCheckboxInvalid ( self ) :
fd = self . fc . get_form_def ( ' test_val_checkbox ' )
fd = self . fc . get_form_def ( ' test_val_checkbox ' )
@ -280,22 +280,27 @@ class WebAppTest(unittest.TestCase):
cls . auth_admin = requests . auth . HTTPBasicAuth ( ' admin ' , ' admin ' )
cls . auth_admin = requests . auth . HTTPBasicAuth ( ' admin ' , ' admin ' )
cls . auth_user = requests . auth . HTTPBasicAuth ( ' user ' , ' user ' )
cls . auth_user = requests . auth . HTTPBasicAuth ( ' user ' , ' user ' )
# Run the server in a thread, so we can execute the tests in the main
# program.
def server_thread ( sf ) :
def server_thread ( sf ) :
sf . run ( listen_port = 8002 )
sf . run ( listen_port = 8002 )
cls . sf = scriptform . ScriptForm ( ' test_webapp.json ' )
cls . sf = scriptform . ScriptForm ( ' test_webapp.json ' )
thread . start_new_thread ( server_thread , ( cls . sf , ) )
# Wait until the webserver is ready
thread = threading . Thread ( target = server_thread , args = ( cls . sf , ) )
thread . start ( )
while True :
while True :
time . sleep ( 0.1 )
time . sleep ( 0.1 )
if cls . sf . running :
if cls . sf . running is True :
break
break
@classmethod
@classmethod
def tearDownClass ( cls ) :
def tearDownClass ( cls ) :
# Shut down the webserver and wait until it has shut down.
cls . sf . shutdown ( )
cls . sf . shutdown ( )
while True :
while True :
time . sleep ( 0.1 )
time . sleep ( 0.1 )
if not cls . sf . running :
if cls . sf . running is False :
break
break
def testError404 ( self ) :
def testError404 ( self ) :
@ -356,12 +361,12 @@ class WebAppTest(unittest.TestCase):
}
}
import random
import random
f = file ( ' data.csv ' , ' w ' )
with open ( ' data.csv ' , ' w ' ) as fh :
for i in range ( 1024 ) :
for i in range ( 1024 ) :
f . write ( chr ( random . randint ( 0 , 255 ) ) )
fh . write ( chr ( random . randint ( 0 , 255 ) ) )
f . close ( )
files = { ' file ' : open ( ' data.csv ' , ' rb ' ) }
with open ( ' data.csv ' , ' rb ' ) as fh :
files = { ' file ' : fh }
r = requests . post ( " http://localhost:8002/submit " , data = data , files = files , auth = self . auth_user )
r = requests . post ( " http://localhost:8002/submit " , data = data , files = files , auth = self . auth_user )
self . assertIn ( ' string=12345 ' , r . text )
self . assertIn ( ' string=12345 ' , r . text )
@ -391,12 +396,12 @@ class WebAppTest(unittest.TestCase):
}
}
import random
import random
f = file ( ' data.txt ' , ' w ' )
with open ( ' data.txt ' , ' w ' ) as fh :
for i in range ( 1024 ) :
for i in range ( 1024 ) :
f . write ( chr ( random . randint ( 0 , 255 ) ) )
fh . write ( chr ( random . randint ( 0 , 255 ) ) )
f . close ( )
files = { ' file ' : open ( ' data.txt ' , ' rb ' ) }
with open ( ' data.txt ' , ' rb ' ) as fh :
files = { ' file ' : fh }
r = requests . post ( " http://localhost:8002/submit " , data = data , files = files , auth = self . auth_user )
r = requests . post ( " http://localhost:8002/submit " , data = data , files = files , auth = self . auth_user )
self . assertIn ( ' Maximum length is 7 ' , r . text )
self . assertIn ( ' Maximum length is 7 ' , r . text )
@ -431,12 +436,12 @@ class WebAppTest(unittest.TestCase):
}
}
import random
import random
f = file ( ' data.txt ' , ' w ' )
with open ( ' data.txt ' , ' w ' ) as fh :
for i in range ( 1024 ) :
for i in range ( 1024 ) :
f . write ( chr ( random . randint ( 0 , 255 ) ) )
fh . write ( chr ( random . randint ( 0 , 255 ) ) )
f . close ( )
files = { ' file ' : open ( ' data.txt ' , ' rb ' ) }
with open ( ' data.txt ' , ' rb ' ) as fh :
files = { ' file ' : fh }
r = requests . post ( " http://localhost:8002/submit " , data = data , files = files , auth = self . auth_user )
r = requests . post ( " http://localhost:8002/submit " , data = data , files = files , auth = self . auth_user )
self . assertIn ( ' value= " 123 " ' , r . text )
self . assertIn ( ' value= " 123 " ' , r . text )
self . assertIn ( ' value= " 12 " ' , r . text )
self . assertIn ( ' value= " 12 " ' , r . text )
@ -476,36 +481,36 @@ class WebAppTest(unittest.TestCase):
def testUpload ( self ) :
def testUpload ( self ) :
import random
import random
f = file ( ' data.raw ' , ' w ' )
with open ( ' data.raw ' , ' w ' ) as fh :
for i in range ( 1024 ) :
fh . write ( chr ( random . randint ( 0 , 255 ) ) )
f . write ( chr ( random . randint ( 0 , 255 ) ) )
f . close ( )
data = {
data = {
" form_name " : " upload "
" form_name " : " upload "
}
}
files = { ' file ' : open ( ' data.raw ' , ' rb ' ) }
with open ( ' data.raw ' , ' rb ' ) as fh :
files = { ' file ' : fh }
r = requests . post ( " http://localhost:8002/submit " , files = files , data = data , auth = self . auth_user )
r = requests . post ( " http://localhost:8002/submit " , files = files , data = data , auth = self . auth_user )
self . assertIn ( ' SAME ' , r . text )
self . assertIn ( ' SAME ' , r . text )
os . unlink ( ' data.raw ' )
os . unlink ( ' data.raw ' )
def testStaticValid ( self ) :
def testStaticValid ( self ) :
r = requests . get ( " http://localhost:8002/static?fname=ssh_server.png " , auth = self . auth_user )
r = requests . get ( " http://localhost:8002/static?fname=ssh_server.png " , auth = self . auth_user )
self . assertEquals ( r . status_code , 200 )
self . assertEqual ( r . status_code , 200 )
f_served = b ' '
f_served = b ' '
for c in r . iter_content ( ) :
for c in r . iter_content ( ) :
f_served + = c
f_served + = c
f_orig = file ( ' static/ssh_server.png ' , ' rb ' ) . read ( )
with open ( ' static/ssh_server.png ' , ' rb ' ) as fh :
self . assertEquals ( f_orig , f_served )
f_orig = fh . read ( )
self . assertEqual ( f_orig , f_served )
def testStaticInvalidFilename ( self ) :
def testStaticInvalidFilename ( self ) :
r = requests . get ( " http://localhost:8002/static?fname=../../ssh_server.png " , auth = self . auth_user )
r = requests . get ( " http://localhost:8002/static?fname=../../ssh_server.png " , auth = self . auth_user )
self . assertEquals ( r . status_code , 403 )
self . assertEqual ( r . status_code , 403 )
def testStaticInvalidNotFound ( self ) :
def testStaticInvalidNotFound ( self ) :
r = requests . get ( " http://localhost:8002/static?fname=nosuchfile.png " , auth = self . auth_user )
r = requests . get ( " http://localhost:8002/static?fname=nosuchfile.png " , auth = self . auth_user )
self . assertEquals ( r . status_code , 404 )
self . assertEqual ( r . status_code , 404 )
def testHiddenField ( self ) :
def testHiddenField ( self ) :
r = requests . get ( ' http://localhost:8002/form?form_name=hidden_field ' , auth = self . auth_user )
r = requests . get ( ' http://localhost:8002/form?form_name=hidden_field ' , auth = self . auth_user )
@ -526,14 +531,18 @@ class WebAppSingleTest(unittest.TestCase):
"""
"""
@classmethod
@classmethod
def setUpClass ( cls ) :
def setUpClass ( cls ) :
# Run the server in a thread, so we can execute the tests in the main
# program.
def server_thread ( sf ) :
def server_thread ( sf ) :
sf . run ( listen_port = 8002 )
sf . run ( listen_port = 8002 )
cls . sf = scriptform . ScriptForm ( ' test_webapp_singleform.json ' )
cls . sf = scriptform . ScriptForm ( ' test_webapp_singleform.json ' )
thread . start_new_thread ( server_thread , ( cls . sf , ) )
# Wait until the webserver is ready
thread = threading . Thread ( target = server_thread , args = ( cls . sf , ) )
thread . start ( )
while True :
while True :
time . sleep ( 0.1 )
time . sleep ( 0.1 )
if cls . sf . running :
if cls . sf . running is True :
break
break
@classmethod
@classmethod
@ -555,7 +564,7 @@ class WebAppSingleTest(unittest.TestCase):
"""
"""
"""
"""
r = requests . get ( " http://localhost:8002/static?fname=nosuchfile.png " )
r = requests . get ( " http://localhost:8002/static?fname=nosuchfile.png " )
self . assertEquals ( r . status_code , 501 )
self . assertEqual ( r . status_code , 501 )
if __name__ == ' __main__ ' :
if __name__ == ' __main__ ' :
@ -575,11 +584,11 @@ if __name__ == '__main__':
cov . stop ( )
cov . stop ( )
cov . save ( )
cov . save ( )
print cov . report ( )
print ( cov . report ( ) )
try :
try :
print cov . html_report ( )
print ( cov . html_report ( ) )
except coverage . misc . CoverageException , e :
except coverage . misc . CoverageException as err :
if " Couldn ' t find static file ' jquery.hotkeys.js ' " in e . message :
if " Couldn ' t find static file ' jquery.hotkeys.js ' " in err . message :
pass
pass
else :
else :
raise
raise