Debian Codegraph @

test/testMockBase.py

raw
1 # Created on 2016.04.17
2 #
3 # Author: Giovanni Cannata & signedbit
4 #
5 # Copyright 2016 - 2020 Giovanni Cannata & signedbit
6 #
7 # This file is part of ldap3.
8 #
9 # ldap3 is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Lesser General Public License as published
11 # by the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # ldap3 is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with ldap3 in the COPYING and COPYING.LESSER files.
21 # If not, see <http://www.gnu.org/licenses/>.
22
23 import unittest
24
25 from ldap3 import SchemaInfo, DsaInfo, Server, Connection, MOCK_SYNC
26 from ldap3.operation import search
27 from ldap3.core.exceptions import LDAPSizeLimitExceededResult
28 from ldap3.protocol.schemas.edir914 import edir_9_1_4_schema, edir_9_1_4_dsa_info
29
30
31 class Test(unittest.TestCase):
32 @classmethod
33 def setUpClass(cls):
34 cls.schema = SchemaInfo.from_json(edir_9_1_4_schema)
35 info = DsaInfo.from_json(edir_9_1_4_dsa_info, cls.schema)
36 cls.server = Server.from_definition('MockSyncServer', info, cls.schema)
37 cls.connection = Connection(cls.server, user='cn=user1,ou=test', password='test1', client_strategy=MOCK_SYNC)
38
39 # create fixtures
40 cls.connection.strategy.add_entry('cn=user1,ou=test', {'userPassword': 'test1', 'revision': 1})
41 cls.connection.strategy.add_entry('cn=user2,ou=test', {'userPassword': 'test2', 'revision': 2})
42 cls.connection.strategy.add_entry('cn=user3,ou=test', {'userPassword': 'test3', 'revision': 3})
43
44 @classmethod
45 def tearDownClass(cls):
46 cls.connection.unbind()
47
48 def test_and_evaluates_correctly_when_first_operand_doesnt_match(self):
49 actual = len(self._evaluate_filter('(&(revision=1)(userPassword=notarealvalue))'))
50 expected = 0
51
52 self.assertEqual(actual, expected)
53
54 def test_and_evaluates_correctly_when_second_operand_doesnt_match(self):
55 actual = len(self._evaluate_filter('(&(userPassword=notarealvalue)(revision=1))'))
56 expected = 0
57
58 self.assertEqual(actual, expected)
59
60 def test_and_evaluates_correctly_when_both_operands_match(self):
61 actual = len(self._evaluate_filter('(&(revision=1)(userPassword=test1))'))
62 expected = 1
63
64 self.assertEqual(actual, expected)
65
66 # def test_raises_size_limit_exceeded_exception(self):
67 # connection = Connection(self.server, user='cn=user1,ou=test', password='test1', client_strategy=MOCK_SYNC, raise_exceptions=True)
68 # # create fixtures
69 # connection.strategy.add_entry('cn=user1,ou=test', {'userPassword': 'test1', 'revision': 1})
70 # connection.strategy.add_entry('cn=user2,ou=test', {'userPassword': 'test2', 'revision': 2})
71 # connection.strategy.add_entry('cn=user3,ou=test', {'userPassword': 'test3', 'revision': 3})
72 # connection.bind()
73 # with self.assertRaises(LDAPSizeLimitExceededResult):
74 # connection.search('ou=test', '(cn=*)', size_limit=1)
75
76 def _evaluate_filter(self, search_filter):
77 filter_root = search.parse_filter(search_filter, self.schema, auto_escape=True, auto_encode=False, validator=None, check_names=False)
78 candidates = self.server.dit
79 return self.connection.strategy.evaluate_filter_node(filter_root, candidates)