Skip to content
test_base.py 15.8 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 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 359 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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
from __future__ import division

__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.
"""

from django.test import TestCase

from tests.test_sandbox import (
    SingleCoursePageSandboxTestBaseMixin, PAGE_ERRORS
)
from tests.base_test_mixins import SingleCoursePageTestMixin
from tests.utils import mock

SANDBOX_TITLE_PATTERN = "<title>[SB] %s - RELATE </title>"

TEXT_QUESTION_MARKDOWN = """
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
- <case_sens_regex>(?:operator\s+)?\s*map

"""

OPTIONAL_PAGE_WITH_VALUE_ATTR = """
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
- <case_sens_regex>(?:operator\s+)?\s*map

"""

PAGE_WITH_TITLE_MARKDOWN_PATTERN = """
type: Page
%(attr_title)s
id: welcome

content: |

    %(content_title)s

    Don't be scared.
"""


TEST_ANSWER_MARKDOWN = """
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$
"""


def make_page_data_side_effect_has_data(self):
    return {"data": "foo"}


class PageBaseTest(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 compatiblity "
                "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("'%s' is not warned as expected" % expected_warn_msg)


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 PageBaseGradeDeprecateTest(SingleCoursePageTestMixin, TestCase):

    flow_id = "quiz-test"

    def setUp(self):
        super(PageBaseGradeDeprecateTest, self).setUp()
        self.c.force_login(self.student_participation.user)
        self.start_flow(flow_id=self.flow_id)

    def test_update_grade_data_from_grading_form(self):
        page_id = "hgtext"
        self.post_answer_by_page_id(
            page_id, answer_data={"answer": TEST_ANSWER_MARKDOWN})
        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 = {
                "grade_percent": ["100"],
                "released": ["on"]
            }
            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 "
                "compatiblity 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("'%s' is not warned as expected" % expected_warn_msg)

    def test_post_form_deprecated(self):
        page_id = "half"

        with mock.patch(
                "course.page.text.TextQuestionBase.process_form_post",
                autospec=True
        ) as mock_process_form_post, mock.patch(
                "course.page.text.TextQuestionBase.post_form",
                autospec=True) as mock_post_form, mock.patch(
                "warnings.warn") as mock_warn:

            mock_process_form_post.side_effect = process_form_post_side_effect_super
            mock_post_form.side_effect = post_form_side_effect

            self.post_answer_by_page_id(
                page_id, answer_data={"answer": "1/2"})

            self.assertTrue(mock_warn.call_count >= 1)

            expected_warn_msg = (
                "TextQuestion is using the post_form compatiblity 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("'%s' is not warned as expected" % expected_warn_msg)

        self.assertEqual(self.end_flow().status_code, 200)
        self.assertSessionScoreEqual(5)


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.")


class PageBaseWithTitleTest(SingleCoursePageSandboxTestBaseMixin, TestCase):

    def test_markup_body_for_title_not_implemented(self):
        with mock.patch("course.page.static.Page.markup_body_for_title")\
                as mock_markup_body_for_title,\
                mock.patch("warnings.warn") as mock_warn:
            mock_markup_body_for_title.side_effect = NotImplementedError
            mock_warn.side_effect = [None, None, None]

            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 are 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("%s is not warned as expected" % expected_warn_msg)

    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": "title: %s" % 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": "# %s" % 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": "title: %s" % expected_title_str,
                   "content_title": "# %s" % 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": "# %s" % 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": "# %s" % 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": "# %s" % 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": "# %s" % 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")


# vim: fdm=marker