def test_OrValidator_failure_with_exception(): toss_error = mock.MagicMock( side_effect=UploadError('something bad happened')) or_validator = validators.OrValidator(toss_error, toss_error, toss_error) with pytest.raises(UploadError) as excinfo: or_validator(DummyFile(filename='wolololo.wav'), {}) assert toss_error.call_count == 3 assert len(excinfo.value.args[0]) == 3 assert all('something bad happened' == e for e in excinfo.value.args[0])
def test_AndValidator_failure_with_exception(): toss_error = mock.MagicMock( side_effect=UploadError('something bad happened')) dummy_file = DummyFile(filename='kindamatters.exe') and_validator = validators.AndValidator(toss_error, toss_error) with pytest.raises(UploadError) as excinfo: and_validator(dummy_file, {}) assert 'something bad happened' == str(excinfo.value)
def check_disk_usage(filehandle, meta): """Checks the upload directory to see if the uploaded file would exceed the total disk allotment. Meant as a quick and dirty example. """ # limit it at twenty kilobytes if no default is provided MAX_DISK_USAGE = current_app.config.get('MAX_DISK_USAGE', 20 * 1024) CURRENT_USAGE = really_bad_du(current_app.config['UPLOAD_PATH']) filehandle.seek(0, os.SEEK_END) if CURRENT_USAGE + filehandle.tell() > MAX_DISK_USAGE: filehandle.close() raise UploadError("Upload exceeds allotment.") filehandle.seek(0) return filehandle
def filename_all_lower(filehandle, metadata): "Validates that the filename is all lowercase." if not filehandle.filename.islower(): raise UploadError('require lowercase filename') return True
def toss_error(fh, m): raise UploadError('never actually seen')
def derp(filehandle, meta): raise UploadError(str(next(counter)))
def derp(filehandle, meta): raise UploadError('error')