Skip to content
test_versioning.py 49.7 KiB
Newer Older
from __future__ import annotations


__copyright__ = "Copyright (C) 2017 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.
"""

import unittest
Andreas Klöckner's avatar
Andreas Klöckner committed
from copy import deepcopy
Andreas Klöckner's avatar
Andreas Klöckner committed
import pytest
from django.test import RequestFactory, TestCase
from dulwich.client import FetchPackResult
from dulwich.contrib.paramiko_vendor import ParamikoSSHVendor
Dong Zhuang's avatar
Dong Zhuang committed

Dong Zhuang's avatar
Dong Zhuang committed
from course.constants import participation_permission as pperm
from course.models import Course
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.validation import ValidationWarning
from relate.utils import force_remove_path
from tests import factories
Andreas Klöckner's avatar
Andreas Klöckner committed
from tests.base_test_mixins import (
    SINGLE_COURSE_SETUP_LIST,
    CoursesTestMixinBase,
    MockAddMessageMixing,
Andreas Klöckner's avatar
Andreas Klöckner committed
    SingleCourseTestMixin,
)
from tests.utils import mock, suppress_stdout_decorator

-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA7A1rTpbRpCek4tZZKa8QH14/pYzraN7hDnx3BKrqRxghP/0Q
uc98qeQkA5T3EYjHsConAAArLzbo6PMGwM9353dFixGUHegZe3jUmszX7G2veZx5
1xJ20pffbi8ohjv2Lj+nr799oGw7pGcEUXMr2v0b+UfToNFJAWGY3j9G+vTT7JEY
b9hq+XTOvoF+pLWbU7mIock5CxP3Q5NGS3/SjX2Qv50m6uc0dP34mGL4zDzgAMiC
99OpFms7CoO4AMWa/CfDCKl2vZHqDEF7Pd02vUP3ddb5pyGDeZmfhtK2LDLKi7ke
1pv1B4BwZ6HPmNep2CKEK6jf0+o5fIKm1uy3iQIDAQABAoIBAHCxl2FVr5BnPNju
7HJyGYhgPpKSzHCst1VrJocb8e0vH/CkqK+M1z9ko6zyGWJNosf/186wRe2skVVl
cPvsEJp43sKeCdCdVk0USqv8z7kYRIYSpjh/oCq6RvkbmoU7azR5P10wVpGYGoFK
jU01ZuKNpCVGnUpRoEEAjzLLkt+LyBjcey4ZUntp5L4h+ahm14Z8GLKbO2UCYZk6
nUnfigxiw/otXL+lN4+LjwjgTWS9JvG6Z+OQSf9b/G9DuDjpbvS5JQ0FONmUUH6m
nLwvJrePT50OPdB9mM4f+Ev03oRr9EXBXPL5t33SqRjhVn3dzH2wcuyhMUnZfGg0
MMksBAECgYEA/9VGBzaHYF2Va5OsNe3nvsOU9RI/n4/lN31MzDYwHDj2/NIDPR6S
7y42BxnGXce28t50ly/9ogut/yWfv0gyRbJ1tshl4/Lk4nqi9LwOOydL7XCd0+iM
ZiHfJafkItRNmVJjjX8OT0B0lfdmY/dJQJVH7zo6KzLlDNnzufZJSZECgYEA7DTX
kbGWrqtZl9wIvAdgfAlnagGpS/qUN8rB39TMHN5FpGH4xLV4BCyB6qacbF8YD3FB
14F4DVQh3/d6l0iybRBKFqxx3laUVeVYZNbvfaiCWvRJnnTZxI2ofQvK8nDhINiL
J4dpsRrxnTUrpoxg9xOejaLJHi9uPwOw0tjw0nkCgYBSAPvsbfcg1X6CuBgYRUTm
aezCTXIlZEt16O0H/EqZkUziJzMwkS9KCYb56bIi91RWLyYyHAjxu0qvoVC+UJcE
rjp7N2spkP769ZJsXic1oNf+qP1+Iml2h17uxA0leOXSwoz0mwhsMN3uABpK6sYJ
NJCVRxXEKREweGBeeGpvcQKBgAVkT2dr/lyOXMUyqKBiKrmqHUo2L38kgS2k2zgY
y2/9Qum1stAKtGqj+XM5ymhO42W22CHrOqpTOVK7e3jol+oVbRuHZDIHF+u+CH6E
yYK8zfz1hpivYikycp4oHsHaAcmWJ9cHKEp6qvlDtXNf0PbS49On259sxb96fhbS
DO1BAoGBAMvipNBiAbZXzNm6eTa6NMYcmiZpgV2zvx/Rbq+L2heOoBA7jyH41nl/
XHYymOvk4oiQKCfgal4+fqSpHSs6/S1MyLmHbONTLTwS8yHU9esi6ntEnse28ewc
yZ/atCI8q7Wyi7i2wr/okVekQYWdMSbnamp0A3zkYW6mhKpvibdN
-----END RSA PRIVATE KEY-----
"""
Dong Zhuang's avatar
Dong Zhuang committed
class VersioningTestMixin(CoursesTestMixinBase, MockAddMessageMixing):
    def setUpTestData(cls):
        super().setUpTestData()
        cls.instructor = cls.create_user(
            SINGLE_COURSE_SETUP_LIST[0]["participations"][0]["user"])
        cls.add_user_permission(cls.instructor, "add_course")

    def setUp(self):
        super().setUp()
        self.rf = RequestFactory()
        self.addCleanup(self.force_remove_all_course_dir)

    def get_set_up_new_course_form_data(self):
        return deepcopy(SINGLE_COURSE_SETUP_LIST[0]["course"])


