Skip to content
test_flow.py 205 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
            )
            self.assertEqual(resp.status_code, 200)

            self.assertEqual(self.mock_regrade_task.call_count, 0)
            self.assertEqual(self.mock_redirect.call_count, 0)

    def test_success(self):
        # get success
        resp = self.client.get(self.get_regrade_flows_view_url())
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(resp.status_code, 200)

        # post success
        access_rules_tag = "some_tag"
        flow_id = self.flow_id
        inprog_value_map = {
            "any": None,
            "yes": True,
            "no": False,
        }
        for regraded_session_in_progress, inprog_value in (
                inprog_value_map.items()):
Dong Zhuang's avatar
Dong Zhuang committed
            with self.subTest(
                    regraded_session_in_progress=regraded_session_in_progress):
                self.mock_regrade_task.return_value = mock.MagicMock()
                self.mock_redirect.return_value = http.HttpResponse()
                resp = self.client.post(
Dong Zhuang's avatar
Dong Zhuang committed
                    self.get_regrade_flows_view_url(),
                    data={
                        "regraded_session_in_progress":
                            regraded_session_in_progress,
                        "flow_id": flow_id,
                        "access_rules_tag": access_rules_tag,
                    }
                )
                self.assertFormErrorLoose(resp, None)
                self.assertEqual(self.mock_regrade_task.call_count, 1)
                self.mock_regrade_task.assert_called_once_with(
                    self.course.id,
                    flow_id,
                    access_rules_tag,
                    inprog_value,
                )
                self.mock_regrade_task.reset_mock()
                self.assertEqual(self.mock_redirect.call_count, 1)
                self.assertIn(
                    "relate-monitor_task", self.mock_redirect.call_args[0])
                self.mock_redirect.reset_mock()


class ViewUnsubmitFlowPageTest(SingleCourseQuizPageTestMixin, TestCase):
    # test flow.view_unsubmit_flow_page

    page_id = "half"

    @classmethod
    def setUpTestData(cls):  # noqa
        super().setUpTestData()
        client = Client()
        client.force_login(cls.student_participation.user)
        cls.start_flow(client, cls.flow_id)
        cls.submit_page_answer_by_page_id_and_test(client, page_id=cls.page_id)
Dong Zhuang's avatar
Dong Zhuang committed

    def setUp(self):
        self.client.force_login(self.instructor_participation.user)
