Changeset 5175
- Timestamp:
- 08/20/08 12:21:52 (3 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/turbogears/identity/tests/test_visit.py
r4695 r5175 1 1 import time 2 3 from Cookie import SimpleCookie 2 4 from unittest import TestCase 5 3 6 import cherrypy 7 4 8 from turbogears import config, controllers, expose, startup, testutil, visit 5 from Cookie import SimpleCookie6 9 7 10 … … 15 18 @expose() 16 19 def index(self): 17 new = None 18 if visit.current(): 19 new = visit.current().is_new 20 new = visit_key = None 21 cur_visit = visit.current() 22 if cur_visit: 23 new = cur_visit.is_new 24 key = cur_visit.key 20 25 visit_on = config.get('visit.on') 21 return dict(new=new, visit_on=visit_on)26 return dict(new=new, key=key, visit_on=visit_on) 22 27 23 28 … … 27 32 testutil.stop_server(tg_only = True) 28 33 self._visit_on = config.get('visit.on', False) 34 self._visit_source = config.get('visit.source', 'cookie') 29 35 config.update({'visit.on': True}) 30 36 self._visit_timeout = config.get('visit.timeout', 20) 31 37 config.update({'visit.timeout': 50}) 32 38 self.cookie_name = config.get("visit.cookie.name", 'tg-visit') 39 self._visit_key_param = config.get("visit.form.name", 'tg_visit') 33 40 self.app = testutil.make_app(VisitRoot) 34 41 testutil.start_server() … … 54 61 # first visit's cookie 55 62 print "Headers", response.headers 56 print " Config", config.get('visit.on')63 print "Visit on", config.get('visit.on') 57 64 morsel = response.cookies_set[self.cookie_name] 58 65 response = self.app.get("/", headers=cookie_header(response)) 59 66 assert not response.raw['new'] 67 68 def test_visit_from_form(self): 69 """Test if the visit key is retrieved from the request params.""" 70 _app = self.app 71 try: 72 testutil.stop_server(tg_only = True) 73 config.update({'visit.source': 'cookie,form'}) 74 self.app = testutil.make_app(VisitRoot) 75 testutil.start_server() 76 # first visit's cookie 77 first_key = self.app.get("/").raw.get('key') 78 # second request with no cookies 79 self.app.cookies = {} 80 response = self.app.get("/", 81 params={self._visit_key_param: first_key}) 82 finally: 83 config.update({'visit.source': self._visit_source}) 84 self.app = _app 85 assert first_key == response.raw['key'] 86 87 def test_visit_from_form_with_cookie_fallback(self): 88 """Test if visit key is retrieved from cookie if not found in params.""" 89 _app = self.app 90 try: 91 testutil.stop_server(tg_only = True) 92 config.update({'visit.source': 'form,cookie'}) 93 _app = testutil.make_app(VisitRoot) 94 testutil.start_server() 95 # first visit's cookie 96 first_key = self.app.get("/").raw.get('key') 97 # second request with no params 98 response = self.app.get("/") 99 finally: 100 config.update({'visit.source': self._visit_source}) 101 self.app = _app 102 assert first_key == response.raw['key'] 60 103 61 104 def test_cookie_expires(self): … … 108 151 should_expire = time.mktime(time.gmtime()) + int(morsel['max-age']) 109 152 assert abs(should_expire - expires) < 3, (should_expire, expires, should_expire - expires) 110 branches/1.1/turbogears/qstemplates/quickstart/+package+/config/app.cfg_tmpl
r4815 r5175 93 93 # visit.timeout=20 94 94 95 # Where to look for the key of an existing visit in the request and in which 96 # order. Comma-separated list of possible values: 'cookie', 'form'. 97 # By default only use visit key in session cookie. 98 # visit.source="cookie" 99 95 100 # The name of the cookie to transmit to the visitor's browser. 96 101 # visit.cookie.name="tg-visit" 102 103 # The name of the request parameter with the session key (for when 104 # 'visit.source' includes 'form'). Name MUST NOT contain dashes or dots! 105 # visit.form.name="tg_visit" 97 106 98 107 # Domain name to specify when setting the cookie (must begin with . according to branches/1.1/turbogears/visit/api.py
r4695 r5175 2 2 import sha 3 3 import threading 4 import time 5 4 6 from random import random 5 7 from datetime import timedelta, datetime 6 import time 8 9 import cherrypy 7 10 import pkg_resources 8 import cherrypy 11 9 12 from cherrypy.filters.basefilter import BaseFilter 10 13 from turbogears import config … … 175 178 log.info("Visit filter initialised") 176 179 get = config.get 180 # Where to look for the session key in the request and in which order 181 self.source = [s.strip().lower() for s in 182 get("visit.source", "cookie").split(',')] 183 if set(self.source).difference(('cookie', 'form')): 184 log.warning("Unsupported 'visit.source' '%s' in configuration.") 177 185 # Get the name to use for the identity cookie. 178 186 self.cookie_name = get("visit.cookie.name", "tg-visit") 187 # and the name of the request param. MUST NOT contain dashes or dots, 188 # otherwise the NestedVariablesFilter will chocke on it. 189 self.visit_key_param = get("visit.form.name", "tg_visit") 179 190 # TODO: The path should probably default to whatever 180 191 # the root is masquerading as in the event of a … … 196 207 set_current(None) 197 208 return 209 cpreq = cherrypy.request 198 210 visit = current() 199 cookies = cherrypy.request.simple_cookie200 211 if not visit: 201 if self.cookie_name in cookies: 202 # Process visit based on cookie 203 visit_key = cookies[self.cookie_name].value 204 visit = _manager.visit_for_key(visit_key) 212 visit_key = None 213 for source in self.source: 214 if source == 'cookie': 215 visit_key = cpreq.simple_cookie.get(self.cookie_name) 216 if visit_key: 217 visit_key = visit_key.value 218 log.debug("Retrieved visit key '%s' from cookie '%s'.", 219 visit_key, self.cookie_name) 220 elif source == 'form': 221 visit_key = cpreq.params.pop(self.visit_key_param, None) 222 log.debug( 223 "Retrieved visit key '%s' from request param '%s'.", 224 visit_key, self.visit_key_param) 225 if visit_key: 226 visit = _manager.visit_for_key(visit_key) 227 break 205 228 if not visit: 206 229 visit_key = self._generate_key() 207 230 visit = _manager.new_visit_with_key(visit_key) 231 log.debug("Created new visit with key: %s", visit_key) 232 else: 233 log.debug("Using visit from request with key: %s", visit_key) 208 234 self.send_cookie(visit_key) 209 235 set_current(visit)