Ejemplo n.º 1
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.º 2
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)