Skip to content
test_versioning.py 45.1 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
            versioning._get_commit_message_as_html(repo, commit_sha_2),
            expected_msg)

        expected_msg = "abc\\uDC80"
        self.assertEqual(
            versioning._get_commit_message_as_html(repo, commit_sha_3),
            expected_msg)


Dong Zhuang's avatar
Dong Zhuang committed
class UpdateCourseTest(SingleCourseTestMixin, MockAddMessageMixing, TestCase):
Dong Zhuang's avatar
Dong Zhuang committed
    def test_no_permission(self):
        with self.temporarily_switch_to_user(self.student_participation.user):
            resp = self.c.get(self.get_update_course_url())
            self.assertEqual(resp.status_code, 403)

            for command in versioning.ALLOWED_COURSE_REVISIOIN_COMMANDS:
                resp = self.post_update_course_content(
                    "some_commit_sha", command=command,
                    force_login_instructor=False)
                self.assertEqual(resp.status_code, 403)

    def test_participation_with_preview_permission(self):
        # Just to make sure it won't fail, Todo: assersion on form kwargs
        from course.models import ParticipationPermission
        pp = ParticipationPermission(
            participation=self.student_participation,
            permission=pperm.preview_content)
        pp.save()
        self.student_participation.individual_permissions.set([pp])

        with self.temporarily_switch_to_user(self.student_participation.user):
            for command in versioning.ALLOWED_COURSE_REVISIOIN_COMMANDS:
                resp = self.post_update_course_content(
                    "some_commit_sha", command=command,
                    force_login_instructor=False)
                self.assertEqual(resp.status_code, 200, command)

    def test_participation_with_update_permission(self):
        # Just to make sure it won't fail, Todo: assersion on form kwargs
        from course.models import ParticipationPermission
        pp = ParticipationPermission(
            participation=self.student_participation,
            permission=pperm.update_content)
        pp.save()
        self.student_participation.individual_permissions.set([pp])

        with self.temporarily_switch_to_user(self.student_participation.user):
            for command in versioning.ALLOWED_COURSE_REVISIOIN_COMMANDS:
                resp = self.post_update_course_content(
                    "some_commit_sha", command=command,
                    force_login_instructor=False)
                self.assertEqual(resp.status_code, 200, command)

    def test_get(self):
        with self.temporarily_switch_to_user(self.instructor_participation.user):
            resp = self.c.get(self.get_update_course_url())
            self.assertEqual(resp.status_code, 200)

    def test_unknown_command(self):
        resp = self.post_update_course_content(
            "some_commit_sha", command="unknown")
        self.assertEqual(resp.status_code, 400)

    @suppress_stdout_decorator(suppress_stderr=True)
    def test_run_course_update_command_failure(self):
        with mock.patch(
            "course.versioning.run_course_update_command"
        ) as mock_run_update:
            error_msg = "my runtime error"
            mock_run_update.side_effect = RuntimeError(error_msg)
            resp = self.post_update_course_content(
                self.course.active_git_commit_sha, command="update")
            self.assertEqual(resp.status_code, 200)
Dong Zhuang's avatar
Dong Zhuang committed
            self.assertAddMessageCallCount(1)
Dong Zhuang's avatar
Dong Zhuang committed
            expected_error_msg = "Error: RuntimeError %s" % error_msg
Dong Zhuang's avatar
Dong Zhuang committed
            self.assertAddMessageCalledWith(expected_error_msg)
Dong Zhuang's avatar
Dong Zhuang committed

    def test_form_not_valid(self):
        with mock.patch(
                "course.versioning.GitUpdateForm.is_valid"
        ) as mock_form_valid, mock.patch(
            "course.versioning.run_course_update_command"
        ) as mock_run_update:
            mock_form_valid.return_value = False
            resp = self.post_update_course_content(
                "some_commit_sha", command="update")
            self.assertEqual(resp.status_code, 200)
            self.assertEqual(mock_run_update.call_count, 0)

    def test_repo_is_in_subdir(self):
        self.course.course_root_path = "/subdir"
        self.course.save()

        from course.content import get_course_repo, SubdirRepoWrapper
        self.assertIsInstance(get_course_repo(self.course), SubdirRepoWrapper)

        with mock.patch(
            "course.versioning.run_course_update_command"
        ) as mock_run_update:
            self.post_update_course_content(
                self.course.active_git_commit_sha, command="update")

            self.assertEqual(mock_run_update.call_count, 1)

            from course.content import SubdirRepoWrapper
            from dulwich.repo import Repo
            self.assertIsInstance(mock_run_update.call_args[0][1], Repo)