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_user.present aplying changes on existing mongodb user even if Test=true #53965

6 changes: 5 additions & 1 deletion salt/states/mongodb_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ def present(name,
# fill changes if the roles and current roles differ
if not set(current_roles) == set(roles):
ret['changes'].update({name: {'database': database, 'roles': {'old': current_roles, 'new': roles}}})
ret['comment'] = 'User {0} is already present, but has new roles'.format(name)
ret['result'] = None

__salt__['mongodb.user_create'](name, passwd, user, password, host, port, database=database, authdb=authdb, roles=roles)
if not __opts__['test']:
__salt__['mongodb.user_create'](name, passwd, user, password, host, port, database=database, authdb=authdb, roles=roles)
ret['result'] = True
return ret

# if the check does not return a boolean, return an error
Expand Down
62 changes: 59 additions & 3 deletions tests/unit/states/test_mongodb_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class MongodbUserTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
return {mongodb_user: {'__opts__': {'test': True}}}

# 'present' function tests: 1
# 'present' function tests: 2

def test_present(self):
def test_present_new_user(self):
'''
Test to ensure that the user is present with the specified properties.
Test to ensure that the user is present with the specified properties for a new account.
'''
name = 'myapp'
passwd = 'password-of-myapp'
Expand Down Expand Up @@ -68,6 +68,62 @@ def test_present(self):
'changes': {name: 'Present'}})
self.assertDictEqual(mongodb_user.present(name, passwd), ret)

def test_present_existing_user(self):
'''
Test to ensure that the user is present with the specified properties for an existing account.
'''
name = 'myapp'
passwd = 'password-of-myapp'
db = 'myapp-database'
current_role_string = 'current-mongodb-role'
current_role = [current_role_string]
new_role = ['new-mongodb-role']

ret = {'name': name,
'result': False,
'comment': '',
'changes': {}}

comt = ('Port ({}) is not an integer.')
ret.update({'comment': comt})
self.assertDictEqual(mongodb_user.present(name, passwd, port={}), ret)

mock_t = MagicMock(return_value=True)
mock = MagicMock(return_value=[{'user': name, 'roles': [{'db': db, 'role': current_role_string}]}])
with patch.dict(mongodb_user.__salt__,
{
'mongodb.user_create': mock_t,
'mongodb.user_find': mock
}):
comt = ('User {0} is already present'
).format(name)
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mongodb_user.present(name, passwd, database=db, roles=current_role), ret)

with patch.dict(mongodb_user.__opts__, {'test': True}):
comt = ('User {0} is already present'
.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mongodb_user.present(name, passwd, database=db, roles=current_role), ret)

with patch.dict(mongodb_user.__opts__, {'test': False}):
comt = ('User {0} is already present'.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(mongodb_user.present(name, passwd, database=db, roles=current_role), ret)

with patch.dict(mongodb_user.__opts__, {'test': True}):
comt = ('User {0} is already present, but has new roles'
.format(name))
ret.update({'comment': comt, 'result': None,
'changes': {name: {'database': db, 'roles': {'old': current_role, 'new': new_role}}}})
self.assertDictEqual(mongodb_user.present(name, passwd, database=db, roles=new_role), ret)

with patch.dict(mongodb_user.__opts__, {'test': False}):
comt = ('User {0} is already present, but has new roles'.format(name))
ret.update({'comment': comt, 'result': True,
'changes': {name: {'database': db, 'roles': {'old': current_role, 'new': new_role}}}})
self.assertDictEqual(mongodb_user.present(name, passwd, database=db, roles=new_role), ret)

# 'absent' function tests: 1

def test_absent(self):
Expand Down