# Path: lib/session_manager.py
# class SessionManager(object):
#     def __init__(self, cfg):
#         self.cfg = cfg
#         self.store = SessionStore()
#         self.secure_store = StoreAdapterFactory.instantiate(cfg.secrets_store)()
#         self.secure_store.setup()
# 
#     def get_by_userid(self, user_id):
#         """
#         Fetch information from the store by user_id.
#         param: user_id[string]  A string uniquely identifying the chat user.
#         """
#         session = self.store.get_by_userid(user_id)
#         if session is False:
#             raise SessionInvalidError
#         return session
# 
#     def get_by_uuid(self, _uuid):
#         """
#         Fetch information related to a session by its UUID.
#         If a session doesn't exist, False is returned.
#         """
#         session = self.store.get_by_uuid(_uuid)
#         if session is False:
#             raise SessionInvalidError
#         return session
# 
#     def create(self, user_id, user_secret, session_ttl):
#         """
#         param: user_id - Chat user unique identifier.
#         param: user_secret - A pseudo-secret word provided by
#         the user and used as part of the UUID hashing process.
#         """
#         # Don't create a new session if one already exists.
#         if self.exists(user_id):
#             raise SessionExistsError
#         session = Session(user_id, user_secret, session_ttl)
#         # Store the session.
#         self.store.put(session)
#         return session
# 
#     def delete(self, user_id):
#         """
#         Remove a session from the manager
#         """
#         session = self.get_by_userid(user_id)
#         if session:
#             self.secure_store.delete(session.id())
#         self.store.delete(user_id)
# 
#     def list_sessions(self):
#         """
#         List all available sessions.
#         """
#         return self.store.list()
# 
#     def update(self, session):
#         raise NotImplementedError
# 
#     def exists(self, user_id):
#         return self.store.get_by_userid(user_id) is not False
# 
#     def put_secret(self, session_id, secret):
#         self.secure_store.set(session_id, secret)
#         return True
# 
#     def get_secret(self, session_id):
#         return self.secure_store.get(session_id)
# 
# Path: lib/store_adapters.py
# class ClearTextStoreAdapter(AbstractStoreAdapter):
#     """
#     The clear text store adapter doesn't encrypt data in memory, but doesn't persist it to disk
#     either.  If more secure methods are required to operate, open an issue requesting a new feature.
#     """
# 
#     def __init__(self):
#         self.associations = {}
# 
#     def __str__(self):
#         return str(self.associations)
# 
#     def setup(self):
#         pass
# 
#     def set(self, name, secret, namespace=""):
#         self.associations[name] = secret
#         return True
# 
#     def get(self, name, namespace=""):
#         return self.associations.get(name)
# 
#     def delete(self, name, namespace=""):
#         if name in self.associations:
#             del self.associations[name]
# 
#     def teardown(self):
#         pass
# 
# Path: lib/errors.py
# class SessionInvalidError(Error):
#     def __init__(self, message="Session is invalid."):
#         super(SessionInvalidError, self).__init__()
#         self.message = message
# 
# class SessionExistsError(Error):
#     def __init__(self, message="Session already exists."):
#         super(SessionExistsError, self).__init__()
#         self.message = message


import pytest
from mock import Mock
from lib.session_manager import SessionManager
from lib.store_adapters import ClearTextStoreAdapter
from lib.errors import SessionInvalidError, SessionExistsError

    session_manager = SessionManager(cfg)

    # Have requested backend
    assert isinstance(session_manager.secure_store, ClearTextStoreAdapter)

    # create a new user
    s = session_manager.create(user_id, user_secret, session_ttl)
    assert s.user_id == user_id

    # 1 session in list
    assert len(session_manager.list_sessions()) == 1

    # create same user raise exception
    with pytest.raises(SessionExistsError):
        session_manager.create(user_id, user_secret, session_ttl)

    # get an existing session
    s1 = session_manager.get_by_uuid(s.id())
    assert s1.id() == s.id()

    # get a non-existent session
    with pytest.raises(SessionInvalidError):
        session_manager.get_by_uuid(s.id() + "no_uuid")

    # get an existing session by user
    s2 = session_manager.get_by_userid(user_id)
    assert s2.id() == s.id()

    # get a non-existent session by user

