Exemple #1
0
 def test_duplicate_tag(self):
     """Ensure trying to define a duplicate tag is an error"""
     tag = 'foobar'
     mock_sock = MockLDAPSocket()
     mock_sock.add_root_dse()
     ldap = LDAP(mock_sock)
     ldap.obj('o=foo', tag=tag)
     with self.assertRaises(exceptions.TagError):
         ldap.obj('o=bar', tag=tag)
Exemple #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())
Exemple #3
0
    def test_tagging(self):
        """Ensure setting and retreiving tags works"""
        tag = 'foobar'
        mock_sock = MockLDAPSocket()
        mock_sock.add_root_dse()
        ldap = LDAP(mock_sock)
        expected_obj = ldap.obj('o=foo', tag=tag)
        actual_obj = ldap.tag(tag)
        self.assertIs(expected_obj, actual_obj)

        with self.assertRaises(exceptions.TagError):
            ldap.tag('not_a_defined_tag')
Exemple #4
0
    def test_replace_attrs(self):
        """Excercise replace_attrs"""
        mock_sock = MockLDAPSocket()
        mock_sock.add_root_dse()
        ldap = LDAP(mock_sock)

        obj = ldap.obj('o=test', {
            'test': ['abc'],
            'deleteme': ['foo'],
        })
        mock_sock.add_ldap_result(rfc4511.ModifyResponse, 'modifyResponse')
        obj.replace_attrs({
            'test': ['def'],
            'foo': ['bar'],
            'deleteme': [],
        })
        self.assertIn('test', obj)
        self.assertIn('foo', obj)
        self.assertNotIn('deleteme', obj)
        self.assertEqual(obj['test'], ['def'])
        self.assertEqual(obj['foo'], ['bar'])
Exemple #5
0
    def test_refresh_missing(self):
        """Ensure refresh_missing behaves correctly"""
        mock_sock = MockLDAPSocket()
        mock_sock.add_root_dse()
        ldap = LDAP(mock_sock)
        mock_sock.clear_sent()

        # ensure we do not send any request if all listed attributes are present
        obj = ldap.obj('o=test', {
            'foo': ['bar'],
            'abc': ['def'],
        })
        obj.refresh_missing(['foo', 'abc'])
        self.assertEqual(mock_sock.num_sent(), 0)

        # ensure new attributes get filled when we do search
        mock_sock.add_search_res_entry('o=test', {
            'new': ['attr']
        })
        mock_sock.add_search_res_done('o=test')
        obj.refresh_missing(['new'])
        self.assertIn('new', obj)
        self.assertEqual(obj['new'], ['attr'])
Exemple #6
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': []})
Exemple #7
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])