def test_adduser(self): ''' Test if it add a user to a group ''' with patch(win_groupadd.win32.client, 'flag', None): self.assertDictEqual( win_groupadd.adduser('dc=foo', 'dc=\\username'), { 'changes': { 'Users Added': ['dc=\\username'] }, 'comment': '', 'name': 'dc=foo', 'result': True }) with patch(win_groupadd.win32.client, 'flag', 1): comt = ('Failed to add dc=\\username to group dc=foo. C') self.assertDictEqual( win_groupadd.adduser('dc=foo', 'dc=\\username'), { 'changes': { 'Users Added': [] }, 'name': 'dc=foo', 'comment': comt, 'result': False })
def test_adduser(self): ''' Test if it add a user to a group ''' with patch(win_groupadd.win32.client, 'flag', None): self.assertDictEqual(win_groupadd.adduser('dc=foo', 'dc=\\username'), {'changes': {'Users Added': ['dc=\\username']}, 'comment': '', 'name': 'dc=foo', 'result': True}) with patch(win_groupadd.win32.client, 'flag', 1): comt = ('Failed to add dc=\\username to group dc=foo. C') self.assertDictEqual(win_groupadd.adduser('dc=foo', 'dc=\\username'), {'changes': {'Users Added': []}, 'name': 'dc=foo', 'comment': comt, 'result': False})
def test_adduser_error(self): """ Test adding a user and encountering an error """ msg = "An unknown directory object was requested" error = pywintypes.com_error( -1234, "Exception occurred.", (0, None, msg, None, 0, -2147352567), None ) # Create mock group object with mocked Add function which raises the # exception we need in order to test the error case. class GroupObj(MockGroupObj): def Add(self, name): raise error obj_group_mock = MagicMock(return_value=GroupObj("foo", ["WinNT://HOST/steve"])) with patch.object( win_groupadd, "_get_group_object", obj_group_mock ), patch.object(salt.utils.win_functions, "get_sam_name", self.sam_mock): with TstSuiteLoggingHandler() as handler: self.assertFalse(win_groupadd.adduser("foo", "username")) expected = ( "ERROR:Failed to add HOST\\username to group foo. An unknown" " directory object was requested" ) self.assertIn(expected, handler.messages)
def test_adduser_already_exists(self): ''' Test adding a user that already exists ''' obj_group_mock = MagicMock( return_value=MockGroupObj('foo', ['WinNT://HOST/steve'])) with patch.object(win_groupadd, '_get_group_object', obj_group_mock), \ patch.object(salt.utils.win_functions, 'get_sam_name', sam_mock): self.assertFalse(win_groupadd.adduser('foo', 'steve'))
def test_adduser(self): ''' Test adding a user to a group ''' obj_group_mock = MagicMock( return_value=MockGroupObj('foo', ['WinNT://HOST/steve'])) with patch.object(win_groupadd, '_get_group_object', obj_group_mock), \ patch.object(salt.utils.win_functions, 'get_sam_name', sam_mock): self.assertTrue(win_groupadd.adduser('foo', 'spongebob'))
def test_adduser_already_exists(self): """ Test adding a user that already exists """ obj_group_mock = MagicMock( return_value=MockGroupObj("foo", ["WinNT://HOST/steve"]) ) with patch.object( win_groupadd, "_get_group_object", obj_group_mock ), patch.object(salt.utils.win_functions, "get_sam_name", self.sam_mock): self.assertFalse(win_groupadd.adduser("foo", "steve"))
def test_adduser(self): """ Test adding a user to a group """ obj_group_mock = MagicMock( return_value=MockGroupObj("foo", ["WinNT://HOST/steve"]) ) with patch.object( win_groupadd, "_get_group_object", obj_group_mock ), patch.object(salt.utils.win_functions, "get_sam_name", self.sam_mock): self.assertTrue(win_groupadd.adduser("foo", "spongebob"))
def test_adduser_group_does_not_exist(self): groupobj_mock = MagicMock(side_effect=PYWINTYPES_ERROR) with patch.object(win_groupadd, '_get_group_object', groupobj_mock), \ patch.object(salt.utils.win_functions, 'get_sam_name', sam_mock): self.assertDictEqual( win_groupadd.adduser('foo', 'spongebob'), { 'changes': { 'Users Added': [] }, 'name': 'foo', 'comment': 'Failure accessing group foo. C', 'result': False })
def test_adduser_error(self): ''' Test adding a user and encountering an error ''' # Create mock group object with mocked Add function which raises the # exception we need in order to test the error case. class GroupObj(MockGroupObj): def Add(self, name): raise PYWINTYPES_ERROR obj_group_mock = MagicMock( return_value=GroupObj('foo', ['WinNT://HOST/steve'])) with patch.object(win_groupadd, '_get_group_object', obj_group_mock), \ patch.object(salt.utils.win_functions, 'get_sam_name', sam_mock): self.assertFalse(win_groupadd.adduser('foo', 'username'))
def test_adduser_already_exists(self): ''' Test adding a user that already exists ''' groupobj_mock = MagicMock( return_value=MockGroupObj('foo', ['WinNT://HOST/steve'])) with patch.object(win_groupadd, '_get_group_object', groupobj_mock), \ patch.object(salt.utils.win_functions, 'get_sam_name', sam_mock): self.assertDictEqual( win_groupadd.adduser('foo', 'steve'), { 'changes': { 'Users Added': [] }, 'comment': 'User HOST\\steve is already a member of foo', 'name': 'foo', 'result': None })
def test_adduser(self): ''' Test adding a user to a group ''' groupobj_mock = MagicMock( return_value=MockGroupObj('foo', ['WinNT://HOST/steve'])) with patch.object(win_groupadd, '_get_group_object', groupobj_mock), \ patch.object(salt.utils.win_functions, 'get_sam_name', sam_mock): self.assertDictEqual( win_groupadd.adduser('foo', 'spongebob'), { 'changes': { 'Users Added': ['HOST\\spongebob'] }, 'comment': '', 'name': 'foo', 'result': True })
def test_adduser_error(self): ''' Test adding a user and encountering an error ''' # Create mock group object with mocked Add function which raises the # exception we need in order to test the error case. class GroupObj(MockGroupObj): def Add(self, name): raise PYWINTYPES_ERROR groupobj_mock = MagicMock( return_value=GroupObj('foo', ['WinNT://HOST/steve'])) with patch.object(win_groupadd, '_get_group_object', groupobj_mock), \ patch.object(salt.utils.win_functions, 'get_sam_name', sam_mock): self.assertDictEqual( win_groupadd.adduser('foo', 'username'), { 'changes': { 'Users Added': [] }, 'name': 'foo', 'comment': 'Failed to add HOST\\username to group foo. C', 'result': False })
def test_adduser_group_does_not_exist(self): obj_group_mock = MagicMock(side_effect=PYWINTYPES_ERROR) with patch.object(win_groupadd, '_get_group_object', obj_group_mock): self.assertFalse(win_groupadd.adduser('foo', 'spongebob'))