class CourseCreationTest(VersioningTestMixin, TestCase):
Dong Zhuang's avatar
Dong Zhuang committed
    def test_get_set_up_new_course_view(self):
        with self.temporarily_switch_to_user(self.instructor):
            resp = self.client.get(self.get_set_up_new_course_url(),
Dong Zhuang's avatar
Dong Zhuang committed
                              data=SINGLE_COURSE_SETUP_LIST[0]["course"])
            self.assertEqual(resp.status_code, 200)

    def test_non_auth_set_up_new_course(self):
        with self.temporarily_switch_to_user(None):
            resp = self.get_set_up_new_course()
            self.assertTrue(resp.status_code, 403)

            data = SINGLE_COURSE_SETUP_LIST[0]["course"]
            resp = self.post_create_course(data, raise_error=False,
                                           login_superuser=False)
            self.assertTrue(resp.status_code, 403)
            self.assertEqual(Course.objects.count(), 0)

Dong Zhuang's avatar
Dong Zhuang committed
    def test_post_set_up_new_course_form_not_valid(self):
        data = SINGLE_COURSE_SETUP_LIST[0]["course"].copy()
        del data["identifier"]
        resp = self.post_create_course(data, raise_error=False)
        self.assertTrue(resp.status_code, 200)
        self.assertEqual(Course.objects.count(), 0)
        self.assertFormErrorLoose(resp, "This field is required.")

    def test_set_up_new_course_no_perm(self):
        # create a user which has no perm for creating course
        ta = self.create_user(
            SINGLE_COURSE_SETUP_LIST[0]["participations"][1]["user"])
        self.assertFalse(ta.has_perm("course.add_course"))
        self.assertFalse(ta.has_perm("course.change_course"))
        self.assertFalse(ta.has_perm("course.delete_course"))

        with self.temporarily_switch_to_user(ta):
            resp = self.get_set_up_new_course()
            self.assertTrue(resp.status_code, 403)

            data = self.get_set_up_new_course_form_data()
            resp = self.post_create_course(data, raise_error=False,
                                           login_superuser=False)
            self.assertTrue(resp.status_code, 403)
            self.assertEqual(Course.objects.count(), 0)

    def test_set_up_new_course_form_invalid(self):
        for field_name in ["identifier", "name", "number", "time_period",
                           "git_source", "from_email", "notify_email"]:
            form_data = self.get_set_up_new_course_form_data()
            del form_data[field_name]
            request = self.rf.post(self.get_set_up_new_course_url(), data=form_data)
            request.user = self.instructor
            form = versioning.CourseCreationForm(request.POST)
            self.assertFalse(form.is_valid())

Dong Zhuang's avatar
Dong Zhuang committed
    @suppress_stdout_decorator(suppress_stderr=True)
    def test_set_up_new_course_error_with_no_repo_created(self):
        resp = self.get_set_up_new_course()
        self.assertTrue(resp.status_code, 403)

        data = SINGLE_COURSE_SETUP_LIST[0]["course"]

Dong Zhuang's avatar
Dong Zhuang committed
        with mock.patch("course.versioning.Repo.init") as mock_repo_innit:
Dong Zhuang's avatar
Dong Zhuang committed
            mock_repo_innit.side_effect = RuntimeError("Repo init error")
            resp = self.post_create_course(data, raise_error=False)
            self.assertTrue(resp.status_code, 200)
            self.assertEqual(Course.objects.count(), 0)

Dong Zhuang's avatar
Dong Zhuang committed
            self.assertAddMessageCalledWith(
                "Course creation failed: RuntimeError: Repo init error")
Dong Zhuang's avatar
Dong Zhuang committed

    @suppress_stdout_decorator(suppress_stderr=True)
    def test_set_up_new_course_failed_to_delete_repo(self):
        resp = self.get_set_up_new_course()
        self.assertTrue(resp.status_code, 403)

        def force_remove_path_side_effect(path):
            # we need to delete the path, or tests followed will fail
            force_remove_path(path)
            raise OSError("my os error")

        data = SINGLE_COURSE_SETUP_LIST[0]["course"]

        with mock.patch(
                "dulwich.client.GitClient.fetch"
        ) as mock_fetch, mock.patch(
                "relate.utils.force_remove_path"
Dong Zhuang's avatar
Dong Zhuang committed
        )as mock_force_remove_path:
Dong Zhuang's avatar
Dong Zhuang committed
            mock_fetch.side_effect = RuntimeError("my fetch error")
            mock_force_remove_path.side_effect = force_remove_path_side_effect
            resp = self.post_create_course(data, raise_error=False)
            self.assertTrue(resp.status_code, 200)
            self.assertEqual(Course.objects.count(), 0)

Dong Zhuang's avatar
Dong Zhuang committed
            self.assertAddMessageCallCount(2)
Dong Zhuang's avatar
Dong Zhuang committed

Dong Zhuang's avatar
Dong Zhuang committed
            self.assertAddMessageCalledWith(
                "Failed to delete unused repository directory", reset=False)
            self.assertAddMessageCalledWith(
                "Course creation failed: RuntimeError: my fetch error")
