def test_update_email(self):
        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        self.assertTrue(UserManagementInterface.update_user_information(user_id2,
                                                                        user2["user_name"],
                                                                        user2["display_name"],
                                                                        "*****@*****.**",
                                                                        user2["active"]))

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)
    def test_deactivate_activate_user(self):
        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        # Deactivate user
        self.assertTrue(UserManagementInterface.deactivate_user(user_id2))

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], False)

        # Activate user
        self.assertTrue(UserManagementInterface.activate_user(user_id2))

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)
    def test_create_user(self):
        # Positive tests ---------------------------------------------------------------------------
        self.assertIsNotNone(self.create_user_test1())

        # Negative tests ---------------------------------------------------------------------------
        # Try to create a user with an invalid user name
        self.assertIsNone(
            UserManagementInterface.create_user("", "Test Other",
                                                "*****@*****.**", "basic",
                                                {"password": "******"}))

        # Try to create a user with the same user name as another user
        self.assertIsNone(
            UserManagementInterface.create_user("test1", "Test Other",
                                                "*****@*****.**", "basic",
                                                {"password": "******"}))

        # Try to create a user with an invalid display name: "test123"}))

        self.assertIsNone(
            UserManagementInterface.create_user("test_other", "",
                                                "*****@*****.**", "basic",
                                                {"password": "******"}))

        # Try to create a user with the same display name as another user
        self.assertIsNone(
            UserManagementInterface.create_user("test_other", "Test 1",
                                                "*****@*****.**", "basic",
                                                {"password": "******"}))

        # Try to create a user with an invalid authentication type
        self.assertIsNone(
            UserManagementInterface.create_user("test_other", "Test Other",
                                                "*****@*****.**", "",
                                                {"password": "******"}))
    def test_read_all_user_ids(self):
        # Create users
        user_id1 = self.create_user_test1()
        self.assertIsNotNone(user_id1)

        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        # Check active users
        user_ids = UserManagementInterface.read_all_user_ids(UserSelection.Active)

        self.assertEqual(len(user_ids), 3)
        self.assertListEqual(user_ids, [self.__admin_user_id, user_id1, user_id2])

        # Deactivate user
        self.assertTrue(UserManagementInterface.deactivate_user(user_id1))

        # Recheck active users
        user_ids = UserManagementInterface.read_all_user_ids(UserSelection.Active)

        self.assertEqual(len(user_ids), 2)
        self.assertListEqual(user_ids, [self.__admin_user_id, user_id2])

        # Check inactive users
        user_ids = UserManagementInterface.read_all_user_ids(UserSelection.Inactive)

        self.assertEqual(len(user_ids), 1)
        self.assertListEqual(user_ids, [user_id1])

        # Check all users
        user_ids = UserManagementInterface.read_all_user_ids(UserSelection.All)

        self.assertEqual(len(user_ids), 3)
        self.assertListEqual(user_ids, [self.__admin_user_id, user_id1, user_id2])
    def test_update_email(self):
        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        self.assertTrue(
            UserManagementInterface.update_user_information(
                user_id2, user2["user_name"], user2["display_name"],
                "*****@*****.**", user2["active"]))

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)
    def test_deactivate_activate_user(self):
        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        # Deactivate user
        self.assertTrue(UserManagementInterface.deactivate_user(user_id2))

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], False)

        # Activate user
        self.assertTrue(UserManagementInterface.activate_user(user_id2))

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)
Пример #7
0
    def post(self):
        """
        Logs out the user
        """
        # Extract session token from the request
        token = self._read_session_token()

        # Log out
        success = False
        error_code = None
        error_message = None

        connection = DatabaseInterface.create_connection()

        try:
            success = connection.begin_transaction()

            # Get the session token
            session_token = None

            if success:
                session_token = UserManagementInterface.read_session_token(connection, token)

                if session_token is None:
                    success = False
                    error_code = 400
                    error_message = "Invalid session token"

            # Delete session token
            if success:
                success = UserManagementInterface.delete_session_token(connection, token)

                if not success:
                    error_code = 500
                    error_message = "Failed to log out, please try again"

            if success:
                connection.commit_transaction()
            else:
                connection.rollback_transaction()
        except:
            connection.rollback_transaction()
            abort(500, message="Internal error, please try again")

        # Return response
        if success:
            return None
        else:
            if (error_code is not None) and (error_message is not None):
                abort(error_code, message=error_message)
            else:
                abort(500, message="Internal error")
    def test_update_user_authentication(self):
        user_id1 = self.create_user_test1()
        self.assertIsNotNone(user_id1)

        self.assertTrue(UserManagementInterface.authenticate_user("test1",
                                                                  {"password": "******"}))

        # Positive tests ---------------------------------------------------------------------------
        self.assertTrue(UserManagementInterface.update_user_authentication(user_id1,
                                                                           "basic",
                                                                           {"password": "******"}))

        self.assertTrue(UserManagementInterface.authenticate_user("test1",
                                                                  {"password": "******"}))
        self.assertFalse(UserManagementInterface.authenticate_user("test1",
                                                                   {"password": "******"}))

        # Negative tests ---------------------------------------------------------------------------
        self.assertFalse(UserManagementInterface.update_user_authentication(user_id1,
                                                                            "",
                                                                            {"password": "******"}))
        self.assertFalse(UserManagementInterface.update_user_authentication(user_id1,
                                                                            "some_type",
                                                                            {"password": "******"}))

        self.assertTrue(UserManagementInterface.authenticate_user("test1",
                                                                  {"password": "******"}))
        self.assertFalse(UserManagementInterface.authenticate_user("test1",
                                                                   {"password": "******"}))
    def test_update_user_authentication(self):
        user_id1 = self.create_user_test1()
        self.assertIsNotNone(user_id1)

        self.assertTrue(
            UserManagementInterface.authenticate_user("test1",
                                                      {"password": "******"}))

        # Positive tests ---------------------------------------------------------------------------
        self.assertTrue(
            UserManagementInterface.update_user_authentication(
                user_id1, "basic", {"password": "******"}))

        self.assertTrue(
            UserManagementInterface.authenticate_user("test1",
                                                      {"password": "******"}))
        self.assertFalse(
            UserManagementInterface.authenticate_user("test1",
                                                      {"password": "******"}))

        # Negative tests ---------------------------------------------------------------------------
        self.assertFalse(
            UserManagementInterface.update_user_authentication(
                user_id1, "", {"password": "******"}))
        self.assertFalse(
            UserManagementInterface.update_user_authentication(
                user_id1, "some_type", {"password": "******"}))

        self.assertTrue(
            UserManagementInterface.authenticate_user("test1",
                                                      {"password": "******"}))
        self.assertFalse(
            UserManagementInterface.authenticate_user("test1",
                                                      {"password": "******"}))
    def test_authenticate_user(self):
        self.assertIsNotNone(self.create_user_test1())
        self.assertIsNotNone(self.create_user_test2())

        # Positive tests ---------------------------------------------------------------------------
        self.assertTrue(UserManagementInterface.authenticate_user("test1",
                                                                  {"password": "******"}))

        self.assertTrue(UserManagementInterface.authenticate_user("test2",
                                                                  {"password": "******"}))

        # Negative tests ---------------------------------------------------------------------------
        self.assertFalse(UserManagementInterface.authenticate_user("",
                                                                   {"password": "******"}))

        self.assertFalse(UserManagementInterface.authenticate_user("test1",
                                                                   {"password": "******"}))

        self.assertFalse(UserManagementInterface.authenticate_user("test1",
                                                                   {"password": "******"}))

        self.assertFalse(UserManagementInterface.authenticate_user("test1",
                                                                   {"password": ""}))

        self.assertFalse(UserManagementInterface.authenticate_user("test2",
                                                                   {"password": "******"}))

        self.assertFalse(UserManagementInterface.authenticate_user("test2",
                                                                   {"password": "******"}))
    def test_read_user_by_id(self):
        user_id = self.create_user_test1()
        self.assertIsNotNone(user_id)

        # Positive tests ---------------------------------------------------------------------------
        user = UserManagementInterface.read_user_by_id(user_id)

        self.assertEqual(user["id"], user_id)
        self.assertEqual(user["user_name"], "test1")
        self.assertEqual(user["display_name"], "Test 1")
        self.assertEqual(user["email"], "*****@*****.**")
        self.assertEqual(user["active"], True)

        # Negative tests ---------------------------------------------------------------------------
        self.assertIsNone(UserManagementInterface.read_user_by_id(999))
    def test_read_user_by_id(self):
        user_id = self.create_user_test1()
        self.assertIsNotNone(user_id)

        # Positive tests ---------------------------------------------------------------------------
        user = UserManagementInterface.read_user_by_id(user_id)

        self.assertEqual(user["id"], user_id)
        self.assertEqual(user["user_name"], "test1")
        self.assertEqual(user["display_name"], "Test 1")
        self.assertEqual(user["email"], "*****@*****.**")
        self.assertEqual(user["active"], True)

        # Negative tests ---------------------------------------------------------------------------
        self.assertIsNone(UserManagementInterface.read_user_by_id(999))
