def test_or_aggregate(self):
     filt = Filter.OR([
         Filter.attribute('givenName').equal_to('bilbo'),
         Filter.attribute('sn').equal_to('baggens')
     ])
     string = filt.to_string()
     assert string == '(|(givenName=bilbo)(sn=baggens))'
 def test_and_aggregate(self):
     filt = Filter.AND([
         Filter.attribute('firstName').equal_to('Alice'),
         Filter.attribute('lastName').ends_with('Chains')
     ])
     assert filt.match({'firstName': 'Alice', 'lastName': 'Chains'})
     assert filt.match({'firstName': 'Alice', 'lastName': 'In-Chains'})
     assert not filt.match({'firstName': 'Bob', 'lastName': 'Chains'})
     assert not filt.match({'firstName': 'Alice'})
 def test_contains(self):
     filt = Filter.attribute('sn').contains('smith')
     assert filt.match({'sn': 'smith'})
     assert filt.match({'sn': 'smith-jonson'})
     assert filt.match({'sn': 'jonson-smith'})
     assert filt.match({'sn': 'Von Ubersmith'})
     assert not filt.match({'sn': 'Jonson'})
 def test_less_than_numeric(self):
     filt = Filter.attribute('age').lte('10')
     assert filt.match({'age': 9})
     assert filt.match({'age': '9'})
     assert filt.match({'age': 10})
     assert filt.match({'age': '10'})
     assert not filt.match({'age': 11})
     assert not filt.match({'age': '11'})
Exemplo n.º 5
0
    def filter(self, *args, **kwargs):
        """
        If there are positional arguments, they must all be F() objects.  This
        allows us to preconstruct an unbound filter and then use it later.

        Of the Django filter suffixes we support the following:

            * ``__iexact``
            * ``__istartswith``
            * ``__iendswith``
            * ``__icontains``
            * ``__in``

        Note that only the case insensitive versions of these filters are supported.
        LDAP searches are case insensitive, so we make you use ``__i*`` versions to
        remind you of that.

        We do support one additional LDAP specific filter: ``__exists``.  This
        will cause a filter to be added that just ensures that the returned
        objects have the associated attribute.  Unlike in SQL, where every row
        contains all available columns, in LDAP, attributes can be absent
        entirely from the record.
        """
        steps = self.__validate_positional_args(args)
        for key, value in kwargs.items():
            if isinstance(value, str):
                value = value.strip()
            if key.endswith('__istartswith'):
                steps.append(Filter.attribute(self.get_attribute(key[:-13])).starts_with(value))
            elif key.endswith('__iendswith'):
                steps.append(Filter.attribute(self.get_attribute(key[:-11])).ends_with(value))
            elif key.endswith('__icontains'):
                steps.append(Filter.attribute(self.get_attribute(key[:-11])).contains(value))
            elif key.endswith('__in'):
                if not isinstance(value, list):
                    raise ValueError('When using the "__in" filter you must supply a list')
                in_steps = []
                for v in value:
                    in_steps.append(Filter.attribute(self.get_attribute(key[:-4])).equal_to(v))
                steps.append(Filter.OR(in_steps))
            elif key.endswith('__exists'):
                # This one doesn't exist as a Django field lookup
                steps.append(Filter.attribute(self.get_attribute(key[:-8])).present())
            elif key.endswith('__iexact') or '__' not in key:
                # no suffix means do an __exact
                if '__' in key:
                    key = key[:-8]
                if value is None:
                    # If value is None, we search for the absence of that
                    # attribute
                    steps.append(Filter.NOT(Filter.attribute(self.get_attribute(key))))
                else:
                    steps.append(Filter.attribute(self.get_attribute(key)).equal_to(value))
            else:
                raise F.UnknownSuffix('The search filter "{}" uses an unknown filter suffix')
        self.chain.append(Filter.AND(steps))
        return self
 def test_match_escaped_substrings(self):
     filt = Filter.attribute('sub').raw('*jerry\\5c \\2a j*s*')
     assert filt.match({'sub': 'Jerry\\ * Jones'})
 def test_present(self):
     filt = Filter.attribute('sn').present()
     assert filt.match({'sn': 'smith'})
     assert filt.match({'sn': 'alex'})
     assert not filt.match({'mail': 'smith'})
 def test_match_substrings(self):
     filt = Filter.attribute('sub').raw('*jer* jo*e*')
     assert filt.match({'sub': 'Jerry Jones'})
 def test_match_escaped(self):
     filt = Filter.attribute('sub').raw('jerry\\2a \\28jones\\29 \\5c')
     assert filt.match({'sub': 'Jerry* (Jones) \\'})
 def test_raw(self):
     filt = Filter.attribute('attr').raw('value*value')
     string = filt.to_string()
     assert string == '(attr=value*value)'