Dong Zhuang's avatar
Dong Zhuang committed

    @suppress_stdout_decorator(suppress_stderr=True)
    def test_set_up_new_course_git_source_invalid(self):
        data = self.get_set_up_new_course_form_data()
        request = self.rf.post(self.get_set_up_new_course_url(), data=data)
        request.user = self.instructor
        with mock.patch("dulwich.client.GitClient.fetch",
                        return_value=FetchPackResult(
                            refs={},
                            symrefs={},
                            agent="Git")), \
                mock.patch("course.models.Course.save") as mock_save, \
                mock.patch("course.versioning.render"):
            resp = versioning.set_up_new_course(request)
            self.assertTrue(resp.status_code, 200)
            self.assertEqual(mock_save.call_count, 0)
Dong Zhuang's avatar
Dong Zhuang committed
            self.assertAddMessageCalledWith(
                "No refs found in remote repository")
Josh Asplund's avatar
Josh Asplund committed
@pytest.mark.slow
@pytest.mark.django_db
class ParamikoSSHVendorTest(TestCase):
    # A simple integration tests, making sure ParamikoSSHVendor is used
    # for ssh protocol.

        data = deepcopy(SINGLE_COURSE_SETUP_LIST[0]["course"])
        data["identifier"] = "my-private-course"
        data["git_source"] = "git+ssh://foo.com:1234/bar/baz"
Josh Asplund's avatar
Josh Asplund committed
        data["ssh_private_key"] = TEST_PRIVATE_KEY
        return data

    def make_ssh_vendor(cls):
        course = factories.CourseFactory.create(**cls.prepare_data())
        git_client, _ = (
            versioning.get_dulwich_client_and_remote_path_from_course(course))
        ssh_vendor = git_client.ssh_vendor
        assert isinstance(ssh_vendor, ParamikoSSHVendor)
        return ssh_vendor

    def test_invalid(self):
        from paramiko.ssh_exception import SSHException
        expected_error_msgs = [
                "Authentication failed",
                "key cannot be used for signing",

                # Raised when run in a user account that has
                # an (encrypted) key file in $HOME/.ssh.
                "Private key file is encrypted"]
        with self.assertRaises(SSHException) as cm:
            # This is also used to ensure paramiko.client.MissingHostKeyPolicy
            # is added to the client
                host="github.com",
                command="git-upload-pack '/bar/baz'",
                username=None,
                port=None)
        self.assertTrue(any(
            msg in str(cm.exception) for msg in expected_error_msgs))
        with self.assertRaises(SSHException) as cm:
                host="github.com",
                command="git-upload-pack '/bar/baz'",
                username="me",
                port=22)
        self.assertTrue(any(
            msg in str(cm.exception) for msg in expected_error_msgs))
        # Hi Andreas, before you start changing these: run the tests
        # with your Yubikey unplugged.
        expected_error_msg = "Bad authentication type"
        with self.assertRaises(SSHException) as cm:
                host="github.com",
                command="git-upload-pack '/bar/baz'",
                password="mypass")

        self.assertIn(expected_error_msg, str(cm.exception))

        with self.assertRaises(FileNotFoundError) as cm:
                host="github.com",
                command="git-upload-pack '/bar/baz'",
                key_filename="key_file")

        expected_error_msg = "No such file or directory: 'key_file'"
        self.assertIn(expected_error_msg, str(cm.exception))

        with self.assertRaises(AttributeError) as cm:
                host="github.com",
                command="git-upload-pack '/bar/baz'",
                pkey="invalid_key")

    @suppress_stdout_decorator(suppress_stderr=True)
    def test_set_up_ensure_get_transport_called(self):
        with mock.patch(
                "course.versioning.paramiko.SSHClient.connect"
        ) as mock_connect, mock.patch(
            "course.versioning.paramiko.SSHClient.get_transport"
        ) as mock_get_transport:
            mock_channel = mock.MagicMock()
            mock_get_transport.return_value.open_session.return_value = mock_channel
            mock_channel.exec_command = mock.MagicMock()
            mock_channel.recv.side_effect = [b"10"]
            mock_channel.recv_stderr.side_effect = [b"my custom error", "", ""]

            try:
                    host="github.com",
                    command="git-upload-pack '/bar/baz'")
            except StopIteration:
                pass

            self.assertEqual(mock_connect.call_count, 1)

            from paramiko.rsakey import RSAKey
            used_rsa_key = None

            # make sure rsa_key is used when connect
            for v in mock_connect.call_args[1].values():
                if isinstance(v, RSAKey):
                    used_rsa_key = v

            self.assertIsNotNone(used_rsa_key)

            # make sure get_transport is called
            self.assertEqual(mock_get_transport.call_count, 1)

            # make sure exec_command is called
            self.assertEqual(mock_channel.exec_command.call_count, 1)
            self.assertIn(
                "git-upload-pack '/bar/baz'",
                mock_channel.exec_command.call_args[0])


class FakeCommit:
Dong Zhuang's avatar
Dong Zhuang committed
    def __init__(self, name, parents=None, id=None,
                 message=b"my commit message"):
        self.name = name
        self.parents = parents or []
        self.message = message
        self.id = id

    def __repr__(self):
        return "{}: {}".format(*self.__class__.__name__) + str(self.name)
Dong Zhuang's avatar
Dong Zhuang committed


