Skip to content

Commit 268c0d6

Browse files
authored
Merge pull request #281 from Ameriks/byte_string_fix
Fixed issue with byte string displaying in admin
2 parents e3265a5 + 2626876 commit 268c0d6

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

tests/test_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
from django.test import TestCase
88
from django.utils import six
99
from django.utils.six.moves.urllib.parse import parse_qsl, urlparse
10+
from django_otp.util import random_hex
1011

11-
from two_factor.models import PhoneDevice
12+
from two_factor.models import PhoneDevice, random_hex_str
1213
from two_factor.utils import (
1314
backup_phones, default_device, get_otpauth_url, totp_digits,
1415
)
@@ -102,3 +103,15 @@ def test_get_totp_digits(self):
102103
for no_digits in (6, 8):
103104
with self.settings(TWO_FACTOR_TOTP_DIGITS=no_digits):
104105
self.assertEqual(totp_digits(), no_digits)
106+
107+
def test_random_hex(self):
108+
# test that returned random_hex_str is string
109+
h = random_hex_str()
110+
self.assertIsInstance(h, six.string_types)
111+
# hex string must be 40 characters long. If cannot be longer, because CharField max_length=40
112+
self.assertEqual(len(h), 40)
113+
114+
# Added tests to verify that we can safely remove IF statement from random_hex_str function
115+
hh = random_hex().decode('utf-8')
116+
self.assertIsInstance(hh, six.string_types)
117+
self.assertEqual(len(hh), 40)

two_factor/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def key_validator(*args, **kwargs):
5555
return hex_validator()(*args, **kwargs)
5656

5757

58+
def random_hex_str():
59+
return random_hex().decode('utf-8')
60+
61+
5862
class PhoneDevice(Device):
5963
"""
6064
Model with phone number and token seed linked to a user.
@@ -65,7 +69,7 @@ class Meta:
6569
number = PhoneNumberField()
6670
key = models.CharField(max_length=40,
6771
validators=[key_validator],
68-
default=random_hex,
72+
default=random_hex_str,
6973
help_text="Hex-encoded secret key")
7074
method = models.CharField(max_length=4, choices=PHONE_METHODS,
7175
verbose_name=_('method'))

0 commit comments

Comments
 (0)