Пример #13
0
    def __read_user_by_display_name(token: str, display_name: str) -> dict:
        """
        Reads current user's information

        :param token:           Session token which contains the current user's information
        :param display_name:    Display name

        :return:    User information object

        Returned dictionary contains items:

        - id
        - user_name
        - display_name
        - email
        - active
        """
        user = None
        success = False
        error_code = None
        error_message = None

        connection = DatabaseInterface.create_connection()

        try:
            success = connection.begin_transaction()

            # Extract session user
            if success:
                session_user = RestrictedResource._read_session_user(
                    connection, token)

                if session_user is None:
                    success = False
                    error_code = 400
                    error_message = "Invalid session token"

            # Read requested user
            if success:
                user = UserManagementInterface.read_user_by_display_name(
                    connection, display_name)

                if user is None:
                    success = False
                    error_code = 400
                    error_message = "Invalid user name"

            connection.rollback_transaction()
        except:
            connection.rollback_transaction()
            abort(500, message="Internal error, please try again")

        # Return user
        if success:
            return jsonify(user)
        else:
            if (error_code is not None) and (error_message is not None):
                abort(error_code, message=error_message)
            else:
                abort(500, message="Internal error")
 def create_user_test2(self):
     user_id = UserManagementInterface.create_user("test2",
                                                   "Test 2",
                                                   "*****@*****.**",
                                                   "basic",
                                                   {"password": "******"})
     return user_id
