From 85d563f0cd2f747270b8b8b6d0e65092f7479b65 Mon Sep 17 00:00:00 2001 From: SatCFDI Date: Wed, 12 Jun 2024 14:18:09 -0600 Subject: [PATCH] Deprecated use of utcnow --- satcfdi/certifica/pkcs7.py | 4 ++-- satcfdi/pacs/sat.py | 8 ++++---- tests/test_certifica.py | 2 +- tests/test_pac_sat.py | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/satcfdi/certifica/pkcs7.py b/satcfdi/certifica/pkcs7.py index 9aa0dbe..491815d 100644 --- a/satcfdi/certifica/pkcs7.py +++ b/satcfdi/certifica/pkcs7.py @@ -1,5 +1,5 @@ import os -from datetime import datetime +from datetime import datetime, UTC from ..ans1e import Ans1Encoder, Numbers, Classes, to_utc_time from ..models import Signer @@ -19,7 +19,7 @@ def create_pkcs7(data, signer: Signer, hash_algorithm): hash_object.update(data) digest = hash_object.finalize() - utctime = to_utc_time(datetime.utcnow()) + utctime = to_utc_time(datetime.now(UTC).replace(tzinfo=None)) e = Ans1Encoder() with e.seq(): diff --git a/satcfdi/pacs/sat.py b/satcfdi/pacs/sat.py index a78d5a5..2aec404 100644 --- a/satcfdi/pacs/sat.py +++ b/satcfdi/pacs/sat.py @@ -6,7 +6,7 @@ import time from abc import abstractmethod from collections.abc import Sequence -from datetime import date, datetime, timedelta +from datetime import date, datetime, timedelta, UTC from enum import IntEnum, Enum from functools import cache from itertools import islice @@ -213,7 +213,7 @@ class _CFDIAutenticacion(_SATRequest): DATE_TIME_FORMAT: str = '%Y-%m-%dT%H:%M:%S.%fZ' def _prepare_payload(self, root): - date_created = datetime.utcnow() + date_created = datetime.now(UTC).replace(tzinfo=None) date_expires = date_created + timedelta(seconds=self.arguments.get("seconds", 300)) security = root.find('{*}Header/{*}Security') @@ -364,12 +364,12 @@ def _request(self, soap_url, data, soap_action, needs_token_fn, verify=True): ) def _get_token_comprobante(self): - if self.token_comprobante is None or self.token_comprobante["Expires"] <= datetime.utcnow() + timedelta(seconds=30): + if self.token_comprobante is None or self.token_comprobante["Expires"] <= datetime.now(UTC).replace(tzinfo=None) + timedelta(seconds=30): self.token_comprobante = self._autentica_comprobante() return self.token_comprobante["AutenticaResult"] def _get_token_retencion(self): - if self.token_retencion is None or self.token_retencion["Expires"] <= datetime.utcnow() + timedelta(seconds=30): + if self.token_retencion is None or self.token_retencion["Expires"] <= datetime.now(UTC).replace(tzinfo=None) + timedelta(seconds=30): self.token_retencion = self._autentica_retencion() return self.token_retencion["AutenticaResult"] diff --git a/tests/test_certifica.py b/tests/test_certifica.py index 0956e15..df4827a 100644 --- a/tests/test_certifica.py +++ b/tests/test_certifica.py @@ -196,7 +196,7 @@ def test_pkcs7(): signer = get_signer('cacx7605101p8') with mock.patch(f'{module}.certifica.pkcs7.datetime') as d: - d.utcnow = mock.Mock(return_value=datetime(2023, 6, 28, 19, 28, 1, tzinfo=timezone.utc)) + d.now = mock.Mock(return_value=datetime(2023, 6, 28, 19, 28, 1, tzinfo=timezone.utc)) assert data == create_pkcs7(zip_data, signer, hash_algorithm=hashes.SHA1()) diff --git a/tests/test_pac_sat.py b/tests/test_pac_sat.py index 7d05c05..f16eaa8 100644 --- a/tests/test_pac_sat.py +++ b/tests/test_pac_sat.py @@ -1,6 +1,6 @@ import os.path import types -from datetime import datetime, timedelta +from datetime import datetime, timedelta, UTC from decimal import Decimal from pprint import PrettyPrinter from unittest import mock @@ -118,7 +118,7 @@ def test_sat_service_authentication(): signer = get_signer('xiqb891116qe4') with mock.patch(f'{module}.pacs.sat.datetime') as m: - m.utcnow = mock.Mock(return_value=datetime(2022, 1, 1)) + m.now = mock.Mock(return_value=datetime(2022, 1, 1)) req = _CFDIAutenticacion(signer=signer) res = req.get_payload() @@ -131,7 +131,7 @@ def test_sat_service_solicitud(): signer = get_signer('xiqb891116qe4') with mock.patch(f'{module}.pacs.sat.datetime') as m: - m.utcnow = mock.Mock(return_value=datetime(2022, 1, 1)) + m.now = mock.Mock(return_value=datetime(2022, 1, 1)) req = _CFDISolicitaDescarga( signer=signer, @@ -170,7 +170,7 @@ def test_pac_sat_uuid(): sat_service = SAT(environment=Environment.TEST, signer=signer) sat_service.token_comprobante = { - "Expires": datetime.utcnow() + timedelta(seconds=3600), + "Expires": datetime.now(UTC).replace(tzinfo=None) + timedelta(seconds=3600), "AutenticaResult": "token_comprobante" } @@ -198,7 +198,7 @@ def test_pac_sat_rfc(): sat_service = SAT(environment=Environment.TEST, signer=signer) sat_service.token_comprobante = { - "Expires": datetime.utcnow() + timedelta(seconds=3600), + "Expires": datetime.now(UTC).replace(tzinfo=None) + timedelta(seconds=3600), "AutenticaResult": "token_comprobante" }