def test_get_file_path(self): path1 = views.get_file_path("mykey1") path2 = views.get_file_path("mykey2") self.assertEqual( os.path.join( settings.ORA2_FILEUPLOAD_ROOT, settings.FILE_UPLOAD_STORAGE_BUCKET_NAME, "mykey1" ), os.path.dirname(path1) ) self.assertNotEqual(path1, path2)
def test_hack_get_file_path(self): expected_path = os.path.join( settings.ORA2_FILEUPLOAD_ROOT, settings.FILE_UPLOAD_STORAGE_BUCKET_NAME, "key", "content" ) self.assertEqual( expected_path, os.path.abspath(views.get_file_path("../key")) ) self.assertEqual( expected_path, os.path.abspath(views.get_file_path("../key/")) ) self.assertEqual( expected_path, os.path.abspath(views.get_file_path(" ../key/ ")) )
def test_delete_file_data_on_metadata_saving_error(self): key = "key" file_path = views.get_file_path(key) non_existing_path = "/non/existing/path" with patch('openassessment.fileupload.views_filesystem.get_metadata_path') as mock_get_metadata_path: mock_get_metadata_path.return_value = non_existing_path self.assertRaises( exceptions.FileUploadRequestError, views.save_to_file, "key", "content", "metadata" ) self.assertFalse(os.path.exists(file_path)) self.assertFalse(os.path.exists(non_existing_path))
def test_upload_download(self): upload_url = self.backend.get_upload_url(self.key, self.content_type) download_url = self.backend.get_download_url(self.key) file_path = views.get_file_path(self.key_name) upload_response = self.client.put(upload_url, data=self.content.read(), content_type=self.content_type) download_response = self.client.get(download_url) self.content.seek(0) self.assertIn("/" + self.key, upload_url) self.assertEqual(200, upload_response.status_code) self.assertEqual("", upload_response.content) self.assertEqual(200, download_response.status_code) self.assertEqual( "attachment; filename=" + self.key, download_response.get('Content-Disposition') ) self.assertEqual(self.content_type, download_response.get('Content-Type')) self.assertIn("foobar content", download_response.content) self.assertTrue(os.path.exists(file_path), "File %s does not exist" % file_path) with open(file_path) as f: self.assertEqual(self.content.read(), f.read())
def test_upload_download(self): upload_url = self.backend.get_upload_url(self.key, self.content_type) download_url = self.backend.get_download_url(self.key) file_path = views.get_file_path(self.key_name) upload_response = self.client.put(upload_url, data=self.content.read(), content_type=self.content_type) download_response = self.client.get(download_url) self.content.seek(0) self.assertIn("/" + self.key, upload_url) self.assertEqual(200, upload_response.status_code) self.assertEqual("", upload_response.content) self.assertEqual(200, download_response.status_code) self.assertEqual("attachment; filename=" + self.key, download_response.get('Content-Disposition')) self.assertEqual(self.content_type, download_response.get('Content-Type')) self.assertIn("foobar content", download_response.content) self.assertTrue(os.path.exists(file_path), "File %s does not exist" % file_path) with open(file_path) as f: self.assertEqual(self.content.read(), f.read())
def remove_file(self, key): from openassessment.fileupload.views_filesystem import safe_remove, get_file_path return safe_remove(get_file_path(self._get_key_name(key)))
def _file_exists(self, key_name): from openassessment.fileupload.views_filesystem import get_file_path file_path = get_file_path(key_name) file_path = Path(file_path) return file_path.exists()