def test_clean_zip__invalid(self): form = ThemeForm() data_file = self._data_file('zips/invalid.zip') form.cleaned_data = {'theme_files_zip': data_file} self.assertRaisesMessage(ValidationError, 'Must be a valid zip archive.', form.clean_theme_files_zip) data_file.close()
def test_clean_zip__evil_relative(self): form = ThemeForm() data_file = self._data_file('zips/evil_relative.zip') form.cleaned_data = {'theme_files_zip': data_file} zip_file = zipfile.ZipFile(data_file, 'r') namelist = zip_file.namelist() zip_file.close() self.assertEqual(len(namelist), 1) self.assertTrue('..' in namelist[0].split('/')) self.assertRaisesMessage(ValidationError, 'Zip archive contains invalid names.', form.clean_theme_files_zip) data_file.close()
def test_clean_zip__empty(self): form = ThemeForm() data_file = self._data_file('zips/empty.zip') form.cleaned_data = {'theme_files_zip': data_file} # Python 2.6 can't actually open this empty file because of a bug. if is_zipfile(data_file): zip_file = zipfile.ZipFile(data_file) self.assertEqual(len(zip_file.namelist()), 0) zip_file.close() self.assertRaisesMessage(ValidationError, 'Zip archive cannot be empty.', form.clean_theme_files_zip) else: self.assertRaisesMessage(ValidationError, 'Must be a valid zip archive.', form.clean_theme_files_zip) data_file.close()
def test_clean_zip__valid(self): form = ThemeForm() data_file = self._data_file('zips/theme.zip') form.cleaned_data = {'theme_files_zip': data_file} self.assertEqual(form.clean_theme_files_zip(), data_file) data_file.close()