Repository: oppia/oppia

Issue:
Add validation for the svg_filename in the new Math RTE schema
Add validation for the svg_filename in the new Math RTE schema once the functionality for generating SVGs for Math expressions is ready.
Also, add proper SVG filenames in the test files
This can be found by grepping "TODO(#9379)" in the codebase.
Pls look in file  `/extensions/rich_text_components/components_test.py`


Discussion:
@donosco98 Please note that TODOs should point to the issue number, not a username.

Perhaps you could also list the relevant files to look at in this issue thread.

Resolving patch:
diff --git a/extensions/rich_text_components/components.py b/extensions/rich_text_components/components.py
--- a/extensions/rich_text_components/components.py
+++ b/extensions/rich_text_components/components.py
@@ -142,7 +142,16 @@ class Link(BaseRteComponent):
 class Math(BaseRteComponent):
     """Class for Math component."""
 
-    pass
+    @classmethod
+    def validate(cls, value_dict):
+        """Validates Math component."""
+        super(Math, cls).validate(value_dict)
+        filename_pattern_regex = constants.constants.MATH_SVG_FILENAME_REGEX
+        filename = value_dict['math_content-with-value']['svg_filename']
+        if not re.match(filename_pattern_regex, filename):
+            raise Exception(
+                'Invalid svg_filename attribute in math component: %s' % (
+                    filename))
 
 
 class Skillreview(BaseRteComponent):


Test patch:
diff --git a/core/domain/html_validation_service_test.py b/core/domain/html_validation_service_test.py
--- a/core/domain/html_validation_service_test.py
+++ b/core/domain/html_validation_service_test.py
@@ -927,8 +927,9 @@ def test_validate_customization_args(self):
         ), (
             '<p><oppia-noninteractive-math math_content-with-value="'
             '{&amp;quot;raw_latex&amp;quot;:&amp;quot;abc&amp;quot;'
-            ',&amp;quot;svg_filename&amp;quot;:&amp;quot;&amp;quot;}">'
-            '</oppia-noninteractive-math></p>'
+            ',&amp;quot;svg_filename&amp;quot;:&amp;quot;mathImg_202'
+            '07261338_r3ir43lmfd_height_2d456_width_6d124_vertical_0'
+            'd231.svg&amp;quot;}"></oppia-noninteractive-math></p>'
         ), (
             '<p><oppia-noninteractive-math url-with-value="&amp;quot;'
             'http://link.com&amp;quot;></oppia-noninteractive-math></p>'
diff --git a/extensions/rich_text_components/components_test.py b/extensions/rich_text_components/components_test.py
--- a/extensions/rich_text_components/components_test.py
+++ b/extensions/rich_text_components/components_test.py
@@ -131,19 +131,21 @@ def test_link_validation(self):
         self.check_validation(
             components.Link, valid_items, invalid_items_with_error_messages)
 
-    # TODO(#9379): Add validations for svg_filename field and add proper
-    # values in the tests.
     def test_math_validation(self):
         """Tests collapsible component validation."""
         valid_items = [{
             'math_content-with-value': {
                 u'raw_latex': u'123456',
-                u'svg_filename': u''
+                u'svg_filename': (
+                    u'mathImg_20201216_331234_r3ir43lmfd_height_2d456_width_6d1'
+                    '24_vertical_0d231.svg')
             }
         }, {
             'math_content-with-value': {
                 u'raw_latex': u'\\frac{x}{y}',
-                u'svg_filename': u''
+                u'svg_filename': (
+                    u'mathImg_20200216_133832_imzlvnf23a_height_4d123_width_23d'
+                    '122_vertical_2d123.svg')
             }
         }]
         invalid_items_with_error_messages = [
@@ -166,7 +168,35 @@ def test_math_validation(self):
                     u'raw_latex': 123,
                     u'svg_filename': 11
                 }
-            }, 'Expected unicode string, received 123')]
+            }, 'Expected unicode string, received 123'),
+            ({
+                'math_content-with-value': {
+                    u'raw_latex': 'x^2',
+                    u'svg_filename': 'img.svg'
+                }
+            }, 'Invalid svg_filename attribute in math component: img.svg'),
+            ({
+                'math_content-with-value': {
+                    u'raw_latex': 'x^3',
+                    u'svg_filename': (
+                        u'invalid_2020761338_imzlvnf23a_height_4d123_width_23d'
+                        '122_vertical_2d123.svg')
+                }
+            }, (
+                'Invalid svg_filename attribute in math component: invalid_202'
+                '0761338_imzlvnf23a_height_4d123_width_23d122_vertical_2d123'
+                '.svg')),
+            ({
+                'math_content-with-value': {
+                    u'raw_latex': 'x^3',
+                    u'svg_filename': (
+                        u'mathImg_20207361338_imzlvnf23a_invalid_4d123_width_2'
+                        '3d122_vertical_2d123.svg')
+                }
+            }, (
+                'Invalid svg_filename attribute in math component: mathImg_202'
+                '07361338_imzlvnf23a_invalid_4d123_width_23d122_vertical_2d123'
+                '.svg'))]
 
         self.check_validation(
             components.Math, valid_items, invalid_items_with_error_messages)