Ejemplo n.º 1
0
    def runTest(self) -> None:
        """
        Simply create two instances of :py:class:`UserManager <arobito.controlinterface.BackendManager.SessionManager>`
        and compare them.
        """

        session_manager1 = SessionManager()
        session_manager2 = SessionManager()
        self.assertIsNotNone(session_manager1, 'Session Manager 1 is None')
        self.assertIsNotNone(session_manager2, 'Session Manager 2 is None')
        self.assertEqual(session_manager1, session_manager2,
                         'Session Manager objects are not equal')
Ejemplo n.º 2
0
    def __init__(self):
        """
        Initialize the main App with the :py:class:`SessionManager
        <arobito.controlinterface.BackendManager.SessionManager>` instance.
        """

        self.locked = True
        try:
            self.session_manager = SessionManager()
            self.locked = False
        except IOError:
            pass
Ejemplo n.º 3
0
    def __init__(self):
        """
        Initialize the main App with the :py:class:`SessionManager
        <arobito.controlinterface.BackendManager.SessionManager>` instance.
        """

        self.locked = True
        try:
            self.session_manager = SessionManager()
            self.locked = False
        except IOError:
            pass
Ejemplo n.º 4
0
class App(object):
    """
    This is the backend class for :py:class:`ControllerFrontend.App <arobito.controlinterface.ControllerFrontend.App>`.
    """

    #: The default response when authorization fails.
    auth_default_response = dict(
        auth=dict(success=False,
                  status='failed',
                  reason='User unknown or password wrong'))

    def __init__(self):
        """
        Initialize the main App with the :py:class:`SessionManager
        <arobito.controlinterface.BackendManager.SessionManager>` instance.
        """

        self.locked = True
        try:
            self.session_manager = SessionManager()
            self.locked = False
        except IOError:
            pass

    def auth(self, json_req: dict) -> dict:
        """
        Backend method for :py:meth:`ControllerFrontend.App.auth <.ControllerFrontend.App.auth>`

        :param json_req: The JSON request dict
        :return: Response as dictionary
        """

        if self.locked:
            return App.auth_default_response

        if json_req is None:
            raise ValueError('json_req cannot be None')
        if not isinstance(json_req, dict):
            raise ValueError('json_req must be a dict')

        username = None
        password = None
        if 'username' in json_req:
            username = json_req['username']
        if 'password' in json_req:
            password = json_req['password']
        if username is None or password is None:
            return App.auth_default_response
        key = self.session_manager.login(username, password)
        if key is None:
            return App.auth_default_response
        return dict(
            auth=dict(success=True, status='Login successful', key=key))

    def logout(self, json_req: dict) -> dict:
        """
        Backend method for :py:meth:`ControllerFrontend.App.logout <.ControllerFrontend.App.logout>`

        :param json_req: The JSON request dict
        :return: Response as dictionary
        """

        if json_req is None:
            raise ValueError('json_req cannot be None')
        if not isinstance(json_req, dict):
            raise ValueError('json_req must be a dict')

        if 'key' in json_req:
            self.session_manager.logout(json_req['key'])
        return dict(logout=True)

    def shutdown(self, json_req: dict) -> dict:
        """
        Backend method for :py:meth:`ControllerFrontend.App.shutdown <.ControllerFrontend.App.shutdown>`

        :param json_req: The JSON request dict
        :return: Response as dictionary
        """

        if json_req is None:
            raise ValueError('json_req cannot be None')
        if not isinstance(json_req, dict):
            raise ValueError('json_req must be a dict')

        if not 'key' in json_req:
            return dict(shutdown=False)
        user = self.session_manager.get_user(json_req['key'])
        if user is None:
            return dict(shutdown=False)
        if user['level'] == 'Administrator':
            Helper.shutdown(delay=10)
            return dict(shutdown=True)
        else:
            return dict(shutdown=False)

    def get_session_count(self, json_req: dict) -> dict:
        """
        Backend method for :py:meth:`ControllerFrontend.App.get_session_count
        <.ControllerFrontend.App.get_session_count>`

        :param json_req: The JSON request dict
        :return: Response as dictionary
        """

        if json_req is None:
            raise ValueError('json_req cannot be None')
        if not isinstance(json_req, dict):
            raise ValueError('json_req must be a dict')

        if not 'key' in json_req:
            return dict(session_count=-1)
        user = self.session_manager.get_user(json_req['key'])
        if user is None:
            return dict(session_count=-1)
        if user['level'] == 'Administrator':
            return dict(
                session_count=self.session_manager.get_current_sessions())
        else:
            return dict(session_count=-1)
