Fix testcases for py3

master
Ferry Boender 4 years ago
parent f762472090
commit 6f8557ef1d
  1. 201
      test/test.py

@ -1,14 +1,12 @@
import logging
import sys
import unittest
from StringIO import StringIO
import json
import os
import copy
import thread
import threading
import time
import requests
import StringIO
import re
@ -51,22 +49,24 @@ class FormConfigTestCase(unittest.TestCase):
fc = sf.get_form_config()
fd = fc.get_form_def('test_store')
res = runscript.run_script(fd, {}, {})
self.assertEquals(res['exitcode'], 33)
self.assertTrue('stdout' in res['stdout'])
self.assertTrue('stderr' in res['stderr'])
self.assertEqual(res['exitcode'], 33)
self.assertTrue(b'stdout' in res['stdout'])
self.assertTrue(b'stderr' in res['stderr'])
def testCallbackRaw(self):
"""Test a callback that returns raw output"""
sf = scriptform.ScriptForm('test_formconfig_callback.json')
fc = sf.get_form_config()
fd = fc.get_form_def('test_raw')
stdout = file('tmp_stdout', 'w+') # can't use StringIO
stderr = file('tmp_stderr', 'w+')
stdout = open('tmp_stdout', 'w+') # can't use StringIO
stderr = open('tmp_stderr', 'w+')
exitcode = runscript.run_script(fd, {}, {}, stdout, stderr)
stdout.seek(0)
stderr.seek(0)
self.assertTrue(exitcode == 33)
self.assertTrue('stdout' in stdout.read())
stdout.close()
stderr.close()
def testCallbackMissingParams(self):
"""
@ -115,7 +115,7 @@ class FormDefinitionTest(unittest.TestCase):
form_values = {"val_string": "1234"}
errors, values = fd.validate(form_values)
self.assertNotIn('val_string', errors)
self.assertEquals(values['val_string'], "1234")
self.assertEqual(values['val_string'], "1234")
def testValidateIntegerInvalid(self):
fd = self.fc.get_form_def('test_val_integer')
@ -143,7 +143,7 @@ class FormDefinitionTest(unittest.TestCase):
form_values = {"val_integer": 6}
errors, values = fd.validate(form_values)
self.assertNotIn('val_integer', errors)
self.assertEquals(values['val_integer'], 6)
self.assertEqual(values['val_integer'], 6)
def testValidateFloatInvalid(self):
fd = self.fc.get_form_def('test_val_float')
@ -171,7 +171,7 @@ class FormDefinitionTest(unittest.TestCase):
form_values = {"val_float": 2.29}
errors, values = fd.validate(form_values)
self.assertNotIn('val_float', errors)
self.assertEquals(values['val_float'], 2.29)
self.assertEqual(values['val_float'], 2.29)
def testValidateDateInvalid(self):
fd = self.fc.get_form_def('test_val_date')
@ -200,14 +200,14 @@ class FormDefinitionTest(unittest.TestCase):
form_values = {"val_date": '2015-03-03'}
errors, values = fd.validate(form_values)
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):
fd = self.fc.get_form_def('test_val_select')
form_values = {"val_select": 'option_a'}
errors, values = fd.validate(form_values)
self.assertNotIn('val_select', errors)
self.assertEquals(values['val_select'], 'option_a')
self.assertEqual(values['val_select'], 'option_a')
def testValidateSelectInvalid(self):
fd = self.fc.get_form_def('test_val_select')
@ -221,14 +221,14 @@ class FormDefinitionTest(unittest.TestCase):
form_values = {"val_checkbox": 'on'}
errors, values = fd.validate(form_values)
self.assertNotIn('val_checkbox', errors)
self.assertEquals(values['val_checkbox'], 'on')
self.assertEqual(values['val_checkbox'], 'on')
def testValidateCheckboxDefaultOn(self):
fd = self.fc.get_form_def('test_val_checkbox_on')
form_values = {"val_checkbox_on": 'off'}
errors, values = fd.validate(form_values)
self.assertNotIn('val_checkbox_on', errors)
self.assertEquals(values['val_checkbox_on'], 'off')
self.assertEqual(values['val_checkbox_on'], 'off')
def testValidateCheckboxInvalid(self):
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_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):
sf.run(listen_port=8002)
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:
time.sleep(0.1)
if cls.sf.running:
if cls.sf.running is True:
break
@classmethod
def tearDownClass(cls):
# Shut down the webserver and wait until it has shut down.
cls.sf.shutdown()
while True:
time.sleep(0.1)
if not cls.sf.running:
if cls.sf.running is False:
break
def testError404(self):
@ -356,25 +361,25 @@ class WebAppTest(unittest.TestCase):
}
import random
f = file('data.csv', 'w')
for i in range(1024):
f.write(chr(random.randint(0, 255)))
f.close()
files = {'file': open('data.csv', 'rb')}
r = requests.post("http://localhost:8002/submit", data=data, files=files, auth=self.auth_user)
self.assertIn('string=12345', r.text)
self.assertIn('integer=12', r.text)
self.assertIn('float=0.6', r.text)
self.assertIn('date=2015-01-02', r.text)
self.assertIn('text=1234567890', r.text)
self.assertIn('password=12345', r.text)
self.assertIn('radio=One', r.text)
self.assertIn('checkbox=on', r.text)
self.assertIn('select=option_a', r.text)
os.unlink('data.csv')
with open('data.csv', 'w') as fh:
for i in range(1024):
fh.write(chr(random.randint(0, 255)))
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)
self.assertIn('string=12345', r.text)
self.assertIn('integer=12', r.text)
self.assertIn('float=0.6', r.text)
self.assertIn('date=2015-01-02', r.text)
self.assertIn('text=1234567890', r.text)
self.assertIn('password=12345', r.text)
self.assertIn('radio=One', r.text)
self.assertIn('checkbox=on', r.text)
self.assertIn('select=option_a', r.text)
os.unlink('data.csv')
def testValidateIncorrectData(self):
data = {
@ -391,26 +396,26 @@ class WebAppTest(unittest.TestCase):
}
import random
f = file('data.txt', 'w')
for i in range(1024):
f.write(chr(random.randint(0, 255)))
f.close()
files = {'file': open('data.txt', 'rb')}
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('Minimum value is 10', r.text)
self.assertIn('Maximum value is 1.0', r.text)
self.assertIn('Maximum value is 2015-02-01', r.text)
self.assertIn('Invalid value for radio button: Ten', r.text)
self.assertIn('Minimum length is 10', r.text)
self.assertIn('Minimum length is 5', r.text)
self.assertIn('Only file types allowed: csv', r.text)
self.assertIn('Invalid value for radio button', r.text)
self.assertIn('Invalid value for dropdown', r.text)
os.unlink('data.txt')
with open('data.txt', 'w') as fh:
for i in range(1024):
fh.write(chr(random.randint(0, 255)))
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)
self.assertIn('Maximum length is 7', r.text)
self.assertIn('Minimum value is 10', r.text)
self.assertIn('Maximum value is 1.0', r.text)
self.assertIn('Maximum value is 2015-02-01', r.text)
self.assertIn('Invalid value for radio button: Ten', r.text)
self.assertIn('Minimum length is 10', r.text)
self.assertIn('Minimum length is 5', r.text)
self.assertIn('Only file types allowed: csv', r.text)
self.assertIn('Invalid value for radio button', r.text)
self.assertIn('Invalid value for dropdown', r.text)
os.unlink('data.txt')
def testValidateRefill(self):
"""
@ -431,23 +436,23 @@ class WebAppTest(unittest.TestCase):
}
import random
f = file('data.txt', 'w')
for i in range(1024):
f.write(chr(random.randint(0, 255)))
f.close()
files = {'file': open('data.txt', 'rb')}
r = requests.post("http://localhost:8002/submit", data=data, files=files, auth=self.auth_user)
self.assertIn('value="123"', r.text)
self.assertIn('value="12"', r.text)
self.assertIn('value="0.6"', r.text)
self.assertIn('value="2015-01-02"', r.text)
self.assertIn('>1234567890<', r.text)
self.assertIn('value="12345"', r.text)
self.assertIn('value="on"', r.text)
self.assertIn('selected>Option B', r.text)
os.unlink('data.txt')
with open('data.txt', 'w') as fh:
for i in range(1024):
fh.write(chr(random.randint(0, 255)))
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)
self.assertIn('value="123"', r.text)
self.assertIn('value="12"', r.text)
self.assertIn('value="0.6"', r.text)
self.assertIn('value="2015-01-02"', r.text)
self.assertIn('>1234567890<', r.text)
self.assertIn('value="12345"', r.text)
self.assertIn('value="on"', r.text)
self.assertIn('selected>Option B', r.text)
os.unlink('data.txt')
def testOutputEscaped(self):
"""Form with 'escaped' output should have HTML entities escaped"""
@ -476,36 +481,36 @@ class WebAppTest(unittest.TestCase):
def testUpload(self):
import random
f = file('data.raw', 'w')
for i in range(1024):
f.write(chr(random.randint(0, 255)))
f.close()
with open('data.raw', 'w') as fh:
fh.write(chr(random.randint(0, 255)))
data = {
"form_name": "upload"
}
files = {'file': open('data.raw', 'rb')}
r = requests.post("http://localhost:8002/submit", files=files, data=data, auth=self.auth_user)
self.assertIn('SAME', r.text)
os.unlink('data.raw')
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)
self.assertIn('SAME', r.text)
os.unlink('data.raw')
def testStaticValid(self):
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''
for c in r.iter_content():
f_served += c
f_orig = file('static/ssh_server.png', 'rb').read()
self.assertEquals(f_orig, f_served)
with open('static/ssh_server.png', 'rb')as fh:
f_orig = fh.read()
self.assertEqual(f_orig, f_served)
def testStaticInvalidFilename(self):
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):
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):
r = requests.get('http://localhost:8002/form?form_name=hidden_field', auth=self.auth_user)
@ -526,14 +531,18 @@ class WebAppSingleTest(unittest.TestCase):
"""
@classmethod
def setUpClass(cls):
# Run the server in a thread, so we can execute the tests in the main
# program.
def server_thread(sf):
sf.run(listen_port=8002)
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:
time.sleep(0.1)
if cls.sf.running:
if cls.sf.running is True:
break
@classmethod
@ -555,7 +564,7 @@ class WebAppSingleTest(unittest.TestCase):
"""
"""
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__':
@ -575,11 +584,11 @@ if __name__ == '__main__':
cov.stop()
cov.save()
print cov.report()
print(cov.report())
try:
print cov.html_report()
except coverage.misc.CoverageException, e:
if "Couldn't find static file 'jquery.hotkeys.js'" in e.message:
print(cov.html_report())
except coverage.misc.CoverageException as err:
if "Couldn't find static file 'jquery.hotkeys.js'" in err.message:
pass
else:
raise

Loading…
Cancel
Save