diff --git a/course/calendar.py b/course/calendar.py index c0da25d3152a9b4074f2a13d96faca24df6f955d..aca6dd68697dedb379b0854a30fea712988db465 100644 --- a/course/calendar.py +++ b/course/calendar.py @@ -367,6 +367,7 @@ def view_calendar(pctx): if not pctx.has_permission(pperm.view_calendar): raise PermissionDenied(_("may not view calendar")) + # must import locally for mock to work from course.views import get_now_or_fake_time now = get_now_or_fake_time(pctx.request) diff --git a/course/exam.py b/course/exam.py index 8e97a8a4186ca900a84e071f13994d8f0aeadbc6..b2321bd01e0e08848621147fbb33e7c13af2d525 100644 --- a/course/exam.py +++ b/course/exam.py @@ -55,7 +55,6 @@ from course.constants import ( exam_ticket_states, participation_status, participation_permission as pperm) -from course.views import get_now_or_fake_time from relate.utils import StyledForm, string_concat @@ -140,6 +139,8 @@ class IssueTicketForm(StyledForm): @permission_required("course.can_issue_exam_tickets", raise_exception=True) def issue_exam_ticket(request): + # must import locally for mock to work + from course.views import get_now_or_fake_time now_datetime = get_now_or_fake_time(request) if request.method == "POST": @@ -538,6 +539,8 @@ class ExamCheckInForm(StyledForm): @csrf_protect @never_cache def check_in_for_exam(request): + # must import locally for mock to work + from course.views import get_now_or_fake_time now_datetime = get_now_or_fake_time(request) if request.method == "POST": @@ -790,6 +793,8 @@ class ExamLockdownMiddleware(object): # {{{ list available exams def list_available_exams(request): + # must import locally for mock to work + from course.views import get_now_or_fake_time now_datetime = get_now_or_fake_time(request) if request.user.is_authenticated: diff --git a/tests/test_exam.py b/tests/test_exam.py index 896c0de7a0e8a8b5cb6a01315adbe292df70016a..b57adc2102056b24d62f002aaf4bd7b0cd9ecabb 100644 --- a/tests/test_exam.py +++ b/tests/test_exam.py @@ -77,6 +77,12 @@ class ExamTestMixin(SingleCourseTestMixin, MockAddMessageMixing): super(ExamTestMixin, self).setUp() 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) + def get_post_data(self, **kwargs): data = { "user": self.student_participation.user.pk, @@ -129,6 +135,8 @@ class IssueExamTicketTest(ExamTestMixin, TestCase): 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) @@ -138,6 +146,8 @@ class IssueExamTicketTest(ExamTestMixin, TestCase): 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()) @@ -146,6 +156,8 @@ class IssueExamTicketTest(ExamTestMixin, TestCase): 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( @@ -157,6 +169,8 @@ class IssueExamTicketTest(ExamTestMixin, TestCase): 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 + prior_ticket = factories.ExamTicketFactory( exam=self.exam, participation=self.student_participation,