@@ -2872,6 +2872,127 @@ def test_fail(self):
28722872 self .assertIn (expected_error_msg , str (cm .exception ))
28732873
28742874
2875+ class ValidateFormIdTest (ValidationTestMixin , unittest .TestCase ):
2876+ # test validation.validate_form_id
2877+
2878+ def test_success (self ):
2879+ flow_id = "abc-def"
2880+ validation .validate_form_id (vctx , location , flow_id )
2881+ flow_id = "abc_def1"
2882+ validation .validate_form_id (vctx , location , flow_id )
2883+
2884+ def test_fail (self ):
2885+ expected_error_msg = (
2886+ "invalid form name. Form names may only contain (roman) "
2887+ "letters, numbers, dashes and underscores." )
2888+
2889+ flow_id = "abc def"
2890+ with self .assertRaises (ValidationError ) as cm :
2891+ validation .validate_form_id (vctx , location , flow_id )
2892+ self .assertIn (expected_error_msg , str (cm .exception ))
2893+
2894+ flow_id = "abc/def"
2895+ with self .assertRaises (ValidationError ) as cm :
2896+ validation .validate_form_id (vctx , location , flow_id )
2897+ self .assertIn (expected_error_msg , str (cm .exception ))
2898+
2899+
2900+ class ValidateFormFieldTest (ValidationTestMixin , unittest .TestCase ):
2901+ # test validation.validate_form_field
2902+
2903+ def get_updated_form_field (self , ** kwargs ):
2904+ field_desc = {"id" : "my_page_id" ,
2905+ "type" : "Text" }
2906+ field_desc .update (kwargs )
2907+ return dict_to_struct (field_desc )
2908+
2909+ def test_success (self ):
2910+ validation .validate_form_field (vctx , location ,
2911+ self .get_updated_form_field (id = "abc" ))
2912+
2913+ def test_invalid_form_field_id (self ):
2914+ expected_error_msg = (
2915+ "invalid form field id. Form field id may only contain (roman) "
2916+ "letters, numbers, dashes and underscores." )
2917+ with self .assertRaises (ValidationError ) as cm :
2918+ validation .validate_form_field (vctx , location ,
2919+ self .get_updated_form_field (id = "abc def" ))
2920+ self .assertIn (expected_error_msg , str (cm .exception ))
2921+
2922+ def test_invalid_form_field_type (self ):
2923+ expected_error_msg = (
2924+ "some_where: form field type 'qwe' not recognized" )
2925+ with self .assertRaises (ValidationError ) as cm :
2926+ validation .validate_form_field (vctx , location ,
2927+ self .get_updated_form_field (type = "qwe" ))
2928+ self .assertIn (expected_error_msg , str (cm .exception ))
2929+
2930+
2931+ class ValidateFormTest (ValidationTestMixin , unittest .TestCase ):
2932+ # test validation.validate_form_desc
2933+
2934+ def setUp (self ):
2935+ super (ValidateFormTest , self ).setUp ()
2936+ patch = mock .patch ("course.validation.validate_role" )
2937+ self .mock_validate_role = patch .start ()
2938+ self .addCleanup (patch .stop )
2939+
2940+ patch = mock .patch ("course.validation.validate_form_field" )
2941+ self .mock_validate_form_field = patch .start ()
2942+ self .addCleanup (patch .stop )
2943+
2944+ def get_updated_form_desc (self , ** kwargs ):
2945+ form_desc = {
2946+ "title" : "title" ,
2947+ "description" : "description" ,
2948+ "type" : "flow" ,
2949+ "access_roles" : ["ta" , "ta2" ],
2950+ "fields" : [
2951+ dict_to_struct ({"id" : "template_in" , "type" : "Text" }),
2952+ dict_to_struct ({"id" : "template_out" , "type" : "Text" }),
2953+ ],
2954+ }
2955+ form_desc .update (kwargs )
2956+ return dict_to_struct (form_desc )
2957+
2958+ def test_validate_role_called (self ):
2959+ validation .validate_form_desc (vctx , location ,
2960+ self .get_updated_form_desc (access_roles = []))
2961+ self .assertEqual (self .mock_validate_role .call_count , 0 )
2962+ self .assertEqual (self .mock_validate_form_field .call_count , 2 )
2963+
2964+ validation .validate_form_desc (vctx , location ,
2965+ self .get_updated_form_desc ())
2966+ self .assertEqual (self .mock_validate_role .call_count , 2 )
2967+
2968+ def test_field_id_unique (self ):
2969+ expected_error_msg = ("some_where: form field id 'template_in' not unique" )
2970+ fields = [
2971+ dict_to_struct ({"id" : "template_in" , "type" : "Text" }),
2972+ dict_to_struct ({"id" : "template_in" , "type" : "Text" }),
2973+ dict_to_struct ({"id" : "template_out" , "type" : "Text" }),
2974+ ]
2975+ with self .assertRaises (ValidationError ) as cm :
2976+ validation .validate_form_desc (vctx , location ,
2977+ self .get_updated_form_desc (fields = fields ))
2978+ self .assertIn (expected_error_msg , str (cm .exception ))
2979+
2980+ def test_field_required (self ):
2981+ fields = [
2982+ dict_to_struct ({"id" : "template_in" , "type" : "Text" }),
2983+ dict_to_struct ({"id" : "template_out" , "type" : "Text" }),
2984+ ]
2985+
2986+ for field_name in ["template_in" , "template_out" ]:
2987+ expected_error_msg = (
2988+ "some_where: required form field id '%s' not found" % field_name )
2989+ test_fields = [field for field in fields if field .id != field_name ]
2990+ with self .assertRaises (ValidationError ) as cm :
2991+ validation .validate_form_desc (vctx , location ,
2992+ self .get_updated_form_desc (fields = test_fields ))
2993+ self .assertIn (expected_error_msg , str (cm .exception ))
2994+
2995+
28752996class ValidateStaticPageNameTest (ValidationTestMixin , unittest .TestCase ):
28762997 # test validation.validate_static_page_name
28772998
0 commit comments