# coding:utf-8
# import os
import time
# import sys
# import st2
import pytest
from lib.session import Session
from lib.errors import SessionExpiredError, SessionInvalidError, SessionConsumedError, \
    SessionExistsError

pytest_plugins = ["errbot.backends.test"]
extra_plugin_dir = '.'


# secret_store
## get a secret
## set a secret
## delete a secret
## list secrets

# access_control
# create a session
# get a session by session_id
# get a session by chat_user
# delete a session by session_id
# delete a session by chat_user
# list sessions
# set an st2 token by chat_user
# get an st2 token by session_id

# chat_adapters
# slack
# xmpp
# mattermost
# rocketchat
# gitter
# discord

# bot_commands
# display help
# match and execute action-alias


# session tests
def test_session():
    user_id = "test_id"
    secret = "test_secret"
    session = Session(user_id, secret)

    # Session not expired
    assert False is session.is_expired()

    # Unseal a session
    assert True is session.is_sealed()
    session.unseal()
    assert False is session.is_sealed()

    # Time-to-live
    assert 3600 == session.ttl()
    session.ttl(300)
    assert 300 == session.ttl()

    # Secret
    assert True is session.match_secret(secret)

    # Expired session
    session.ttl(1)
    time.sleep(2)
    with pytest.raises(SessionExpiredError):
        session.is_expired()


if __name__ == "__main__":
    print("Run with pytest")
    exit(1)

@@ -46,37 +46,80 @@
 rt s
-ys%0A# 
+t2%0A%0A
 import 
-st2%0Aimport pytest
+pytest%0Afrom mock import Mock%0A%0Afrom lib.session import Session
 %0Afro
@@ -123,32 +123,40 @@
 from lib.session
+_manager
  import Session%0A
@@ -146,32 +146,132 @@
 r import Session
+Manager%0Afrom lib.store_adapters import ClearTextStoreAdapter, KeyringStoreAdapter, VaultStoreAdapter
 %0Afrom lib.errors
@@ -434,17 +434,16 @@
  = '.'%0A%0A
-%0A
 # secret
@@ -449,17 +449,16 @@
 t_store%0A
-#
 # get a 
@@ -464,17 +464,16 @@
  secret%0A
-#
 # set a 
@@ -479,17 +479,16 @@
  secret%0A
-#
 # delete
@@ -497,17 +497,16 @@
  secret%0A
-#
 # list s
@@ -901,43 +901,1635 @@
 s%0A%0A%0A
-# session tests%0Adef test_session():
+def test_session_manager():%0A    %22%22%22%0A    SessionManager class tests%0A    %22%22%22%0A    cfg = Mock()%0A    cfg.secrets_store = %22cleartext%22%0A%0A    user_id = 'test%25user'%0A    user_secret = 'secret_for_test'%0A    user_token = '1234567890-0987654321'%0A%0A    session_manager = SessionManager(cfg)%0A%0A    # Have requested backend%0A    assert isinstance(session_manager.secure_store, ClearTextStoreAdapter)%0A%0A    # create a new user%0A    s = session_manager.create(user_id, user_secret)%0A    assert s.user_id == user_id%0A%0A    # 1 session in list%0A    assert len(session_manager.list_sessions()) == 1%0A%0A    # create same user raise exception%0A    with pytest.raises(SessionExistsError):%0A        session_manager.create(user_id, user_secret)%0A%0A    # get an existing session%0A    s1 = session_manager.get_by_uuid(s.id())%0A    assert s1.id() == s.id()%0A%0A    # get a non-existent session%0A    with pytest.raises(SessionInvalidError):%0A        session_manager.get_by_uuid(s.id() + %22no_uuid%22)%0A%0A    # get an existing session by user%0A    s2 = session_manager.get_by_userid(user_id)%0A    assert s2.id() == s.id()%0A%0A    # get a non-existent session by user%0A    with pytest.raises(SessionInvalidError):%0A        session_manager.get_by_userid(user_id + %22no_user%22)%0A%0A    # confirm existence%0A    assert session_manager.exists(user_id) is True%0A%0A    # confirm non-existence%0A    assert session_manager.exists(user_id+%22no_user%22) is False%0A%0A    # Set value in secret store%0A    assert session_manager.put_secret(s.id(), user_token) is True%0A%0A    # Get value in secret store%0A    assert session_manager.get_secret(s.id()) == user_token%0A%0A%0Adef test_session():%0A    %22%22%22%0A    Session class tests%0A    %22%22%22
 %0A   

Add tests for SessionManager class and fix PEP8 errors.