| 1 |
from beaker.middleware import SessionMiddleware |
| 2 |
from beaker.session import Session, CookieSession |
| 3 |
import datetime |
| 4 |
import re |
| 5 |
|
| 6 |
def test_cookie_expires(): |
| 7 |
"""Explore valid arguments for cookie_expires.""" |
| 8 |
def app(*args, **kw): |
| 9 |
pass |
| 10 |
|
| 11 |
key = 'beaker.session.cookie_expires' |
| 12 |
now = datetime.datetime.now() |
| 13 |
|
| 14 |
values = ['300', 300, |
| 15 |
True, 'True', 'true', 't', |
| 16 |
False, 'False', 'false', 'f', |
| 17 |
datetime.timedelta(minutes=5), now] |
| 18 |
|
| 19 |
expected = [datetime.timedelta(seconds=300), |
| 20 |
datetime.timedelta(seconds=300), |
| 21 |
True, True, True, True, |
| 22 |
False, False, False, False, |
| 23 |
datetime.timedelta(minutes=5), now] |
| 24 |
|
| 25 |
actual = [] |
| 26 |
|
| 27 |
for pos, v in enumerate(values): |
| 28 |
try: |
| 29 |
s = SessionMiddleware(app, config={key:v}) |
| 30 |
val = s.options['cookie_expires'] |
| 31 |
except: |
| 32 |
val = None |
| 33 |
assert val == expected[pos] |
| 34 |
|
| 35 |
|
| 36 |
def cookie_expiration(session): |
| 37 |
cookie = session.cookie.output() |
| 38 |
expiry_m = re.match('Set-Cookie: beaker.session.id=[0-9a-f]{32}(; expires=[^;]+)?; Path=/', cookie) |
| 39 |
assert expiry_m |
| 40 |
expiry = expiry_m.group(1) |
| 41 |
if expiry is None: |
| 42 |
return True |
| 43 |
if re.match('; expires=(Mon|Tue), 1[89]-Jan-2038 [0-9:]{8} GMT', expiry): |
| 44 |
return False |
| 45 |
else: |
| 46 |
return expiry[10:] |
| 47 |
|
| 48 |
|
| 49 |
def test_cookie_exprires_2(): |
| 50 |
"""Exhibit Set-Cookie: values.""" |
| 51 |
expires = cookie_expiration(Session({}, cookie_expires=True)) |
| 52 |
|
| 53 |
assert expires is True, expires |
| 54 |
no_expires = cookie_expiration(Session({}, cookie_expires=False)) |
| 55 |
|
| 56 |
assert no_expires is False, no_expires |
| 57 |
|
| 58 |
def test_cookie_expires_different_locale(): |
| 59 |
from locale import setlocale, LC_TIME |
| 60 |
expires_date = datetime.datetime(2019, 5, 22) |
| 61 |
setlocale(LC_TIME, 'it_IT.UTF-8') |
| 62 |
|
| 63 |
assert expires_date.strftime("%a, %d-%b-%Y %H:%M:%S GMT").startswith('mer,') |
| 64 |
session = Session({}, cookie_expires=True, validate_key='validate_key') |
| 65 |
assert session._set_cookie_expires(expires_date) |
| 66 |
expires = cookie_expiration(session) |
| 67 |
assert expires == 'Wed, 22-May-2019 00:00:00 GMT', expires |
| 68 |
setlocale(LC_TIME, '') |
| 69 |
|
| 70 |
def test_set_cookie_expires(): |
| 71 |
"""Exhibit Set-Cookie: values.""" |
| 72 |
session = Session({}, cookie_expires=True) |
| 73 |
assert cookie_expiration(session) is True |
| 74 |
session._set_cookie_expires(False) |
| 75 |
assert cookie_expiration(session) is False |
| 76 |
session._set_cookie_expires(True) |
| 77 |
assert cookie_expiration(session) is True |
| 78 |
|
| 79 |
def test_cookiesession_expires_values(): |
| 80 |
BASE_OPTIONS = { |
| 81 |
'invalidate_corrupt': True, |
| 82 |
'type': 'cookie', |
| 83 |
'data_dir': None, |
| 84 |
'key': 'ckan', |
| 85 |
'timeout': None, |
| 86 |
'save_accessed_time': True, |
| 87 |
'secret': '12341234', |
| 88 |
'log_file': None, |
| 89 |
'data_serializer': 'json', |
| 90 |
'validate_key': 'asdfasdf', |
| 91 |
'httponly': True, |
| 92 |
'secure': False, |
| 93 |
'samesite': 'Strict', |
| 94 |
'auto': False, |
| 95 |
'cookie_domain': None |
| 96 |
} |
| 97 |
|
| 98 |
for cookie_expires in (True, False, datetime.timedelta(seconds=10)): |
| 99 |
options = dict(BASE_OPTIONS, cookie_expires=cookie_expires) |
| 100 |
cookie_session = CookieSession( |
| 101 |
{}, |
| 102 |
**options, |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
cookie_session.save() |
| 107 |
if cookie_expires != True: |
| 108 |
assert "expires" in cookie_session.cookie.output() |
| 109 |
|
| 110 |
|
| 111 |
cookie_session.save() |
| 112 |
if cookie_expires != True: |
| 113 |
assert "expires" in cookie_session.cookie.output() |
| 114 |
|
| 115 |
|
| 116 |
loaded_back = CookieSession({"cookie": cookie_session.cookie}, **options) |
| 117 |
loaded_back.save() |
| 118 |
if cookie_expires != True: |
| 119 |
assert "expires" in cookie_session.cookie.output() |