Ejemplo n.º 1
0
    def test_delete_attrs(self):
        """Ensure delete_attrs behaves correctly"""
        mock_sock = MockLDAPSocket()
        mock_sock.add_root_dse()
        ldap = LDAP(mock_sock)
        mock_sock.clear_sent()

        # current is not None
        # should NOT perform a search
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        ldap.delete_attrs('', {'foo': ['bar']}, current=LDAPObject('', {'foo': ['bar']}))
        self.assertEqual(mock_sock.num_sent(), 1)
        protoutils.unpack('modifyRequest', mock_sock.read_sent())

        # current is None and not strict_modify
        # should perform a search before modify
        mock_sock.add_search_res_entry('', {'foo': ['bar']})
        mock_sock.add_search_res_done('')
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        ldap.strict_modify = False
        ldap.delete_attrs('', {'foo': ['bar']})
        self.assertEqual(mock_sock.num_sent(), 2)
        protoutils.unpack('searchRequest', mock_sock.read_sent())
        protoutils.unpack('modifyRequest', mock_sock.read_sent())

        # current is None and strict_modify
        # should NOT perform a search
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        ldap.strict_modify = True
        ldap.delete_attrs('', {'foo': ['bar']})
        self.assertEqual(mock_sock.num_sent(), 1)
        protoutils.unpack('modifyRequest', mock_sock.read_sent())
Ejemplo n.º 2
0
    def test_delete_attrs(self):
        """Ensure LDAPObject.delete_attrs behaves correctly."""
        mock_sock = MockLDAPSocket()
        mock_sock.add_root_dse()
        ldap = LDAP(mock_sock)
        mock_sock.clear_sent()

        # should perform a search and then a modify if strict_modify is False and attrs in attrs_dict are not present
        # on the object
        obj = ldap.obj('o=test', {
            'test': ['abc', 'def']
        })
        ldap.strict_modify = False
        mock_sock.add_search_res_entry('o=test', {
            'foo': ['bar']
        })
        mock_sock.add_search_res_done('o=test')
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        obj.delete_attrs({
            'foo': ['bar']
        })
        self.assertEqual(mock_sock.num_sent(), 2)
        protoutils.unpack('searchRequest', mock_sock.read_sent())
        protoutils.unpack('modifyRequest', mock_sock.read_sent())

        # should never perform an extra search if all attrs in attrs_dict are present
        # strict_modify False and strict_modify True
        ldap.strict_modify = False
        obj = ldap.obj('o=test', {
            'test': ['abc', 'def']
        })
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        obj.delete_attrs({
            'test': ['abc']
        })
        self.assertEqual(mock_sock.num_sent(), 1)
        protoutils.unpack('modifyRequest', mock_sock.read_sent())

        ldap.strict_modify = True
        obj = ldap.obj('o=test', {
            'test': ['abc', 'def']
        })
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        obj.delete_attrs({
            'test': ['abc']
        })
        self.assertEqual(mock_sock.num_sent(), 1)
        protoutils.unpack('modifyRequest', mock_sock.read_sent())
Ejemplo n.º 3
0
    def test_replace_attrs(self):
        """Ensure replace_attrs behaves correctly"""
        class MockValidator(Validator):
            def validate_object(self, obj, write=True):
                pass

            def validate_modify(self, dn, modlist, current):
                pass

            def _validate_attribute(self, attr_name, values, write):
                pass

        mock_sock = MockLDAPSocket()
        mock_sock.add_root_dse()
        ldap = LDAP(mock_sock)
        mock_sock.clear_sent()

        # current is None and have validators and not strict_modify
        # SHOULD perform a search before modify
        ldap.validators = [MockValidator()]
        ldap.strict_modify = False
        mock_sock.add_search_res_entry('', {'foo': ['bar']})
        mock_sock.add_search_res_done('')
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        ldap.strict_modify = False
        ldap.replace_attrs('', {'foo': ['bar']})
        self.assertEqual(mock_sock.num_sent(), 2)
        protoutils.unpack('searchRequest', mock_sock.read_sent())
        protoutils.unpack('modifyRequest', mock_sock.read_sent())

        # else
        # SHOULD NOT perform a search, only modify
        ldap.validators = []
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        ldap.replace_attrs('', {'foo': ['bar']})
        self.assertEqual(mock_sock.num_sent(), 1)
        protoutils.unpack('modifyRequest', mock_sock.read_sent())
Ejemplo n.º 4
0
    def test_add_attrs(self):
        """Ensure LDAPObject.add_attrs behaves correctly."""
        mock_sock = MockLDAPSocket()
        mock_sock.add_root_dse()
        ldap = LDAP(mock_sock)
        mock_sock.clear_sent()
        test_add_attr = 'foo'
        test_add_vals = ['bar']

        # should perform a search and then a modify if strict_modify is False and attrs in attrs_dict are not present
        # on the object
        obj = ldap.obj('o=test', {
            'test': ['abc', 'def']
        })
        ldap.strict_modify = False
        mock_sock.add_search_res_entry('o=test', {
            test_add_attr: ['baz']
        })
        mock_sock.add_search_res_done('o=test')
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        obj.add_attrs({
            test_add_attr: test_add_vals
        })
        self.assertEqual(mock_sock.num_sent(), 2)
        protoutils.unpack('searchRequest', mock_sock.read_sent())
        protoutils.unpack('modifyRequest', mock_sock.read_sent())
        self.assertIn(test_add_attr, obj)
        self.assertIn(test_add_vals[0], obj[test_add_attr])

        # should only perform a modify regardless of missing attrs if strict_modify is True
        obj = ldap.obj('o=test', {
            'test': ['abc', 'def']
        })
        ldap.strict_modify = True
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        obj.add_attrs({
            test_add_attr: test_add_vals
        })
        self.assertEqual(mock_sock.num_sent(), 1)
        protoutils.unpack('modifyRequest', mock_sock.read_sent())
        self.assertIn(test_add_attr, obj)
        self.assertEqual(obj[test_add_attr], test_add_vals)

        # should never perform an extra search if all attrs in attrs_dict are present
        test_add_attr = 'test'
        test_add_vals = ['foo']

        ldap.strict_modify = False
        obj = ldap.obj('o=test', {
            'test': ['abc', 'def']
        })
        ldap.strict_modify = True
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        obj.add_attrs({
            test_add_attr: test_add_vals
        })
        self.assertEqual(mock_sock.num_sent(), 1)
        protoutils.unpack('modifyRequest', mock_sock.read_sent())
        self.assertIn(test_add_attr, obj)
        self.assertIn(test_add_vals[0], obj[test_add_attr])

        ldap.strict_modify = True
        obj = ldap.obj('o=test', {
            'test': ['abc', 'def']
        })
        ldap.strict_modify = True
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        obj.add_attrs({
            test_add_attr: test_add_vals
        })
        self.assertEqual(mock_sock.num_sent(), 1)
        protoutils.unpack('modifyRequest', mock_sock.read_sent())
        self.assertIn(test_add_attr, obj)
        self.assertIn(test_add_vals[0], obj[test_add_attr])