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_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_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_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_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))
def __read_user_by_user_id(token: str, user_id: int) -> dict: """ Reads current user's information :param token: Session token which contains the current user's information :param user_id: ID of the user to read :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_id( connection, user_id) if user is None: success = False error_code = 400 error_message = "Invalid user ID" 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 __read_user_by_user_id(token: str, user_id: int) -> dict: """ Reads current user's information :param token: Session token which contains the current user's information :param user_id: ID of the user to read :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_id(connection, user_id) if user is None: success = False error_code = 400 error_message = "Invalid user ID" 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_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_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 _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_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 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))