From 5a55ec095957fe1ce1aa0c69aca0758f6b0084cf Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Mon, 7 Nov 2022 05:54:35 -0400 Subject: [PATCH] test: fix date-related timezone errors in tests (#4717) * test: compare UTC dates in test_search_mail_archive() * test: compare datetimes, not dates, in action holders test * test: adjust test to match TZ used for IPR notification message * test: fix timezone in date_today() calls for liaisons tests * test: use settings.TIME_ZONE for date in nomination tests * test: correctly use settings.TIME_ZONE instead of system time zone --- ietf/doc/tests_review.py | 2 +- ietf/doc/tests_utils.py | 8 ++++---- ietf/ipr/tests.py | 6 +++++- ietf/liaisons/tests.py | 8 ++++---- ietf/nomcom/tests.py | 16 ++++++++++------ 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/ietf/doc/tests_review.py b/ietf/doc/tests_review.py index a504d8b58..bffd1fd27 100644 --- a/ietf/doc/tests_review.py +++ b/ietf/doc/tests_review.py @@ -526,7 +526,7 @@ class ReviewTests(TestCase): messages = r.json()["messages"] self.assertEqual(len(messages), 2) - today = date_today() + today = date_today(datetime.timezone.utc) self.assertEqual(messages[0]["url"], "https://www.example.com/testmessage") self.assertTrue("John Doe" in messages[0]["content"]) diff --git a/ietf/doc/tests_utils.py b/ietf/doc/tests_utils.py index f5f2fdd6b..6bea96492 100644 --- a/ietf/doc/tests_utils.py +++ b/ietf/doc/tests_utils.py @@ -5,6 +5,7 @@ import debug # pyflakes:ignore from unittest.mock import patch from django.db import IntegrityError +from django.utils import timezone from ietf.group.factories import GroupFactory, RoleFactory from ietf.name.models import DocTagName @@ -16,7 +17,6 @@ from ietf.doc.models import State, DocumentActionHolder, DocumentAuthor, Documen from ietf.doc.utils import (update_action_holders, add_state_change_event, update_documentauthors, fuzzy_find_documents, rebuild_reference_relations) from ietf.utils.draft import Draft, PlaintextDraft -from ietf.utils.timezone import date_today from ietf.utils.xmldraft import XMLDraft @@ -147,10 +147,10 @@ class ActionHoldersTests(TestCase): dah.time_added = datetime.datetime(2020, 1, 1, tzinfo=datetime.timezone.utc) # arbitrary date in the past dah.save() - today = date_today() - self.assertNotEqual(doc.documentactionholder_set.get(person=self.ad).time_added.date(), today) + right_now = timezone.now() + self.assertLess(doc.documentactionholder_set.get(person=self.ad).time_added, right_now) self.update_doc_state(doc, State.objects.get(slug='ad-eval')) - self.assertEqual(doc.documentactionholder_set.get(person=self.ad).time_added.date(), today) + self.assertGreaterEqual(doc.documentactionholder_set.get(person=self.ad).time_added, right_now) def test_update_action_holders_add_tag_need_rev(self): """Adding need-rev tag adds authors as action holders""" diff --git a/ietf/ipr/tests.py b/ietf/ipr/tests.py index ab6b01a5b..8554f74a7 100644 --- a/ietf/ipr/tests.py +++ b/ietf/ipr/tests.py @@ -7,6 +7,7 @@ import datetime from pyquery import PyQuery from urllib.parse import quote, urlparse +from zoneinfo import ZoneInfo from django.conf import settings from django.urls import reverse as urlreverse @@ -595,7 +596,10 @@ I would like to revoke this declaration. r = self.client.post(url, data ) self.assertEqual(r.status_code,302) self.assertEqual(len(outbox),2) - self.assertIn('Secretariat on '+ipr.get_latest_event_submitted().time.strftime("%Y-%m-%d"), get_payload_text(outbox[1]).replace('\n',' ')) + self.assertIn( + 'Secretariat on ' + ipr.get_latest_event_submitted().time.astimezone(ZoneInfo(settings.TIME_ZONE)).strftime("%Y-%m-%d"), + get_payload_text(outbox[1]).replace('\n',' '), + ) self.assertIn(f'{settings.IDTRACKER_BASE_URL}{urlreverse("ietf.ipr.views.showlist")}', get_payload_text(outbox[1]).replace('\n',' ')) def send_ipr_email_helper(self): diff --git a/ietf/liaisons/tests.py b/ietf/liaisons/tests.py index 845271db7..f2fd5c452 100644 --- a/ietf/liaisons/tests.py +++ b/ietf/liaisons/tests.py @@ -700,7 +700,7 @@ class LiaisonManagementTests(TestCase): from_groups = [ str(g.pk) for g in Group.objects.filter(type="sdo") ] to_group = Group.objects.get(acronym="mars") submitter = Person.objects.get(user__username="marschairman") - today = date_today() + today = date_today(datetime.timezone.utc) related_liaison = liaison r = self.client.post(url, dict(from_groups=from_groups, @@ -779,7 +779,7 @@ class LiaisonManagementTests(TestCase): from_group = Group.objects.get(acronym="mars") to_group = Group.objects.filter(type="sdo")[0] submitter = Person.objects.get(user__username="marschairman") - today = date_today() + today = date_today(datetime.timezone.utc) related_liaison = liaison r = self.client.post(url, dict(from_groups=str(from_group.pk), @@ -847,7 +847,7 @@ class LiaisonManagementTests(TestCase): from_group = Group.objects.get(acronym="mars") to_group = Group.objects.filter(type="sdo")[0] submitter = Person.objects.get(user__username="marschairman") - today = date_today() + today = date_today(datetime.timezone.utc) r = self.client.post(url, dict(from_groups=str(from_group.pk), from_contact=submitter.email_address(), @@ -866,7 +866,7 @@ class LiaisonManagementTests(TestCase): self.assertEqual(len(outbox), mailbox_before + 1) def test_liaison_add_attachment(self): - liaison = LiaisonStatementFactory(deadline=date_today()+datetime.timedelta(days=1)) + liaison = LiaisonStatementFactory(deadline=date_today(DEADLINE_TZINFO)+datetime.timedelta(days=1)) LiaisonStatementEventFactory(statement=liaison,type_id='submitted') self.assertEqual(liaison.attachments.count(),0) diff --git a/ietf/nomcom/tests.py b/ietf/nomcom/tests.py index 0577ca558..f3f11d268 100644 --- a/ietf/nomcom/tests.py +++ b/ietf/nomcom/tests.py @@ -10,6 +10,7 @@ import shutil from pyquery import PyQuery from urllib.parse import urlparse from itertools import combinations +from zoneinfo import ZoneInfo from django.db import IntegrityError from django.db.models import Max @@ -1506,11 +1507,12 @@ class NewActiveNomComTests(TestCase): def test_accept_reject_nomination_edges(self): self.client.logout() np = self.nc.nominee_set.order_by('pk').first().nomineeposition_set.order_by('pk').first() + date_str = np.time.astimezone(ZoneInfo(settings.TIME_ZONE)).strftime("%Y%m%d") kwargs={'year':self.nc.year(), 'nominee_position_id':np.id, 'state':'accepted', - 'date':np.time.strftime("%Y%m%d"), - 'hash':get_hash_nominee_position(np.time.strftime("%Y%m%d"),np.id), + 'date':date_str, + 'hash':get_hash_nominee_position(date_str, np.id), } url = reverse('ietf.nomcom.views.process_nomination_status', kwargs=kwargs) response = self.client.get(url) @@ -1520,8 +1522,9 @@ class NewActiveNomComTests(TestCase): settings.DAYS_TO_EXPIRE_NOMINATION_LINK = 2 np.time = np.time - datetime.timedelta(days=3) np.save() - kwargs['date'] = np.time.strftime("%Y%m%d") - kwargs['hash'] = get_hash_nominee_position(np.time.strftime("%Y%m%d"),np.id) + date_str = np.time.astimezone(ZoneInfo(settings.TIME_ZONE)).strftime("%Y%m%d") + kwargs['date'] = date_str + kwargs['hash'] = get_hash_nominee_position(date_str, np.id) url = reverse('ietf.nomcom.views.process_nomination_status', kwargs=kwargs) response = self.client.get(url) self.assertEqual(response.status_code,403) @@ -1535,12 +1538,13 @@ class NewActiveNomComTests(TestCase): def test_accept_reject_nomination_comment(self): np = self.nc.nominee_set.order_by('pk').first().nomineeposition_set.order_by('pk').first() - hash = get_hash_nominee_position(np.time.strftime("%Y%m%d"),np.id) + date_str = np.time.astimezone(ZoneInfo(settings.TIME_ZONE)).strftime("%Y%m%d") + hash = get_hash_nominee_position(date_str, np.id) url = reverse('ietf.nomcom.views.process_nomination_status', kwargs={'year':self.nc.year(), 'nominee_position_id':np.id, 'state':'accepted', - 'date':np.time.strftime("%Y%m%d"), + 'date':date_str, 'hash':hash, } )