def test_confirm_submission_success_noaction_withpom(self): userobj = User.objects.create_user(username='******', password='******', is_superuser=True) userobj.save() jarfile = SimpleUploadedFile('SomeApp-1.0.0.jar', b'data', content_type='application/zip') pending = views._create_pending(userobj, 'SomeApp', '1.0.0', 'workswith', [], jarfile) testpomfile = SimpleUploadedFile('pom.xml', PomParseTest.get_valid_pom_as_bytes(), content_type='text/xml') testpomfile.open(mode='r') pending.pom_xml_file.save(os.path.basename(testpomfile.name), testpomfile) testpomfile.close() res = self.client.login(username='******', password='******') self.assertTrue(res) response = self.client.post('/submit_app/confirm/' + str(pending.id), follow=True) self.assertEqual(200, response.status_code) self.assertTrue(b'Cytoscape App Store - ' b'Confirm Submission' in response.content) self.assertTrue(b'es.imim' in response.content) self.assertTrue(b'DisGeNET' in response.content) self.assertTrue(b'6.3.2' in response.content)
def test_verify_javadocs_jar_invalid_zip(self): testzipfile = SimpleUploadedFile('javadoc.jar', b'invalidzip', content_type='application/zip') testzipfile.open(mode='rb') res = views._verify_javadocs_jar(testzipfile) self.assertTrue('not a valid jar' in res) testzipfile.close()
def populate_project(self): f = SimpleUploadedFile("questions", "\n".join(self.quiz.keys())) f.open("rb") context = {"action": "Upload", "upload": f} target = WebTarget("POST", main.views.project.project_upload, args=(self.project.id,)) expectation = ViewExpectation(Conditions.null(), target) expectation.check(self, context)
def populate_project(self): f = SimpleUploadedFile("questions", "\n".join(list(self.quiz.keys()))) f.open("rb") context = {"action": "Upload", "upload": f} target = WebTarget("POST", main.views.project.project_upload, args=(self.project.id, )) expectation = ViewExpectation(Conditions.null(), target) expectation.check(self, context)
def test_changing_size(self, mock_is_testing_mode): mock_is_testing_mode.return_value = False TESTFILE = "small_kitten.jpeg" data = SimpleUploadedFile(name=TESTFILE, content=open(TESTFILE, "rb").read()) initial_size = data.size optimize_from_buffer(data) resized_data_size = data.size data.open() actual_final_size = len(data.read()) self.assertEqual(actual_final_size, resized_data_size) self.assertLess(actual_final_size, initial_size, msg="Test not valid - image was not reduced")
def test_verify_javadocs_jar_with_double_dot_in_file(self): temp_dir = tempfile.mkdtemp() try: temp_zip = os.path.join(temp_dir, 'foo.jar') zf = zipfile.ZipFile(temp_zip, mode='w') zf.writestr('yo/../hi.xml', 'somedata') zf.close() with open(temp_zip, 'rb') as f: zipdata = f.read() testzipfile = SimpleUploadedFile('javadoc.jar', zipdata, content_type='application/zip') testzipfile.open(mode='rb') res = views._verify_javadocs_jar(testzipfile) self.assertTrue('path that is illegal: yo/../hi.xml' in res) testzipfile.close() finally: shutil.rmtree(temp_dir)
def test_verify_javadocs_jar_valid_zip(self): temp_dir = tempfile.mkdtemp() try: temp_zip = os.path.join(temp_dir, 'foo.jar') zf = zipfile.ZipFile(temp_zip, mode='w') zf.writestr('hi.xml', 'somedata') zf.close() with open(temp_zip, 'rb') as f: zipdata = f.read() testzipfile = SimpleUploadedFile('javadoc.jar', zipdata, content_type='application/zip') testzipfile.open(mode='rb') res = views._verify_javadocs_jar(testzipfile) self.assertEqual(None, res) testzipfile.close() finally: shutil.rmtree(temp_dir)
class CVResumeTests(APITestCase): """ Tests CVResume model. """ def setUp(self): self.user = create_user() # Create a file like objects representing uploaded data self.cv = SimpleUploadedFile('my_cv.pdf', b'Impresive CV') self.resume = SimpleUploadedFile('my_resume.pdf', b'Impresive Resume') def tearDown(self): """ Close open files. """ self.cv.close() self.resume.close() def test_cv_created(self): """ Test whether a cv is saved correctly. """ # Pass a cv file object to CVResume to save to db/disk CVResume.objects.create( user=self.user, cv=self.cv ) self.cv.open() # Reopen the cv object - set seek to 0 cv_resume = CVResume.objects.get(pk=1) self.assertEqual( self.cv.read(), cv_resume.cv.read(), # Compare file like objects 'Created cv does not match the provided one.' ) def test_resume_created(self): """ Test whether a resume is saved correctly. """ # Pass a resume file object to CVResume to save to db/disk CVResume.objects.create( user=self.user, resume=self.resume ) self.resume.open() # Reopen the resume object - set seek to 0 cv_resume = CVResume.objects.get(pk=1) self.assertEqual( self.resume.read(), cv_resume.resume.read(), # Compare file like objects 'Created reusme does not match the provided one.' ) def test_cv_and_resume_created(self): """ Test whether both cv and resume are saved correctly. """ CVResume.objects.create( user=self.user, cv=self.cv, resume=self.resume ) # Set file positions to the begining self.cv.open() self.resume.open() # Retrieve the saved object cv_resume = CVResume.objects.get(pk=1) # Check if cv match self.assertEqual( self.cv.read(), cv_resume.cv.read(), # Compare file like objects 'Created cv does not match the provided one.' ) # Check if resume match self.assertEqual( self.resume.read(), cv_resume.resume.read(), # Compare file like objects 'Created reusme does not match the provided one.' )
class AttachmentUploadTestCase(TestCase): def setUp(self): self.api_url = '/api/attachments' self.file = SimpleUploadedFile('test', b'0' * 2 ** 25) self.md5 = '' def tearDown(self): try: attachment = Attachment.objects.get(md5=self.md5) except: pass else: attachment.delete_file() def test_attachment_upload(self): HTTP_CONTENT_RANGE = 'bytes {start}-{end}/{total}' chunk_size = 2097152 md5_value = md5() while True: data_flow = self.file.read(chunk_size) # 每次读入2M进入内存 if not data_flow: # 读取完后返回空值,False break md5_value.update(data_flow) self.md5 = md5_value.hexdigest() url = self.api_url + '/' + self.md5 start = 0 end = 0 self.file.open('rb') while True: start = end end = start + chunk_size data_flow = self.file.read(chunk_size) if not data_flow or end >= self.file._size / 2: # 模拟文件上传到一半断开连接 break headers = { 'HTTP_CONTENT_RANGE': HTTP_CONTENT_RANGE.format( start=start, end=end, total=self.file._size ) } res = self.client.put(url, encode_multipart(BOUNDARY, { 'filename': self.file.name, 'file': SimpleUploadedFile('test', data_flow) }), content_type=MULTIPART_CONTENT, **headers) self.assertEqual(res.status_code, 200) res = self.client.get(url) self.assertEqual(res.status_code, 200) status = 1 # 上传中的状态 offset = res.json()['offset'] start = offset end = offset self.file.open('rb') self.file.seek(offset) while True: start = end end = start + chunk_size data_flow = self.file.read(chunk_size) if not data_flow: break headers = { 'HTTP_CONTENT_RANGE': HTTP_CONTENT_RANGE.format( start=start, end=end, total=self.file._size ) } res = self.client.put(url, encode_multipart(BOUNDARY, { 'filename': self.file.name, 'file': SimpleUploadedFile('test', data_flow) }), content_type=MULTIPART_CONTENT, **headers) self.assertEqual(res.status_code, 200) status = res.json()['status'] self.assertEqual(status, 2)