From cc7dbb17d11bff3a8e3d14b800c3de8c674f3d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 14 Feb 2020 10:52:43 +0100 Subject: [PATCH] update invoice CRC checks for Python 3 (#39863) --- auquotidien/modules/payments.py | 4 ++-- tests/test_payment.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/test_payment.py diff --git a/auquotidien/modules/payments.py b/auquotidien/modules/payments.py index a4b7980..0de01a4 100644 --- a/auquotidien/modules/payments.py +++ b/auquotidien/modules/payments.py @@ -103,7 +103,7 @@ class Invoice(StorableObject): 'f%s' % (self.formdef_id or 'x'), ''.join([r.choice(string.digits) for x in range(5)]) ]) - crc = '%0.2d' % (ord(hashlib.md5(id).digest()[0]) % 100) + crc = '%0.2d' % (hashlib.md5(id.encode('utf-8')).digest()[0] % 100) id = id + '-' + crc if not self.has_key(id): return id @@ -116,7 +116,7 @@ class Invoice(StorableObject): def check_crc(cls, id): try: - return int(id[-2:]) == (ord(hashlib.md5(id[:-3]).digest()[0]) % 100) + return int(id[-2:]) == (hashlib.md5(id[:-3].encode('utf-8')).digest()[0] % 100) except: return False check_crc = classmethod(check_crc) diff --git a/tests/test_payment.py b/tests/test_payment.py new file mode 100644 index 0000000..558ef93 --- /dev/null +++ b/tests/test_payment.py @@ -0,0 +1,30 @@ +import shutil + +from quixote import cleanup +from wcs.qommon.http_request import HTTPRequest +from auquotidien.modules import payments + +from utilities import get_app, login, create_temporary_pub + +def setup_module(module): + cleanup() + + global pub + + pub = create_temporary_pub() + + req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'}) + pub.set_app_dir(req) + pub.cfg['identification'] = {'methods': ['password']} + pub.write_cfg() + + +def teardown_module(module): + shutil.rmtree(pub.APP_DIR) + + +def test_invoice_crc(): + invoice = payments.Invoice() + for i in range(20): + new_id = invoice.get_new_id() + assert payments.Invoice.check_crc(new_id)