Newer
Older
__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.
"""
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import mail
from django.test import RequestFactory, TestCase
from django.test.utils import override_settings # noqa
participation_status as p_status,
user_status as u_status,
)
from course.models import Participation, ParticipationPreapproval, ParticipationRole
from relate.utils import string_concat
from tests import factories
CoursesTestMixinBase,
MockAddMessageMixing,
SingleCoursePageTestMixin,
from tests.utils import LocmemBackendTestsMixin, mock
TEST_EMAIL_SUFFIX1 = "@suffix.com"
TEST_EMAIL_SUFFIX2 = "suffix.com"
EMAIL_CONNECTIONS = "EMAIL_CONNECTIONS"
EMAIL_CONNECTION_DEFAULT = "EMAIL_CONNECTION_DEFAULT"
NO_REPLY_EMAIL_FROM = "NO_REPLY_EMAIL_FROM"
NOTIFICATION_EMAIL_FROM = "NOTIFICATION_EMAIL_FROM"
GRADER_FEEDBACK_EMAIL_FROM = "GRADER_FEEDBACK_EMAIL_FROM"
STUDENT_INTERACT_EMAIL_FROM = "STUDENT_INTERACT_EMAIL_FROM"
ENROLLMENT_EMAIL_FROM = "ENROLLMENT_EMAIL_FROM"
# {{{ message constants
"Enrollment request sent. You will receive notification "
"by email once your request has been acted upon.")
"Your enrollment request is pending. You will be "
"notified once it has been acted upon.")
"Your enrollment request had been denied. Enrollment is not allowed.")
"You had been dropped from the course. Re-enrollment is not allowed.")
"You have previously sent the enrollment request. "
"Re-sending the request is not allowed.")
"A participation already exists. Enrollment attempt aborted.")
MESSAGE_CANNOT_REENROLL_TEXT = ("Already enrolled. Cannot re-enroll.")
MESSAGE_SUCCESSFULLY_ENROLLED_TEXT = ("Successfully enrolled.")
MESSAGE_EMAIL_SUFFIX_REQUIRED_PATTERN = (
"Enrollment not allowed. Please use your '%s' email to enroll.")
MESSAGE_NOT_ACCEPTING_ENROLLMENTS_TEXT = ("Course is not accepting enrollments.")
MESSAGE_ENROLL_ONLY_ACCEPT_POST_REQUEST_TEXT = (
MESSAGE_ENROLLMENT_DENIED_TEXT = "Successfully denied."
MESSAGE_ENROLLMENT_DROPPED_TEXT = "Successfully dropped."
"%(n_created)d preapprovals created, "
"%(n_exist)d already existed, "
"%(n_requested_approved)d pending requests approved.")
"Your email address is not yet confirmed. "
"Confirm your email to continue.")
MESSAGE_PARTICIPATION_CHANGE_SAVED_TEXT = ("Changes saved.")
EMAIL_NEW_ENROLLMENT_REQUEST_TITLE_PATTERN = (
EMAIL_ENROLLMENT_DECISION_TITLE_PATTERN = (
"This user has not confirmed his/her email.")
# }}}
def get_not_empty_count_from_list(lst):
return len([data for data in lst if data.strip()])
class EnrollmentTestMixin(MockAddMessageMixing, CoursesTestMixinBase):
@property
def course_page_url(self):
return self.get_course_page_url(self.course.identifier)
@property
def enroll_request_url(self):
return reverse("relate-enroll", args=[self.course.identifier])
@classmethod
def get_participation_edit_url(cls, participation_id):
return reverse("relate-edit_participation",
args=[cls.course.identifier, participation_id])
def get_participation_count_by_status(self, status):
return Participation.objects.filter(
course__identifier=self.course.identifier,
status=status
).count()
def update_course(self, **kwargs):
self.course.__dict__.update(kwargs)
self.course.save()
def update_require_approval_course(self, **kwargs):
self.course.__dict__.update(kwargs)
self.course.enrollment_approval_required = True
self.course.save()
def get_test_participation(self, **kwargs):
return factories.ParticipationFactory(
course=self.course, **kwargs)
def get_test_preapproval(self, **kwargs):
defaults = {"course": self.course,
"email": None,
"institutional_id": None}
defaults.update(kwargs)
return factories.ParticipationPreapprovalFactory(**defaults)
def assertParticipationStatusCallCount(self, expected_counts): # noqa
from collections import OrderedDict
d = OrderedDict()
counts = []
for status in sorted(
dict(constants.PARTICIPATION_STATUS_CHOICES).keys()):
count = Participation.objects.filter(
course=self.course, status=status
).count()
d[status] = count
counts.append(count)
self.assertListEqual(counts, expected_counts, repr(d))
@property
def student_role_post_data(self):
role, _ = (ParticipationRole.objects.get_or_create(
course=self.course, identifier="student"))
return [str(role.pk)]
@property
def preapproval_url(self):
return reverse("relate-create_preapprovals",
args=[self.course.identifier])
@property
def default_preapprove_role(self):
role, _ = (ParticipationRole.objects.get_or_create(
course=self.course, identifier="student"))
return [str(role.pk)]
def get_preapproval_count(self):
return ParticipationPreapproval.objects.all().count()
class EnrollViewTest(EnrollmentTestMixin, TestCase):
# test enrollment.enroll_view
Loading
Loading full blame...