Skip to content
base_test_mixins.py 110 KiB
Newer Older
from __future__ import annotations


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

Andreas Klöckner's avatar
Andreas Klöckner committed
import datetime
import hashlib
Dong Zhuang's avatar
Dong Zhuang committed
import os
Andreas Klöckner's avatar
Andreas Klöckner committed
import re
Andreas Klöckner's avatar
Andreas Klöckner committed
import sys
import tempfile
from collections import OrderedDict
from copy import deepcopy
from functools import partial
Andreas Klöckner's avatar
Andreas Klöckner committed
from types import MethodType
import memcache
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
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.test import Client, RequestFactory, override_settings
from django.urls import resolve, reverse
Dong Zhuang's avatar
Dong Zhuang committed
from course.constants import (
    flow_permission as fperm,
    grade_aggregation_strategy as g_strategy,
    participation_status,
    user_status,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from course.content import get_course_repo_path, get_repo_blob
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.flow import GradeInfo
from course.models import (
    Course,
    FlowPageData,
    FlowPageVisit,
    FlowSession,
    GradingOpportunity,
    Participation,
    ParticipationRole,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from tests.constants import (
    COMMIT_SHA_MAP,
    FAKED_YAML_PATH,
    QUIZ_FLOW_ID,
    TEST_PAGE_TUPLE,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
Josh Asplund's avatar
Josh Asplund committed
from tests.utils import mock
Dong Zhuang's avatar
Dong Zhuang committed

Andreas Klöckner's avatar
Andreas Klöckner 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 (
        f"test_course:{url_hash}",
        f"test_sha:{url_hash}"
Loading
Loading full blame...