# Path: toto/session.py
# class TotoSession(object):
#   '''Instances of ``TotoSession`` provide dictionary-like access to current session variables, and the current
#   account (if authenticated).
#   '''
# 
#   __serializer = pickle
# 
#   def __init__(self, db, session_data, session_cache=None, key=None):
#     self._db = db
#     self._session_cache = session_cache
#     self.user_id = session_data['user_id']
#     self.expires = session_data['expires']
#     self.session_id = session_data['session_id']
#     self.state = session_data.get('state') and TotoSession.loads(session_data['state']) or {}
#     key = key or session_data.get('key')
#     self.key = key or None
# 
#   def get_account(self, *args):
#     '''Load the account associated with this session (if authenticated). Session properties are
#     serialized to a binary string and stored as the ``TotoSession.state`` property, so you don't need to configure your database to handle them in
#     advance.
#     '''
#     raise Exception("Unimplemented operation: get_account")
# 
#   def session_data(self):
#     '''Return a session data ``dict`` that could be used to instantiate a session identical to the current one.
#     '''
#     data = {'user_id': self.user_id, 'expires': self.expires, 'session_id': self.session_id, 'state': TotoSession.dumps(self.state)}
#     if self.key:
#       data['key'] = self.key
#     return data
# 
#   def __getitem__(self, key):
#     return key in self.state and self.state[key] or None
# 
#   def __setitem__(self, key, value):
#     self.state[key] = value
# 
#   def __delitem__(self, key):
#     if key in self.state:
#       del self.state[key]
# 
#   def __iter__(self):
#     return self.state.__iter__()
# 
#   def iterkeys():
#     return self.__iter__()
# 
#   def __contains__(self, key):
#     return key in self.state
# 
#   def __str__(self):
#     return str({'user_id': self.user_id, 'expires': self.expires, 'id': self.session_id, 'state': self.state})
# 
#   def _refresh_cache(self):
#     if self._session_cache:
#       return self._session_cache.load_session(self.session_id)
#     return None
# 
#   def refresh(self):
#     '''Refresh the current session to the state in the database.
#     '''
#     raise Exception("Unimplemented operation: refresh")
# 
#   def _save_cache(self):
#     if self._session_cache:
#       updated_session_id = self._session_cache.store_session(self.session_data())
#       if updated_session_id:
#         self.session_id = updated_session_id
#       return True
#     return False
# 
#   def save(self):
#     '''Save the session to the database.
#     '''
#     raise Exception("Unimplemented operation: save")
# 
#   def verify(self, mac, signature):
#     '''Verify the mac and signature with this session's authenticated key.
#     '''
#     if not self.key or not mac or not signature:
#       raise TotoException(ERROR_INVALID_HMAC, "Invalid HMAC")
#     if self.hmac(signature) != mac:
#       raise TotoException(ERROR_INVALID_HMAC, "Invalid HMAC")
#     return self
# 
#   def hmac(self, signature):
#     '''Return the hmac of the signature signed with the authenticated key.
#     '''
#     return b64encode(hmac.new(self.key, signature, sha1).digest())
# 
#   @classmethod
#   def set_serializer(cls, serializer):
#     '''Set the module that instances of ``TotoSession`` and ``TotoSessionCache`` will use to serialize session state. The module must implement ``loads`` and ``dumps``
#     and support serialization and deserialization of any data you want to store in the session.
#     By default, ``cPickle`` is used.
#     '''
#     cls.__serializer = serializer
# 
#   @classmethod
#   def loads(cls, data):
#     '''A convenience method to call ``serializer.loads()`` on the active serializer.
#     '''
#     return cls.__serializer.loads(str(data))
# 
#   @classmethod
#   def dumps(cls, data):
#     '''A convenience method to call ``serializer.dumps()`` on the active serializer.
#     '''
#     return cls.__serializer.dumps(data)
# 
#   @classmethod
#   def generate_id(cls):
#     '''Generate a random 22 character url safe session ID string.
#     '''
#     return b64encode(uuid4().bytes, '-_')[:-2]
# 
# SESSION_ID_LENGTH = 22


import unittest
import urllib2
import urllib
import urlparse
import json
import os
import signal
import cPickle as pickle
from uuid import uuid4
from toto.secret import *
from multiprocessing import Process, active_children
from toto.session import TotoSession, SESSION_ID_LENGTH
from time import sleep, time

class TestSession(unittest.TestCase):

  def test_generate_id(self):
    session_id = TotoSession.generate_id()

