def test_present(): """ Test to ensure that the user is present with the specified properties. """ name = "myapp" passwd = "password-of-myapp" ret = {"name": name, "result": False, "comment": "", "changes": {}} comt = "Port ({}) is not an integer." ret.update({"comment": comt}) assert mongodb_user.present(name, passwd, port={}) == ret mock_t = MagicMock(return_value=True) mock_f = MagicMock(return_value=[]) with patch.dict( mongodb_user.__salt__, {"mongodb.user_create": mock_t, "mongodb.user_find": mock_f}, ): comt = ("User {} is not present and needs to be created").format(name) ret.update({"comment": comt, "result": None}) assert mongodb_user.present(name, passwd) == ret with patch.dict(mongodb_user.__opts__, {"test": True}): comt = "User {} is not present and needs to be created".format(name) ret.update({"comment": comt, "result": None}) assert mongodb_user.present(name, passwd) == ret with patch.dict(mongodb_user.__opts__, {"test": False}): comt = "User {} has been created".format(name) ret.update({"comment": comt, "result": True, "changes": {name: "Present"}}) assert mongodb_user.present(name, passwd) == ret
def test_present_new_user(self): ''' Test to ensure that the user is present with the specified properties for a new account. ''' name = 'myapp' passwd = 'password-of-myapp' 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_f = MagicMock(return_value=[]) with patch.dict(mongodb_user.__salt__, { 'mongodb.user_create': mock_t, 'mongodb.user_find': mock_f }): comt = ( 'User {0} is not present and needs to be created').format(name) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(mongodb_user.present(name, passwd), ret) with patch.dict(mongodb_user.__opts__, {'test': True}): comt = ('User {0} is not present and needs to be created'. format(name)) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(mongodb_user.present(name, passwd), ret) with patch.dict(mongodb_user.__opts__, {'test': False}): comt = ('User {0} has been created'.format(name)) ret.update({ 'comment': comt, 'result': True, 'changes': { name: 'Present' } }) self.assertDictEqual(mongodb_user.present(name, passwd), ret)
def test_present(self): ''' Test to ensure that the user is present with the specified properties. ''' name = 'myapp' passwd = 'password-of-myapp' 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 = MagicMock(side_effect=[True, False, False]) mock_t = MagicMock(return_value=True) with patch.dict(mongodb_user.__salt__, {'mongodb.user_exists': mock, 'mongodb.user_create': mock_t}): comt = ('User {0} is already present'.format(name)) ret.update({'comment': comt, 'result': True}) self.assertDictEqual(mongodb_user.present(name, passwd), ret) with patch.dict(mongodb_user.__opts__, {'test': True}): comt = ('User {0} is not present and needs to be created' .format(name)) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(mongodb_user.present(name, passwd), ret) with patch.dict(mongodb_user.__opts__, {'test': False}): comt = ('User {0} has been created'.format(name)) ret.update({'comment': comt, 'result': True, '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)