Projet

Général

Profil

0001-sessions-expose-hashed-id-as-session_hash_id-39784.patch

Frédéric Péters, 12 février 2020 13:19

Télécharger (2,51 ko)

Voir les différences:

Subject: [PATCH] sessions: expose hashed id as {{session_hash_id}} (#39784)

 tests/test_sessions.py | 6 ++++--
 wcs/qommon/sessions.py | 3 +++
 2 files changed, 7 insertions(+), 2 deletions(-)
tests/test_sessions.py
200 200
    formdef.fields = [fields.PageField(id='0', label='1st PAGE', type='page',
201 201
            condition={'type': 'python', 'value': 'vars().get("session_id") is not None'}),
202 202
            fields.CommentField(id='7', label='COM1 [session_id]', type='comment'),
203
            fields.CommentField(id='10', label='COMHASH1 [session_hash_id]', type='comment'),
203 204
            fields.PageField(id='8', label='2nd PAGE', type='page'),
204 205
            fields.CommentField(id='9', label='COM2 [session_id]', type='comment')]
205 206
    formdef.store()
206 207

  
207 208
    resp = app.get('/foobar/')
208 209
    assert pub.session_manager.session_class.count() == 1
209
    session_id = pub.session_manager.session_class.select()[0].id
210
    assert 'COM1' in resp.text
210
    session = pub.session_manager.session_class.select()[0]
211
    assert 'COM1 %s' % session.id in resp.text
212
    assert 'COMHASH1 %s' % session.get_substitution_variables().get('session_hash_id') in resp.text
211 213

  
212 214

  
213 215
def test_session_clean_job(pub, user, app, freezer):
wcs/qommon/sessions.py
16 16

  
17 17
import copy
18 18
import json
19
import hashlib
19 20
import os
20 21
import time
21 22

  
......
26 27
from quixote.util import randbytes
27 28

  
28 29
from django.conf import settings
30
from django.utils.encoding import force_bytes
29 31
from django.core.signing import Signer, BadSignature
30 32

  
31 33
from . import misc
......
341 343
    def get_substitution_variables(self, prefix='session_'):
342 344
        d = {}
343 345
        d[prefix + 'id'] = self.id
346
        d[prefix + 'hash_id'] = hashlib.sha1(force_bytes(self.id)).hexdigest()
344 347
        if self.extra_variables:
345 348
            for k, v in self.extra_variables.items():
346 349
                d[prefix + 'var_' + k] = v
347
-