def require_valid_thumbnail_filename(cls, thumbnail_filename, strict=False): """Checks whether the thumbnail filename of the blog post is a valid one. Args: thumbnail_filename: str. The thumbnail filename to validate. strict: bool. Enable strict checks on the blog post when the blog post is published or is going to be published. Raises: ValidationError. Provided thumbnail filename is invalid. """ if strict: if not isinstance(thumbnail_filename, python_utils.BASESTRING): raise utils.ValidationError( 'Expected thumbnail filename to be a string, received: %s.' % thumbnail_filename) if thumbnail_filename == '': raise utils.ValidationError( 'Thumbnail filename should not be empty.') utils.require_valid_image_filename(thumbnail_filename)
def _assert_valid_image_filename( self, expected_error_substring: str, image_filename: str ) -> None: """Helper method for test_require_valid_image_filename.""" with self.assertRaisesRegex( # type: ignore[no-untyped-call] utils.ValidationError, expected_error_substring): utils.require_valid_image_filename( image_filename)
def test_require_valid_image_filename(self) -> None: """Test image filename validation.""" self._assert_valid_image_filename( 'Expected image filename to be a string, received 10', 10) # type: ignore[arg-type] self._assert_valid_image_filename( 'Image filename should not start with a dot.', '.name') self._assert_valid_image_filename( 'Image filename should not include slashes or ' 'consecutive dot characters.', 'file/name') self._assert_valid_image_filename( 'Image filename should not include slashes or ' 'consecutive dot characters.', 'file..name') self._assert_valid_image_filename( 'Image filename should include an extension.', 'name') filename = 'filename.svg' utils.require_valid_image_filename(filename)