Exemple #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())
Exemple #2
0
    def test_error_empty_list(self):
        """Ensure the error_empty_list option is respected with all invocations"""

        mock_sock = MockLDAPSocket()
        mock_sock.add_root_dse()
        ldap = LDAP(mock_sock, error_empty_list=True)

        with self.assertRaises(exceptions.LDAPError):
            ldap.modify('o=foo', [Mod(Mod.REPLACE, 'foo', [])])

        with self.assertRaises(exceptions.LDAPError):
            ldap.replace_attrs('o=foo', {'foo': []})

        with self.assertRaises(exceptions.LDAPError):
            ldap.delete_attrs('o=foo', {'foo': []}, current={'foo': ['bar']})

        with self.assertRaises(exceptions.LDAPError):
            ldap.obj('o=foo').modify([Mod(Mod.REPLACE, 'foo', [])])

        with self.assertRaises(exceptions.LDAPError):
            ldap.obj('o=foo').replace_attrs({'foo': []})

        with self.assertRaises(exceptions.LDAPError):
            ldap.obj('o=foo', attrs_dict={'foo': ['bar']}).delete_attrs({'foo': []})