class IsParentCommitTest(unittest.TestCase):
    def setUp(self):
        repo_dict = {}
        repo = mock.MagicMock()
        repo.__getitem__.side_effect = repo_dict.__getitem__
        repo.__setitem__.side_effect = repo_dict.__setitem__
        self.repo = repo

    def test_false(self):
        c0 = FakeCommit(b"head", None)
        self.repo[b"HEAD"] = c0
        self.repo[c0] = FakeCommit(b"none", None)

        c1 = FakeCommit(b"first", [c0])

        self.assertFalse(
            versioning.is_ancestor_commit(
                self.repo, potential_ancestor=c0, child=c1))
Dong Zhuang's avatar
Dong Zhuang committed

        self.assertFalse(
            versioning.is_ancestor_commit(
                self.repo, potential_ancestor=c0, child=c1,
Dong Zhuang's avatar
Dong Zhuang committed
                max_history_check_size=2))

    def test_true(self):
        c0 = FakeCommit(b"head", None)
        self.repo[b"HEAD"] = c0
        self.repo[c0] = c0

        c1 = FakeCommit(b"first", [c0])
        self.repo[c1] = FakeCommit(b"first_c", [c0])

        c2 = FakeCommit(b"second", [c1])
        self.repo[c2] = FakeCommit(b"second_c", [c1])

        c3 = FakeCommit(b"third", [c2])

        self.assertFalse(
            versioning.is_ancestor_commit(
                self.repo, potential_ancestor=c0, child=c3,
Dong Zhuang's avatar
Dong Zhuang committed
                max_history_check_size=1))

        self.assertTrue(
            versioning.is_ancestor_commit(
                self.repo, potential_ancestor=c0, child=c3,
Dong Zhuang's avatar
Dong Zhuang committed
                max_history_check_size=20))


Isuru Fernando's avatar
Isuru Fernando committed
class DirectGitEndpointTest(TestCase):
    def test_no_authentication_headers(self):
        course = factories.CourseFactory()

        request = mock.MagicMock()
        obj = object()

        def no_header_mock(a, b=obj):
            if b == obj:
                raise KeyError
            return b

        request.META.get = no_header_mock
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)

    def test_b64encoded_authentication_headers(self):
        from base64 import b64encode
        course = factories.CourseFactory()
        request = mock.MagicMock()

        request.META.get.return_value = "foo"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)

Isuru Fernando's avatar
Isuru Fernando committed
        request.META.get.return_value = "NonBasic foo"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)

Isuru Fernando's avatar
Isuru Fernando committed
        request.META.get.return_value = "Basic foo"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)

        auth_data = b64encode(b"foo").decode("utf-8")
        request.META.get.return_value = f"Basic {auth_data}"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)

    def test_auth_student(self):
        from base64 import b64encode
Andreas Klöckner's avatar
Andreas Klöckner committed

Isuru Fernando's avatar
Isuru Fernando committed
        from django.contrib.auth.hashers import make_password

Andreas Klöckner's avatar
Andreas Klöckner committed
        from course.models import AuthenticationToken

Isuru Fernando's avatar
Isuru Fernando committed
        course = factories.CourseFactory()
        student = factories.UserFactory()
Isuru Fernando's avatar
Isuru Fernando committed
        student2 = factories.UserFactory()
Isuru Fernando's avatar
Isuru Fernando committed
        student_role = factories.ParticipationRoleFactory(
            course=course,
            identifier="student"
        )
        participation1 = factories.ParticipationFactory(
            course=course,
            user=student)
        participation1.roles.set([student_role])

        auth_token = AuthenticationToken(
                user=student,
                participation=participation1,
                token_hash=make_password("spam"))
        auth_token.save()

Isuru Fernando's avatar
Isuru Fernando committed
        # Check invalid token format
Isuru Fernando's avatar
Isuru Fernando committed
        auth_data_unencoded = "{}:{}".format(student.username, "spam").encode()
        auth_data = b64encode(auth_data_unencoded).decode("utf-8")
Isuru Fernando's avatar
Isuru Fernando committed
        request = mock.MagicMock()
        request.META.get.return_value = f"Basic {auth_data}"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)
Isuru Fernando's avatar
Isuru Fernando committed

Isuru Fernando's avatar
Isuru Fernando committed
        # Check invalid token id
        auth_data_unencoded = "{}:{}_{}".format(student.username,
                                                "eggs", "ham").encode()
        auth_data = b64encode(auth_data_unencoded).decode("utf-8")
        request = mock.MagicMock()
        request.META.get.return_value = f"Basic {auth_data}"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)

        # Check non-existing user
        auth_data_unencoded = "{}:{}_{}".format("spam",
                                                "eggs", "ham").encode()
        auth_data = b64encode(auth_data_unencoded).decode("utf-8")
        request = mock.MagicMock()
        request.META.get.return_value = f"Basic {auth_data}"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)

        # Check token from other user
        auth_data_unencoded = "{}:{}_{}".format(student2.username,
Isuru Fernando's avatar
Isuru Fernando committed
                                                auth_token.id, "ham").encode()
Isuru Fernando's avatar
Isuru Fernando committed
        auth_data = b64encode(auth_data_unencoded).decode("utf-8")
Isuru Fernando's avatar
Isuru Fernando committed
        request = mock.MagicMock()
        request.META.get.return_value = f"Basic {auth_data}"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)

