From 5115ff8ca6cb1686ad0d06002160a27c1efd6af0 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Mon, 18 Feb 2019 22:04:31 -0600 Subject: [PATCH] Fix date mocking in test_exam.IssueExamTicketTest --- course/calendar.py | 1 + course/exam.py | 7 ++++++- tests/test_exam.py | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/course/calendar.py b/course/calendar.py index c0da25d3..aca6dd68 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 8e97a8a4..b2321bd0 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 896c0de7..b57adc21 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, -- GitLab