Skip to content
base_test_mixins.py 111 KiB
Newer Older
Dong Zhuang's avatar
Dong Zhuang committed
__copyright__ = "Copyright (C) 2017 Dong Zhuang, Andreas Kloeckner, Zesheng Wang"

__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 tempfile
Dong Zhuang's avatar
Dong Zhuang committed
import os
import shutil
import hashlib
Dong Zhuang's avatar
Dong Zhuang committed
import datetime
from types import MethodType
from functools import partial

from collections import OrderedDict
from copy import deepcopy
from django.test import Client, override_settings, RequestFactory
Dong Zhuang's avatar
Dong Zhuang committed
from django.urls import reverse, resolve
from django.conf import settings
Dong Zhuang's avatar
Dong Zhuang committed
from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured

from course.flow import GradeInfo
Dong Zhuang's avatar
Dong Zhuang committed
from course.models import (
    Course, Participation, ParticipationRole, FlowSession, FlowPageData,
Dong Zhuang's avatar
Dong Zhuang committed
    FlowPageVisit, GradingOpportunity)
Dong Zhuang's avatar
Dong Zhuang committed
from course.constants import (
    participation_status, user_status,
    grade_aggregation_strategy as g_strategy,
    flow_permission as fperm)
from course.content import get_course_repo_path, get_repo_blob
from tests.constants import (
    QUIZ_FLOW_ID, TEST_PAGE_TUPLE, FAKED_YAML_PATH, COMMIT_SHA_MAP)
Josh Asplund's avatar
Josh Asplund committed
from tests.utils import mock
Dong Zhuang's avatar
Dong Zhuang committed

Dong Zhuang's avatar
Dong Zhuang committed
CORRECTNESS_ATOL = 1e-05
Dong Zhuang's avatar
Dong Zhuang committed

CREATE_SUPERUSER_KWARGS = {
    "username": "test_admin",
    "password": "test_admin",
    "email": "test_admin@example.com",
    "first_name": "Test",
    "last_name": "Admin"}

SINGLE_COURSE_SETUP_LIST = [
    {
        "course": {
            "identifier": "test-course",
            "name": "Test Course",
            "number": "CS123",
            "time_period": "Fall 2016",
            "hidden": False,
            "listed": True,
            "accepts_enrollment": True,
            "git_source": "https://github.com/inducer/relate-sample.git",
Dong Zhuang's avatar
Dong Zhuang committed
            "course_file": "course.yml",
            "events_file": "events.yml",
            "enrollment_approval_required": False,
            "enrollment_required_email_suffix": "",
            "preapproval_require_verified_inst_id": True,
Dong Zhuang's avatar
Dong Zhuang committed
            "from_email": "inform@tiker.net",
            "notify_email": "inform@tiker.net",
            },
Dong Zhuang's avatar
Dong Zhuang committed
        "participations": [
            {
                "role_identifier": "instructor",
                "user": {
                    "username": "test_instructor",
                    "password": "test_instructor",
                    "email": "test_instructor@example.com",
                    "first_name": "Test_ins",
Dong Zhuang's avatar
Dong Zhuang committed
                    "last_name": "Instructor"},
                "status": participation_status.active
            },
            {
                "role_identifier": "ta",
                "user": {
                    "username": "test_ta",
                    "password": "test",
                    "email": "test_ta@example.com",
                    "first_name": "Test_ta",
Dong Zhuang's avatar
Dong Zhuang committed
                    "last_name": "TA"},
                "status": participation_status.active
            },
            {
                "role_identifier": "student",
                "user": {
                    "username": "test_student",
                    "password": "test",
                    "email": "test_student@example.com",
                    "first_name": "Test_stu",
Dong Zhuang's avatar
Dong Zhuang committed
                    "last_name": "Student"},
                "status": participation_status.active
            }
TWO_COURSE_SETUP_LIST = deepcopy(SINGLE_COURSE_SETUP_LIST)
TWO_COURSE_SETUP_LIST[0]["course"]["identifier"] = "test-course1"
TWO_COURSE_SETUP_LIST += deepcopy(SINGLE_COURSE_SETUP_LIST)
TWO_COURSE_SETUP_LIST[1]["course"]["identifier"] = "test-course2"

NONE_PARTICIPATION_USER_CREATE_KWARG_LIST = [
    {
        "username": "test_user1",
        "password": "test_user1",
        "email": "test_user1@suffix.com",
        "first_name": "Test",
        "last_name": "User1",
        "institutional_id": "test_user1_institutional_id",
        "institutional_id_verified": True,
        "status": user_status.active
    },
    {
        "username": "test_user2",
        "password": "test_user2",
        "email": "test_user2@nosuffix.com",
        "first_name": "Test",
        "last_name": "User2",
        "institutional_id": "test_user2_institutional_id",
        "institutional_id_verified": False,
        "status": user_status.active
    },
    {
        "username": "test_user3",
        "password": "test_user3",
        "email": "test_user3@suffix.com",
        "first_name": "Test",
        "last_name": "User3",
        "institutional_id": "test_user3_institutional_id",
        "institutional_id_verified": True,
        "status": user_status.unconfirmed
    },
    {
        "username": "test_user4",
        "password": "test_user4",
        "email": "test_user4@no_suffix.com",
        "first_name": "Test",
        "last_name": "User4",
        "institutional_id": "test_user4_institutional_id",
        "institutional_id_verified": False,
        "status": user_status.unconfirmed
    mc = memcache.Client(["127.0.0.1:11211"])
SELECT2_HTML_FIELD_ID_SEARCH_PATTERN = re.compile(r'data-field_id="([^"]+)"')

def git_source_url_to_cache_keys(url):
    url_hash = hashlib.md5(url.encode("utf-8")).hexdigest()
    return (
        "test_course:%s" % url_hash,
        "test_sha:%s" % url_hash
    )

Dong Zhuang's avatar
Dong Zhuang committed

class CourseCreateFailure(Exception):
    pass


class classmethod_with_client:  # noqa: N801
    """This acts like Python's built-in ``classmethod``, with one change:
    When called on an instance (i.e. not a class), it automatically supplies
    ``self.client`` as the first argument.

    .. note::

        This isn't immensely logical, but it helped avoid an expensive
Loading
Loading full blame...