|
13 | 13 | from django.core.management import CommandError, call_command |
14 | 14 | from django.test import override_settings, tag # noqa |
15 | 15 |
|
| 16 | +# app imports |
| 17 | +from auth_enhanced.models import UserEnhancement |
| 18 | + |
16 | 19 | # app imports |
17 | 20 | from .utils.testcases import AuthEnhancedTestCase |
18 | 21 |
|
|
22 | 25 | from io import StringIO |
23 | 26 |
|
24 | 27 |
|
| 28 | +@tag('command') |
| 29 | +class CheckAdminNotificationTests(AuthEnhancedTestCase): |
| 30 | + """These tests target the 'check_admin_notification()'-function. |
| 31 | +
|
| 32 | + See auth_enhanced/management/commands/_lib.py |
| 33 | +
|
| 34 | + FIXME: Get rid of the setUp()-method and include the stuff into a fixture.""" |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + |
| 38 | + # prepare test environment to capture stdout |
| 39 | + self.out = StringIO() |
| 40 | + |
| 41 | + user_model = get_user_model() |
| 42 | + |
| 43 | + # two fully valid superusers |
| 44 | + u = user_model.objects.create_superuser(**{ |
| 45 | + user_model.USERNAME_FIELD: 'django', |
| 46 | + user_model.EMAIL_FIELD: 'django@localhost', |
| 47 | + 'password': 'foo' |
| 48 | + }) |
| 49 | + UserEnhancement.objects.create( |
| 50 | + user=u, |
| 51 | + email_verification_status=UserEnhancement.EMAIL_VERIFICATION_COMPLETED |
| 52 | + ) |
| 53 | + v = user_model.objects.create_superuser(**{ |
| 54 | + user_model.USERNAME_FIELD: 'foo', |
| 55 | + user_model.EMAIL_FIELD: 'foo@localhost', |
| 56 | + 'password': 'foo' |
| 57 | + }) |
| 58 | + UserEnhancement.objects.create( |
| 59 | + user=v, |
| 60 | + email_verification_status=UserEnhancement.EMAIL_VERIFICATION_COMPLETED |
| 61 | + ) |
| 62 | + |
| 63 | + # superuser without verified email address |
| 64 | + w = user_model.objects.create_superuser(**{ |
| 65 | + user_model.USERNAME_FIELD: 'bar', |
| 66 | + user_model.EMAIL_FIELD: 'bar@localhost', |
| 67 | + 'password': 'foo' |
| 68 | + }) |
| 69 | + UserEnhancement.objects.create( |
| 70 | + user=w, |
| 71 | + email_verification_status=UserEnhancement.EMAIL_VERIFICATION_FAILED |
| 72 | + ) |
| 73 | + |
| 74 | + # a non-superuser |
| 75 | + x = user_model.objects.create_user(**{ |
| 76 | + user_model.USERNAME_FIELD: 'baz', |
| 77 | + user_model.EMAIL_FIELD: 'baz@localhost', |
| 78 | + 'password': 'foo' |
| 79 | + }) |
| 80 | + UserEnhancement.objects.create( |
| 81 | + user=x, |
| 82 | + email_verification_status=UserEnhancement.EMAIL_VERIFICATION_COMPLETED |
| 83 | + ) |
| 84 | + |
| 85 | + @override_settings( |
| 86 | + DAE_ADMIN_SIGNUP_NOTIFICATION=( |
| 87 | + ('django', 'django@localhost', ('mail', )), |
| 88 | + ('foo', 'foo@localhost', ('mail', )), |
| 89 | + ) |
| 90 | + ) |
| 91 | + def test_all_valid(self): |
| 92 | + """Should print a success message to stdout.""" |
| 93 | + |
| 94 | + call_command('authenhanced', 'admin-notification', stdout=self.out) |
| 95 | + self.assertIn('[ok] Notification settings are valid!', self.out.getvalue()) |
| 96 | + |
| 97 | + @override_settings( |
| 98 | + DAE_ADMIN_SIGNUP_NOTIFICATION=( |
| 99 | + ('django', 'django@localhost', ('mail', )), |
| 100 | + ('foo', 'foo@localhost', ('mail', )), |
| 101 | + ('bar', 'bar@localhost', ('mail', )), |
| 102 | + ) |
| 103 | + ) |
| 104 | + def test_address_unverified(self): |
| 105 | + """One of the accounts has an unverified email address.""" |
| 106 | + |
| 107 | + with self.assertRaisesMessage( |
| 108 | + CommandError, |
| 109 | + "The following accounts do not have a verified email address: bar. " |
| 110 | + "Administrative notifications will only be sent to verfified email " |
| 111 | + "addresses." |
| 112 | + ): |
| 113 | + call_command('authenhanced', 'admin-notification', stdout=self.out) |
| 114 | + |
| 115 | + @override_settings( |
| 116 | + DAE_ADMIN_SIGNUP_NOTIFICATION=( |
| 117 | + ('django', 'django@localhost', ('mail', )), |
| 118 | + ('foo', 'foo@localhost', ('mail', )), |
| 119 | + ('baz', 'baz@localhost', ('mail', )), |
| 120 | + ) |
| 121 | + ) |
| 122 | + def test_insufficient_permissions(self): |
| 123 | + """One of the accounts does not have sufficient permissions.""" |
| 124 | + |
| 125 | + with self.assertRaisesMessage( |
| 126 | + CommandError, |
| 127 | + "The following accounts do not have the sufficient permissions to " |
| 128 | + "actually modify accounts: baz." |
| 129 | + ): |
| 130 | + call_command('authenhanced', 'admin-notification', stdout=self.out) |
| 131 | + |
| 132 | + |
| 133 | +@tag('command') |
25 | 134 | class CheckEmailUniquenessTests(AuthEnhancedTestCase): |
26 | 135 | """These tests target the 'check_email_uniqueness()'-function. |
27 | 136 |
|
|
0 commit comments