|
2 | 2 | """Includes tests targeting the app-specific forms. |
3 | 3 |
|
4 | 4 | - 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' |
6 | 7 |
|
7 | 8 | The app's checks rely on Django's system check framework.""" |
8 | 9 |
|
|
11 | 12 |
|
12 | 13 | # Django imports |
13 | 14 | from django.contrib.auth import get_user_model |
| 15 | +from django.core.signing import SignatureExpired |
14 | 16 | from django.forms import ValidationError |
15 | 17 | from django.test import override_settings, tag # noqa |
16 | 18 |
|
17 | 19 | # 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 |
19 | 23 |
|
20 | 24 | # app imports |
21 | 25 | from .utils.testcases import AuthEnhancedTestCase |
22 | 26 |
|
| 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 | + |
23 | 136 |
|
24 | 137 | @tag('forms', 'signup') |
25 | 138 | class SignupFormTests(AuthEnhancedTestCase): |
|
0 commit comments