def test_find_ticket_with_principal(self, db_session): from paildocket.models import UserTicket ticket = 'abcdefg12' alice, alice_ticket = self._create_user_and_ticket(db_session, ticket) found_ticket_instance = UserTicket.find_ticket_with_principal( db_session, ticket, alice.email) assert found_ticket_instance is alice_ticket
def authenticated_userid(self, request): # TODO: break this up, it's way too complex if self.debug: logger.debug('`authenticated_userid` called') userid = request.auth.get('userid') if userid is not None: if self.debug: fmt = 'Found userid {0!r} already in request.auth' logger.debug(fmt.format(userid)) return userid result = self.cookie.bind(request).get_value() if not result: if self.debug: logger.debug('Failed to find auth ticket in cookie') return None principal = result['principal'] ticket = result['ticket'] issued_unparsed = result['issued'] issued = datetime.datetime.strptime(issued_unparsed, _iso_format) if self.debug: fmt = ( 'Cookie contains ticket {0!r} for principal {1!r} issued {2!r}' ) logger.debug(fmt.format(ticket, principal, issued_unparsed)) ticket_instance = UserTicket.find_ticket_with_principal( request.db_session, ticket, principal) if ticket_instance is None: fmt = ( 'Failed to locate ticket {0!r} for principal {1!r} in database' ) logger.debug(fmt.format(ticket, principal)) return None userid = ticket_instance.user_id # TODO fix this, authenticated_userid must return None if timed out self._timeout_or_reissue(request, ticket_instance, issued, principal) request.auth['userid'] = userid request.auth['ticket_instance'] = ticket_instance return userid