0001-allow-setting-session-durations-8887.patch
tests/test_sessions.py | ||
---|---|---|
1 |
import os |
|
2 |
import shutil |
|
3 |
import time |
|
4 |
import pytest |
|
5 | ||
6 |
from quixote import cleanup |
|
7 | ||
8 |
from wcs.qommon.ident.password_accounts import PasswordAccount |
|
9 | ||
10 |
from utilities import create_temporary_pub, get_app, login |
|
11 | ||
12 |
def setup_module(): |
|
13 |
# cargo cult: don't know what it does, but seen it everywhere |
|
14 |
cleanup() |
|
15 | ||
16 | ||
17 |
def teardown_module(): |
|
18 |
pass |
|
19 | ||
20 | ||
21 |
@pytest.fixture(scope='function') |
|
22 |
def pub(request): |
|
23 |
pub = create_temporary_pub() |
|
24 |
def fin(): |
|
25 |
shutil.rmtree(pub.APP_DIR) |
|
26 |
request.addfinalizer(fin) |
|
27 |
pub.cfg['identification'] = {'methods': ['password']} |
|
28 |
pub.cfg['misc'] = {'charset': 'utf-8'} |
|
29 |
pub.cfg['language'] = {'language': 'en'} |
|
30 |
pub.write_cfg() |
|
31 |
return pub |
|
32 | ||
33 | ||
34 |
@pytest.fixture |
|
35 |
def user(pub): |
|
36 |
user = pub.user_class() |
|
37 |
user.email = 'foo@localhost' |
|
38 |
user.store() |
|
39 |
account = PasswordAccount(id='foo') |
|
40 |
account.set_password('foo') |
|
41 |
account.user_id = user.id |
|
42 |
account.store() |
|
43 |
return user |
|
44 | ||
45 | ||
46 |
@pytest.fixture |
|
47 |
def app(pub): |
|
48 |
return get_app(pub) |
|
49 | ||
50 | ||
51 |
def test_session_max_age(pub, user, app): |
|
52 |
with file(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as cfg: |
|
53 |
cfg.write('''[options] |
|
54 |
session_max_age: 1 |
|
55 |
''') |
|
56 |
pub.load_site_options() |
|
57 | ||
58 |
login(app, username='foo', password='foo') |
|
59 |
assert 'Logout' in app.get('/') |
|
60 |
time.sleep(0.5) |
|
61 |
assert 'Logout' in app.get('/') |
|
62 |
time.sleep(0.6) |
|
63 |
assert 'Logout' not in app.get('/') |
wcs/qommon/sessions.py | ||
---|---|---|
84 | 84 | |
85 | 85 |
username = None # only set on password authentication |
86 | 86 | |
87 |
def is_expired(self): |
|
88 |
duration = get_publisher().get_site_option('session_max_age') |
|
89 |
if duration is None: |
|
90 |
return False |
|
91 |
try: |
|
92 |
duration = int(duration) |
|
93 |
except ValueError: |
|
94 |
return False |
|
95 |
return (time.time() - self.get_access_time()) > duration |
|
96 | ||
87 | 97 |
def has_info(self): |
88 | 98 |
return self.name_identifier or self.after_url or \ |
89 | 99 |
self.lasso_session_dump or self.message or \ |
... | ... | |
248 | 258 | |
249 | 259 |
def __getitem__(self, session_id): |
250 | 260 |
try: |
251 |
return BasicSession.get(session_id) |
|
261 |
session = BasicSession.get(session_id) |
|
262 |
if session.is_expired(): |
|
263 |
try: |
|
264 |
session.remove_self() |
|
265 |
except OSError: |
|
266 |
pass |
|
267 |
raise KeyError |
|
268 |
return session |
|
252 | 269 |
except KeyError: |
253 | 270 |
raise KeyError |
254 | 271 | |
255 | 272 |
def get(self, session_id, default = None): |
256 | 273 |
try: |
257 |
return BasicSession.get(session_id)
|
|
274 |
return self[session_id]
|
|
258 | 275 |
except KeyError: |
259 | 276 |
return default |
260 | 277 |
except ValueError: # happens for "insecure string pickle" |
261 |
- |