Isuru Fernando's avatar
Isuru Fernando committed
        # Check student with no permission
Isuru Fernando's avatar
Isuru Fernando committed
        auth_data_unencoded = "{}:{}_{}".format(student.username,
                                                auth_token.id, "spam").encode()
        auth_data = b64encode(auth_data_unencoded).decode("utf-8")
        request = mock.MagicMock()
        request.META.get.return_value = f"Basic {auth_data}"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        response = versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(response.status_code, 401)

    def test_auth_instructor(self):
        from base64 import b64encode
Andreas Klöckner's avatar
Andreas Klöckner committed

Isuru Fernando's avatar
Isuru Fernando committed
        from django.contrib.auth.hashers import make_password

Andreas Klöckner's avatar
Andreas Klöckner committed
        from course.constants import participation_permission as pp
        from course.models import AuthenticationToken, ParticipationRolePermission

Isuru Fernando's avatar
Isuru Fernando committed
        course = factories.CourseFactory()
        instructor = factories.UserFactory()
        instructor_role = factories.ParticipationRoleFactory(
            course=course,
            identifier="instructor"
        )
        participation1 = factories.ParticipationFactory(
            course=course,
            user=instructor)
        participation1.roles.set([instructor_role])
        ParticipationRolePermission(role=instructor_role,
                                    permission=pp.use_git_endpoint).save()
Isuru Fernando's avatar
Isuru Fernando committed

        auth_token = AuthenticationToken(
                user=instructor,
                participation=participation1,
                token_hash=make_password("spam"))
        auth_token.save()

        fake_call_wsgi_app = mock.patch("course.versioning.call_wsgi_app")
        fake_get_course_repo = mock.patch("course.content.get_course_repo")
        mock_call_wsgi_app = fake_call_wsgi_app.start()
Isuru Fernando's avatar
Isuru Fernando committed
        mock_get_course_repo = fake_get_course_repo.start()
        self.addCleanup(fake_call_wsgi_app.stop)
        self.addCleanup(fake_get_course_repo.stop)
Isuru Fernando's avatar
Isuru Fernando committed

        auth_data_unencoded = "{}:{}_{}".format(instructor.username,
                                                auth_token.id, "spam").encode()
        auth_data = b64encode(auth_data_unencoded).decode("utf-8")
        request = mock.MagicMock()
        request.headers.get.return_value = f"Basic {auth_data}"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(mock_call_wsgi_app.call_count, 1)
Isuru Fernando's avatar
Isuru Fernando committed
        self.assertEqual(mock_get_course_repo.call_count, 1)
Isuru Fernando's avatar
Isuru Fernando committed

Isuru Fernando's avatar
Isuru Fernando committed
        fake_dulwich_web_backend = mock.patch("dulwich.web.DictBackend")
        fake_get_course_repo = mock.patch("course.content.get_course_repo")
        mock_dulwich_web_backend = fake_dulwich_web_backend.start()
        self.addCleanup(fake_dulwich_web_backend.stop)
Isuru Fernando's avatar
Isuru Fernando committed
        request = mock.MagicMock()
        request.headers.get.return_value = f"Basic {auth_data}"
        request.environ = request.META
Isuru Fernando's avatar
Isuru Fernando committed
        versioning.git_endpoint(request, course.identifier, "")
        self.assertEqual(mock_dulwich_web_backend.call_count, 1)
        self.assertEqual(mock_get_course_repo.call_count, 2)
Isuru Fernando's avatar
Isuru Fernando committed

Isuru Fernando's avatar
Isuru Fernando committed

Dong Zhuang's avatar
Dong Zhuang committed
FETCHED_LITERAL = "Fetch successful."
VALIDATE_SUCCESS_LITERAL = "Course content validated successfully."
PREVIEW_END_LITERAL = "Preview ended."
UPDATE_APPLIED_LITERAL = "Update applied."
WARNINGS_LITERAL = "Course content validated OK, with warnings:"
VALIDATE_FAILURE_LITERAL = "Course content did not validate successfully:"
NOT_UPDATED_LITERAL = "Update not applied."
FAILURE_MSG = "my validation error."
LOCATION1 = "location1"
LOCATION2 = "location2"
WARNING1 = "some warning1"
WARNING2 = "some warning2"
Josh Asplund's avatar
Josh Asplund committed
@pytest.mark.django_db
Dong Zhuang's avatar
Dong Zhuang committed
class RunCourseUpdateCommandTest(MockAddMessageMixing, unittest.TestCase):
Dong Zhuang's avatar
Dong Zhuang committed
    # test versioning.run_course_update_command

    default_preview_sha = "preview_sha"
    default_old_sha = "old_sha"
    default_switch_to_sha = "switch_sha"
    default_latest_sha = "latest_sha"
Dong Zhuang's avatar
Dong Zhuang committed

    def setUp(self):
        super().setUp()
