Skip to content

Commit

Permalink
Deprecated use of utcnow
Browse files Browse the repository at this point in the history
  • Loading branch information
satcfdi committed Jun 12, 2024
1 parent 97ed8aa commit 85d563f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions satcfdi/certifica/pkcs7.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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():
Expand Down
8 changes: 4 additions & 4 deletions satcfdi/pacs/sat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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"]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_certifica.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
10 changes: 5 additions & 5 deletions tests/test_pac_sat.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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"
}

Expand Down Expand Up @@ -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"
}

Expand Down

0 comments on commit 85d563f

Please sign in to comment.