diff options
| author | jfriedli | 2020-03-27 10:59:16 -0700 |
|---|---|---|
| committer | jvoisin | 2020-03-27 10:59:16 -0700 |
| commit | 7104b2107db592756f4090ba772869eb86d6a44f (patch) | |
| tree | 4864a5c07d3f47f5952ef7164507881918ce77e5 /test/test_api.py | |
| parent | d50f68ae446df413fc6459be9933f373e9f7cdda (diff) | |
file removal background jobv0.2.0
Diffstat (limited to '')
| -rw-r--r-- | test/test_api.py | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/test/test_api.py b/test/test_api.py index de297c4..3074bd5 100644 --- a/test/test_api.py +++ b/test/test_api.py | |||
| @@ -4,9 +4,10 @@ import json | |||
| 4 | import os | 4 | import os |
| 5 | import shutil | 5 | import shutil |
| 6 | import zipfile | 6 | import zipfile |
| 7 | |||
| 8 | from six import BytesIO | 7 | from six import BytesIO |
| 9 | 8 | ||
| 9 | from unittest.mock import patch | ||
| 10 | |||
| 10 | import main | 11 | import main |
| 11 | 12 | ||
| 12 | 13 | ||
| @@ -122,6 +123,23 @@ class Mat2APITestCase(unittest.TestCase): | |||
| 122 | rv = self.app.get('/api/extension', headers={'Origin': 'origin1.gnu'}) | 123 | rv = self.app.get('/api/extension', headers={'Origin': 'origin1.gnu'}) |
| 123 | self.assertEqual(rv.headers['Access-Control-Allow-Origin'], 'origin1.gnu') | 124 | self.assertEqual(rv.headers['Access-Control-Allow-Origin'], 'origin1.gnu') |
| 124 | 125 | ||
| 126 | def test_api_cleaning_failed(self): | ||
| 127 | request = self.app.post('/api/upload', | ||
| 128 | data='{"file_name": "test_name.zip", ' | ||
| 129 | '"file": "UEsDBBQACAAIAPicPE8AAAAAAAAAAAAAAAAXACAAZmFpbGluZy5ub3Qt' | ||
| 130 | 'd29ya2luZy1leHRVVA0AB+Saj13kmo9d5JqPXXV4CwABBOkDAAAE6QMAAAMAUEsHCAAA' | ||
| 131 | 'AAACAAAAAAAAAFBLAwQUAAgACAD6nDxPAAAAAAAAAAAAAAAACQAgAHRlc3QuanNvblVUD' | ||
| 132 | 'QAH6JqPXeiaj13omo9ddXgLAAEE6QMAAATpAwAAAwBQSwcIAAAAAAIAAAAAAAAAUEsBAhQD' | ||
| 133 | 'FAAIAAgA+Jw8TwAAAAACAAAAAAAAABcAIAAAAAAAAAAAAKSBAAAAAGZhaWxpbmcubm90LXd' | ||
| 134 | 'vcmtpbmctZXh0VVQNAAfkmo9d5JqPXeSaj111eAsAAQTpAwAABOkDAABQSwECFAMUAAgACAD6' | ||
| 135 | 'nDxPAAAAAAIAAAAAAAAACQAgAAAAAAAAAAAApIFnAAAAdGVzdC5qc29uVVQNAAfomo9d6JqPXe' | ||
| 136 | 'iaj111eAsAAQTpAwAABOkDAABQSwUGAAAAAAIAAgC8AAAAwAAAAAAA"}', | ||
| 137 | headers={'content-type': 'application/json'} | ||
| 138 | ) | ||
| 139 | error = json.loads(request.data.decode('utf-8'))['message'] | ||
| 140 | self.assertEqual(error, 'Unable to clean application/zip') | ||
| 141 | |||
| 142 | |||
| 125 | def test_api_download(self): | 143 | def test_api_download(self): |
| 126 | request = self.app.post('/api/upload', | 144 | request = self.app.post('/api/upload', |
| 127 | data='{"file_name": "test_name.jpg", ' | 145 | data='{"file_name": "test_name.jpg", ' |
| @@ -263,7 +281,6 @@ class Mat2APITestCase(unittest.TestCase): | |||
| 263 | ) | 281 | ) |
| 264 | 282 | ||
| 265 | response = json.loads(request.data.decode('utf-8')) | 283 | response = json.loads(request.data.decode('utf-8')) |
| 266 | print(response) | ||
| 267 | self.assertEqual(response['message']['download_list'][0]['0'][0]['file_name'][0], 'required field') | 284 | self.assertEqual(response['message']['download_list'][0]['0'][0]['file_name'][0], 'required field') |
| 268 | self.assertEqual(response['message']['download_list'][0]['0'][0]['key'][0], 'required field') | 285 | self.assertEqual(response['message']['download_list'][0]['0'][0]['key'][0], 'required field') |
| 269 | self.assertEqual(request.status_code, 400) | 286 | self.assertEqual(request.status_code, 400) |
| @@ -344,6 +361,34 @@ class Mat2APITestCase(unittest.TestCase): | |||
| 344 | response = json.loads(request.data.decode('utf-8')) | 361 | response = json.loads(request.data.decode('utf-8')) |
| 345 | self.assertEqual('File not found', response['message']) | 362 | self.assertEqual('File not found', response['message']) |
| 346 | 363 | ||
| 364 | @patch('file_removal_scheduler.random.randint') | ||
| 365 | def test_api_upload_leftover(self, randint_mock): | ||
| 366 | os.environ['MAT2_MAX_FILE_AGE_FOR_REMOVAL'] = '0' | ||
| 367 | app = main.create_app() | ||
| 368 | self.upload_folder = tempfile.mkdtemp() | ||
| 369 | app.config.update( | ||
| 370 | TESTING=True, | ||
| 371 | UPLOAD_FOLDER=self.upload_folder | ||
| 372 | ) | ||
| 373 | app = app.test_client() | ||
| 374 | randint_mock.return_value = 1 | ||
| 375 | self.upload_download_test_jpg_and_assert_response_code(app, 200) | ||
| 376 | randint_mock.return_value = 0 | ||
| 377 | self.upload_download_test_jpg_and_assert_response_code(app, 404) | ||
| 378 | |||
| 379 | os.environ['MAT2_MAX_FILE_AGE_FOR_REMOVAL'] = '9999' | ||
| 380 | |||
| 381 | def upload_download_test_jpg_and_assert_response_code(self, app, code): | ||
| 382 | request = app.post('/api/upload', | ||
| 383 | data='{"file_name": "test_name.jpg", ' | ||
| 384 | '"file": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAf' | ||
| 385 | 'FcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="}', | ||
| 386 | headers={'content-type': 'application/json'} | ||
| 387 | ) | ||
| 388 | download_link = json.loads(request.data.decode('utf-8'))['download_link'] | ||
| 389 | request = app.get(download_link) | ||
| 390 | self.assertEqual(code, request.status_code) | ||
| 391 | |||
| 347 | 392 | ||
| 348 | if __name__ == '__main__': | 393 | if __name__ == '__main__': |
| 349 | unittest.main() | 394 | unittest.main() |
