From 9f7b7ddc16a95d7de8bcd83b6d873d859d99eac0 Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Tue, 26 May 2015 09:20:12 +0200 Subject: [PATCH] More unit tests. --- test/test.py | 30 +++++++++++++++++++++++++++--- test/test_upload.sh | 10 ++++++++++ test/test_webapp.json | 13 +++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100755 test/test_upload.sh diff --git a/test/test.py b/test/test.py index 6d653b6..649cd9c 100644 --- a/test/test.py +++ b/test/test.py @@ -90,7 +90,6 @@ class FormDefinitionTest(unittest.TestCase): def testRequired(self): fd = self.fc.get_form_def('test_required') - form_values = {} errors, values = fd.validate(form_values) self.assertIn('string', errors) @@ -234,14 +233,24 @@ class WebAppTest(unittest.TestCase): r = requests.get('http://localhost:8002/') self.assertEqual(r.status_code, 401) - def testAuthFormNoAuth(self): + def testAuthFormNoAuthGet(self): r = requests.get('http://localhost:8002/form?form_name=admin_only') self.assertEqual(r.status_code, 401) - def testAuthFormWrongAuth(self): + def testAuthFormNoAuthPost(self): + data = {"form_name": 'admin_only'} + r = requests.post('http://localhost:8002/submit', data) + self.assertEqual(r.status_code, 401) + + def testAuthFormWrongAuthGet(self): r = requests.get('http://localhost:8002/form?form_name=admin_only', auth=self.auth_user) self.assertEqual(r.status_code, 401) + def testAuthFormWrongAuthPost(self): + data = {"form_name": 'admin_only'} + r = requests.post('http://localhost:8002/submit', data, auth=self.auth_user) + self.assertEqual(r.status_code, 401) + def testHidden(self): """Hidden forms shouldn't appear in the output""" r = requests.get('http://localhost:8002/', auth=self.auth_user) @@ -275,6 +284,21 @@ class WebAppTest(unittest.TestCase): r = requests.post('http://localhost:8002/submit', data, auth=self.auth_user) self.assertIn('string=', r.text) + def testUpload(self): + import random + f = file('data.raw', 'w') + for i in range(1024): + f.write(chr(random.randint(0, 255))) + f.close() + + 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') + if __name__ == '__main__': import coverage diff --git a/test/test_upload.sh b/test/test_upload.sh new file mode 100755 index 0000000..699d628 --- /dev/null +++ b/test/test_upload.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +MD5_UPLOAD=$(md5sum ${file} | cut -d" " -f1) +MD5_ORIG=$(md5sum "data.raw" | cut -d" " -f1) + +if [ "$MD5_UPLOAD" = "$MD5_ORIG" ]; then + echo "SAME" +else + echo "DIFFERENT" +fi diff --git a/test/test_webapp.json b/test/test_webapp.json index cf96c8e..2ec4fa9 100644 --- a/test/test_webapp.json +++ b/test/test_webapp.json @@ -125,6 +125,19 @@ "extensions": ["csv"] } ] + }, + { + "name": "upload", + "title": "Upload", + "description": "Upload", + "script": "test_upload.sh", + "fields": [ + { + "name": "file", + "title": "File upload", + "type": "file" + } + ] } ] }