# Path: tests/helpers.py
# class BaseTestCase(unittest.TestCase):
#     def shortDescription(self):
#         return None
# 
# def fake_home_dir():
#     return '%s/data' % os.path.dirname(__file__)
# 
# class DatabaseMock(Mock):
#     active_sessions = {}
#     endpoints = {}
# 
#     def __init__(self, *args, **kwargs):
#         super(DatabaseMock, self).__init__(*args, **kwargs)
#         self.add = Mock(side_effect=set_primary_key)
# 
#     def get_active_sessions(self, provider_id=None):
#         return self.active_sessions.values()
# 
#     def get_session(self, session_id):
#         return self.active_sessions.get(str(session_id))
# 
#     def get_endpoint(self, endpoint):
#         pass
# 
#     def get_endpoints(self, *args, **kwargs):
#         return self.endpoints.values()
# 
#     @staticmethod
#     def get_last_session_step(session_id):
#         return Mock()
# 
#     def register_provider(self, name, url, platforms):
#         from core.db.models import Provider
#         return Provider(name, url, platforms)
# 
# Path: core/constants.py
# SESSION_CLOSE_REASON_API_CALL = "Session closed via API stop_session call"
# GET_ENDPOINT_ATTEMPTS = 10
# ENDPOINT_REBUILD_ATTEMPTS = 3
# CREATE_SESSION_REQUEST_TIMEOUT = 60
# REQUEST_TIMEOUT = 60
# MAKE_REQUEST_ATTEMPTS_AMOUNT = 3
# REQUEST_SLEEP_BASE_TIME = 5
# REQUEST_TIMEOUT_ON_CREATE_ENDPOINT = 60
# ANY = u'ANY'
# GET_SESSION_SLEEP_TIME = 2
# ER_SLEEP_TIME = 2
# EP_SLEEP_TIME = 2


import json
from datetime import datetime
from mock import Mock, patch, PropertyMock
from lode_runner import dataprovider
from tests.helpers import BaseTestCase, fake_home_dir, DatabaseMock
from core import constants
        from core.config import setup_config
            from vmmaster.server import create_app
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session
        from core.db.models import Session
        from core.config import setup_config
            from vmpool.server import create_app
            from vmpool.virtual_machines_pool import VirtualMachinesPool
    def test_api_sessions(self):
        session = Session(self.platform, "session1", self.desired_caps["desiredCapabilities"])
        session.created = session.modified = datetime.now()

        with patch('flask.current_app.sessions.active',
                   Mock(return_value=[session])):
            response = self.vmmaster_client.get('/api/sessions')
        body = json.loads(response.data)
        self.assertEqual(200, response.status_code)

        sessions = body['result']['sessions']
        self.assertEqual(1, len(sessions))
        self.assertEqual(self.platform, session.platform)
        self.assertEqual(200, body['metacode'])

        session.failed()

    def test_api_stop_session(self):
        session = Session("some_platform")
        session.failed = Mock()

        with patch(
            'flask.current_app.sessions.get_session',
            Mock(return_value=session)
        ):
            response = self.vmmaster_client.get("/api/session/{}/stop".format(session.id))
        body = json.loads(response.data)
        self.assertEqual(200, body['metacode'])

        session.failed.assert_any_call(

# Path: tests/helpers.py
# class BaseTestCase(unittest.TestCase):
#     def shortDescription(self):
#         return None
# 
# def fake_home_dir():
#     return '%s/data' % os.path.dirname(__file__)
# 
# class DatabaseMock(Mock):
#     active_sessions = {}
#     endpoints = {}
# 
#     def __init__(self, *args, **kwargs):
#         super(DatabaseMock, self).__init__(*args, **kwargs)
#         self.add = Mock(side_effect=set_primary_key)
# 
#     def get_active_sessions(self, provider_id=None):
#         return self.active_sessions.values()
# 
#     def get_session(self, session_id):
#         return self.active_sessions.get(str(session_id))
# 
#     def get_endpoint(self, endpoint):
#         pass
# 
#     def get_endpoints(self, *args, **kwargs):
#         return self.endpoints.values()
# 
#     @staticmethod
#     def get_last_session_step(session_id):
#         return Mock()
# 
#     def register_provider(self, name, url, platforms):
#         from core.db.models import Provider
#         return Provider(name, url, platforms)
# 
# Path: core/constants.py
# SESSION_CLOSE_REASON_API_CALL = "Session closed via API stop_session call"
# GET_ENDPOINT_ATTEMPTS = 10
# ENDPOINT_REBUILD_ATTEMPTS = 3
# CREATE_SESSION_REQUEST_TIMEOUT = 60
# REQUEST_TIMEOUT = 60
# MAKE_REQUEST_ATTEMPTS_AMOUNT = 3
# REQUEST_SLEEP_BASE_TIME = 5
# REQUEST_TIMEOUT_ON_CREATE_ENDPOINT = 60
# ANY = u'ANY'
# GET_SESSION_SLEEP_TIME = 2
# ER_SLEEP_TIME = 2
# EP_SLEEP_TIME = 2


# Path: tests/unit/test_api.py
import json
from datetime import datetime
from mock import Mock, patch, PropertyMock
from lode_runner import dataprovider
from tests.helpers import BaseTestCase, fake_home_dir, DatabaseMock
from core import constants
        from core.config import setup_config
            from vmmaster.server import create_app
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session, Endpoint, Provider
        from core.db.models import Session
        from core.db.models import Session
        from core.config import setup_config
            from vmpool.server import create_app
            from vmpool.virtual_machines_pool import VirtualMachinesPool

    def test_api_sessions(self):
        session = Session(self.platform, "session1", self.desired_caps["desiredCapabilities"])
        session.created = session.modified = datetime.now()

        with patch('flask.current_app.sessions.active',
                   Mock(return_value=[session])):
            response = self.vmmaster_client.get('/api/sessions')
        body = json.loads(response.data)
        self.assertEqual(200, response.status_code)

        sessions = body['result']['sessions']
        self.assertEqual(1, len(sessions))
        self.assertEqual(self.platform, session.platform)
        self.assertEqual(200, body['metacode'])

        session.failed()

    def test_api_stop_session(self):
        session = Session("some_platform")
        session.failed = Mock()

        with patch(
            'flask.current_app.sessions.get_session',
            Mock(return_value=session)
        ):
            response = self.vmmaster_client.get("/api/session/{}/stop".format(session.id))
        body = json.loads(response.data)
        self.assertEqual(200, body['metacode'])

        session.failed.assert_any_call(

            reason=constants.SESSION_CLOSE_REASON_API_CALL)