Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb: return False when user_create fails #60078

1 change: 1 addition & 0 deletions changelog/53377.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When user_create or user_remove fail, return False instead of returning the error.
5 changes: 2 additions & 3 deletions salt/modules/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down
52 changes: 52 additions & 0 deletions tests/pytests/unit/modules/test_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down