Dong Zhuang's avatar
Dong Zhuang committed
        self.course = factories.CourseFactory(
            active_git_commit_sha=self.default_old_sha)
        user = factories.UserFactory()
        instructor_role = factories.ParticipationRoleFactory(
            course=self.course,
            identifier="instructor"
        )

        self.participation = factories.ParticipationFactory(
            course=self.course,
            preview_git_commit_sha=None,
            user=user)
        self.participation.roles.set([instructor_role])

        self.request = mock.MagicMock()
        self.request.user = user

        self.pctx = mock.MagicMock()
        self.pctx.course = self.course
        self.pctx.participation = self.participation

        self.repo = mock.MagicMock()
        self.content_repo = self.repo

        fake_get_dulwich_client_and_remote_path_from_course = mock.patch(
            "course.versioning.get_dulwich_client_and_remote_path_from_course")
        self.mock_get_dulwich_client_and_remote_path_from_course = (
            fake_get_dulwich_client_and_remote_path_from_course.start()
        )

        self.mock_client = mock.MagicMock()
        remote_path = "/remote/path"
        self.mock_get_dulwich_client_and_remote_path_from_course.return_value = (
            self.mock_client, remote_path
        )
        self.mock_client.fetch.return_value = FetchPackResult(
                refs={b"HEAD": self.default_switch_to_sha.encode()},
                symrefs={},
                agent="Git")
Dong Zhuang's avatar
Dong Zhuang committed

        self.addCleanup(fake_get_dulwich_client_and_remote_path_from_course.stop)

        fake_transfer_remote_refs = mock.patch(
            "course.versioning.transfer_remote_refs")
        self.mock_transfer_remote_refs = fake_transfer_remote_refs.start()
        self.addCleanup(fake_transfer_remote_refs.stop)

        fake_is_ancestor_commit = mock.patch("course.versioning.is_ancestor_commit")
        self.mock_is_ancestor_commit = fake_is_ancestor_commit.start()
        self.mock_is_ancestor_commit.return_value = False
        self.addCleanup(fake_is_ancestor_commit.stop)
Dong Zhuang's avatar
Dong Zhuang committed

        fake_validate_course_content = mock.patch(
            "course.validation.validate_course_content")
        self.mock_validate_course_content = fake_validate_course_content.start()
        self.mock_validate_course_content.return_value = []
        self.addCleanup(fake_validate_course_content.stop)

    def tearDown(self):
        for course in Course.objects.all():
            course.delete()

    def test_is_ancestor_commit_checked(self):
Dong Zhuang's avatar
Dong Zhuang committed
        may_update = True
        prevent_discarding_revisions = True

        command_tup = [
            ("fetch", True),
            ("fetch_update", True),
            ("update", False),
            ("fetch_preview", True),
            ("preview", False),
            ("end_preview", False)]

        for command, will_check in command_tup:
            self.mock_is_ancestor_commit.reset_mock()
Dong Zhuang's avatar
Dong Zhuang committed
            with self.subTest(
                    command=command,
                    prevent_discarding_revisions=prevent_discarding_revisions):
                versioning.run_course_update_command(
                    self.request, self.repo, self.content_repo, self.pctx, command,
                    self.default_switch_to_sha.encode(), may_update,
                    prevent_discarding_revisions)
                if will_check and self.mock_is_ancestor_commit.call_count != 1:
Dong Zhuang's avatar
Dong Zhuang committed
                    self.fail(
                        f"'is_ancestor_commit' is expected for command '{command}' to "
                        "be called while not")
                elif not will_check and self.mock_is_ancestor_commit.call_count > 0:
Dong Zhuang's avatar
Dong Zhuang committed
                    self.fail(
                        f"'is_ancestor_commit' is not expected for command '{command}' to "  # noqa: E501
                        "be called while called")
Dong Zhuang's avatar
Dong Zhuang committed

        # when not prevent_discarding_revisions, is_ancestor_commit
Dong Zhuang's avatar
Dong Zhuang committed
        # should not be checked (expensive operation)

        prevent_discarding_revisions = False
        for command, _ in command_tup:
            self.mock_is_ancestor_commit.reset_mock()
Dong Zhuang's avatar
Dong Zhuang committed
            with self.subTest(
                    command=command,
                    prevent_discarding_revisions=prevent_discarding_revisions):
                versioning.run_course_update_command(
                    self.request, self.repo, self.content_repo, self.pctx, command,
                    self.default_switch_to_sha.encode(),
                    may_update, prevent_discarding_revisions)
                if self.mock_is_ancestor_commit.call_count > 0:
Dong Zhuang's avatar
Dong Zhuang committed
                    self.fail(
                        f"'is_ancestor_commit' is not expected for command '{command}' to "  # noqa: E501
                        "be called while called (expensive)")
                elif self.mock_is_ancestor_commit.call_count > 0:
Dong Zhuang's avatar
Dong Zhuang committed
                    self.fail(
                        f"'is_ancestor_commit' is not expected for command '{command}' to "  # noqa: E501
                        "be called while called")
Dong Zhuang's avatar
Dong Zhuang committed

    def test_is_content_validated(self):
        may_update = True

        command_tup = [
            ("fetch", False),
            ("fetch_update", True),
            ("update", True),
            ("fetch_preview", True),
            ("preview", True),
            ("end_preview", False)]

        for command, will_validate in command_tup:
            self.mock_validate_course_content.reset_mock()
            with self.subTest(command=command):
                versioning.run_course_update_command(
                    self.request, self.repo, self.content_repo, self.pctx, command,
                    self.default_switch_to_sha.encode(),
                    may_update, prevent_discarding_revisions=False)
                if (will_validate
                        and self.mock_validate_course_content.call_count != 1):
                    self.fail(
                        "'validate_course_content' is expected for "
                        f"command '{command}' to be called while not")
