Skip to content

Commit 731a185

Browse files
committed
Test validation
1 parent 4c4f30e commit 731a185

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

tests/test_validation/test_validate_course_content.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@
139139
staticpage2_id = "spage2"
140140
staticpage2_desc = mock.MagicMock()
141141

142+
form1_path = "forms/form1.yml"
143+
form1_location = "form1.yml"
144+
form1_id = "form1"
145+
form1_desc = mock.MagicMock()
146+
142147
flow1_path = "flows/flow1.yml"
143148
flow1_location = "flow1.yml"
144149
flow1_id = "flow1"
@@ -203,6 +208,9 @@ def get_yaml_from_repo_safely_side_effect(repo, full_name, commit_sha):
203208
if full_name == staticpage2_path:
204209
return staticpage2_desc
205210

211+
if full_name == form1_path:
212+
return form1_desc
213+
206214
return get_yaml_from_repo_safely(repo, full_name, commit_sha)
207215

208216

@@ -223,6 +231,9 @@ def get_yaml_from_repo_safely_with_duplicate_grade_identifier_side_effect(
223231
if full_name == staticpage2_path:
224232
return staticpage2_desc
225233

234+
if full_name == form1_path:
235+
return form1_desc
236+
226237
return get_yaml_from_repo_safely(repo, full_name, commit_sha)
227238

228239

@@ -247,6 +258,7 @@ def get_repo_blob_side_effect(repo, full_name, commit_sha, allow_tree=True):
247258
if full_name == "forms":
248259
tree = Tree()
249260
tree.add(b"not_a_form", stat.S_IFREG, b"not a form")
261+
tree.add(form1_location.encode(), stat.S_IFREG, b"a form")
250262
return tree
251263
if full_name == "":
252264
return Tree()
@@ -343,6 +355,11 @@ def setUp(self):
343355
self.mock_validate_staticpage_desc = fake_validate_staticpage_desc.start()
344356
self.addCleanup(fake_validate_staticpage_desc.stop)
345357

358+
fake_validate_form_desc = mock.patch(
359+
"course.validation.validate_form_desc")
360+
self.mock_validate_form_desc = fake_validate_form_desc.start()
361+
self.addCleanup(fake_validate_form_desc.stop)
362+
346363
fake_get_yaml_from_repo = mock.patch(
347364
"course.content.get_yaml_from_repo")
348365
self.mock_get_yaml_from_repo = fake_get_yaml_from_repo.start()
@@ -419,6 +436,10 @@ def test_course_none(self):
419436
self.assertSetEqual(expected_validate_staticpage_desc_call_args,
420437
args_set)
421438

439+
# make sure validate_form_desc was called with expected args
440+
self.assertEqual(self.mock_validate_form_desc.call_args_list[0][0][1:],
441+
(form1_path, form1_desc))
442+
422443
# validate_calendar_desc_struct is called
423444
self.assertEqual(self.mock_validate_calendar_desc_struct.call_count, 1)
424445

tests/test_validation/test_validation_tools.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
28752996
class ValidateStaticPageNameTest(ValidationTestMixin, unittest.TestCase):
28762997
# test validation.validate_static_page_name
28772998

0 commit comments

Comments
 (0)