diff --git a/app/main/forms.py b/app/main/forms.py index 4b1deece3..fd0e85f50 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -53,6 +53,7 @@ from app.main.validators import ( NoCommasInPlaceHolders, NoEmbeddedImagesInSVG, NoPlaceholders, + NoTextInSVG, OnlySMSCharacters, ValidEmail, ValidGovEmail, @@ -1884,7 +1885,8 @@ class SVGFileUpload(StripWhitespaceForm): validators=[ FileAllowed(['svg'], 'SVG Images only!'), DataRequired(message="You need to upload a file to submit"), - NoEmbeddedImagesInSVG() + NoEmbeddedImagesInSVG(), + NoTextInSVG(), ] ) diff --git a/app/main/validators.py b/app/main/validators.py index ec46113ac..949c1e941 100644 --- a/app/main/validators.py +++ b/app/main/validators.py @@ -1,4 +1,5 @@ import re +from abc import ABC, abstractmethod from notifications_utils.field import Field from notifications_utils.formatters import formatted_list @@ -77,18 +78,35 @@ class NoCommasInPlaceHolders: raise ValidationError(self.message) -class NoEmbeddedImagesInSVG: +class NoElementInSVG(ABC): - def __init__(self, message='This SVG has an embedded raster image in it and will not render well'): - self.message = message + @property + @abstractmethod + def element(self): + pass + + @property + @abstractmethod + def message(self): + pass def __call__(self, form, field): - is_image_embedded = ' + + ''', + 'This SVG has an embedded raster image in it and will not render well', + ), + ( + ''' + + Will render differently depending on fonts installed + + ''', + 'This SVG has text which has not been converted to paths and may not render well', + ), +)) +def test_create_letter_branding_fails_validation_when_uploading_SVG_with_bad_element( mocker, - platform_admin_client, - fake_uuid + client_request, + platform_admin_user, + fake_uuid, + svg_contents, + expected_error, ): filename = 'test.svg' mock_s3_upload = mocker.patch('app.s3_client.s3_logo_client.utils_s3upload') - response = platform_admin_client.post( - url_for('.create_letter_branding'), - data={'file': (BytesIO(""" - - - """.encode('utf-8')), filename)}, - follow_redirects=True, + client_request.login(platform_admin_user) + page = client_request.post( + '.create_letter_branding', + _data={'file': (BytesIO(svg_contents.encode('utf-8')), filename)}, + _follow_redirects=True, ) - page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') - assert normalize_spaces(page.find('h1').text) == "Add letter branding" - message = 'This SVG has an embedded raster image in it and will not render well' - assert normalize_spaces(page.find("span", {"class": "error-message"}).text) == message + assert normalize_spaces(page.select_one(".error-message").text) == expected_error assert page.findAll('div', {'id': 'logo-img'}) == []