# Path: toto/session.py
# class TotoSession(object):
#   '''Instances of ``TotoSession`` provide dictionary-like access to current session variables, and the current
#   account (if authenticated).
#   '''
# 
#   __serializer = pickle
# 
#   def __init__(self, db, session_data, session_cache=None, key=None):
#     self._db = db
#     self._session_cache = session_cache
#     self.user_id = session_data['user_id']
#     self.expires = session_data['expires']
#     self.session_id = session_data['session_id']
#     self.state = session_data.get('state') and TotoSession.loads(session_data['state']) or {}
#     key = key or session_data.get('key')
#     self.key = key or None
# 
#   def get_account(self, *args):
#     '''Load the account associated with this session (if authenticated). Session properties are
#     serialized to a binary string and stored as the ``TotoSession.state`` property, so you don't need to configure your database to handle them in
#     advance.
#     '''
#     raise Exception("Unimplemented operation: get_account")
# 
#   def session_data(self):
#     '''Return a session data ``dict`` that could be used to instantiate a session identical to the current one.
#     '''
#     data = {'user_id': self.user_id, 'expires': self.expires, 'session_id': self.session_id, 'state': TotoSession.dumps(self.state)}
#     if self.key:
#       data['key'] = self.key
#     return data
# 
#   def __getitem__(self, key):
#     return key in self.state and self.state[key] or None
# 
#   def __setitem__(self, key, value):
#     self.state[key] = value
# 
#   def __delitem__(self, key):
#     if key in self.state:
#       del self.state[key]
# 
#   def __iter__(self):
#     return self.state.__iter__()
# 
#   def iterkeys():
#     return self.__iter__()
# 
#   def __contains__(self, key):
#     return key in self.state
# 
#   def __str__(self):
#     return str({'user_id': self.user_id, 'expires': self.expires, 'id': self.session_id, 'state': self.state})
# 
#   def _refresh_cache(self):
#     if self._session_cache:
#       return self._session_cache.load_session(self.session_id)
#     return None
# 
#   def refresh(self):
#     '''Refresh the current session to the state in the database.
#     '''
#     raise Exception("Unimplemented operation: refresh")
# 
#   def _save_cache(self):
#     if self._session_cache:
#       updated_session_id = self._session_cache.store_session(self.session_data())
#       if updated_session_id:
#         self.session_id = updated_session_id
#       return True
#     return False
# 
#   def save(self):
#     '''Save the session to the database.
#     '''
#     raise Exception("Unimplemented operation: save")
# 
#   def verify(self, mac, signature):
#     '''Verify the mac and signature with this session's authenticated key.
#     '''
#     if not self.key or not mac or not signature:
#       raise TotoException(ERROR_INVALID_HMAC, "Invalid HMAC")
#     if self.hmac(signature) != mac:
#       raise TotoException(ERROR_INVALID_HMAC, "Invalid HMAC")
#     return self
# 
#   def hmac(self, signature):
#     '''Return the hmac of the signature signed with the authenticated key.
#     '''
#     return b64encode(hmac.new(self.key, signature, sha1).digest())
# 
#   @classmethod
#   def set_serializer(cls, serializer):
#     '''Set the module that instances of ``TotoSession`` and ``TotoSessionCache`` will use to serialize session state. The module must implement ``loads`` and ``dumps``
#     and support serialization and deserialization of any data you want to store in the session.
#     By default, ``cPickle`` is used.
#     '''
#     cls.__serializer = serializer
# 
#   @classmethod
#   def loads(cls, data):
#     '''A convenience method to call ``serializer.loads()`` on the active serializer.
#     '''
#     return cls.__serializer.loads(str(data))
# 
#   @classmethod
#   def dumps(cls, data):
#     '''A convenience method to call ``serializer.dumps()`` on the active serializer.
#     '''
#     return cls.__serializer.dumps(data)
# 
#   @classmethod
#   def generate_id(cls):
#     '''Generate a random 22 character url safe session ID string.
#     '''
#     return b64encode(uuid4().bytes, '-_')[:-2]
# 
# SESSION_ID_LENGTH = 22


# Path: tests/test_session.py
import unittest
import urllib2
import urllib
import urlparse
import json
import os
import signal
import cPickle as pickle
from uuid import uuid4
from toto.secret import *
from multiprocessing import Process, active_children
from toto.session import TotoSession, SESSION_ID_LENGTH
from time import sleep, time


class TestSession(unittest.TestCase):

  def test_generate_id(self):
    session_id = TotoSession.generate_id()

    self.assertEqual(len(session_id), SESSION_ID_LENGTH)