Dong Zhuang's avatar
Dong Zhuang committed
                elif (not will_validate
                      and self.mock_validate_course_content.call_count > 0):
                    self.fail(
                        "'validate_course_content' is not expected for "
                        f"command '{command}' to be called while called")
Dong Zhuang's avatar
Dong Zhuang committed

    def test_unknown_command(self):
        command = "unknown"
        may_update = True
        prevent_discarding_revisions = False

        with self.assertRaises(RuntimeError) as cm:
            versioning.run_course_update_command(
                self.request, self.repo, self.content_repo, self.pctx, command,
                self.default_switch_to_sha.encode(),
                may_update, prevent_discarding_revisions)

        self.assertEqual(self.mock_validate_course_content.call_count, 0)
        self.assertEqual(self.mock_is_ancestor_commit.call_count, 0)
Dong Zhuang's avatar
Dong Zhuang committed

        expected_error_msg = "invalid command"
        self.assertIn(expected_error_msg, str(cm.exception))

    def check_command_message_result(
            self, command, add_message_expected_call_count=0,
            expected_add_message_literals=None,
            not_expected_add_message_literals=None,
            is_previewing=False,
            expected_error_type=None,
            expected_error_msg=None,
            **call_kwargs
    ):
        kwargs = {
            "request": self.request,
            "repo": self.repo,
            "content_repo": self.content_repo,
            "pctx": self.pctx,
            "command": command,
            "new_sha": self.default_switch_to_sha.encode(),
            "may_update": True,
            "prevent_discarding_revisions": True
        }
        kwargs.update(call_kwargs)

        if expected_add_message_literals is None:
            expected_add_message_literals = []
        else:
            assert isinstance(expected_add_message_literals, list)

        if not_expected_add_message_literals is None:
            not_expected_add_message_literals = []
        else:
            assert isinstance(not_expected_add_message_literals, list)

        if is_previewing:
            self.participation.preview_git_commit_sha = self.default_preview_sha
            self.participation.save()

        if expected_error_type:
            assert expected_error_msg is not None
            with self.assertRaises(expected_error_type) as cm:
                versioning.run_course_update_command(**kwargs)

            self.assertIn(expected_error_msg, str(cm.exception))
        else:
            versioning.run_course_update_command(**kwargs)

Dong Zhuang's avatar
Dong Zhuang committed
        self.assertAddMessageCallCount(add_message_expected_call_count)
        self.assertAddMessageCalledWith(
            expected_add_message_literals, reset=False)
        self.assertAddMessageNotCalledWith(
            not_expected_add_message_literals, reset=False)
        self.reset_add_message_mock()
Dong Zhuang's avatar
Dong Zhuang committed

    def test_fetch(self):
        self.check_command_message_result(
            command="fetch",
            add_message_expected_call_count=1,
            expected_add_message_literals=[
                FETCHED_LITERAL
            ],
            not_expected_add_message_literals=[PREVIEW_END_LITERAL]
        )

    def test_end_preview(self):
        self.check_command_message_result(
            command="end_preview",
            add_message_expected_call_count=1,
            is_previewing=True,
            expected_add_message_literals=[
                PREVIEW_END_LITERAL
            ]
        )
        self.assertIsNone(self.participation.preview_git_commit_sha)

    def test_fetch_not_prevent_discarding_revisions(self):
        self.mock_client.fetch.return_value = FetchPackResult(
                refs={b"HEAD": self.default_latest_sha.encode()},
                symrefs={},
                agent="Git")
Dong Zhuang's avatar
Dong Zhuang committed

        command_tup = (
            ("fetch", 1, [FETCHED_LITERAL], [UPDATE_APPLIED_LITERAL],
             self.default_old_sha),
            ("fetch_update", 3, [FETCHED_LITERAL, UPDATE_APPLIED_LITERAL,
                                 VALIDATE_SUCCESS_LITERAL], [],
             self.default_latest_sha)
Dong Zhuang's avatar
Dong Zhuang committed
        )

        for (command, add_message_call_count, expected, not_expected,
             expected_course_sha) in command_tup:
            with self.subTest(command=command):
                self.mock_is_ancestor_commit.return_value = False
Dong Zhuang's avatar
Dong Zhuang committed
                self.check_command_message_result(
                    command=command,
                    add_message_expected_call_count=add_message_call_count,
                    expected_add_message_literals=expected,
                    not_expected_add_message_literals=not_expected,
                    prevent_discarding_revisions=False
                )

                self.mock_is_ancestor_commit.return_value = True
Dong Zhuang's avatar
Dong Zhuang committed
                self.check_command_message_result(
                    command=command,
                    add_message_expected_call_count=add_message_call_count,
                    expected_add_message_literals=expected,
                    not_expected_add_message_literals=not_expected,
                    prevent_discarding_revisions=False
                )

                self.assertEqual(
                    self.course.active_git_commit_sha, expected_course_sha)

Isuru Fernando's avatar
Isuru Fernando committed
    def test_internal_git_repo_more_commits(self):
Isuru Fernando's avatar
Isuru Fernando committed
        from collections import defaultdict
        self.mock_is_ancestor_commit.return_value = False
Isuru Fernando's avatar
Isuru Fernando committed
        repo = defaultdict(lambda: "bar")
        repo[b"HEAD"] = "foo"
