1
1
from time import sleep
2
+ from datetime import timedelta
2
3
from django .test import TestCase
3
4
from django .conf import settings
4
5
from django .contrib .auth import get_user_model
@@ -24,7 +25,6 @@ def assertLoginRequiredRedirect(self):
24
25
return resp
25
26
26
27
def _logout_session_time (self ):
27
- settings .AUTO_LOGOUT = {'SESSION_TIME' : 1 }
28
28
self .assertLoginRequiredRedirect ()
29
29
30
30
self .client .force_login (self .user )
@@ -34,35 +34,68 @@ def _logout_session_time(self):
34
34
self .assertLoginRequiredRedirect ()
35
35
36
36
def test_logout_session_time (self ):
37
+ settings .AUTO_LOGOUT = {'SESSION_TIME' : 1 }
37
38
settings .USE_TZ = False
38
39
self ._logout_session_time ()
39
40
40
41
def test_logout_session_time_using_tz_utc (self ):
42
+ settings .AUTO_LOGOUT = {'SESSION_TIME' : 1 }
41
43
settings .USE_TZ = True
42
44
self ._logout_session_time ()
43
45
44
46
def test_logout_session_time_using_tz_non_utc (self ):
47
+ settings .AUTO_LOGOUT = {'SESSION_TIME' : 1 }
45
48
settings .USE_TZ = True
46
49
settings .TIME_ZONE = 'Asia/Yekaterinburg'
47
50
self ._logout_session_time ()
48
51
49
- def test_logout_idle_time_no_idle (self ):
50
- settings .AUTO_LOGOUT = {'IDLE_TIME' : 1 }
52
+ def test_logout_session_time_timedelta (self ):
53
+ settings .AUTO_LOGOUT = {'SESSION_TIME' : timedelta (seconds = 1 )}
54
+ settings .USE_TZ = False
55
+ self ._logout_session_time ()
56
+
57
+ def test_logout_session_time_using_tz_utc_timedelta (self ):
58
+ settings .AUTO_LOGOUT = {'SESSION_TIME' : timedelta (seconds = 1 )}
59
+ settings .USE_TZ = True
60
+ self ._logout_session_time ()
61
+
62
+ def test_logout_session_time_using_tz_non_utc_timedelta (self ):
63
+ settings .AUTO_LOGOUT = {'SESSION_TIME' : timedelta (seconds = 1 )}
64
+ settings .USE_TZ = True
65
+ settings .TIME_ZONE = 'Asia/Yekaterinburg'
66
+ self ._logout_session_time ()
67
+
68
+ def _test_logout_idle_time_no_idle (self ):
51
69
self .client .force_login (self .user )
52
70
self .assertLoginRequiredIsOk ()
53
71
54
72
for _ in range (10 ):
55
73
sleep (0.5 )
56
74
self .assertLoginRequiredIsOk ()
57
75
58
- def test_logout_idle_time (self ):
76
+ def test_logout_idle_time_no_idle (self ):
59
77
settings .AUTO_LOGOUT = {'IDLE_TIME' : 1 }
78
+ self ._test_logout_idle_time_no_idle ()
79
+
80
+ def test_logout_idle_time_no_idle_timedelta (self ):
81
+ settings .AUTO_LOGOUT = {'IDLE_TIME' : timedelta (seconds = 1 )}
82
+ self ._test_logout_idle_time_no_idle ()
83
+
84
+ def _test_logout_idle_time (self ):
60
85
self .client .force_login (self .user )
61
86
self .assertLoginRequiredIsOk ()
62
87
63
88
sleep (1.5 )
64
89
self .assertLoginRequiredRedirect ()
65
90
91
+ def test_logout_idle_time (self ):
92
+ settings .AUTO_LOGOUT = {'IDLE_TIME' : 1 }
93
+ self ._test_logout_idle_time ()
94
+
95
+ def test_logout_idle_time_timedelta (self ):
96
+ settings .AUTO_LOGOUT = {'IDLE_TIME' : timedelta (seconds = 1 )}
97
+ self ._test_logout_idle_time ()
98
+
66
99
def test_combine_idle_and_session_time (self ):
67
100
settings .AUTO_LOGOUT = {
68
101
'IDLE_TIME' : 1 ,
@@ -81,6 +114,30 @@ def test_combine_idle_and_session_time(self):
81
114
sleep (0.5 )
82
115
self .assertLoginRequiredRedirect ()
83
116
117
+ def test_session_time_config_time (self ):
118
+ settings .AUTO_LOGOUT = {
119
+ 'IDLE_TIME' : 1 ,
120
+ 'SESSION_TIME' : '2' ,
121
+ }
122
+
123
+ self .client .force_login (self .user )
124
+
125
+ exc_message = "AUTO_LOGOUT['SESSION_TIME'] should be `int` or `timedelta`, not `str`."
126
+ with self .assertRaisesMessage (TypeError , exc_message ):
127
+ self .client .get ('/login-required/' )
128
+
129
+ def test_idle_time_config_time (self ):
130
+ settings .AUTO_LOGOUT = {
131
+ 'IDLE_TIME' : '1' ,
132
+ 'SESSION_TIME' : 2 ,
133
+ }
134
+
135
+ self .client .force_login (self .user )
136
+
137
+ exc_message = "AUTO_LOGOUT['IDLE_TIME'] should be `int` or `timedelta`, not `str`."
138
+ with self .assertRaisesMessage (TypeError , exc_message ):
139
+ self .client .get ('/login-required/' )
140
+
84
141
def test_combine_idle_and_session_time_but_session_less_than_idle (self ):
85
142
settings .AUTO_LOGOUT = {
86
143
'IDLE_TIME' : 2 ,
0 commit comments