Newer
Older
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner, Zesheng Wang, 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 datetime
import pytz
import unittest
from django.test import TestCase, override_settings
from django import http
from django.urls import reverse
from django.utils.timezone import now, timedelta
from course.models import ExamTicket, FlowSession
from course import constants, exam
from tests.constants import (
DATE_TIME_PICKER_TIME_FORMAT)
from tests.base_test_mixins import (
SingleCourseTestMixin, MockAddMessageMixing, SingleCoursePageTestMixin)
from tests.utils import mock, reload_urlconf
from tests import factories
class GenTicketCodeTest(unittest.TestCase):
"""test exam.gen_ticket_code"""
def test_unique(self):
code = set()
for i in range(10):
code.add(exam.gen_ticket_code())
self.assertEqual(len(code), 10)
class ExamTestMixin(SingleCourseTestMixin, MockAddMessageMixing):
force_login_student_for_each_test = False
default_faked_now = datetime.datetime(2019, 1, 1, tzinfo=pytz.UTC)
default_valid_start_time = default_faked_now
default_valid_end_time = default_valid_start_time + timedelta(hours=3)
@classmethod
def setUpTestData(cls): # noqa
cls.add_user_permission(
cls.instructor_participation.user, "can_issue_exam_tickets",
model=ExamTicket)
cls.exam = factories.ExamFactory(course=cls.course)
def setUp(self):
self.c.force_login(self.instructor_participation.user)
fake_get_now_or_fake_time = mock.patch(
"course.views.get_now_or_fake_time")
self.mock_get_now_or_fake_time = fake_get_now_or_fake_time.start()
self.mock_get_now_or_fake_time.return_value = now()
self.addCleanup(fake_get_now_or_fake_time.stop)
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def get_post_data(self, **kwargs):
data = {
"user": self.student_participation.user.pk,
"exam": self.exam.pk,
"valid_start_time": (
self.default_valid_start_time.strftime(
DATE_TIME_PICKER_TIME_FORMAT)),
"valid_end_time": (
self.default_valid_end_time.strftime(
DATE_TIME_PICKER_TIME_FORMAT))}
data.update(kwargs)
return data
class IssueExamTicketTest(ExamTestMixin, TestCase):
"""test exam.issue_exam_ticket
"""
def get_issue_exam_ticket_url(self):
from django.urls import reverse
return reverse("relate-issue_exam_ticket")
def get_issue_exam_ticket_view(self):
return self.c.get(self.get_issue_exam_ticket_url())
def post_issue_exam_ticket_view(self, data):
return self.c.post(self.get_issue_exam_ticket_url(), data)
def test_not_authenticated(self):
with self.temporarily_switch_to_user(None):
resp = self.get_issue_exam_ticket_view()
self.assertEqual(resp.status_code, 403)
resp = self.post_issue_exam_ticket_view(data={})
self.assertEqual(resp.status_code, 403)
self.assertEqual(ExamTicket.objects.count(), 0)
def test_no_pperm(self):
with self.temporarily_switch_to_user(self.student_participation.user):
resp = self.get_issue_exam_ticket_view()
self.assertEqual(resp.status_code, 403)
resp = self.post_issue_exam_ticket_view(data={})
self.assertEqual(resp.status_code, 403)
self.assertEqual(ExamTicket.objects.count(), 0)
def test_get_success(self):
resp = self.get_issue_exam_ticket_view()
self.assertEqual(resp.status_code, 200)
self.assertEqual(ExamTicket.objects.count(), 0)
def test_post_success(self):
self.mock_get_now_or_fake_time.return_value = self.default_faked_now
resp = self.post_issue_exam_ticket_view(data=self.get_post_data())
self.assertFormErrorLoose(resp, None)
self.assertEqual(resp.status_code, 200)
self.assertEqual(ExamTicket.objects.count(), 1)
self.assertAddMessageCallCount(1)
self.assertAddMessageCalledWith("Ticket issued for", reset=False)
self.assertAddMessageCalledWith("The ticket code is")
def test_form_invalid(self):
self.mock_get_now_or_fake_time.return_value = self.default_faked_now
with mock.patch("course.exam.IssueTicketForm.is_valid") as mock_is_valid:
mock_is_valid.return_value = False
resp = self.post_issue_exam_ticket_view(data=self.get_post_data())
self.assertFormErrorLoose(resp, None)
self.assertEqual(resp.status_code, 200)
self.assertEqual(ExamTicket.objects.count(), 0)
def test_participation_not_match(self):
self.mock_get_now_or_fake_time.return_value = self.default_faked_now
another_exam = factories.ExamFactory(
course=factories.CourseFactory(identifier="another-course"))
resp = self.post_issue_exam_ticket_view(
data=self.get_post_data(exam=another_exam.pk))
self.assertFormErrorLoose(resp, None)
self.assertEqual(resp.status_code, 200)
self.assertEqual(ExamTicket.objects.count(), 0)
self.assertAddMessageCallCount(1)
self.assertAddMessageCalledWith("User is not enrolled in course.")
def test_revoke_revoke_prior_ticket(self):
self.mock_get_now_or_fake_time.return_value = self.default_faked_now
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
prior_ticket = factories.ExamTicketFactory(
exam=self.exam,
participation=self.student_participation,
state=constants.exam_ticket_states.valid)
resp = self.post_issue_exam_ticket_view(
data=self.get_post_data(revoke_prior=True))
self.assertFormErrorLoose(resp, None)
self.assertEqual(resp.status_code, 200)
self.assertEqual(ExamTicket.objects.count(), 2)
prior_ticket.refresh_from_db()
self.assertEqual(prior_ticket.state, constants.exam_ticket_states.revoked)
self.assertAddMessageCallCount(1)
self.assertAddMessageCalledWith("Ticket issued for", reset=False)
self.assertAddMessageCalledWith("The ticket code is")
class BatchIssueExamTicketsTest(ExamTestMixin, TestCase):
def get_batch_issue_exam_ticket_url(self, course_identifier=None):
course_identifier = course_identifier or self.get_default_course_identifier()
return self.get_course_view_url(
"relate-batch_issue_exam_tickets",
course_identifier=course_identifier)
def get_batch_issue_exam_ticket_view(self):
return self.c.get(self.get_batch_issue_exam_ticket_url())
def post_batch_issue_exam_ticket_view(self, data):
return self.c.post(self.get_batch_issue_exam_ticket_url(), data)
def get_post_data(self, **kwargs):
data = super().get_post_data()
Loading
Loading full blame...