Skip to content

Commit 7302723

Browse files
committed
system checks refactor + tests
1 parent 1d71987 commit 7302723

File tree

3 files changed

+83
-26
lines changed

3 files changed

+83
-26
lines changed

rest_registration/checks.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,51 @@
1-
from django.core.checks import Error, register
1+
from django.core.checks import register
22

3+
from rest_registration.decorators import simple_check
34
from rest_registration.settings import registration_settings
45

56

6-
def check(message, error_code):
7-
8-
def decorator(predicate):
9-
10-
def f(app_configs, **kwargs):
11-
errors = []
12-
if not predicate():
13-
errors.append(
14-
Error(
15-
message,
16-
hint=None,
17-
id='rest_registration.E{0}'.format(error_code),
18-
)
19-
)
20-
return errors
21-
22-
return f
23-
24-
return decorator
7+
class ErrorCode(object):
8+
NO_RESET_PASSWORD_VER_URL = 'E001'
9+
NO_REGISTER_VER_URL = 'E002'
10+
NO_REGISTER_EMAIL_VER_URL = 'E003'
11+
NO_VER_FROM_EMAIL = 'E004'
2512

2613

2714
@register()
28-
@check('RESET_PASSWORD_VERIFICATION_URL is not set', '001')
15+
@simple_check('RESET_PASSWORD_VERIFICATION_URL is not set',
16+
ErrorCode.NO_RESET_PASSWORD_VER_URL)
2917
def reset_password_verification_url_check():
3018
return registration_settings.RESET_PASSWORD_VERIFICATION_URL
3119

3220

3321
@register()
34-
@check('register verification is enabled,'
35-
' but REGISTER_VERIFICATION_URL is not set', '002')
22+
@simple_check('register verification is enabled,'
23+
' but REGISTER_VERIFICATION_URL is not set',
24+
ErrorCode.NO_REGISTER_VER_URL)
3625
def register_verification_url_check():
3726
return (not registration_settings.REGISTER_VERIFICATION_ENABLED or
3827
registration_settings.REGISTER_VERIFICATION_URL)
3928

4029

4130
@register()
42-
@check('register email verification is enabled,'
43-
' but REGISTER_EMAIL_VERIFICATION_URL is not set', '003')
31+
@simple_check('register email verification is enabled,'
32+
' but REGISTER_EMAIL_VERIFICATION_URL is not set',
33+
ErrorCode.NO_REGISTER_EMAIL_VER_URL)
4434
def register_email_verification_url_check():
4535
return (not registration_settings.REGISTER_EMAIL_VERIFICATION_ENABLED or
4636
registration_settings.REGISTER_EMAIL_VERIFICATION_URL)
4737

4838

4939
@register()
50-
@check('VERIFICATION_FROM_EMAIL is not set', '004')
40+
@simple_check('VERIFICATION_FROM_EMAIL is not set',
41+
ErrorCode.NO_VER_FROM_EMAIL)
5142
def verification_from_check():
5243
return registration_settings.VERIFICATION_FROM_EMAIL
44+
45+
46+
__ALL_CHECKS__ = [
47+
reset_password_verification_url_check,
48+
register_verification_url_check,
49+
register_email_verification_url_check,
50+
verification_from_check,
51+
]

rest_registration/decorators.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import functools
12
import types
23

4+
from django.core.checks import Error
5+
36

47
def serializer_class_getter(class_getter):
58

@@ -16,3 +19,26 @@ def decorator(func):
1619
apiview_cls)
1720
return func
1821
return decorator
22+
23+
24+
def simple_check(error_message, error_code, obj=None):
25+
26+
def decorator(predicate):
27+
28+
@functools.wraps(predicate)
29+
def check_fun(app_configs, **kwargs):
30+
errors = []
31+
if not predicate():
32+
errors.append(
33+
Error(
34+
error_message,
35+
obj=obj,
36+
hint=None,
37+
id='rest_registration.{0}'.format(error_code),
38+
)
39+
)
40+
return errors
41+
42+
return check_fun
43+
44+
return decorator

tests/test_checks.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from django.apps import apps
2+
from django.test import TestCase
3+
from django.test.utils import override_settings
4+
5+
from rest_registration.checks import __ALL_CHECKS__
6+
7+
8+
def simulate_checks():
9+
app_configs = apps.app_configs
10+
errors = []
11+
for check in __ALL_CHECKS__:
12+
errors.extend(check(app_configs))
13+
return errors
14+
15+
16+
class ChecksTestCase(TestCase):
17+
18+
def test_checks_default(self):
19+
errors = simulate_checks()
20+
self.assertEqual(len(errors), 4)
21+
22+
@override_settings(
23+
REST_REGISTRATION={
24+
'REGISTER_VERIFICATION_URL': '/verify-account/',
25+
'REGISTER_EMAIL_VERIFICATION_URL': '/verify-email/',
26+
'RESET_PASSWORD_VERIFICATION_URL': '/reset-password/',
27+
'VERIFICATION_FROM_EMAIL': 'jon.doe@example.com',
28+
}
29+
)
30+
def test_checks_minmal_setup(self):
31+
errors = simulate_checks()
32+
self.assertEqual(len(errors), 0)

0 commit comments

Comments
 (0)