Isuru Fernando's avatar
Isuru Fernando committed

        self.check_command_message_result(
            command="fetch",
            expected_error_type=RuntimeError,
            expected_error_msg="internal git repo has more commits."
Isuru Fernando's avatar
Isuru Fernando committed
                               " Fetch, merge and push.",
Isuru Fernando's avatar
Isuru Fernando committed
            add_message_expected_call_count=0,
Isuru Fernando's avatar
Isuru Fernando committed
            prevent_discarding_revisions=True,
            repo=repo,
Isuru Fernando's avatar
Isuru Fernando committed
        )
        self.assertAddMessageCallCount(0)

Dong Zhuang's avatar
Dong Zhuang committed
    def test_fetch_update_success_with_warnings(self):
        self.mock_client.fetch.return_value = FetchPackResult(
                refs={b"HEAD": self.default_latest_sha.encode()},
                symrefs={},
                agent="Git")
Dong Zhuang's avatar
Dong Zhuang committed
        self.mock_validate_course_content.return_value = (
            ValidationWarning(LOCATION1, WARNING1),
            ValidationWarning(LOCATION2, WARNING2),
        )

        self.check_command_message_result(
            command="fetch_update",
            add_message_expected_call_count=3,
            expected_add_message_literals=[
                FETCHED_LITERAL,
                WARNINGS_LITERAL, LOCATION1, WARNING1, LOCATION2, WARNING2,
            ],
            not_expected_add_message_literals=[PREVIEW_END_LITERAL])
        self.assertIsNone(self.participation.preview_git_commit_sha)
        self.assertEqual(
            self.course.active_git_commit_sha, self.default_latest_sha)
Dong Zhuang's avatar
Dong Zhuang committed

    def test_fetch_update_success_with_warnings_previewing(self):
        self.mock_client.fetch.return_value = FetchPackResult(
                refs={b"HEAD": self.default_latest_sha.encode()},
                symrefs={},
                agent="Git")

Dong Zhuang's avatar
Dong Zhuang committed
        self.mock_validate_course_content.return_value = (
            ValidationWarning(LOCATION1, WARNING1),
            ValidationWarning(LOCATION2, WARNING2),
        )

        self.check_command_message_result(
            command="fetch_update",
            is_previewing=True,
            add_message_expected_call_count=4,
            expected_add_message_literals=[
                FETCHED_LITERAL,
                WARNINGS_LITERAL, LOCATION1, WARNING1, LOCATION2, WARNING2,
                PREVIEW_END_LITERAL
            ])
        self.assertIsNone(self.participation.preview_git_commit_sha)
        self.assertEqual(
            self.course.active_git_commit_sha, self.default_latest_sha)
Dong Zhuang's avatar
Dong Zhuang committed

    def test_fetch_update_with_validation_error(self):
        from course.validation import ValidationError
        my_validation_error_msg = "my validation error."
        self.mock_validate_course_content.side_effect = (
            ValidationError(my_validation_error_msg))

        self.check_command_message_result(
            command="fetch_update",
            add_message_expected_call_count=2,
            expected_add_message_literals=[
                FETCHED_LITERAL,
                VALIDATE_FAILURE_LITERAL, my_validation_error_msg],
            not_expected_add_message_literals=[PREVIEW_END_LITERAL])
        self.assertIsNone(self.participation.preview_git_commit_sha)
        self.assertEqual(self.course.active_git_commit_sha, self.default_old_sha)

    def test_update_with_validation_error(self):
        from course.validation import ValidationError
        my_validation_error_msg = "my validation error."
        self.mock_validate_course_content.side_effect = (
            ValidationError(my_validation_error_msg))

        self.check_command_message_result(
            command="update",
            add_message_expected_call_count=1,
            expected_add_message_literals=[
                VALIDATE_FAILURE_LITERAL, my_validation_error_msg],
            not_expected_add_message_literals=[PREVIEW_END_LITERAL]
        )
        self.assertIsNone(self.participation.preview_git_commit_sha)
        self.assertEqual(self.course.active_git_commit_sha, self.default_old_sha)

    def test_update_with_validation_error_previewing(self):
        from course.validation import ValidationError
        my_validation_error_msg = "my validation error."
        self.mock_validate_course_content.side_effect = (
            ValidationError(my_validation_error_msg))

        self.check_command_message_result(
            command="update",
            add_message_expected_call_count=1,
            is_previewing=True,
            expected_add_message_literals=[
                VALIDATE_FAILURE_LITERAL, my_validation_error_msg],
            not_expected_add_message_literals=[PREVIEW_END_LITERAL]
        )
        self.assertIsNotNone(self.participation.preview_git_commit_sha)
        self.assertEqual(
            self.participation.preview_git_commit_sha, self.default_preview_sha)
        self.assertEqual(self.course.active_git_commit_sha, self.default_old_sha)

    def test_fetch_not_may_update(self):
        self.mock_client.fetch.return_value = FetchPackResult(
                refs={b"HEAD": self.default_latest_sha.encode()},
                symrefs={},
                agent="Git")
Dong Zhuang's avatar
Dong Zhuang committed

        command_tup = (
            ("fetch", 1, [FETCHED_LITERAL], [UPDATE_APPLIED_LITERAL],
             self.default_old_sha),
            ("fetch_update", 2, [FETCHED_LITERAL, VALIDATE_SUCCESS_LITERAL],
             [UPDATE_APPLIED_LITERAL],
             self.default_old_sha)
        )

        for (command, add_message_call_count, expected, not_expected,