def test_user_present_create_user_exists_opts_test(self): """ UT: nxos module:user_present method - user exists """ username = "******" password = "******" roles = ["vdc-admin", "dev-opts"] new_roles = ["network-operator"] encrypted = True crypt_salt = "foobar123" algorithm = "md5" side_effect = MagicMock(side_effect=[True, roles, "user_exists"]) with patch.dict(nxos_state.__opts__, {"test": True}): with patch.dict(nxos_state.__salt__, {"nxos.cmd": side_effect}): result = nxos_state.user_present( username, password=password, roles=new_roles, encrypted=encrypted, crypt_salt=crypt_salt, algorithm=algorithm, ) remove = result["changes"]["roles"]["remove"] remove.sort() self.assertEqual(result["name"], "daniel") self.assertEqual(result["result"], None) self.assertEqual(result["changes"]["roles"]["add"], ["network-operator"]) self.assertEqual(remove, ["dev-opts", "vdc-admin"]) self.assertEqual(result["comment"], "User will be updated")
def test_user_present_create_encrypted_password_no_roles_opts_test(self): """ UT: nxos module:user_present method - encrypted password, no roles """ username = "******" password = "******" encrypted = True crypt_salt = "foobar123" algorithm = "md5" side_effect = MagicMock(side_effect=[False, "", "new_user", True]) with patch.dict(nxos_state.__opts__, {"test": True}): with patch.dict(nxos_state.__salt__, {"nxos.cmd": side_effect}): result = nxos_state.user_present( username, password=password, encrypted=encrypted, crypt_salt=crypt_salt, algorithm=algorithm, ) self.assertEqual(result["name"], "daniel") self.assertEqual(result["result"], None) self.assertEqual(result["changes"]["password"], True) self.assertEqual(result["comment"], "User will be created")
def test_user_present_create_user_exists(self): """ UT: nxos module:user_present method - user exists """ username = "******" password = "******" encrypted = True crypt_salt = "foobar123" algorithm = "md5" side_effect = MagicMock(side_effect=[True, "user_exists"]) with patch.dict(nxos_state.__opts__, {"test": False}): with patch.dict(nxos_state.__salt__, {"nxos.cmd": side_effect}): result = nxos_state.user_present( username, password=password, encrypted=encrypted, crypt_salt=crypt_salt, algorithm=algorithm, ) self.assertEqual(result["name"], "daniel") self.assertTrue(result["result"]) self.assertEqual(result["changes"], {}) self.assertEqual(result["comment"], "User already exists")
def test_user_present_create_user_exists(): """ user_present method - user exists """ username = "******" password = "******" encrypted = True crypt_salt = "foobar123" algorithm = "md5" salt_mock = { "nxos.check_password": MagicMock(side_effect=[True]), "nxos.get_user": MagicMock(side_effect=["user_exists"]), } with patch.dict(nxos_state.__opts__, {"test": False}): with patch.dict(nxos_state.__salt__, salt_mock): result = nxos_state.user_present( username, password=password, encrypted=encrypted, crypt_salt=crypt_salt, algorithm=algorithm, ) assert result["name"] == "daniel" assert result["result"] assert result["changes"] == {} assert result["comment"] == "User already exists"
def test_user_present_create_encrypted_password_no_roles_opts_test(): """ user_present method - encrypted password, no roles """ username = "******" password = "******" encrypted = True crypt_salt = "foobar123" algorithm = "md5" salt_mock = { "nxos.check_password": MagicMock(side_effect=[False, True]), "nxos.get_user": MagicMock(side_effect=[""]), "nxos.set_password": MagicMock(side_effect=["new_user"]), } with patch.dict(nxos_state.__opts__, {"test": True}): with patch.dict(nxos_state.__salt__, salt_mock): result = nxos_state.user_present( username, password=password, encrypted=encrypted, crypt_salt=crypt_salt, algorithm=algorithm, ) assert result["name"] == "daniel" assert result["result"] is None assert result["changes"]["password"] is True assert result["comment"] == "User will be created"
def test_user_present_create_opts_test(self): """ UT: nxos module:user_present method - create """ roles = ["vdc-admin"] side_effect = MagicMock(side_effect=[[], "", "set_role", roles, roles]) with patch.dict(nxos_state.__opts__, {"test": True}): with patch.dict(nxos_state.__salt__, {"nxos.cmd": side_effect}): result = nxos_state.user_present("daniel", roles=roles) self.assertEqual(result["name"], "daniel") self.assertEqual(result["result"], None) self.assertEqual(result["changes"]["role"]["add"], ["vdc-admin"]) self.assertEqual(result["changes"]["role"]["remove"], []) self.assertEqual(result["comment"], "User will be created")
def test_user_present_create_non_defaults(): """ user_present method - create non default opts """ username = "******" password = "******" roles = ["vdc-admin", "dev-ops"] encrypted = False crypt_salt = "foobar123" algorithm = "md5" # [change_password, cur_roles, old_user, new_user, set_role, set_role, # get_roles, correct_password, cur_roles] side_effect = MagicMock( side_effect=[ False, [], "", "new_user", "set_role", "set_role", roles, True, roles, ] ) with patch.dict(nxos_state.__opts__, {"test": False}): with patch.dict(nxos_state.__salt__, {"nxos.cmd": side_effect}): result = nxos_state.user_present( username, password=password, roles=roles, encrypted=encrypted, crypt_salt=crypt_salt, algorithm=algorithm, ) assert result["name"] == "daniel" assert result["result"] assert result["changes"]["password"]["new"] == "new_user" assert result["changes"]["password"]["old"] == "" assert result["changes"]["roles"]["new"] == ["vdc-admin", "dev-ops"] assert result["changes"]["roles"]["old"] == [] assert result["comment"] == "User set correctly"
def test_user_present_create_opts_test(): """ user_present method - create opts """ roles = ["vdc-admin"] side_effect = MagicMock(side_effect=[[], "", "set_role", roles, roles]) with patch.dict(nxos_state.__opts__, {"test": True}): with patch.dict(nxos_state.__salt__, {"nxos.cmd": side_effect}): result = nxos_state.user_present("daniel", roles=roles) assert result["name"] == "daniel" assert result["result"] is None assert result["changes"]["role"]["add"] == ["vdc-admin"] assert result["changes"]["role"]["remove"] == [] assert result["comment"] == "User will be created"
def test_user_present_create(): """ user_present method - create """ roles = ["vdc-admin"] side_effect = MagicMock(side_effect=[[], "", "set_role", roles, roles]) with patch.dict(nxos_state.__opts__, {"test": False}): with patch.dict(nxos_state.__salt__, {"nxos.cmd": side_effect}): result = nxos_state.user_present("daniel", roles=roles) assert result["name"] == "daniel" assert result["result"] assert result["changes"]["roles"]["new"] == ["vdc-admin"] assert result["changes"]["roles"]["old"] == [] assert result["comment"] == "User set correctly"
def test_user_present_create_non_defaults(): """ user_present method - create non default opts """ username = "******" password = "******" roles = ["vdc-admin", "dev-ops"] encrypted = False crypt_salt = "foobar123" algorithm = "md5" salt_mock = { "nxos.check_password": MagicMock(side_effect=[False, True]), "nxos.get_roles": MagicMock(side_effect=[[], roles, roles]), "nxos.get_user": MagicMock(side_effect=[""]), "nxos.set_password": MagicMock(side_effect=["new_user"]), "nxos.set_role": MagicMock(side_effect=["set_role", "set_role"]), } with patch.dict(nxos_state.__opts__, {"test": False}): with patch.dict(nxos_state.__salt__, salt_mock): result = nxos_state.user_present( username, password=password, roles=roles, encrypted=encrypted, crypt_salt=crypt_salt, algorithm=algorithm, ) assert result["name"] == "daniel" assert result["result"] assert result["changes"]["password"]["new"] == "new_user" assert result["changes"]["password"]["old"] == "" assert result["changes"]["roles"]["new"] == [ "vdc-admin", "dev-ops" ] assert result["changes"]["roles"]["old"] == [] assert result["comment"] == "User set correctly"
def test_user_present_create_user_exists_opts_test(): """ user_present method - user exists with opts """ username = "******" password = "******" roles = ["vdc-admin", "dev-opts"] new_roles = ["network-operator"] encrypted = True crypt_salt = "foobar123" algorithm = "md5" salt_mock = { "nxos.check_password": MagicMock(side_effect=[True]), "nxos.get_roles": MagicMock(side_effect=[roles]), "nxos.get_user": MagicMock(side_effect=["user_exists"]), } with patch.dict(nxos_state.__opts__, {"test": True}): with patch.dict(nxos_state.__salt__, salt_mock): result = nxos_state.user_present( username, password=password, roles=new_roles, encrypted=encrypted, crypt_salt=crypt_salt, algorithm=algorithm, ) remove = result["changes"]["roles"]["remove"] remove.sort() assert result["name"] == "daniel" assert result["result"] is None assert result["changes"]["roles"]["add"] == ["network-operator"] assert remove == ["dev-opts", "vdc-admin"] assert result["comment"] == "User will be updated"