diff --git a/changelog/53377.fixed b/changelog/53377.fixed new file mode 100644 index 000000000000..d1eac3cfff3e --- /dev/null +++ b/changelog/53377.fixed @@ -0,0 +1 @@ +When user_create or user_remove fail, return False instead of returning the error. diff --git a/salt/modules/mongodb.py b/salt/modules/mongodb.py index 8c01ec869cc3..5e75935b3540 100644 --- a/salt/modules/mongodb.py +++ b/salt/modules/mongodb.py @@ -455,7 +455,7 @@ def user_create( except pymongo.errors.PyMongoError as err: log.error("Creating user %s failed with error: %s", name, err) - return str(err) + return False return True @@ -499,8 +499,7 @@ def user_remove( mdb.command("dropUser", name) except pymongo.errors.PyMongoError as err: log.error("Removing user %s failed with error: %s", name, err) - return str(err) - + return False return True diff --git a/tests/pytests/unit/modules/test_mongodb.py b/tests/pytests/unit/modules/test_mongodb.py index 51249fe61772..b8b011dfdac9 100644 --- a/tests/pytests/unit/modules/test_mongodb.py +++ b/tests/pytests/unit/modules/test_mongodb.py @@ -323,6 +323,32 @@ def test_user_create(): assert ret +def test_user_create_exception(): + """ + Test mongodb.user_create + """ + user_create_mock = MagicMock(side_effect=pymongo.errors.PyMongoError) + + pymongo_database_mock = MagicMock(autospec=True, return_value=MockPyMongoDatabase()) + mongodb_client_mock = MagicMock(autospec=True, return_value=MockMongoConnect()) + config_option_mock = MagicMock( + autospec=True, side_effect=["user", "password", "localhost", "27017"] + ) + + patch_pymongo_database = patch("pymongo.database.Database", pymongo_database_mock) + patch_mongo_client = patch("pymongo.MongoClient", mongodb_client_mock) + patch_pymongo_command = patch.object( + MockPyMongoDatabase, "command", user_create_mock + ) + patch_salt_dict = patch.dict( + mongodb.__salt__, {"config.option": config_option_mock} + ) + + with patch_mongo_client, patch_pymongo_database, patch_salt_dict, patch_pymongo_command: + ret = mongodb.user_create("test_user", "test_password") + assert not ret + + def test_user_remove(): """ Test mongodb.user_remove @@ -349,6 +375,32 @@ def test_user_remove(): assert ret +def test_user_remove_exception(): + """ + Test mongodb.user_remove + """ + user_remove_mock = MagicMock(autospec=True, side_effect=pymongo.errors.PyMongoError) + + pymongo_database_mock = MagicMock(autospec=True, return_value=MockPyMongoDatabase()) + mongodb_client_mock = MagicMock(autospec=True, return_value=MockMongoConnect()) + config_option_mock = MagicMock( + autospec=True, side_effect=["user", "password", "localhost", "27017"] + ) + + patch_pymongo_database = patch("pymongo.database.Database", pymongo_database_mock) + patch_mongo_client = patch("pymongo.MongoClient", mongodb_client_mock) + patch_pymongo_command = patch.object( + MockPyMongoDatabase, "command", user_remove_mock + ) + patch_salt_dict = patch.dict( + mongodb.__salt__, {"config.option": config_option_mock} + ) + + with patch_mongo_client, patch_pymongo_database, patch_salt_dict, patch_pymongo_command: + ret = mongodb.user_remove("test_user") + assert not ret + + def test_user_roles_exists(): """ Test mongodb.user_roles_exist