Ejemplo n.º 5
0
    def runTest(self) -> None:
        """
        Simulate a complete workflow through the class' methods
        """

        session_manager = SessionManager()
        self.assertIsNotNone(session_manager, 'Session Manager is None')

        # There should be no active session
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 0)

        # Correct credentials
        key_correct = session_manager.login('arobito', 'arobito')
        self.assertIsNotNone(key_correct, 'Key is None')
        self.assertIsInstance(key_correct, str, 'Key is not a String')
        self.assertRegex(key_correct, '^[a-zA-Z0-9]{64}$',
                         'Key does not match expectations')

        # Count should be 1 now
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Incorrect credentials
        key_invalid = session_manager.login('arobito', 'wrong_password')
        self.assertIsNone(key_invalid, 'Invalid key produced')

        # Count should be still 1
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Call cleanup explicitly
        session_manager.cleanup()

        # Count should be still 1
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Try logout - with invalid key
        session_manager.logout('invalid_key')

        # Count should be still 1
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Logout with the working key
        session_manager.logout(key_correct)

        # Count should now be 0
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 0)

        # Login loop
        key_list = list()
        for i in range(0, 1000):
            key = session_manager.login('arobito', 'arobito')
            self.assertIsNotNone(key, 'Key in loop run {:d} is None'.format(i))
            self.assertIsInstance(
                key, str, 'Key in loop run {:d} is not a String'.format(i))
            self.assertRegex(
                key, '^[a-zA-Z0-9]{64}$',
                'Key in loop run does not match expectations'.format(i))
            key_list.append(key)
            session_count = session_manager.get_current_sessions()
            self.__check_count(session_count, i + 1)

        self.assertEqual(len(key_list), 1000,
                         'Key list is not of the size expected')

        # Logout loop
        for i in range(1000, 0, -1):
            session_manager.logout(key_list.pop())
            session_count = session_manager.get_current_sessions()
            self.__check_count(session_count, i - 1)

        self.assertEqual(len(key_list), 0,
                         'Key list is not of the size expected')

        # Count should now be 0
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 0)

        # Create a key for the next tests (redundant to the tests above)
        key = session_manager.login('arobito', 'arobito')
        self.assertIsNotNone(key, 'Key is None')
        self.assertIsInstance(key, str, 'Key is not a String')
        self.assertRegex(key, '^[a-zA-Z0-9]{64}$',
                         'Key does not match expectations')

        # Try to get an invalid user
        user_object = session_manager.get_user('invalid_session')
        self.assertIsNone(user_object, 'Invalid user fetched')

        # Get a user object from a session
        user_object = session_manager.get_user(key)
        self.assertIsNotNone(user_object, 'User object is None')
        self.assertIsInstance(user_object, dict, 'User object is not a dict')

        # Count should now be 1
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Evaluate the user object returned
        evaluate_user_object(self, user_object)

        session_manager.logout(key)
Ejemplo n.º 6
0
    def runTest(self) -> None:
        """
        Simulate a complete workflow through the class' methods
        """

        session_manager = SessionManager()
        self.assertIsNotNone(session_manager, "Session Manager is None")

        # There should be no active session
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 0)

        # Correct credentials
        key_correct = session_manager.login("arobito", "arobito")
        self.assertIsNotNone(key_correct, "Key is None")
        self.assertIsInstance(key_correct, str, "Key is not a String")
        self.assertRegex(key_correct, "^[a-zA-Z0-9]{64}$", "Key does not match expectations")

        # Count should be 1 now
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Incorrect credentials
        key_invalid = session_manager.login("arobito", "wrong_password")
        self.assertIsNone(key_invalid, "Invalid key produced")

        # Count should be still 1
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Call cleanup explicitly
        session_manager.cleanup()

        # Count should be still 1
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Try logout - with invalid key
        session_manager.logout("invalid_key")

        # Count should be still 1
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Logout with the working key
        session_manager.logout(key_correct)

        # Count should now be 0
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 0)

        # Login loop
        key_list = list()
        for i in range(0, 1000):
            key = session_manager.login("arobito", "arobito")
            self.assertIsNotNone(key, "Key in loop run {:d} is None".format(i))
            self.assertIsInstance(key, str, "Key in loop run {:d} is not a String".format(i))
            self.assertRegex(key, "^[a-zA-Z0-9]{64}$", "Key in loop run does not match expectations".format(i))
            key_list.append(key)
            session_count = session_manager.get_current_sessions()
            self.__check_count(session_count, i + 1)

        self.assertEqual(len(key_list), 1000, "Key list is not of the size expected")

        # Logout loop
        for i in range(1000, 0, -1):
            session_manager.logout(key_list.pop())
            session_count = session_manager.get_current_sessions()
            self.__check_count(session_count, i - 1)

        self.assertEqual(len(key_list), 0, "Key list is not of the size expected")

        # Count should now be 0
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 0)

        # Create a key for the next tests (redundant to the tests above)
        key = session_manager.login("arobito", "arobito")
        self.assertIsNotNone(key, "Key is None")
        self.assertIsInstance(key, str, "Key is not a String")
        self.assertRegex(key, "^[a-zA-Z0-9]{64}$", "Key does not match expectations")

        # Try to get an invalid user
        user_object = session_manager.get_user("invalid_session")
        self.assertIsNone(user_object, "Invalid user fetched")

        # Get a user object from a session
        user_object = session_manager.get_user(key)
        self.assertIsNotNone(user_object, "User object is None")
        self.assertIsInstance(user_object, dict, "User object is not a dict")

        # Count should now be 1
        session_count = session_manager.get_current_sessions()
        self.__check_count(session_count, 1)

        # Evaluate the user object returned
        evaluate_user_object(self, user_object)

        session_manager.logout(key)
