Skip to content

Commit

Permalink
Merge pull request #281 from Ameriks/byte_string_fix
Browse files Browse the repository at this point in the history
Fixed issue with byte string displaying in admin
  • Loading branch information
moggers87 authored Nov 13, 2018
2 parents e3265a5 + 2626876 commit 268c0d6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 14 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from django.test import TestCase
from django.utils import six
from django.utils.six.moves.urllib.parse import parse_qsl, urlparse
from django_otp.util import random_hex

from two_factor.models import PhoneDevice
from two_factor.models import PhoneDevice, random_hex_str
from two_factor.utils import (
backup_phones, default_device, get_otpauth_url, totp_digits,
)
Expand Down Expand Up @@ -102,3 +103,15 @@ def test_get_totp_digits(self):
for no_digits in (6, 8):
with self.settings(TWO_FACTOR_TOTP_DIGITS=no_digits):
self.assertEqual(totp_digits(), no_digits)

def test_random_hex(self):
# test that returned random_hex_str is string
h = random_hex_str()
self.assertIsInstance(h, six.string_types)
# hex string must be 40 characters long. If cannot be longer, because CharField max_length=40
self.assertEqual(len(h), 40)

# Added tests to verify that we can safely remove IF statement from random_hex_str function
hh = random_hex().decode('utf-8')
self.assertIsInstance(hh, six.string_types)
self.assertEqual(len(hh), 40)
6 changes: 5 additions & 1 deletion two_factor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def key_validator(*args, **kwargs):
return hex_validator()(*args, **kwargs)


def random_hex_str():
return random_hex().decode('utf-8')


class PhoneDevice(Device):
"""
Model with phone number and token seed linked to a user.
Expand All @@ -65,7 +69,7 @@ class Meta:
number = PhoneNumberField()
key = models.CharField(max_length=40,
validators=[key_validator],
default=random_hex,
default=random_hex_str,
help_text="Hex-encoded secret key")
method = models.CharField(max_length=4, choices=PHONE_METHODS,
verbose_name=_('method'))
Expand Down

0 comments on commit 268c0d6

Please sign in to comment.