From de872282b264b51db2ca10fb9e61f5815bd66b4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Thu, 25 Feb 2021 09:54:46 +0100 Subject: [PATCH] fix: minor edge case in `long_to_base64` utility function. This was detected by mypy. --- CHANGELOG.md | 2 ++ tests/test_utils.py | 4 ++++ tutor/utils.py | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd366f1..9a9f582 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Note: Breaking changes between versions are indicated by "💥". ## Unreleased +- [Bugfix] Fix minor edge case in `long_to_base64` utility function. + ## v11.2.3 (2021-02-20) - [Bugfix] Make LMS celery workers actually process LMS tasks, and not CMS tasks. diff --git a/tests/test_utils.py b/tests/test_utils.py index c056b3d..1770ae5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,3 +1,4 @@ +import base64 import unittest from tutor import utils @@ -30,3 +31,6 @@ class UtilsTests(unittest.TestCase): self.assertNotEqual(encrypted1, encrypted2) self.assertTrue(utils.verify_encrypted(encrypted1, password)) self.assertTrue(utils.verify_encrypted(encrypted2, password)) + + def test_long_to_base64(self): + self.assertEqual(b"\x00", base64.urlsafe_b64decode(utils.long_to_base64(0) + "==")) diff --git a/tutor/utils.py b/tutor/utils.py index f7b859f..9705f01 100644 --- a/tutor/utils.py +++ b/tutor/utils.py @@ -112,7 +112,7 @@ def long_to_base64(n): bys = long2intarr(n) data = struct.pack("%sB" % len(bys), *bys) if not data: - data = "\x00" + data = b"\x00" s = base64.urlsafe_b64encode(data).rstrip(b"=") return s.decode("ascii")