Пример #15
0
    def __read_user_by_display_name(token: str, display_name: str) -> dict:
        """
        Reads current user's information

        :param token:           Session token which contains the current user's information
        :param display_name:    Display name

        :return:    User information object

        Returned dictionary contains items:

        - id
        - user_name
        - display_name
        - email
        - active
        """
        user = None
        success = False
        error_code = None
        error_message = None

        connection = DatabaseInterface.create_connection()

        try:
            success = connection.begin_transaction()

            # Extract session user
            if success:
                session_user = RestrictedResource._read_session_user(connection, token)

                if session_user is None:
                    success = False
                    error_code = 400
                    error_message = "Invalid session token"

            # Read requested user
            if success:
                user = UserManagementInterface.read_user_by_display_name(connection, display_name)

                if user is None:
                    success = False
                    error_code = 400
                    error_message = "Invalid user name"

            connection.rollback_transaction()
        except:
            connection.rollback_transaction()
            abort(500, message="Internal error, please try again")

        # Return user
        if success:
            return jsonify(user)
        else:
            if (error_code is not None) and (error_message is not None):
                abort(error_code, message=error_message)
            else:
                abort(500, message="Internal error")
    def test_default_administrator(self):
        user = UserManagementInterface.read_user_by_id(self.__admin_user_id)

        self.assertIsNotNone(user)
        self.assertEqual(user["id"], self.__admin_user_id)
        self.assertEqual(user["user_name"], "administrator")
        self.assertEqual(user["display_name"], "Administrator")
        self.assertEqual(user["email"], "")
        self.assertEqual(user["active"], True)
    def test_update_user_invalid_user_id(self):
        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        # Try to update a user with a reference to a non-existing user ID
        self.assertFalse(
            UserManagementInterface.update_user_information(
                999, user2["user_name"], user2["display_name"], user2["email"],
                user2["active"]))
    def test_default_administrator(self):
        user = UserManagementInterface.read_user_by_id(self.__admin_user_id)

        self.assertIsNotNone(user)
        self.assertEqual(user["id"], self.__admin_user_id)
        self.assertEqual(user["user_name"], "administrator")
        self.assertEqual(user["display_name"], "Administrator")
        self.assertEqual(user["email"], "")
        self.assertEqual(user["active"], True)
    def test_update_user_invalid_user_id(self):
        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        # Try to update a user with a reference to a non-existing user ID
        self.assertFalse(UserManagementInterface.update_user_information(999,
                                                                         user2["user_name"],
                                                                         user2["display_name"],
                                                                         user2["email"],
                                                                         user2["active"]))
    def test_read_user_authentication(self):
        user_id = self.create_user_test1()
        self.assertIsNotNone(user_id)

        user = UserManagementInterface.read_user_by_id(user_id)

        self.assertEqual(user["id"], user_id)
        self.assertEqual(user["user_name"], "test1")
        self.assertEqual(user["display_name"], "Test 1")
        self.assertEqual(user["email"], "*****@*****.**")
        self.assertEqual(user["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        user_authentication = UserManagementInterface.read_user_authentication(user_id)

        self.assertEqual(user_authentication["authentication_type"], "basic")
        self.assertTrue(len(user_authentication["authentication_parameters"]["password_hash"]) > 0)

        # Negative tests ---------------------------------------------------------------------------
        self.assertFalse(UserManagementInterface.read_user_authentication(999))
    def _read_session_user(connection: Connection,
                           token: str) -> Optional[dict]:
        """
        Reads the user information that belongs to the session

        :param connection:  Database connection
        :param token:       Session token

        :return:    User information object

        Returned dictionary contains items:

        - id
        - user_name
        - display_name
        - email
        - active

        Note:   User information is returned only if the user exists and if it is active
        """
        # Read session token
        session_token = UserManagementInterface.read_session_token(
            connection, token)

        if session_token is None:
            # Error, invalid token
            return None

        # Check if session's user is active
        user = UserManagementInterface.read_user_by_id(
            connection, session_token["user_id"])

        if user is None:
            # Error, user was not found
            return None

        if not user["active"]:
            # Error, user is not active
            return None

        return user
    def test_create_user(self):
        # Positive tests ---------------------------------------------------------------------------
        self.assertIsNotNone(self.create_user_test1())

        # Negative tests ---------------------------------------------------------------------------
        # Try to create a user with an invalid user name
        self.assertIsNone(UserManagementInterface.create_user("",
                                                              "Test Other",
                                                              "*****@*****.**",
                                                              "basic",
                                                              {"password": "******"}))

        # Try to create a user with the same user name as another user
        self.assertIsNone(UserManagementInterface.create_user("test1",
                                                              "Test Other",
                                                              "*****@*****.**",
                                                              "basic",
                                                              {"password": "******"}))

        # Try to create a user with an invalid display name: "test123"}))

        self.assertIsNone(UserManagementInterface.create_user("test_other",
                                                              "",
                                                              "*****@*****.**",
                                                              "basic",
                                                              {"password": "******"}))

        # Try to create a user with the same display name as another user
        self.assertIsNone(UserManagementInterface.create_user("test_other",
                                                              "Test 1",
                                                              "*****@*****.**",
                                                              "basic",
                                                              {"password": "******"}))

        # Try to create a user with an invalid authentication type
        self.assertIsNone(UserManagementInterface.create_user("test_other",
                                                              "Test Other",
                                                              "*****@*****.**",
                                                              "",
                                                              {"password": "******"}))
    def test_read_all_user_ids(self):
        # Create users
        user_id1 = self.create_user_test1()
        self.assertIsNotNone(user_id1)

        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        # Check active users
        user_ids = UserManagementInterface.read_all_user_ids(
            UserSelection.Active)

        self.assertEqual(len(user_ids), 3)
        self.assertListEqual(user_ids,
                             [self.__admin_user_id, user_id1, user_id2])

        # Deactivate user
        self.assertTrue(UserManagementInterface.deactivate_user(user_id1))

        # Recheck active users
        user_ids = UserManagementInterface.read_all_user_ids(
            UserSelection.Active)

        self.assertEqual(len(user_ids), 2)
        self.assertListEqual(user_ids, [self.__admin_user_id, user_id2])

        # Check inactive users
        user_ids = UserManagementInterface.read_all_user_ids(
            UserSelection.Inactive)

        self.assertEqual(len(user_ids), 1)
        self.assertListEqual(user_ids, [user_id1])

        # Check all users
        user_ids = UserManagementInterface.read_all_user_ids(UserSelection.All)

        self.assertEqual(len(user_ids), 3)
        self.assertListEqual(user_ids,
                             [self.__admin_user_id, user_id1, user_id2])
    def test_reads_user_by_user_name(self):
        # Create a user and then deactivate it and create a user with the same user name
        user_id1 = self.create_user_test1()
        self.assertIsNotNone(user_id1)

        user1 = UserManagementInterface.read_user_by_id(user_id1)
        self.assertIsNotNone(user1)

        self.assertTrue(UserManagementInterface.deactivate_user(user_id1))

        user_id2 = self.create_user_test1()
        self.assertIsNotNone(user_id2)

        # Positive tests ---------------------------------------------------------------------------
        users = UserManagementInterface.read_users_by_user_name("test1")
        self.assertEqual(len(users), 2)

        user1 = users[0]
        user2 = users[1]

        self.assertEqual(user1["id"], user_id1)
        self.assertEqual(user1["user_name"], "test1")
        self.assertEqual(user1["display_name"], "Test 1")
        self.assertEqual(user1["email"], "*****@*****.**")
        self.assertEqual(user1["active"], False)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test1")
        self.assertEqual(user2["display_name"], "Test 1")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Negative tests ---------------------------------------------------------------------------
        users = UserManagementInterface.read_users_by_user_name("")
        self.assertEqual(len(users), 0)

        users = UserManagementInterface.read_users_by_user_name("test999")
        self.assertEqual(len(users), 0)
    def test_reads_user_by_user_name(self):
        # Create a user and then deactivate it and create a user with the same user name
        user_id1 = self.create_user_test1()
        self.assertIsNotNone(user_id1)

        user1 = UserManagementInterface.read_user_by_id(user_id1)
        self.assertIsNotNone(user1)

        self.assertTrue(UserManagementInterface.deactivate_user(user_id1))

        user_id2 = self.create_user_test1()
        self.assertIsNotNone(user_id2)

        # Positive tests ---------------------------------------------------------------------------
        users = UserManagementInterface.read_users_by_user_name("test1")
        self.assertEqual(len(users), 2)

        user1 = users[0]
        user2 = users[1]

        self.assertEqual(user1["id"], user_id1)
        self.assertEqual(user1["user_name"], "test1")
        self.assertEqual(user1["display_name"], "Test 1")
        self.assertEqual(user1["email"], "*****@*****.**")
        self.assertEqual(user1["active"], False)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test1")
        self.assertEqual(user2["display_name"], "Test 1")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Negative tests ---------------------------------------------------------------------------
        users = UserManagementInterface.read_users_by_user_name("")
        self.assertEqual(len(users), 0)

        users = UserManagementInterface.read_users_by_user_name("test999")
        self.assertEqual(len(users), 0)
    def _read_session_user(connection: Connection, token: str) -> Optional[dict]:
        """
        Reads the user information that belongs to the session

        :param connection:  Database connection
        :param token:       Session token

        :return:    User information object

        Returned dictionary contains items:

        - id
        - user_name
        - display_name
        - email
        - active

        Note:   User information is returned only if the user exists and if it is active
        """
        # Read session token
        session_token = UserManagementInterface.read_session_token(connection, token)

        if session_token is None:
            # Error, invalid token
            return None

        # Check if session's user is active
        user = UserManagementInterface.read_user_by_id(connection, session_token["user_id"])

        if user is None:
            # Error, user was not found
            return None

        if not user["active"]:
            # Error, user is not active
            return None

        return user
    def test_read_user_authentication(self):
        user_id = self.create_user_test1()
        self.assertIsNotNone(user_id)

        user = UserManagementInterface.read_user_by_id(user_id)

        self.assertEqual(user["id"], user_id)
        self.assertEqual(user["user_name"], "test1")
        self.assertEqual(user["display_name"], "Test 1")
        self.assertEqual(user["email"], "*****@*****.**")
        self.assertEqual(user["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        user_authentication = UserManagementInterface.read_user_authentication(
            user_id)

        self.assertEqual(user_authentication["authentication_type"], "basic")
        self.assertTrue(
            len(user_authentication["authentication_parameters"]
                ["password_hash"]) > 0)

        # Negative tests ---------------------------------------------------------------------------
        self.assertFalse(UserManagementInterface.read_user_authentication(999))
    def test_update_display_name(self):
        user_id1 = self.create_user_test1()
        self.assertIsNotNone(user_id1)

        user1 = UserManagementInterface.read_user_by_id(user_id1)

        self.assertEqual(user1["id"], user_id1)
        self.assertEqual(user1["user_name"], "test1")
        self.assertEqual(user1["display_name"], "Test 1")
        self.assertEqual(user1["email"], "*****@*****.**")
        self.assertEqual(user1["active"], True)

        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        self.assertTrue(UserManagementInterface.update_user_information(user_id2,
                                                                        user2["user_name"],
                                                                        "Test New",
                                                                        user2["email"],
                                                                        user2["active"]))

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test New")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Negative tests ---------------------------------------------------------------------------
        # Try to update a user with an invalid display name
        self.assertFalse(UserManagementInterface.update_user_information(user_id2,
                                                                         user2["user_name"],
                                                                         "",
                                                                         user2["email"],
                                                                         user2["active"]))

        # Try to update a user with the same display name as another user
        self.assertFalse(UserManagementInterface.update_user_information(user_id2,
                                                                         user2["user_name"],
                                                                         user1["display_name"],
                                                                         user2["email"],
                                                                         user2["active"]))
    def test_update_display_name(self):
        user_id1 = self.create_user_test1()
        self.assertIsNotNone(user_id1)

        user1 = UserManagementInterface.read_user_by_id(user_id1)

        self.assertEqual(user1["id"], user_id1)
        self.assertEqual(user1["user_name"], "test1")
        self.assertEqual(user1["display_name"], "Test 1")
        self.assertEqual(user1["email"], "*****@*****.**")
        self.assertEqual(user1["active"], True)

        user_id2 = self.create_user_test2()
        self.assertIsNotNone(user_id2)

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test 2")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Positive tests ---------------------------------------------------------------------------
        self.assertTrue(
            UserManagementInterface.update_user_information(
                user_id2, user2["user_name"], "Test New", user2["email"],
                user2["active"]))

        user2 = UserManagementInterface.read_user_by_id(user_id2)

        self.assertEqual(user2["id"], user_id2)
        self.assertEqual(user2["user_name"], "test2")
        self.assertEqual(user2["display_name"], "Test New")
        self.assertEqual(user2["email"], "*****@*****.**")
        self.assertEqual(user2["active"], True)

        # Negative tests ---------------------------------------------------------------------------
        # Try to update a user with an invalid display name
        self.assertFalse(
            UserManagementInterface.update_user_information(
                user_id2, user2["user_name"], "", user2["email"],
                user2["active"]))

        # Try to update a user with the same display name as another user
        self.assertFalse(
            UserManagementInterface.update_user_information(
                user_id2, user2["user_name"], user1["display_name"],
                user2["email"], user2["active"]))
    def test_authenticate_user(self):
        self.assertIsNotNone(self.create_user_test1())
        self.assertIsNotNone(self.create_user_test2())

        # Positive tests ---------------------------------------------------------------------------
        self.assertTrue(
            UserManagementInterface.authenticate_user("test1",
                                                      {"password": "******"}))

        self.assertTrue(
            UserManagementInterface.authenticate_user("test2",
                                                      {"password": "******"}))

        # Negative tests ---------------------------------------------------------------------------
        self.assertFalse(
            UserManagementInterface.authenticate_user("",
                                                      {"password": "******"}))

        self.assertFalse(
            UserManagementInterface.authenticate_user("test1",
                                                      {"password": "******"}))

        self.assertFalse(
            UserManagementInterface.authenticate_user("test1",
                                                      {"password": "******"}))

        self.assertFalse(
            UserManagementInterface.authenticate_user("test1",
                                                      {"password": ""}))

        self.assertFalse(
            UserManagementInterface.authenticate_user("test2",
                                                      {"password": "******"}))

        self.assertFalse(
            UserManagementInterface.authenticate_user("test2",
                                                      {"password": "******"}))
Пример #31
0
    def post(self):
        """
        Logs in a user with the specified parameters

        :return:    Session token
        """
        # Extract arguments from the request
        request_data = request.get_json()

        if ("user_name" not in request_data) or ("authentication_parameters" not in request_data):
            abort(400, message="Missing parameters")

        # Log in
        token = None
        success = False
        error_code = None
        error_message = None

        connection = DatabaseInterface.create_connection()

        try:
            success = connection.begin_transaction()

            # Authenticate the user
            user_id = None

            if success:
                user_id = UserManagementInterface.authenticate_user(
                    connection,
                    request_data["user_name"],
                    request_data["authentication_parameters"])

                if user_id is None:
                    success = False
                    error_code = 400
                    error_message = "Invalid user name or authentication parameters"

            # Create session token
            if success:
                token = UserManagementInterface.create_session_token(connection, user_id)

                if token is None:
                    success = False
                    error_code = 500
                    error_message = "Failed to generate a session token, please try again"

            if success:
                connection.commit_transaction()
            else:
                connection.rollback_transaction()
        except Exception as e:
            connection.rollback_transaction()
            abort(500, message="Internal error, please try again")

        # Return response
        if success:
            return jsonify({'session_token': token})
        else:
            if (error_code is not None) and (error_message is not None):
                abort(error_code, message=error_message)
            else:
                abort(500, message="Internal error")
 def test_default_administrator(self):
     self.assertTrue(UserManagementInterface.authenticate_user("administrator",
                                                               {"password": "******"}))
 def test_default_administrator(self):
     self.assertTrue(
         UserManagementInterface.authenticate_user(
             "administrator", {"password": "******"}))
 def create_user_test2(self):
     user_id = UserManagementInterface.create_user("test2", "Test 2",
                                                   "*****@*****.**",
                                                   "basic",
                                                   {"password": "******"})
     return user_id