From b951fa4fcefe49d6f304fc69e88f8a94f0d325f2 Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Thu, 2 Jul 2015 08:10:50 +0200 Subject: [PATCH] Additional unit tests. --- test/test.py | 88 ++++++++++++++++++++++++-- test/test_formdefinition_validate.json | 54 ++++++++++++++++ test/test_webapp.json | 5 ++ 3 files changed, 141 insertions(+), 6 deletions(-) diff --git a/test/test.py b/test/test.py index 217a8c6..6ad2eeb 100644 --- a/test/test.py +++ b/test/test.py @@ -212,6 +212,61 @@ class FormDefinitionTest(unittest.TestCase): self.assertNotIn('val_date', errors) self.assertEquals(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') + + def testValidateSelectInvalid(self): + fd = self.fc.get_form_def('test_val_select') + form_values = {"val_select": 'option_c'} + errors, values = fd.validate(form_values) + self.assertIn('val_select', errors) + self.assertIn('Invalid value', errors['val_select'][0]) + + def testValidateCheckbox(self): + fd = self.fc.get_form_def('test_val_checkbox') + form_values = {"val_checkbox": 'on'} + errors, values = fd.validate(form_values) + self.assertNotIn('val_checkbox', errors) + self.assertEquals(values['val_checkbox'], 'on') + + def testValidateCheckboxInvalid(self): + fd = self.fc.get_form_def('test_val_checkbox') + form_values = {"val_checkbox": 'true'} + errors, values = fd.validate(form_values) + self.assertIn('val_checkbox', errors) + self.assertIn('Invalid value', errors['val_checkbox'][0]) + + def testValidateTextMin(self): + fd = self.fc.get_form_def('test_val_text') + form_values = {"val_text": '1234'} + errors, values = fd.validate(form_values) + self.assertIn('val_text', errors) + self.assertIn('Minimum', errors['val_text'][0]) + + def testValidateTextMax(self): + fd = self.fc.get_form_def('test_val_text') + form_values = {"val_text": '12345678901'} + errors, values = fd.validate(form_values) + self.assertIn('val_text', errors) + self.assertIn('Maximum', errors['val_text'][0]) + + def testValidateFileMissingFile(self): + fd = self.fc.get_form_def('test_val_file') + form_values = {} + errors, values = fd.validate(form_values) + self.assertIn('val_file', errors) + self.assertIn('Invalid', errors['val_file'][0]) + + def testValidateFileMissingFileName(self): + fd = self.fc.get_form_def('test_val_file') + form_values = {'val_file': 'foo'} + self.assertRaises(KeyError, fd.validate, form_values) + + class WebAppTest(unittest.TestCase): """ Test the web app by actually running the server and making web calls to it. @@ -291,7 +346,8 @@ class WebAppTest(unittest.TestCase): "date": "2015-01-02", "text": "1234567890", "password": "12345", - "radio": "One" + "radio": "One", + "checkbox": "on", } import random @@ -303,9 +359,14 @@ class WebAppTest(unittest.TestCase): files = {'file': open('data.csv', 'rb')} r = requests.post("http://localhost:8002/submit", data=data, files=files, auth=self.auth_user) - r_error = '(.*)' - for match in re.findall(r_error, r.text): - self.assertEquals(match, u'') + 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) def testValidateIncorrectData(self): data = { @@ -317,6 +378,7 @@ class WebAppTest(unittest.TestCase): "radio": "Ten", "text": "123456789", "password": "1234", + "checkbox": "invalidvalue", } import random @@ -336,6 +398,7 @@ class WebAppTest(unittest.TestCase): 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', r.text) def testOutputEscaped(self): """Form with 'escaped' output should have HTML entities escaped""" @@ -360,7 +423,6 @@ class WebAppTest(unittest.TestCase): "string": '' } r = requests.post('http://localhost:8002/submit', data, auth=self.auth_user) - print r.text self.assertIn('string=', r.text) def testUpload(self): @@ -378,7 +440,7 @@ class WebAppTest(unittest.TestCase): self.assertIn('SAME', r.text) os.unlink('data.raw') - def testStatic(self): + def testStaticValid(self): r = requests.get("http://localhost:8002/static?fname=ssh_server.png", auth=self.auth_user) self.assertEquals(r.status_code, 200) f_served = b'' @@ -388,6 +450,14 @@ class WebAppTest(unittest.TestCase): f_orig = file('static/ssh_server.png', 'rb').read() self.assertEquals(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) + + def testStaticInvalidNotFound(self): + r = requests.get("http://localhost:8002/static?fname=nosuchfile.png", auth=self.auth_user) + self.assertEquals(r.status_code, 404) + def testHiddenField(self): r = requests.get('http://localhost:8002/form?form_name=hidden_field', auth=self.auth_user) self.assertIn('class="hidden"', r.text) @@ -432,6 +502,12 @@ class WebAppSingleTest(unittest.TestCase): r = requests.get("http://localhost:8002/") self.assertIn('only_form', r.text) + def testStaticDisabled(self): + """ + """ + r = requests.get("http://localhost:8002/static?fname=nosuchfile.png") + self.assertEquals(r.status_code, 501) + if __name__ == '__main__': logging.basicConfig(level=logging.FATAL, diff --git a/test/test_formdefinition_validate.json b/test/test_formdefinition_validate.json index 704d786..5cbd0bd 100644 --- a/test/test_formdefinition_validate.json +++ b/test/test_formdefinition_validate.json @@ -69,6 +69,60 @@ "max": "2015-03-05" } ] + }, + { + "name": "test_val_select", + "title": "title", + "description": "description", + "script": "test.sh", + "fields": [ + { + "name": "val_select", + "type": "select", + "options": [ + ["option_a", "Option A"], + ["option_b", "Option B"] + ] + } + ] + }, + { + "name": "test_val_checkbox", + "title": "title", + "description": "description", + "script": "test.sh", + "fields": [ + { + "name": "val_checkbox", + "type": "checkbox" + } + ] + }, + { + "name": "test_val_text", + "title": "title", + "description": "description", + "script": "test.sh", + "fields": [ + { + "name": "val_text", + "type": "text", + "minlen": 5, + "maxlen": 10 + } + ] + }, + { + "name": "test_val_file", + "title": "title", + "description": "description", + "script": "test.sh", + "fields": [ + { + "name": "val_file", + "type": "file" + } + ] } ] } diff --git a/test/test_webapp.json b/test/test_webapp.json index 127f239..e8e58bb 100644 --- a/test/test_webapp.json +++ b/test/test_webapp.json @@ -132,6 +132,11 @@ "required": true, "minlen": 5 }, + { + "name": "checkbox", + "title": "A checkbox input field", + "type": "checkbox" + }, { "name": "file", "title": "A file upload field",