# Path: lib/session_manager.py
# class SessionManager(object):
#     def __init__(self, cfg):
#         self.cfg = cfg
#         self.store = SessionStore()
#         self.secure_store = StoreAdapterFactory.instantiate(cfg.secrets_store)()
#         self.secure_store.setup()
# 
#     def get_by_userid(self, user_id):
#         """
#         Fetch information from the store by user_id.
#         param: user_id[string]  A string uniquely identifying the chat user.
#         """
#         session = self.store.get_by_userid(user_id)
#         if session is False:
#             raise SessionInvalidError
#         return session
# 
#     def get_by_uuid(self, _uuid):
#         """
#         Fetch information related to a session by its UUID.
#         If a session doesn't exist, False is returned.
#         """
#         session = self.store.get_by_uuid(_uuid)
#         if session is False:
#             raise SessionInvalidError
#         return session
# 
#     def create(self, user_id, user_secret, session_ttl):
#         """
#         param: user_id - Chat user unique identifier.
#         param: user_secret - A pseudo-secret word provided by
#         the user and used as part of the UUID hashing process.
#         """
#         # Don't create a new session if one already exists.
#         if self.exists(user_id):
#             raise SessionExistsError
#         session = Session(user_id, user_secret, session_ttl)
#         # Store the session.
#         self.store.put(session)
#         return session
# 
#     def delete(self, user_id):
#         """
#         Remove a session from the manager
#         """
#         session = self.get_by_userid(user_id)
#         if session:
#             self.secure_store.delete(session.id())
#         self.store.delete(user_id)
# 
#     def list_sessions(self):
#         """
#         List all available sessions.
#         """
#         return self.store.list()
# 
#     def update(self, session):
#         raise NotImplementedError
# 
#     def exists(self, user_id):
#         return self.store.get_by_userid(user_id) is not False
# 
#     def put_secret(self, session_id, secret):
#         self.secure_store.set(session_id, secret)
#         return True
# 
#     def get_secret(self, session_id):
#         return self.secure_store.get(session_id)
# 
# Path: lib/store_adapters.py
# class ClearTextStoreAdapter(AbstractStoreAdapter):
#     """
#     The clear text store adapter doesn't encrypt data in memory, but doesn't persist it to disk
#     either.  If more secure methods are required to operate, open an issue requesting a new feature.
#     """
# 
#     def __init__(self):
#         self.associations = {}
# 
#     def __str__(self):
#         return str(self.associations)
# 
#     def setup(self):
#         pass
# 
#     def set(self, name, secret, namespace=""):
#         self.associations[name] = secret
#         return True
# 
#     def get(self, name, namespace=""):
#         return self.associations.get(name)
# 
#     def delete(self, name, namespace=""):
#         if name in self.associations:
#             del self.associations[name]
# 
#     def teardown(self):
#         pass
# 
# Path: lib/errors.py
# class SessionInvalidError(Error):
#     def __init__(self, message="Session is invalid."):
#         super(SessionInvalidError, self).__init__()
#         self.message = message
# 
# class SessionExistsError(Error):
#     def __init__(self, message="Session already exists."):
#         super(SessionExistsError, self).__init__()
#         self.message = message


# Path: tests/test_session_manager.py
import pytest
from mock import Mock
from lib.session_manager import SessionManager
from lib.store_adapters import ClearTextStoreAdapter
from lib.errors import SessionInvalidError, SessionExistsError


    session_manager = SessionManager(cfg)

    # Have requested backend
    assert isinstance(session_manager.secure_store, ClearTextStoreAdapter)

    # create a new user
    s = session_manager.create(user_id, user_secret, session_ttl)
    assert s.user_id == user_id

    # 1 session in list
    assert len(session_manager.list_sessions()) == 1

    # create same user raise exception
    with pytest.raises(SessionExistsError):
        session_manager.create(user_id, user_secret, session_ttl)

    # get an existing session
    s1 = session_manager.get_by_uuid(s.id())
    assert s1.id() == s.id()

    # get a non-existent session
    with pytest.raises(SessionInvalidError):
        session_manager.get_by_uuid(s.id() + "no_uuid")

    # get an existing session by user
    s2 = session_manager.get_by_userid(user_id)
    assert s2.id() == s.id()

    # get a non-existent session by user

    with pytest.raises(SessionInvalidError):