Skip to content

Commit fc6f68a

Browse files
committed
Applied conversion function in 'ready()'-method
1 parent 947189d commit fc6f68a

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

auth_enhanced/apps.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
from django.conf import settings
77
from django.core.checks import register
88
from django.db.models.signals import post_save
9+
from django.utils import six
910

1011
# app imports
1112
from auth_enhanced.checks import check_settings_values
1213
from auth_enhanced.email import (
1314
callback_admin_information_new_signup,
1415
callback_user_signup_email_verification,
1516
)
17+
from auth_enhanced.exceptions import AuthEnhancedConversionError
1618
from auth_enhanced.settings import (
17-
DAE_CONST_MODE_EMAIL_ACTIVATION, set_app_default_settings,
19+
DAE_CONST_MODE_EMAIL_ACTIVATION, DAE_CONST_VERIFICATION_TOKEN_MAX_AGE, convert_to_seconds, set_app_default_settings,
1820
)
1921

2022

@@ -35,6 +37,21 @@ def ready(self):
3537
# apply the default settings
3638
set_app_default_settings()
3739

40+
# convert time-strings to seconds
41+
if isinstance(settings.DAE_VERIFICATION_TOKEN_MAX_AGE, six.string_types):
42+
try:
43+
setattr(
44+
settings,
45+
'DAE_VERIFICATION_TOKEN_MAX_AGE',
46+
convert_to_seconds(settings.DAE_VERIFICATION_TOKEN_MAX_AGE)
47+
)
48+
except AuthEnhancedConversionError:
49+
setattr(
50+
settings,
51+
'DAE_VERIFICATION_TOKEN_MAX_AGE',
52+
DAE_CONST_VERIFICATION_TOKEN_MAX_AGE
53+
)
54+
3855
# register app-specific system checks
3956
register(check_settings_values)
4057

auth_enhanced/settings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
# the name of the login url, as specified in 'urls.py'
4747
DAE_CONST_RECOMMENDED_LOGIN_URL = 'auth_enhanced:login'
4848

49+
# this is the default value for DAE_VERIFICATION_TOKEN_MAX_AGE. It is directly
50+
# given in seconds, because it is the fallback value used in AppConfig
51+
DAE_CONST_VERIFICATION_TOKEN_MAX_AGE = 3600
52+
4953

5054
# #############################################################################
5155
# FUNCTIONS
@@ -153,4 +157,4 @@ def set_app_default_settings():
153157
# - an integer, specifying the maximum age of the token in seconds
154158
# TODO: add possible values: strings consisting of a number and a qualifier
155159
# like 'd', 'h', ... parsing of this (into seconds) has to be done somewhere!
156-
inject_setting('DAE_VERIFICATION_TOKEN_MAX_AGE', 3600)
160+
inject_setting('DAE_VERIFICATION_TOKEN_MAX_AGE', DAE_CONST_VERIFICATION_TOKEN_MAX_AGE)

tests/test_apps.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# Django imports
1111
from django.apps import apps
12+
from django.conf import settings
1213
from django.db.models import signals
1314
from django.test import override_settings, tag # noqa
1415

@@ -18,12 +19,33 @@
1819
)
1920

2021
# app imports
21-
from .utils.testcases import AuthEnhancedPerTestDeactivatedSignalsTestCase
22+
from .utils.testcases import AuthEnhancedTestCase, AuthEnhancedPerTestDeactivatedSignalsTestCase
2223

2324

2425
@tag('appconfig')
25-
class AuthEnhancedConfigTests(AuthEnhancedPerTestDeactivatedSignalsTestCase):
26-
"""These tests target the AppConfig."""
26+
class AuthEnhancedConfigTests(AuthEnhancedTestCase):
27+
"""These tests target the AppConfig, without any signal handling."""
28+
29+
@override_settings(DAE_VERIFICATION_TOKEN_MAX_AGE='1h')
30+
def test_convert_time_strings(self):
31+
"""Time strings are converted to seconds."""
32+
33+
self.assertEqual(settings.DAE_VERIFICATION_TOKEN_MAX_AGE, '1h')
34+
apps.get_app_config('auth_enhanced').ready()
35+
self.assertEqual(settings.DAE_VERIFICATION_TOKEN_MAX_AGE, 3600)
36+
37+
@override_settings(DAE_VERIFICATION_TOKEN_MAX_AGE='foo')
38+
def test_convert_use_fallback(self):
39+
"""If conversion fails, a fallback will be applied."""
40+
41+
self.assertEqual(settings.DAE_VERIFICATION_TOKEN_MAX_AGE, 'foo')
42+
apps.get_app_config('auth_enhanced').ready()
43+
self.assertEqual(settings.DAE_VERIFICATION_TOKEN_MAX_AGE, 3600)
44+
45+
46+
@tag('appconfig')
47+
class AuthEnhancedConfigSignalTests(AuthEnhancedPerTestDeactivatedSignalsTestCase):
48+
"""These tests target the AppConfig, especially the signal handling."""
2749

2850
@override_settings(
2951
DAE_ADMIN_SIGNUP_NOTIFICATION=False,

0 commit comments

Comments
 (0)