mat2-web/tests.py

82 lines
2.5 KiB
Python
Raw Normal View History

2018-12-16 20:36:02 +01:00
import unittest
2018-12-16 21:33:18 +01:00
import tempfile
import shutil
import io
2018-12-16 20:36:02 +01:00
import main
class FlaskrTestCase(unittest.TestCase):
def setUp(self):
main.app.testing = True
2018-12-16 21:33:18 +01:00
main.app.config['UPLOAD_FOLDER'] = tempfile.mkdtemp()
2018-12-16 20:36:02 +01:00
self.app = main.app.test_client()
2018-12-16 21:33:18 +01:00
def tearDown(self):
shutil.rmtree(main.app.config['UPLOAD_FOLDER'])
2018-12-16 20:36:02 +01:00
def test_get_root(self):
rv = self.app.get('/')
self.assertIn(b'mat2-web', rv.data)
def test_check_mimetypes(self):
rv = self.app.get('/')
self.assertIn(b'application/zip', rv.data)
self.assertIn(b'audio/x-flac', rv.data)
2018-12-16 20:36:02 +01:00
def test_get_download_dangerous_file(self):
rv = self.app.get('/download/\..\filename')
self.assertEqual(rv.status_code, 302)
def test_get_download_nonexistant_file(self):
rv = self.app.get('/download/non_existant')
self.assertEqual(rv.status_code, 302)
2018-12-16 21:33:18 +01:00
def test_get_upload_without_file(self):
rv = self.app.post('/')
self.assertEqual(rv.status_code, 302)
def test_get_upload_empty_file(self):
rv = self.app.post('/',
data=dict(
file=(io.BytesIO(b""), 'test.pdf'),
), follow_redirects=False)
self.assertEqual(rv.status_code, 302)
def test_get_upload_empty_file_redir(self):
rv = self.app.post('/',
data=dict(
file=(io.BytesIO(b""), 'test.pdf'),
), follow_redirects=True)
self.assertIn(b'The type application/pdf is not supported',
rv.data)
self.assertEqual(rv.status_code, 200)
2018-12-16 21:41:23 +01:00
def test_get_upload_no_file_name(self):
rv = self.app.post('/',
data=dict(
file=(io.BytesIO(b"aaa"), ''),
), follow_redirects=True)
self.assertIn(b'No file part', rv.data)
self.assertEqual(rv.status_code, 200)
2018-12-16 21:37:15 +01:00
def test_get_upload_harmless_file(self):
rv = self.app.post('/',
data=dict(
file=(io.BytesIO(b"Some text"), 'test.txt'),
), follow_redirects=True)
self.assertIn(b'/download/test.cleaned.txt', rv.data)
self.assertEqual(rv.status_code, 200)
rv = self.app.get('/download/test.cleaned.txt')
self.assertEqual(rv.status_code, 200)
rv = self.app.get('/download/test.cleaned.txt')
self.assertEqual(rv.status_code, 302)
2018-12-16 21:33:18 +01:00
2018-12-16 20:36:02 +01:00
if __name__ == '__main__':
unittest.main()