|
1 |
| -import datetime |
| 1 | +import datetime, time |
2 | 2 | import re
|
3 | 3 | import os
|
4 | 4 |
|
@@ -141,6 +141,65 @@ def invalidate_session_app(environ, start_response):
|
141 | 141 | res = app.get('/')
|
142 | 142 | assert 'expires=' not in res.headers.getall('Set-Cookie')[0]
|
143 | 143 |
|
| 144 | + |
| 145 | +def test_changing_encrypt_key_with_timeout(): |
| 146 | + COMMON_ENCRYPT_KEY = '666a19cf7f61c64c' |
| 147 | + DIFFERENT_ENCRYPT_KEY = 'hello-world' |
| 148 | + |
| 149 | + options = {'session.encrypt_key': COMMON_ENCRYPT_KEY, |
| 150 | + 'session.timeout': 300, |
| 151 | + 'session.validate_key': 'hoobermas', |
| 152 | + 'session.type': 'cookie'} |
| 153 | + app = TestApp(SessionMiddleware(simple_app, **options)) |
| 154 | + res = app.get('/') |
| 155 | + assert 'The current value is: 1' in res, res |
| 156 | + |
| 157 | + # Get the session cookie, so we can reuse it. |
| 158 | + cookies = res.headers['Set-Cookie'] |
| 159 | + |
| 160 | + # Check that we get the same session with the same cookie |
| 161 | + options = {'session.encrypt_key': COMMON_ENCRYPT_KEY, |
| 162 | + 'session.timeout': 300, |
| 163 | + 'session.validate_key': 'hoobermas', |
| 164 | + 'session.type': 'cookie'} |
| 165 | + app = TestApp(SessionMiddleware(simple_app, **options)) |
| 166 | + res = app.get('/', headers={'Cookie': cookies}) |
| 167 | + assert 'The current value is: 2' in res, res |
| 168 | + |
| 169 | + # Now that we are sure that it reuses the same session, |
| 170 | + # change the encrypt_key so that it is unable to understand the cookie. |
| 171 | + options = {'session.encrypt_key': DIFFERENT_ENCRYPT_KEY, |
| 172 | + 'session.timeout': 300, |
| 173 | + 'session.validate_key': 'hoobermas', |
| 174 | + 'session.type': 'cookie'} |
| 175 | + app = TestApp(SessionMiddleware(simple_app, **options)) |
| 176 | + res = app.get('/', headers={'Cookie': cookies}) |
| 177 | + |
| 178 | + # Let's check it created a new session as the old one is invalid |
| 179 | + # in the past it just crashed. |
| 180 | + assert 'The current value is: 1' in res, res |
| 181 | + |
| 182 | + |
| 183 | +def test_cookie_properly_expires(): |
| 184 | + COMMON_ENCRYPT_KEY = '666a19cf7f61c64c' |
| 185 | + |
| 186 | + options = {'session.encrypt_key': COMMON_ENCRYPT_KEY, |
| 187 | + 'session.timeout': 1, |
| 188 | + 'session.validate_key': 'hoobermas', |
| 189 | + 'session.type': 'cookie'} |
| 190 | + app = TestApp(SessionMiddleware(simple_app, **options)) |
| 191 | + res = app.get('/') |
| 192 | + assert 'The current value is: 1' in res, res |
| 193 | + |
| 194 | + res = app.get('/') |
| 195 | + assert 'The current value is: 2' in res, res |
| 196 | + |
| 197 | + # Wait session to expire and check it starts with a clean one |
| 198 | + time.sleep(1) |
| 199 | + res = app.get('/') |
| 200 | + assert 'The current value is: 1' in res, res |
| 201 | + |
| 202 | + |
144 | 203 | if __name__ == '__main__':
|
145 | 204 | from paste import httpserver
|
146 | 205 | wsgi_app = SessionMiddleware(simple_app, {})
|
|
0 commit comments