def test_download_solution( self, exercise: Exercise, student_user: User, ): storage = self.create_zipfile_storage() hash_ = hashing.by_file(storage) solution = conftest.create_solution( exercise=exercise, student_user=student_user, files=list(TestDownloadSolution.get_zip_files()), hash_=hash_, ) client = conftest.get_logged_user(student_user.username) download_response = client.get(f'{routes.DOWNLOADS}/{solution.id}') downloaded_bytes_file = BytesIO(download_response.data) downloaded_zipfile = ZipFile(downloaded_bytes_file, 'r') exist_zipfile = ZipFile(self.zipfile_file, 'r') for exist_filename, downloaded_filename in zip( exist_zipfile.namelist(), downloaded_zipfile.namelist(), ): assert exist_filename == downloaded_filename with exist_zipfile.open(exist_filename, 'r') as exist_file: with downloaded_zipfile.open( downloaded_filename, 'r', ) as downloaded_file: assert exist_file.read() == downloaded_file.read()
def new(user: User, file: FileStorage) -> Tuple[List[int], List[int]]: solution_hash = hashing.by_file(file) if _is_uploaded_before(user, solution_hash): raise AlreadyExists('You try to reupload an old solution.') matches: List[int] = [] misses: List[int] = [] for exercise_id, files in Extractor(file): try: solution = _upload_to_db(exercise_id, user, files, solution_hash) _run_auto_checks(solution) except (UploadError, AlreadyExists) as e: logger.debug(e) misses.append(exercise_id) else: matches.append(exercise_id) return matches, misses
def test_download_solution( self, exercise: Exercise, student_user: User, ): storage = self.create_zipfile_storage() hash_ = hashing.by_file(storage) conftest.create_solution( exercise=exercise, student_user=student_user, files=list(TestDownloadSolution.get_zip_files()), hash_=hash_, ) client = webapp.test_client() client.post( '/login', data=dict( # noqa: S106 username=student_user.username, password='******', ), follow_redirects=True, ) download_response = client.get('/download/1') downloaded_bytes_file = BytesIO(download_response.data) downloaded_zipfile = ZipFile(downloaded_bytes_file, 'r') exist_zipfile = ZipFile(self.zipfile_file, 'r') for exist_filename, downloaded_filename in zip( exist_zipfile.namelist(), downloaded_zipfile.namelist(), ): assert exist_filename == downloaded_filename with exist_zipfile.open(exist_filename, 'r') as exist_file: with downloaded_zipfile.open( downloaded_filename, 'r', ) as downloaded_file: assert exist_file.read() == downloaded_file.read()