-
-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathtest_utils.py
More file actions
122 lines (105 loc) · 4.84 KB
/
test_utils.py
File metadata and controls
122 lines (105 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from unittest import mock
from urllib.parse import parse_qsl, urlparse
from django.contrib.auth.hashers import make_password
from django.test import TestCase, override_settings
from django_otp.util import random_hex
from two_factor.plugins.email.utils import mask_email
from two_factor.utils import (
USER_DEFAULT_DEVICE_ATTR_NAME, default_device, get_otpauth_url,
totp_digits,
)
from two_factor.views.utils import (
get_remember_device_cookie, validate_remember_device_cookie,
)
from .utils import UserMixin
class UtilsTest(UserMixin, TestCase):
def test_default_device(self):
user = self.create_user()
self.assertEqual(default_device(user), None)
def test_get_otpauth_url(self):
for num_digits in (6, 8):
self.assertEqualUrl(
'otpauth://totp/bouke%40example.com?secret=abcdef123&digits=' + str(num_digits),
get_otpauth_url(accountname='[email protected]', secret='abcdef123',
digits=num_digits))
self.assertEqualUrl(
'otpauth://totp/Bouke%20Haarsma?secret=abcdef123&digits=' + str(num_digits),
get_otpauth_url(accountname='Bouke Haarsma', secret='abcdef123',
digits=num_digits))
self.assertEqualUrl(
'otpauth://totp/example.com%3A%20bouke%40example.com?'
'secret=abcdef123&digits=' + str(num_digits) + '&issuer=example.com',
get_otpauth_url(accountname='[email protected]', issuer='example.com',
secret='abcdef123', digits=num_digits))
self.assertEqualUrl(
'otpauth://totp/My%20Site%3A%20bouke%40example.com?'
'secret=abcdef123&digits=' + str(num_digits) + '&issuer=My+Site',
get_otpauth_url(accountname='[email protected]', issuer='My Site',
secret='abcdef123', digits=num_digits))
self.assertEqualUrl(
'otpauth://totp/%E6%B5%8B%E8%AF%95%E7%BD%91%E7%AB%99%3A%20'
'%E6%88%91%E4%B8%8D%E6%98%AF%E9%80%97%E6%AF%94?'
'secret=abcdef123&digits=' + str(num_digits) + '&issuer=测试网站',
get_otpauth_url(accountname='我不是逗比',
issuer='测试网站',
secret='abcdef123', digits=num_digits))
def assertEqualUrl(self, lhs, rhs):
"""
Asserts whether the URLs are canonically equal.
"""
lhs = urlparse(lhs)
rhs = urlparse(rhs)
self.assertEqual(lhs.scheme, rhs.scheme)
self.assertEqual(lhs.netloc, rhs.netloc)
self.assertEqual(lhs.path, rhs.path)
self.assertEqual(lhs.fragment, rhs.fragment)
# We used parse_qs before, but as query parameter order became
# significant with Microsoft Authenticator and possibly other
# authenticator apps, we've switched to parse_qsl.
self.assertEqual(parse_qsl(lhs.query), parse_qsl(rhs.query))
def test_get_totp_digits(self):
# test that the default is 6 if TWO_FACTOR_TOTP_DIGITS is not set
self.assertEqual(totp_digits(), 6)
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 is string
h = random_hex()
self.assertIsInstance(h, str)
# hex string must be 40 characters long. If cannot be longer, because CharField max_length=40
self.assertEqual(len(h), 40)
@override_settings(
TWO_FACTOR_REMEMBER_COOKIE_AGE=60 * 60,
)
def test_create_and_validate_remember_cookie(self):
user = mock.Mock()
user.pk = 123
user.password = make_password("xx")
cookie_value = get_remember_device_cookie(
user=user, otp_device_id="SomeModel/33"
)
self.assertEqual(len(cookie_value.split(':')), 3)
validation_result = validate_remember_device_cookie(
cookie=cookie_value,
user=user,
otp_device_id="SomeModel/33",
)
self.assertTrue(validation_result)
def test_wrong_device_hash(self):
user = mock.Mock()
user.pk = 123
user.password = make_password("xx")
cookie_value = get_remember_device_cookie(
user=user, otp_device_id="SomeModel/33"
)
validation_result = validate_remember_device_cookie(
cookie=cookie_value,
user=user,
otp_device_id="SomeModel/34",
)
self.assertFalse(validation_result)
class EmailUtilsTests(TestCase):
def test_mask_email(self):
self.assertEqual(mask_email('[email protected]'), 'b***[email protected]')
self.assertEqual(mask_email('[email protected]'), 't**@example.com')