Dong Zhuang's avatar
Dong Zhuang committed

        fake_unsubmit_page = mock.patch(
            "course.flow.unsubmit_page")
        self.mock_unsubmit_page = fake_unsubmit_page.start()
        self.addCleanup(fake_unsubmit_page.stop)

        fake_add_message = mock.patch("course.flow.messages.add_message")
        self.mock_add_message = fake_add_message.start()
        self.addCleanup(fake_add_message.stop)

        fake_adjust_flow_session_page_data = mock.patch(
            "course.flow.adjust_flow_session_page_data")
        self.mock_adjust_flow_session_page_data = (
            fake_adjust_flow_session_page_data.start())
        self.mock_adjust_flow_session_page_data.return_value = None
        self.addCleanup(fake_adjust_flow_session_page_data.stop)

    def get_view_unsubmit_flow_page_url(
            self, page_ordinal, course_identifier=None, flow_session_id=None):
        course_identifier = course_identifier or self.get_default_course_identifier()
        flow_session_id = (flow_session_id
                           or self.get_default_flow_session_id(course_identifier))
        return reverse(
            "relate-unsubmit_flow_page",
            kwargs={"course_identifier": course_identifier,
                    "flow_session_id": flow_session_id,
                    "page_ordinal": page_ordinal})

    def get_view_unsubmit_flow_page_url_by_page_id(
            self, page_id, course_identifier=None, flow_session_id=None):
        course_identifier = course_identifier or self.get_default_course_identifier()
        flow_session_id = (flow_session_id
                           or self.get_default_flow_session_id(course_identifier))
        page_ordinal = self.get_page_ordinal_via_page_id(page_id)
        return self.get_view_unsubmit_flow_page_url(
            page_ordinal, course_identifier, flow_session_id)

    def test_anonymous(self):
        with self.temporarily_switch_to_user(None):
            resp = self.client.get(
Dong Zhuang's avatar
Dong Zhuang committed
                self.get_view_unsubmit_flow_page_url_by_page_id(self.page_id))
            self.assertEqual(resp.status_code, 403)

            resp = self.client.post(
Dong Zhuang's avatar
Dong Zhuang committed
                self.get_view_unsubmit_flow_page_url_by_page_id(self.page_id),
                data={"submit": ""})

            self.assertEqual(resp.status_code, 403)
            self.assertEqual(self.mock_unsubmit_page.call_count, 0)

    def test_no_pperm(self):
        with self.temporarily_switch_to_user(self.student_participation.user):
            resp = self.client.get(
Dong Zhuang's avatar
Dong Zhuang committed
                self.get_view_unsubmit_flow_page_url_by_page_id(self.page_id))
            self.assertEqual(resp.status_code, 403)

            resp = self.client.post(
Dong Zhuang's avatar
Dong Zhuang committed
                self.get_view_unsubmit_flow_page_url_by_page_id(self.page_id),
                data={"submit": ""})

            self.assertEqual(resp.status_code, 403)
            self.assertEqual(self.mock_unsubmit_page.call_count, 0)

    def test_session_does_not_exist(self):
        resp = self.client.get(
Dong Zhuang's avatar
Dong Zhuang committed
            self.get_view_unsubmit_flow_page_url_by_page_id(
                self.page_id, flow_session_id=100))
        self.assertEqual(resp.status_code, 404)

        resp = self.client.post(
Dong Zhuang's avatar
Dong Zhuang committed
            self.get_view_unsubmit_flow_page_url_by_page_id(
                self.page_id, flow_session_id=100),
            data={"submit": ""})

        self.assertEqual(resp.status_code, 404)
        self.assertEqual(self.mock_unsubmit_page.call_count, 0)

    def test_success(self):
        # get_success
        resp = self.client.get(
Dong Zhuang's avatar
Dong Zhuang committed
            self.get_view_unsubmit_flow_page_url_by_page_id(self.page_id))
        self.assertEqual(resp.status_code, 200)

        # post_success
        resp = self.client.post(
Dong Zhuang's avatar
Dong Zhuang committed
            self.get_view_unsubmit_flow_page_url_by_page_id(self.page_id),
            data={"submit": ""})

        self.assertEqual(resp.status_code, 302)
        self.assertEqual(self.mock_unsubmit_page.call_count, 1)
        self.assertEqual(self.mock_add_message.call_count, 1)
        self.assertIn(
            "Flow page changes reallowed. ",
            self.mock_add_message.call_args[0]
        )

    def test_postdata_without_submit(self):
        resp = self.client.post(
Dong Zhuang's avatar
Dong Zhuang committed
            self.get_view_unsubmit_flow_page_url_by_page_id(self.page_id),
            data={})

        self.assertEqual(resp.status_code, 302)
        self.assertEqual(self.mock_unsubmit_page.call_count, 0)

        self.assertEqual(self.mock_add_message.call_count, 0)

    def test_post_form_not_valid(self):
        with mock.patch(
                "course.flow.UnsubmitFlowPageForm.is_valid") as mock_form_valid:
            mock_form_valid.return_value = False
            resp = self.client.post(
Dong Zhuang's avatar
Dong Zhuang committed
                self.get_view_unsubmit_flow_page_url_by_page_id(self.page_id),
                data={"submit": ""})

            self.assertEqual(resp.status_code, 200)
            self.assertEqual(self.mock_unsubmit_page.call_count, 0)

    def test_unsubmit_page_has_no_answer_visit(self):
        # this page has not been answered yet
        page_id = "ice_cream_toppings"
        expected_error_msg = "No prior answers found that could be un-submitted."

        resp = self.client.get(
Dong Zhuang's avatar
Dong Zhuang committed
            self.get_view_unsubmit_flow_page_url_by_page_id(page_id))

        self.assertEqual(resp.status_code, 302)
        self.assertEqual(self.mock_unsubmit_page.call_count, 0)

        self.assertEqual(self.mock_add_message.call_count, 1)
        self.assertIn(expected_error_msg, self.mock_add_message.call_args[0])
        self.mock_add_message.reset_mock()

        resp = self.client.post(
Dong Zhuang's avatar
Dong Zhuang committed
            self.get_view_unsubmit_flow_page_url_by_page_id(page_id),
            data={"submit": ""})

        self.assertEqual(resp.status_code, 302)
        self.assertEqual(self.mock_unsubmit_page.call_count, 0)

        self.assertEqual(self.mock_add_message.call_count, 1)
        self.assertIn(expected_error_msg, self.mock_add_message.call_args[0])

Dong Zhuang's avatar
Dong Zhuang committed
# vim: foldmethod=marker