Skip to content

Commit

Permalink
Added new test for mongodb_user.present that verifies the proper func…
Browse files Browse the repository at this point in the history
…tioning when aplying state for an existing user
  • Loading branch information
dbuenor authored and waynew committed Jan 10, 2020
1 parent 04b9501 commit 49d516d
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions tests/unit/states/test_mongodb_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,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 @@ -65,6 +65,50 @@ 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 = 'mongodb-role'
current_role_as_dict = ['mongodb-role']
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}]}])
with patch.dict(mongodb_user.__salt__,
{
'mongodb.user_create': mock_t,
'mongodb.user_find': mock
}):
comt = ('User {0} is already present and should be updated if neccesary.'
).format(name)
ret.update({'comment': comt, 'result': None})
self.assertDictEqual(mongodb_user.present(name, passwd, database=db,roles=new_role), ret)

with patch.dict(mongodb_user.__opts__, {'test': True}):
comt = ('User {0} is already present and should be updated if neccesary.'
.format(name))
ret.update({'comment': comt, 'result': None})
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'.format(name))
ret.update({'comment': comt, 'result': True,
'changes': {name: {'database': db, 'roles': {'old': current_role_as_dict, '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

0 comments on commit 49d516d

Please sign in to comment.