def test_custom_message(self): validator = validators.ExtensionValidator( allowed=['mp3'], message='invalid extension: %(ext)s') with pytest.raises(ValidationError) as exc: with make_dummy_file('something.pdf') as fp: validator(fp) assert (exc.value.messages[0] == "invalid extension: pdf")
def test_fail(self): validator = validators.ExtensionValidator(allowed=['pdf']) with make_dummy_file('something.avi') as fp: with pytest.raises(ValidationError) as exc: validator(fp) assert (exc.value.messages[0] == "File `something.avi` has an invalid extension. " "Valid extension(s): pdf")
def test_run_validators(): with File(io.BytesIO(), name='file.jpeg') as file: with Image.new('RGB', (640, 480)) as img: img.save(file, format='JPEG') try: helpers.run_validators( file, [ validators.ExtensionValidator(['jpg']), validators.ImageMinSizeValidator(800, 600), validators.ImageMaxSizeValidator(1024, 800), ], ) except ValidationError as exc: assert len(exc.messages) == 2 assert ("has an invalid extension. Valid extension(s): jpg" in exc.messages[0]) assert ("is too small. Image should be at least 800x600 pixels." in exc.messages[1])
def test_help_text(self): validator = validators.ExtensionValidator(allowed=['pdf', 'mp3']) assert str(validator.get_help_text()) == 'Allowed extensions: pdf, mp3'
def test_case_insensitive(self): validator = validators.ExtensionValidator(allowed=['Pdf']) with make_dummy_file('something.PDF') as fp: validator(fp)
def test_format_extension_list(self): validator = validators.ExtensionValidator(allowed=[ 'jpg', 'Gif', 'jpeg', 'JPEG', 'PNG', 'gif', '.png', '.Jpg' ]) assert validator.allowed == ('jpg', 'gif', 'jpeg', 'png')