Newer
Older
from __future__ import annotations
__copyright__ = "Copyright (C) 2018 Dong Zhuang"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
HumanTextFeedbackForm,
PageBehavior,
create_default_point_scale,
from course.page.static import Page
from tests.base_test_mixins import SingleCourseQuizPageTestMixin
from tests.test_grading import SingleCourseQuizPageGradeInterfaceTestMixin
from tests.test_sandbox import SingleCoursePageSandboxTestBaseMixin
SANDBOX_TITLE_PATTERN = "<title>[SB] %s - RELATE </title>"
type: TextQuestion
id: eigvec
title: Eigenvectors
value: 2
prompt: |
# What's an eigenvector?
Yadda ___________________ yadda.
answers:
- <plain>matrix
- <case_sens_plain>Eigenmatrix
- <regex>(?:linear\s+)?\s*map
Andreas Klöckner
committed
- type: regex
value: (?:operator\s+)?\s*map
flags: []
type: TextQuestion
id: eigvec
title: Eigenvectors
value: -2
prompt: |
# What's an eigenvector?
Yadda ___________________ yadda.
answers:
- <plain>matrix
- <case_sens_plain>Eigenmatrix
- <regex>(?:linear\s+)?\s*map
Andreas Klöckner
committed
- type: regex
value: (?:operator\s+)?\s*map
flags: []
"""
type: TextQuestion
id: eigvec
is_optional_page: True
value: 2
title: Eigenvectors
prompt: |
# What's an eigenvector?
Yadda ___________________ yadda.
answers:
- <plain>matrix
- <case_sens_plain>Eigenmatrix
- <regex>(?:linear\s+)?\s*map
Andreas Klöckner
committed
- type: regex
value: (?:operator\s+)?\s*map
flags: []
"""
PAGE_WITH_TITLE_MARKDOWN_PATTERN = """
type: Page
%(attr_title)s
id: welcome
content: |
%(content_title)s
Don't be scared.
"""
type: ChoiceQuestion
id: myquestion
shuffle: True
prompt: |
# What is your favorite number?
There are many beautiful numbers. What's your
favorite one?
choices:
- "1"
- "2"
- ~CORRECT~ 15
- ~CORRECT~ $\pi$
- $\sqrt 2$
"""
class CreateDefaultPointScaleTest(unittest.TestCase):
# test create_default_point_scale
def test_create_default_point_scale(self):
test_dict = {
3: [0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75, 3],
7: [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7],
10: [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8,
8.5, 9, 9.5, 10],
15: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
70: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70],
}
with self.subTest(total_points=k):
returned_value = create_default_point_scale(k)
self.assertIsNotNone(returned_value)
self.assertTrue(len(returned_value))
self.assertListEqual(v, returned_value)
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def correct_answer_side_effect_super(
self, page_context, page_data, answer_data, grade_data):
from course.page.text import TextQuestionBase
return super(TextQuestionBase, self).correct_answer(
page_context, page_data, answer_data, grade_data)
def normalized_answer_side_effect_super(
self, page_context, page_data, answer_data):
from course.page.text import TextQuestionBase
return super(TextQuestionBase, self).normalized_answer(
page_context, page_data, answer_data)
def normalized_bytes_answer_side_effect_super(
self, page_context, page_data, answer_data):
from course.page.text import TextQuestionBase
return super(TextQuestionBase, self).normalized_bytes_answer(
page_context, page_data, answer_data)
class PageBaseAPITest(SingleCourseQuizPageTestMixin, TestCase):
page_id = "half"
@classmethod
client = Client()
client.force_login(cls.student_participation.user)
cls.start_flow(client, cls.flow_id)
def test_correctness(self):
self.submit_page_answer_by_page_id_and_test(self.page_id)
resp = self.client.get(self.get_page_url_by_page_id(self.page_id))
self.assertResponseContextIsNotNone(resp, "correct_answer")
# make sure PageBase.correctness works
with mock.patch("course.page.text.TextQuestion.correct_answer",
autospec=True) as mock_correctness:
mock_correctness.side_effect = correct_answer_side_effect_super
resp = self.client.get(self.get_page_url_by_page_id(self.page_id))
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
self.assertResponseContextIsNone(resp, "correct_answer")
def test_normalized_answer(self):
# make sure PageBase.normalized_answer works
with mock.patch("course.page.text.TextQuestion.normalized_answer",
autospec=True) as mock_normalized_answer:
mock_normalized_answer.side_effect = normalized_answer_side_effect_super
self.submit_page_answer_by_page_id_and_test(
self.page_id,
ensure_analytic_page_get_before_grading=True,
ensure_analytic_page_get_after_grading=True, do_grading=True)
def test_normalized_bytes_answer(self):
# make sure PageBase.normalized_answer works
with mock.patch("course.page.text.TextQuestion.normalized_bytes_answer",
autospec=True) as mock_normalized_bytes_answer:
mock_normalized_bytes_answer.side_effect = \
normalized_bytes_answer_side_effect_super
self.submit_page_answer_by_page_id_and_test(
self.page_id,
ensure_download_before_grading=True,
ensure_download_after_grading=True,
dl_file_with_ext_count=0,
do_grading=True)
class PageBaseGetModifiedPermissionsForPageTest(unittest.TestCase):
# test page_base.get_modified_permissions_for_page
def test_get_modified_permissions_for_page(self):
access_rule_permissions_list = [
"view", "submit_answer", "end_session", "see_session_time",
"lock_down_as_exam_session"]
access_rule_permissions = frozenset(access_rule_permissions_list)
page_base_desc = {
"id": "abcd",
"type": "SomePageType",
"content": "Um?",
"title": "Title",
}
with self.subTest(access_rules="Not present"):
page_desc = dict_to_struct(
page = Page(None, "", page_desc)
self.assertSetEqual(
page.get_modified_permissions_for_page(access_rule_permissions),
access_rule_permissions)
with self.subTest(access_rules={}):
page_desc = dict_to_struct(
{
**page_base_desc,
"access_rules": {},
page = Page(None, "", page_desc)
self.assertSetEqual(
page.get_modified_permissions_for_page(access_rule_permissions),
access_rule_permissions)
with self.subTest(access_rules={"add_permissions": [],
"remove_permissions": []}):
page_desc = dict_to_struct(
{
"access_rules": {"add_permissions": [],
"remove_permissions": []}
}
)
page = Page(None, "", page_desc)
self.assertSetEqual(
page.get_modified_permissions_for_page(access_rule_permissions),
access_rule_permissions)
with self.subTest(access_rules={"add_permissions": ["some_perm"],
"remove_permissions": []}):
page_desc = dict_to_struct(
{
"access_rules": {"add_permissions": ["some_perm"],
"remove_permissions": []}
}
)
page = Page(None, "", page_desc)
self.assertSetEqual(
page.get_modified_permissions_for_page(access_rule_permissions),
frozenset([*access_rule_permissions_list, "some_perm"]))
with self.subTest(access_rules={"remove_permissions": ["none_exist_perm"]}):
page_desc = dict_to_struct(
{
"access_rules": {"remove_permissions": ["none_exist_perm"]}
}
)
page = Page(None, "", page_desc)
self.assertSetEqual(
page.get_modified_permissions_for_page(access_rule_permissions),
access_rule_permissions)
with self.subTest(access_rules={
"remove_permissions": [access_rule_permissions_list[0]]}):
page_desc = dict_to_struct(
{
"access_rules": {
"remove_permissions": [access_rule_permissions_list[0]]}
}
)
page = Page(None, "", page_desc)
self.assertSetEqual(
page.get_modified_permissions_for_page(access_rule_permissions),
frozenset(access_rule_permissions_list[1:]))
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
def human_text_feedback_form_clean_side_effect(self):
from course.page.base import StyledForm
return super(StyledForm, self).clean()
class HumanTextFeedbackFormTest(unittest.TestCase):
def test_point_value_vs_field(self):
with mock.patch(
"course.page.base.create_default_point_scale"
) as mock_create_scale:
form = HumanTextFeedbackForm(None)
self.assertNotIn("grade_points", form.fields)
self.assertEqual(mock_create_scale.call_count, 0)
mock_create_scale.reset_mock()
form = HumanTextFeedbackForm(0)
self.assertNotIn("grade_points", form.fields)
self.assertEqual(mock_create_scale.call_count, 0)
mock_create_scale.reset_mock()
form = HumanTextFeedbackForm(1)
self.assertIn("grade_points", form.fields)
self.assertEqual(mock_create_scale.call_count, 1)
mock_create_scale.reset_mock()
def test_form_disagree(self):
form_data = {"grade_percent": 30, "grade_points": 2}
form = HumanTextFeedbackForm(5, form_data)
self.assertFalse(form.is_valid())
self.assertEqual(form.non_field_errors(),
["Grade (percent) and Grade (points) disagree"])
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
def test_form_points_percentage_valid(self):
form_data = {"grade_percent": 30, "grade_points": 1.50001}
form = HumanTextFeedbackForm(5, form_data)
self.assertTrue(form.is_valid())
self.assertTrue(form.cleaned_percent() - 30 < 0.001)
def test_form_no_grade_points(self):
form_data = {"grade_percent": 30}
form = HumanTextFeedbackForm(5, form_data)
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_percent(), 30)
def test_form_no_grade_percent(self):
form_data = {"grade_points": 1.5}
form = HumanTextFeedbackForm(5, form_data)
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_percent(), 30)
def test_form_point_value_none_cleaned_percentage(self):
form_data = {"grade_percent": 30}
form = HumanTextFeedbackForm(None, form_data)
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_percent(), 30)
def test_form_cleaned_percent_raise(self):
with mock.patch("course.page.base.HumanTextFeedbackForm.clean",
autospec=True) as mock_clean:
mock_clean.side_effect = human_text_feedback_form_clean_side_effect
form_data = {"grade_percent": 30, "grade_points": 2}
form = HumanTextFeedbackForm(5, form_data)
self.assertTrue(form.is_valid())
with self.assertRaises(RuntimeError):
form.cleaned_percent()
def make_page_data_side_effect_has_data(self):
return {"data": "foo"}
class PageBaseDeprecationTest(SingleCoursePageSandboxTestBaseMixin, TestCase):
def test_deprecated_make_page_data_has_warning(self):
with mock.patch("course.page.text.TextQuestionBase.make_page_data",
autospec=True) as mock_make_page_data, mock.patch(
"warnings.warn") as mock_warn:
mock_make_page_data.side_effect = make_page_data_side_effect_has_data
resp = self.get_page_sandbox_preview_response(TEXT_QUESTION_MARKDOWN)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
# There are other warnings besides this expected warning
self.assertTrue(mock_warn.call_count >= 1)
expected_warn_msg = (
"TextQuestion is using the make_page_data compatibility "
"hook, which is deprecated.")
warned_with_expected_msg = False
for args in mock_warn.call_args_list:
if expected_warn_msg in args[0]:
warned_with_expected_msg = True
break
if not warned_with_expected_msg:
self.fail(f"'{expected_warn_msg}' is not warned as expected")
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
def update_grade_data_from_grading_form_v2_side_effect_super(
self, request, page_context, page_data, grade_data,
grading_form, files_data):
from course.page.base import PageBaseWithHumanTextFeedback
return (
super(
PageBaseWithHumanTextFeedback, self
).update_grade_data_from_grading_form_v2(
request, page_context, page_data, grade_data, grading_form, files_data))
def process_form_post_side_effect_super(self, page_context, page_data,
post_data, files_data,
page_behavior):
from course.page.text import TextQuestionBase
return (
super(TextQuestionBase, self).process_form_post(page_context, page_data,
post_data, files_data,
page_behavior))
def post_form_side_effect(self, page_context, page_data, post_data, files_data):
from course.page.text import TextAnswerForm
read_only = False
return TextAnswerForm(
read_only,
"default",
self.get_validators(), post_data, files_data,
widget_type=getattr(self.page_desc, "widget", None))
class PageBaseGradeDeprecationTest(SingleCourseQuizPageTestMixin, TestCase):
self.start_flow(flow_id=self.flow_id)
def test_update_grade_data_from_grading_form(self):
page_id = "hgtext"
self.end_flow()
with mock.patch(
"course.page.base.PageBaseWithHumanTextFeedback"
".update_grade_data_from_grading_form_v2",
autospec=True
) as mock_update_grade_data_from_grading_form_v2, mock.patch(
"warnings.warn") as mock_warn:
mock_update_grade_data_from_grading_form_v2.side_effect = (
update_grade_data_from_grading_form_v2_side_effect_super)
grade_data = {
}
resp = self.post_grade_by_page_id(page_id, grade_data)
self.assertTrue(resp.status_code, 200)
# There are other warnings besides this expected warning
self.assertTrue(mock_warn.call_count >= 1)
expected_warn_msg = (
"HumanGradedTextQuestion is using the "
"update_grade_data_from_grading_form "
"compatibility hook, which is deprecated.")
warned_with_expected_msg = False
for args in mock_warn.call_args_list:
if expected_warn_msg in args[0]:
warned_with_expected_msg = True
break
if not warned_with_expected_msg:
self.fail(f"'{expected_warn_msg}' is not warned as expected")
def grading_form_to_html_side_effect_super(
self, request, page_context, grading_form, grade_data):
from course.page.base import PageBaseWithHumanTextFeedback
return (
super(
PageBaseWithHumanTextFeedback, self
).grading_form_to_html(
request, page_context, grading_form, grade_data))
def human_feedback_point_value_side_effect_super(self, page_context, page_data):
from course.page.upload import FileUploadQuestion
return (
super(
FileUploadQuestion, self
).human_feedback_point_value(page_context, page_data))
class PageBaseWithHumanTextFeedbackTest(SingleCourseQuizPageGradeInterfaceTestMixin,
TestCase):
@classmethod
client = Client()
client.force_login(cls.student_participation.user)
cls.submit_page_answer_by_page_id_and_test(client, page_id=cls.page_id)
cls.end_flow(client)
def test_base_class_grading_form_to_html(self):
# make sure subclass grading_form_to_html method works
with mock.patch(
"course.page.base.PageBaseWithHumanTextFeedback"
".grading_form_to_html", autospec=True
) as mock_grading_form_to_html:
mock_grading_form_to_html.side_effect = (
grading_form_to_html_side_effect_super)
with self.temporarily_switch_to_user(
self.instructor_participation.user):
resp = self.client.get(
self.get_page_grading_url_by_page_id(self.page_id))
self.assertEqual(resp.status_code, 200)
def test_human_feedback_point_value_subclass(self):
# make sure subclass PageBase.human_feedback_point_value works
with mock.patch(
"course.page.upload.FileUploadQuestion"
".human_feedback_point_value", autospec=True
) as mock_human_feedback_point_value:
mock_human_feedback_point_value.side_effect = (
human_feedback_point_value_side_effect_super)
with self.temporarily_switch_to_user(
self.instructor_participation.user):
resp = self.client.get(
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
self.get_page_grading_url_by_page_id(self.page_id))
self.assertEqual(resp.status_code, 200)
def test_grade(self):
with self.temporarily_switch_to_user(
self.instructor_participation.user):
# not released
resp = self.post_grade_by_page_id(
self.page_id,
grade_data={})
self.assertIsNone(resp.context.get("feedback"))
self.assertEqual(resp.status_code, 200)
# no grade_percent
resp = self.post_grade_by_page_id(
self.page_id,
grade_data={
"released": "on"}
)
self.assertIsNone(resp.context.get("feedback"))
self.assertEqual(resp.status_code, 200)
expected_feedback_str = "I don't know how to grade"
resp = self.post_grade_by_page_id(
self.page_id,
grade_data={
"released": "on",
"feedback_text": expected_feedback_str}
)
self.assertResponseContextAnswerFeedbackContainsFeedback(
resp, expected_feedback_str)
self.assertResponseContextAnswerFeedbackCorrectnessEquals(resp, None)
self.assertEqual(resp.status_code, 200)
class PageBaseWithValueTest(SingleCoursePageSandboxTestBaseMixin, TestCase):
def test_optional_page_with_value_attr(self):
markdown = OPTIONAL_PAGE_WITH_VALUE_ATTR
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"Attribute 'value' should be removed when "
"'is_optional_page' is True.")
def test_optional_page_with_negative_value_attr(self):
markdown = TEXT_QUESTION_WITH_NEGATIVE_VALUE_MARKDOWN
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"sandboxAttribute 'value' expects a non-negative value, "
"got -2 instead")
class PageBaseWithTitleTest(SingleCoursePageSandboxTestBaseMixin, TestCase):
def test_markup_body_for_title_not_implemented(self):
with mock.patch("course.page.static.Page.markup_body_for_title")\
mock.patch("warnings.warn") as mock_warn:
mock_markup_body_for_title.side_effect = NotImplementedError
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": ""})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"no title found in body or title attribute")
# There may be other warnings besides this expected warning
self.assertTrue(mock_warn.call_count >= 1)
warned_with_expected_msg = False
expected_warn_msg = ("PageBaseWithTitle subclass 'Page' does not "
"implement markup_body_for_title()")
for args in mock_warn.call_args_list:
if expected_warn_msg in args[0]:
warned_with_expected_msg = True
break
if not warned_with_expected_msg:
self.fail(f"{expected_warn_msg} is not warned as expected")
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
def test_no_title(self):
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": ""})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"no title found in body or title attribute")
def test_no_actual_title(self):
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": "#"})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"no title found in body or title attribute")
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": "# "}) # with a space following
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxNotHasValidPage(resp)
self.assertResponseContextContains(
resp, PAGE_ERRORS,
"no title found in body or title attribute")
def test_attr_title(self):
expected_title_str = "This is attribute title"
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": f"title: {expected_title_str}",
"content_title": ""})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
self.assertContains(resp, SANDBOX_TITLE_PATTERN % expected_title_str,
html=True)
def test_content_title(self):
expected_title_str = "This is content title"
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": f"# {expected_title_str}"})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
self.assertContains(resp, SANDBOX_TITLE_PATTERN % expected_title_str,
html=True)
def test_used_attr_title(self):
# when page_desc.title is not None, it will be used as title
expected_title_str = "This is attribute title"
content_title_str = "This is content title"
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": f"title: {expected_title_str}",
"content_title": f"# {content_title_str}"})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
self.assertContains(resp, SANDBOX_TITLE_PATTERN % expected_title_str,
html=True)
def test_title_with_html_tags(self):
title_str = "<strong>Important</strong>"
expected_title_str = "Important"
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": f"# {title_str}"})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
self.assertContains(resp, SANDBOX_TITLE_PATTERN % expected_title_str,
html=True)
def test_title_with_markdown(self):
title_str = "**Important**"
expected_title_str = "Important"
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": f"# {title_str}"})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
self.assertContains(resp, SANDBOX_TITLE_PATTERN % expected_title_str,
html=True)
def test_title_with_lt(self):
title_str = "<"
from django.utils.html import escape
expected_title_str = escape("<")
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": f"# {title_str}"})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(resp, None)
self.assertContains(resp, SANDBOX_TITLE_PATTERN % expected_title_str,
html=True)
def test_title_with_rendered_empty_title_warn(self):
title_str = "<p>"
markdown = (
PAGE_WITH_TITLE_MARKDOWN_PATTERN
% {"attr_title": "",
"content_title": f"# {title_str}"})
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
self.assertSandboxWarningTextContain(
resp, "the rendered title is an empty string")
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
HGTEXT_MARKDOWN = """
type: HumanGradedTextQuestion
id: hgtext
value: 5
widget: "editor:yaml"
validators:
-
type: relate_page
page_type: ChoiceQuestion
prompt: |
# Submit an exam Choice question
rubric: |
(None yet)
correct_answer: |
[see here](some/references)
"""
class PageBaseWithCorrectAnswerTest(SingleCoursePageSandboxTestBaseMixin, TestCase):
def test_correct_answer_not_none(self):
markdown = HGTEXT_MARKDOWN
resp = self.get_page_sandbox_preview_response(markdown)
self.assertEqual(resp.status_code, 200)
self.assertSandboxHasValidPage(resp)
expected_html = '<p><a href="some/references">see here</a></p>'
self.assertResponseContextContains(resp, "correct_answer", expected_html)
class GetEditorInteractionModeTest(unittest.TestCase):
# test get_editor_interaction_mode
def test_get_editor_interaction_mode_flow_session_none(self):
page_context = mock.MagicMock()
page_context.flow_session = None
self.assertEqual(get_editor_interaction_mode(page_context), "default")
def test_get_editor_interaction_mode_participation_none(self):
page_context = mock.MagicMock()
page_context.flow_session = mock.MagicMock()
page_context.flow_session.participation = None
self.assertEqual(get_editor_interaction_mode(page_context), "default")
def test_get_editor_interaction_mode_participation_not_none(self):
page_context = mock.MagicMock()
page_context.flow_session = mock.MagicMock()
page_context.flow_session.participation = mock.MagicMock()
page_context.flow_session.participation.user = mock.MagicMock()
page_context.flow_session.participation.user.editor_mode = "some_mode"
self.assertEqual(get_editor_interaction_mode(page_context), "some_mode")
class PageBehaviorTest(unittest.TestCase):
def test_page_behavior_backward_compatibility(self):
answer_is_final = PageBehavior(show_correctness=False, show_answer=False,
may_change_answer=False)
if not answer_is_final:
self.fail(
"PageBehavior object expected to be True "
"when may_change_answer is False for backward "
"compatibility")
answer_is_final = PageBehavior(show_correctness=False, show_answer=False,
may_change_answer=True)
if answer_is_final:
self.fail(
"PageBehavior object expected to be False "
"when may_change_answer is True for backward "
"compatibility")