def test_old_visit(self): """Test if we can track a visitor over time.""" testutil.create_request("/") # first visit's cookie morsel = cherrypy.response.simple_cookie[self.cookie_name] testutil.create_request("/", headers=cookie_header(morsel)) assert not visit.current().is_new
def login(self): visit_key = visit.current().key #print "User login visit_key", visit_key link = VisitIdentity.query.filter_by(visit_key=visit_key).first() #print "link", link if not link: link = VisitIdentity(visit_key=visit_key, user_id=self.user_id) #print "NEW VisitIdentity" else: link.user_id = self.user_id user_identity = identity.current_provider.load_identity(visit_key) #print "user_identity", user_identity identity.set_current_identity(user_identity)
def switchToUser(user, redirect_url=None, logout=False, check_permission=False): if not identity.current.anonymous: current_user = identity.current.user else: current_user = None if user == current_user: if redirect_url: redirect(redirect_url) else: return if check_permission and current_user and not current_user.hasModeratePermission(): raise identity.IdentityFailure("User %s was not authenticated to change the user" % current_user.email_address) if logout: masterdb.Visit.setOriginalUser(None) try: identity.current.logout() except: pass try: visit_key = visit.current().key try: link = masterdb.VisitIdentity.by_visit_key(visit_key) except SQLObjectNotFound: link = None if not link: link = masterdb.VisitIdentity(visit_key=visit_key, user_id=user.id) else: link.user_id = user.id user_identity = identity.current_provider.load_identity(visit_key) identity.set_current_identity(user_identity) if logout: masterdb.Visit.setOriginalUser(user) if "current_alerts" in cherrypy.session: del cherrypy.session["current_alerts"] except: pass if redirect_url: redirect(redirect_url)
def test_cookie_expires(self): """Test if the visit timeout mechanism works.""" timeout = config.get('visit.timeout', 50) try: # set expiration to one second for this test only config.update({'visit.timeout': 1.0 / 60}) testutil.create_request("/") morsel = cherrypy.response.simple_cookie[self.cookie_name] time.sleep(2) # 2 seconds testutil.create_request("/", headers=cookie_header(morsel)) finally: config.update({'visit.timeout': timeout}) assert cherrypy.response.simple_cookie[ self.cookie_name].value != morsel.value, \ 'cookie values should not match' assert visit.current().is_new, \ 'this should be a new visit, as the cookie has expired'
def test_cookie_expires(self): """Test if the visit timeout mechanism works.""" timeout = config.get('visit.timeout', 50) try: # set expiration to one second for this test only config.update({'visit.timeout': 1.0/60}) testutil.create_request("/") morsel = cherrypy.response.simple_cookie[self.cookie_name] time.sleep(2) # 2 seconds testutil.create_request("/", headers=cookie_header(morsel)) finally: config.update({'visit.timeout': timeout}) assert cherrypy.response.simple_cookie[ self.cookie_name].value != morsel.value, \ 'cookie values should not match' assert visit.current().is_new, \ 'this should be a new visit, as the cookie has expired'
def login(self, forward_url=None, *args, **kw): """Show the login form or forward user to previously requested page.""" if forward_url: if isinstance(forward_url, list): forward_url = forward_url.pop(0) else: del request.params['forward_url'] new_visit = visit.current() if new_visit: new_visit = new_visit.is_new if (not new_visit and not identity.current.anonymous and identity.was_login_attempted() and not identity.get_identity_errors()): redirect(forward_url or '/', kw) if identity.was_login_attempted(): if new_visit: msg = _(u"Cannot log in because your browser " "does not support session cookies.") else: msg = _(u"The credentials you supplied were not correct or " "did not grant access to this resource.") elif identity.get_identity_errors(): msg = _(u"You must provide your credentials before accessing " "this resource.") else: msg = _(u"Please log in.") if not forward_url: forward_url = request.headers.get("Referer", "/") # we do not set the response status here anymore since it # is now handled in the identity exception. return dict(logging_in=True, message=msg, forward_url=forward_url, previous_url=request.path_info, original_parameters=request.params)
def test_new_visit(self): """Test that we can see a new visit on the server.""" testutil.create_request("/") assert visit.current().is_new
def index(self): cur_visit = visit.current() return cur_visit.key