Ejemplo n.º 7
0
class App(object):
    """
    This is the backend class for :py:class:`ControllerFrontend.App <arobito.controlinterface.ControllerFrontend.App>`.
    """

    #: The default response when authorization fails.
    auth_default_response = dict(auth=dict(success=False, status='failed', reason='User unknown or password wrong'))

    def __init__(self):
        """
        Initialize the main App with the :py:class:`SessionManager
        <arobito.controlinterface.BackendManager.SessionManager>` instance.
        """

        self.locked = True
        try:
            self.session_manager = SessionManager()
            self.locked = False
        except IOError:
            pass

    def auth(self, json_req: dict) -> dict:
        """
        Backend method for :py:meth:`ControllerFrontend.App.auth <.ControllerFrontend.App.auth>`

        :param json_req: The JSON request dict
        :return: Response as dictionary
        """

        if self.locked:
            return App.auth_default_response

        if json_req is None:
            raise ValueError('json_req cannot be None')
        if not isinstance(json_req, dict):
            raise ValueError('json_req must be a dict')

        username = None
        password = None
        if 'username' in json_req:
            username = json_req['username']
        if 'password' in json_req:
            password = json_req['password']
        if username is None or password is None:
            return App.auth_default_response
        key = self.session_manager.login(username, password)
        if key is None:
            return App.auth_default_response
        return dict(auth=dict(success=True, status='Login successful', key=key))

    def logout(self, json_req: dict) -> dict:
        """
        Backend method for :py:meth:`ControllerFrontend.App.logout <.ControllerFrontend.App.logout>`

        :param json_req: The JSON request dict
        :return: Response as dictionary
        """

        if json_req is None:
            raise ValueError('json_req cannot be None')
        if not isinstance(json_req, dict):
            raise ValueError('json_req must be a dict')

        if 'key' in json_req:
            self.session_manager.logout(json_req['key'])
        return dict(logout=True)

    def shutdown(self, json_req: dict) -> dict:
        """
        Backend method for :py:meth:`ControllerFrontend.App.shutdown <.ControllerFrontend.App.shutdown>`

        :param json_req: The JSON request dict
        :return: Response as dictionary
        """

        if json_req is None:
            raise ValueError('json_req cannot be None')
        if not isinstance(json_req, dict):
            raise ValueError('json_req must be a dict')

        if not 'key' in json_req:
            return dict(shutdown=False)
        user = self.session_manager.get_user(json_req['key'])
        if user is None:
            return dict(shutdown=False)
        if user['level'] == 'Administrator':
            Helper.shutdown(delay=10)
            return dict(shutdown=True)
        else:
            return dict(shutdown=False)

    def get_session_count(self, json_req: dict) -> dict:
        """
        Backend method for :py:meth:`ControllerFrontend.App.get_session_count
        <.ControllerFrontend.App.get_session_count>`

        :param json_req: The JSON request dict
        :return: Response as dictionary
        """

        if json_req is None:
            raise ValueError('json_req cannot be None')
        if not isinstance(json_req, dict):
            raise ValueError('json_req must be a dict')

        if not 'key' in json_req:
            return dict(session_count=-1)
        user = self.session_manager.get_user(json_req['key'])
        if user is None:
            return dict(session_count=-1)
        if user['level'] == 'Administrator':
            return dict(session_count=self.session_manager.get_current_sessions())
        else:
            return dict(session_count=-1)