def test_download_file_dir_read_only(self): download_dir = 'write_test_dir' if os.path.exists(download_dir): os.removedirs(download_dir) os.mkdir(download_dir, os.R_OK) try: download_file('http://img.com', download_dir, 'name') except IOError as err: os.removedirs(download_dir) raise err
def test_download_file_failure(self, m): m.get('http://example.com/image', content=b'abcdef', status_code=403) download_dir = 'test_download' file_name = 'test_file' os.mkdir(download_dir) success, file_path = download_file('http://example.com/image', download_dir, file_name) self.assertFalse(success) self.assertIsNone(file_path) os.removedirs(download_dir)
def test_download_file_success(self, m): m.get('http://example.com/image', content=b'abcdef', status_code=200) download_dir = 'test_download' file_name = 'test_file' os.mkdir(download_dir) success, file_path = download_file('http://example.com/image', download_dir, file_name) self.assertTrue(success) expected_path = '{0}{1}{2}'.format(download_dir, os.path.sep, file_name) self.assertEqual(expected_path, file_path) os.remove(expected_path) os.removedirs(download_dir)
def post(self) -> (str, int): cfg = current_app.config payload = json.loads(request.data) if self.PayloadKey.Project not in payload: return 'invalid payload', HttpStatus.BadRequest project = payload[self.PayloadKey.Project] artefact = self.find_artefact(payload, self.Artefact.Apk) if artefact is not None: success, file_path = download_file(artefact[self.PayloadKey.Url], cfg['DOWNLOAD_DIR'], artefact[self.PayloadKey.Filename]) if success and os.path.exists(file_path): md5_hash = compute_md5_hash(file_path) if md5_hash == artefact[self.PayloadKey.Checksum]: now = datetime.datetime.today() file_name = self.generate_file_name(project, artefact, now) upload_to_google_drive.delay(file_path=file_path, credentials_dir=cfg['CREDENTIALS_DIR'], file_name=file_name, drive_dir=self.GoogleDriveConfig.BuildDir) self.insert_project_if_not_exists(project) self.insert_build_history_entry(project) return 'upload started', HttpStatus.OK else: if os.path.exists(file_path): os.remove(file_path) return 'could not download the artefact: invalid checksum [{0}]'.format(md5_hash),\ HttpStatus.Forbidden return 'could not download the artefact', HttpStatus.Forbidden else: return 'invalid payload', HttpStatus.BadRequest
def test_download_file_none_name(self): # noinspection PyTypeChecker download_file('http://img.com', '/home/test', None)
def test_download_file_empty_name(self): download_file('http://img.com', '/home/test', '')
def test_download_file_none_dir(self): # noinspection PyTypeChecker download_file('http://img.com', None, 'file.txt')
def test_download_file_empty_dir(self): download_file('http://img.com', '', 'file.txt')
def test_download_file_none_url(self): # noinspection PyTypeChecker download_file(None, '/home/test', 'file.txt')
def test_download_file_empty_url(self): download_file('', '/home/test', 'file.txt')