Newer
Older
from __future__ import division
__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
from celery import states, uuid
from django.test import TestCase, RequestFactory
from django import http
from relate.celery import app
from relate.utils import as_local_time
from course import views, constants, models
CoursesTestMixinBase, SingleCourseTestMixin, HackRepoMixin,
SingleCoursePageTestMixin, MockAddMessageMixing
from tests.constants import DATE_TIME_PICKER_TIME_FORMAT
Dong Zhuang
committed
# intentionally to be different from local_settings_example.py
"test_center1": {
"ip_ranges": [
"192.168.100.0/24",
],
"exams_only": False,
},
}
class SetFakeTimeTest(SingleCourseTestMixin, TestCase):
# test views.set_fake_time
fake_time = datetime.datetime(2038, 12, 31, 0, 0, 0, 0)
set_fake_time_data = {"time": fake_time.strftime(DATE_TIME_PICKER_TIME_FORMAT),
"set": ['']}
unset_fake_time_data = {"time": set_fake_time_data["time"], "unset": ['']}
def test_set_fake_time_by_anonymous(self):
with self.temporarily_switch_to_user(None):
# the faking url is not rendered in template
resp = self.c.get(self.course_page_url)
self.assertNotContains(resp, self.get_fake_time_url())
resp = self.get_set_fake_time()
self.assertEqual(resp.status_code, 302)
resp = self.post_set_fake_time(self.set_fake_time_data, follow=False)
self.assertEqual(resp.status_code, 302)
self.assertSessionFakeTimeIsNone(self.c.session)
def test_set_fake_time_no_pperm(self):
with self.temporarily_switch_to_user(self.student_participation.user):
# the faking url is not rendered in template
resp = self.c.get(self.course_page_url)
self.assertNotContains(resp, self.get_fake_time_url())
resp = self.get_set_fake_time()
self.assertEqual(resp.status_code, 403)
resp = self.post_set_fake_time(self.set_fake_time_data)
self.assertEqual(resp.status_code, 403)
self.assertSessionFakeTimeIsNone(self.c.session)
def test_set_fake_time_by_instructor(self):
with self.temporarily_switch_to_user(self.instructor_participation.user):
# the faking url is rendered in template
resp = self.c.get(self.course_page_url)
self.assertContains(resp, self.get_fake_time_url())
resp = self.get_set_fake_time()
self.assertEqual(resp.status_code, 200)
# set fake time
resp = self.post_set_fake_time(self.set_fake_time_data)
self.assertEqual(resp.status_code, 200)
self.assertSessionFakeTimeEqual(self.c.session, self.fake_time)
# revisit the page, just to make sure it works
resp = self.get_set_fake_time()
self.assertEqual(resp.status_code, 200)
# unset fake time
resp = self.post_set_fake_time(self.unset_fake_time_data)
self.assertEqual(resp.status_code, 200)
self.assertSessionFakeTimeIsNone(self.c.session)
def test_set_fake_time_by_instructor_when_impersonating(self):
with self.temporarily_switch_to_user(self.instructor_participation.user):
resp = self.get_set_fake_time()
self.assertEqual(resp.status_code, 200)
self.post_impersonate_view(impersonatee=self.student_participation.user)
# the faking url is rendered in template
resp = self.c.get(self.course_page_url)
self.assertContains(resp, self.get_fake_time_url())
# set fake time
resp = self.post_set_fake_time(self.set_fake_time_data)
self.assertEqual(resp.status_code, 200)
self.assertSessionFakeTimeEqual(self.c.session, self.fake_time)
# unset fake time
resp = self.post_set_fake_time(self.unset_fake_time_data)
self.assertEqual(resp.status_code, 200)
self.assertSessionFakeTimeIsNone(self.c.session)
def test_form_invalid(self):
with mock.patch("course.views.FakeTimeForm.is_valid") as mock_is_valid:
mock_is_valid.return_value = False
with self.temporarily_switch_to_user(self.instructor_participation.user):
resp = self.post_set_fake_time(self.set_fake_time_data)
self.assertEqual(resp.status_code, 200)
# fake failed
self.assertSessionFakeTimeIsNone(self.c.session)
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
class GetNowOrFakeTimeTest(unittest.TestCase):
# test views.get_now_or_fake_time
mock_now_value = mock.MagicMock()
def setUp(self):
fake_get_fake_time = mock.patch("course.views.get_fake_time")
self.mock_get_fake_time = fake_get_fake_time.start()
self.addCleanup(fake_get_fake_time.stop)
fake_now = mock.patch("django.utils.timezone.now")
self.mock_now = fake_now.start()
self.mock_now.return_value = self.mock_now_value
self.addCleanup(fake_now.stop)
rf = RequestFactory()
self.request = rf.get("/")
def test_fake_time_is_none(self):
self.mock_get_fake_time.return_value = None
self.assertEqual(
views.get_now_or_fake_time(self.request), self.mock_now_value)
def test_fake_time_is_not_none(self):
mock_fake_time = mock.MagicMock()
self.mock_get_fake_time.return_value = mock_fake_time
self.assertEqual(
views.get_now_or_fake_time(self.request), mock_fake_time)
@override_settings(RELATE_FACILITIES=RELATE_FACILITIES)
class TestSetPretendFacilities(SingleCourseTestMixin, TestCase):
set_pretend_facilities_data = {
"facilities": ["test_center1"],
"custom_facilities": [],
"add_pretend_facilities_header": ["on"],
"set": ['']}
unset_pretend_facilities_data = set_pretend_facilities_data.copy()
unset_pretend_facilities_data.pop("set")
unset_pretend_facilities_data["unset"] = ['']
def test_pretend_facilities_by_anonymous(self):
with self.temporarily_switch_to_user(None):
# the pretending url is not rendered in template
resp = self.c.get(self.course_page_url)
self.assertNotContains(resp, self.get_set_pretend_facilities_url())
resp = self.get_set_pretend_facilities()
self.assertEqual(resp.status_code, 302)
resp = self.post_set_pretend_facilities(
self.set_pretend_facilities_data, follow=False)
self.assertEqual(resp.status_code, 302)
self.assertSessionPretendFacilitiesIsNone(self.c.session)
def test_pretend_facilities_no_pperm(self):
Loading
Loading full blame...