/
tests.py
179 lines (139 loc) · 5.73 KB
/
tests.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from datetime import timedelta
from freezegun import freeze_time
from django.core import mail
from django.db import IntegrityError
from django.test.utils import override_settings
from django_otp.forms import OTPAuthenticationForm
from django_otp.tests import TestCase, ThrottlingTestMixin
from .models import EmailDevice
class EmailDeviceMixin:
def setUp(self):
try:
alice = self.create_user('alice', 'password')
except IntegrityError:
self.skipTest("Failed to create user.")
else:
self.device = alice.emaildevice_set.create()
if hasattr(alice, 'email'):
alice.email = 'alice@example.com'
alice.save()
else:
self.skipTest("User model has no email.")
class AuthFormTest(EmailDeviceMixin, TestCase):
@override_settings(OTP_EMAIL_SENDER='test@example.com')
def test_email_interaction(self):
data = {
'username': 'alice',
'password': 'password',
'otp_device': 'otp_email.emaildevice/1',
'otp_token': '',
'otp_challenge': '1',
}
form = OTPAuthenticationForm(None, data)
self.assertFalse(form.is_valid())
alice = form.get_user()
self.assertEqual(alice.get_username(), 'alice')
self.assertIsNone(alice.otp_device)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['alice@example.com'])
self.device.refresh_from_db()
data['otp_token'] = self.device.token
del data['otp_challenge']
form = OTPAuthenticationForm(None, data)
self.assertTrue(form.is_valid())
self.assertIsInstance(form.get_user().otp_device, EmailDevice)
@override_settings(
DEFAULT_FROM_EMAIL="root@localhost",
OTP_EMAIL_THROTTLE_FACTOR=0,
)
class EmailTest(EmailDeviceMixin, TestCase):
def test_token_generator(self):
self.device.generate_token()
self.device.token.isnumeric()
def test_invalid_token(self):
self.device.generate_token()
self.assertFalse(self.device.verify_token(0))
def test_no_reuse(self):
self.device.generate_token()
token = self.device.token
self.assertTrue(self.device.verify_token(token))
self.assertFalse(self.device.verify_token(token))
def test_token_expiry(self):
self.device.generate_token()
token = self.device.token
with freeze_time() as frozen_time:
frozen_time.tick(delta=timedelta(seconds=301))
self.assertFalse(self.device.verify_token(token))
def test_defaults(self):
self.device.generate_challenge()
self.assertEqual(len(mail.outbox), 1)
msg = mail.outbox[0]
with self.subTest(field='from_email'):
self.assertEqual(msg.from_email, "root@localhost")
with self.subTest(field='body'):
self.assertEqual(msg.body, "Test template 1: {}\n".format(self.device.token))
@override_settings(
OTP_EMAIL_SENDER="webmaster@example.com",
OTP_EMAIL_SUBJECT="Test subject",
OTP_EMAIL_BODY_TEMPLATE="Test template 2: {{token}}",
)
def test_settings(self):
self.device.generate_challenge()
self.assertEqual(len(mail.outbox), 1)
msg = mail.outbox[0]
with self.subTest(field='from_email'):
self.assertEqual(msg.from_email, "webmaster@example.com")
with self.subTest(field='subject'):
self.assertEqual(msg.subject, "Test subject")
with self.subTest(field='body'):
self.assertEqual(msg.body, "Test template 2: {}".format(self.device.token))
@override_settings(
OTP_EMAIL_SENDER="webmaster@example.com",
OTP_EMAIL_SUBJECT="Test subject",
OTP_EMAIL_BODY_TEMPLATE_PATH="otp/email/custom.txt",
)
def test_settings_template_path(self):
self.device.generate_challenge()
self.assertEqual(len(mail.outbox), 1)
msg = mail.outbox[0]
with self.subTest(field='from_email'):
self.assertEqual(msg.from_email, "webmaster@example.com")
with self.subTest(field='subject'):
self.assertEqual(msg.subject, "Test subject")
with self.subTest(field='body'):
self.assertEqual(msg.body, "Test template 3: {}\n".format(self.device.token))
@override_settings(
OTP_EMAIL_SENDER="webmaster@example.com",
OTP_EMAIL_SUBJECT="Test subject",
OTP_EMAIL_BODY_TEMPLATE="Test template 4: {{token}} {{foo}} {{bar}}",
)
def test_settings_extra_template_options(self):
extra_context = {"foo": "extra 1", "bar": "extra 2"}
self.device.generate_challenge(extra_context)
self.assertEqual(len(mail.outbox), 1)
msg = mail.outbox[0]
with self.subTest(field='from_email'):
self.assertEqual(msg.from_email, "webmaster@example.com")
with self.subTest(field='subject'):
self.assertEqual(msg.subject, "Test subject")
with self.subTest(field='body'):
self.assertEqual(
msg.body,
"Test template 4: {} {} {}".format(self.device.token, extra_context["foo"], extra_context["bar"])
)
def test_alternative_email(self):
self.device.email = 'alice2@example.com'
self.device.save()
self.device.generate_challenge()
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, ['alice2@example.com'])
@override_settings(
OTP_EMAIL_THROTTLE_FACTOR=1,
)
class ThrottlingTestCase(EmailDeviceMixin, ThrottlingTestMixin, TestCase):
def valid_token(self):
if self.device.token is None:
self.device.generate_token()
return self.device.token
def invalid_token(self):
return -1