Exemplo n.º 11
0
 def test_escaped(self):
     filt = Filter.attribute('escaped').equal_to('*(test)*')
     assert filt.match({'escaped': '*(test)*'})
     assert not filt.match({'escaped': '(test)'})
     assert not filt.match({})
Exemplo n.º 12
0
 def test_less_than_lexical(self):
     filt = Filter.attribute('name').lte('bob')
     assert filt.match({'name': 'acme'})
     assert filt.match({'name': 'bob'})
     assert not filt.match({'name': 'cell'})
Exemplo n.º 13
0
 def test_equality(self):
     filt = Filter.attribute('sn').equal_to('smith')
     assert filt.match({'sn': 'smith'})
     assert filt.match({'sn': 'SMITH'})
     assert not filt.match({'sn': 'bob'})
 def test_filter_convert_negative(self):
     filt = Filter.attribute('number').equal_to(-10)
     string = filt.to_string()
     assert string == '(number=-10)'
 def test_approx(self):
     filt = Filter.attribute('attr').approx('value')
     string = filt.to_string()
     assert string == '(attr~=value)'
 def test_filter_convert_float(self):
     filt = Filter.attribute('number').equal_to(10.26)
     string = filt.to_string()
     assert string == '(number=10.26)'
 def test_filter_escape(self):
     filt = Filter.attribute('escaped').equal_to('a * (complex) \\value')
     string = filt.to_string()
     assert string == '(escaped=a \\2a \\28complex\\29 \\5cvalue)'
 def test_present(self):
     filt = Filter.attribute('attr').present()
     string = filt.to_string()
     assert string == '(attr=*)'
 def test_starts_with(self):
     filt = Filter.attribute('attr').starts_with('value')
     string = filt.to_string()
     assert string == '(attr=value*)'
 def test_ends_with(self):
     filt = Filter.attribute('attr').ends_with('value')
     string = filt.to_string()
     assert string == '(attr=*value)'
Exemplo n.º 21
0
 def test_ends_with(self):
     filt = Filter.attribute('sn').ends_with('smith')
     assert filt.match({'sn': 'smith'})
     assert filt.match({'sn': 'Von Ubersmith'})
     assert not filt.match({'sn': 'smith-jonson'})
 def test_contains(self):
     filt = Filter.attribute('attr').contains('value')
     string = filt.to_string()
     assert string == '(attr=*value*)'
 def test_not_aggregate(self):
     filt = Filter.NOT([
         Filter.attribute('givenName').equal_to('bilbo')
     ])
     string = filt.to_string()
     assert string == '(!(givenName=bilbo))'
 def test_lesser_than(self):
     filt = Filter.attribute('attr').lte('value')
     string = filt.to_string()
     assert string == '(attr<=value)'
Exemplo n.º 25
0
 def test_approx(self):
     filt = Filter.attribute('name').approx('ashcroft')
     assert filt.match({'name': 'Ashcroft'})
     assert filt.match({'name': 'Ashcraft'})
     assert not filt.match({'name': 'Ashsoft'})
Exemplo n.º 26
0
 def test_not_aggregate(self):
     filt = Filter.NOT([Filter.attribute('firstName').equal_to('Alice')])
     assert filt.match({'firstName': 'Bob'})
     assert filt.match({})
     assert not filt.match({'firstName': 'Alice'})
     assert not filt.match({'firstName': 'Alice', 'lastName': 'Chains'})
Exemplo n.º 27
0
 def test_multi_value_equality(self):
     filt = Filter.attribute('sn').equal_to('smith')
     data = {'sn': ['Sam', 'Smith', 'Swanson', 'Samson']}
     assert filt.match(data)
     data = {'sn': ['Sam', 'Swanson', 'Samson']}
     assert not filt.match(data)
 def test_equal_to(self):
     filt = Filter.attribute('attr').equal_to('value')
     string = filt.to_string()
     assert string == '(attr=value)'