diff --git a/oioioi/clock/tests.py b/oioioi/clock/tests.py index 7934f7957..b400dcd30 100644 --- a/oioioi/clock/tests.py +++ b/oioioi/clock/tests.py @@ -1,8 +1,6 @@ -import calendar import time from datetime import datetime, timezone # pylint: disable=E0611 -from dateutil.parser import parse as parse_date from django.contrib.auth.models import User from django.test.utils import override_settings from django.urls import reverse @@ -65,12 +63,24 @@ def test_countdown_with_extended_rounds(self): @override_settings(CONTEST_MODE=ContestMode.neutral) def test_admin_time(self): self.assertTrue(self.client.login(username='test_admin')) - session = self.client.session - session['admin_time'] = datetime(2012, 12, 12, tzinfo=timezone.utc).isoformat() - session.save() + # As seconds since the epoch + changed_time = datetime(2012, 12, 12, tzinfo=timezone.utc).timestamp() + response = self.client.get(reverse('get_status')).json() + current_time = response['time'] + post_data = {'ok-button': '', 'admin-time': '2012-12-12+0:0:0'} + post_url = reverse('admin_time') + + response = self.client.post(post_url, post_data, follow=True) + self.assertEqual(response.status_code, 200) response = self.client.get(reverse('get_status')).json() self.assertTrue(response['is_admin_time_set']) - self.assertEqual( - response['time'], - calendar.timegm(parse_date(session['admin_time']).timetuple()), - ) + self.assertEqual(response['time'], changed_time) + + post_data = {'reset-button': '', 'admin-time': '2012-12-12+0:0:0'} + + response = self.client.post(post_url, post_data, follow=True) + self.assertEqual(response.status_code, 200) + response = self.client.get(reverse('get_status')).json() + self.assertFalse(response['is_admin_time_set']) + # This test shouldn't take more than a minute. + self.assertLess(abs(response['time'] - current_time), 60) diff --git a/oioioi/clock/views.py b/oioioi/clock/views.py index e2da2880f..30d7a886d 100644 --- a/oioioi/clock/views.py +++ b/oioioi/clock/views.py @@ -103,9 +103,9 @@ def admin_time(request, next_page=None): messages.error(request, _("Invalid date. Admin-time was not set.")) return safe_redirect(request, next_page) if current_admin_time.year >= 1900: + local_tz = timezone.localtime().tzinfo request.session['admin_time'] = ( - timezone.localtime(timezone.now()) - .tzinfo.localize(current_admin_time) + current_admin_time.replace(tzinfo=local_tz) .astimezone(pytz.utc) .isoformat() )