Skip to content

Commit d96dc22

Browse files
committed
Added tests for 'EmailVerificationForm'
1 parent c405363 commit d96dc22

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

auth_enhanced/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def activate_user(self):
7878
except UserEnhancement.DoesNotExist:
7979
# logical database integrity FAILED
8080
# TODO: Should this be raised? Or handle it gracefully by creating an enhancement-object?
81-
raise
81+
enhancement = UserEnhancement.objects.create(user=user_to_be_activated)
8282

8383
# update the verification status
8484
enhancement.email_verification_status = enhancement.EMAIL_VERIFICATION_COMPLETED

tests/test_forms.py

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"""Includes tests targeting the app-specific forms.
33
44
- target file: auth_enhanced/forms.py
5-
- included tags: 'forms', 'settings', 'setting_operation_mode', 'signup'
5+
- included tags: 'forms', 'settings', 'setting_operation_mode', 'signup',
6+
'verification'
67
78
The app's checks rely on Django's system check framework."""
89

@@ -11,15 +12,127 @@
1112

1213
# Django imports
1314
from django.contrib.auth import get_user_model
15+
from django.core.signing import SignatureExpired
1416
from django.forms import ValidationError
1517
from django.test import override_settings, tag # noqa
1618

1719
# app imports
18-
from auth_enhanced.forms import SignupForm
20+
from auth_enhanced.crypto import EnhancedCrypto
21+
from auth_enhanced.forms import EmailVerificationForm, SignupForm
22+
from auth_enhanced.models import UserEnhancement
1923

2024
# app imports
2125
from .utils.testcases import AuthEnhancedTestCase
2226

27+
try:
28+
from unittest import mock
29+
except ImportError:
30+
import mock # FIXME: How the fuck is this library called in P2.7?!?
31+
32+
33+
@tag('forms', 'verification')
34+
class EmailVerificationFormTests(AuthEnhancedTestCase):
35+
"""These tests target the EmailVerificationForm."""
36+
37+
class MockedEnhancedCrypto(object):
38+
39+
def verify_token_valid(self, token=None):
40+
return 'foo'
41+
42+
def verify_token_expired(self, token=None):
43+
raise SignatureExpired('bar')
44+
45+
def verify_token_error(self, token=None):
46+
raise EnhancedCrypto.EnhancedCryptoException('bar')
47+
48+
@mock.patch('auth_enhanced.crypto.EnhancedCrypto.verify_token', MockedEnhancedCrypto.verify_token_valid)
49+
def test_clean_token_valid(self):
50+
"""A valid token is simply returned and the 'username'-attribute populated.
51+
52+
See 'clean_token()'-method."""
53+
54+
form = EmailVerificationForm(
55+
data={
56+
'token': 'foo',
57+
}
58+
)
59+
60+
form.is_valid()
61+
cleaned_token = form.clean_token()
62+
self.assertEqual(cleaned_token, 'foo')
63+
self.assertEqual(form.username, 'foo')
64+
65+
@override_settings(DAE_VERIFICATION_TOKEN_MAX_AGE=5)
66+
@mock.patch('auth_enhanced.crypto.EnhancedCrypto.verify_token', MockedEnhancedCrypto.verify_token_expired)
67+
def test_clean_token_expired(self):
68+
"""An expired token will state a clear 'ValidationError'.
69+
70+
See 'clean_token()'-method."""
71+
72+
form = EmailVerificationForm(
73+
data={
74+
'token': 'foo',
75+
}
76+
)
77+
78+
self.assertFalse(form.is_valid())
79+
self.assertRaisesMessage(
80+
ValidationError,
81+
"It seems like you have submitted a valid verification "
82+
"token, that is expired. Be aware, that verification "
83+
"tokens are considered valid for 5 seconds and must be "
84+
"used within that time period."
85+
)
86+
self.assertEqual(form.username, None)
87+
88+
@mock.patch('auth_enhanced.crypto.EnhancedCrypto.verify_token', MockedEnhancedCrypto.verify_token_error)
89+
def test_clean_token_error(self):
90+
"""A failing token will raise a 'ValidationError' without real information.
91+
92+
See 'clean_token()'-method."""
93+
94+
form = EmailVerificationForm(
95+
data={
96+
'token': 'foo',
97+
}
98+
)
99+
100+
self.assertFalse(form.is_valid())
101+
self.assertRaisesMessage(ValidationError, "Your submitted token could not be verified!")
102+
self.assertEqual(form.username, None)
103+
104+
def test_activate_user_valid(self):
105+
"""A valid user will get activated and its 'email_verification_status' updated.
106+
107+
See 'activate_user()'-method.
108+
109+
This implicitly tests a non-existent UserEnhancement."""
110+
111+
u = get_user_model().objects.create(username='foo', is_active=False)
112+
113+
form = EmailVerificationForm()
114+
form.username = u.username
115+
116+
self.assertFalse(u.is_active)
117+
118+
form.activate_user()
119+
120+
self.assertTrue(get_user_model().objects.get(username='foo').is_active)
121+
self.assertEqual(u.enhancement.email_verification_status, UserEnhancement.EMAIL_VERIFICATION_COMPLETED)
122+
123+
def test_activate_user_invalid_user(self):
124+
"""A non-existent user can not be activated and raises an exception.
125+
126+
See 'activate_user()'-method."""
127+
128+
u = get_user_model().objects.create(username='foo', is_active=False)
129+
130+
form = EmailVerificationForm()
131+
form.username = 'bar' # this username does not exist
132+
133+
with self.assertRaises(get_user_model().DoesNotExist):
134+
form.activate_user()
135+
23136

24137
@tag('forms', 'signup')
25138
class SignupFormTests(AuthEnhancedTestCase):

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ setenv =
1919
PYTHONDONTWRITEBYTECODE=1
2020
deps =
2121
coverage
22-
py27: mock # currently unused
22+
py27: mock
2323
django111: Django>=1.11, <2.0
2424
django20: Django>=2.0b1, <2.1
2525
commands =

0 commit comments

Comments
 (0)