1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-10 01:18:15 +03:00

netcmd: models: fix build_expression did not work with EnumField

Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Rob van der Linde 2024-01-18 15:47:52 +13:00 committed by Andrew Bartlett
parent 9bd7a56364
commit cbcc8039d1
2 changed files with 41 additions and 2 deletions

View File

@ -209,6 +209,10 @@ class EnumField(Field):
else: else:
return MessageElement(str(value.value), flags, self.name) return MessageElement(str(value.value), flags, self.name)
def expression(self, value):
"""Returns the ldb search expression for this field."""
return f"({self.name}={binary_encode(str(value.value))})"
class DateTimeField(Field): class DateTimeField(Field):
"""A field for parsing ldb timestamps into Python datetime.""" """A field for parsing ldb timestamps into Python datetime."""

View File

@ -27,8 +27,8 @@ from xml.etree import ElementTree
from ldb import FLAG_MOD_ADD, MessageElement, SCOPE_ONELEVEL from ldb import FLAG_MOD_ADD, MessageElement, SCOPE_ONELEVEL
from samba.dcerpc import security from samba.dcerpc import security
from samba.dcerpc.misc import GUID from samba.dcerpc.misc import GUID
from samba.netcmd.domain.models import (Group, Site, User, StrongNTLMPolicy, from samba.netcmd.domain.models import (AccountType, Group, Site, User,
fields) StrongNTLMPolicy, fields)
from samba.ndr import ndr_pack, ndr_unpack from samba.ndr import ndr_pack, ndr_unpack
from .base import SambaToolCmdTest from .base import SambaToolCmdTest
@ -37,6 +37,41 @@ HOST = "ldap://{DC_SERVER}".format(**os.environ)
CREDS = "-U{DC_USERNAME}%{DC_PASSWORD}".format(**os.environ) CREDS = "-U{DC_USERNAME}%{DC_PASSWORD}".format(**os.environ)
class ModelTests(SambaToolCmdTest):
@classmethod
def setUpClass(cls):
cls.samdb = cls.getSamDB("-H", HOST, CREDS)
super().setUpClass()
def test_query_count(self):
"""Test count property on Query object without converting to a list."""
groups = Group.query(self.samdb)
self.assertEqual(groups.count, len(list(groups)))
def test_query_filter_bool(self):
"""Tests filtering by a BooleanField."""
total = Group.query(self.samdb).count
system_groups = Group.query(self.samdb,
is_critical_system_object=True).count
user_groups = Group.query(self.samdb,
is_critical_system_object=False).count
self.assertNotEqual(system_groups, 0)
self.assertNotEqual(user_groups, 0)
self.assertEqual(system_groups + user_groups, total)
def test_query_filter_enum(self):
"""Tests filtering by an EnumField."""
robots_vs_humans = User.query(self.samdb).count
robots = User.query(self.samdb,
account_type=AccountType.WORKSTATION_TRUST).count
humans = User.query(self.samdb,
account_type=AccountType.NORMAL_ACCOUNT).count
self.assertNotEqual(robots, 0)
self.assertNotEqual(humans, 0)
self.assertEqual(robots + humans, robots_vs_humans)
class FieldTestMixin: class FieldTestMixin:
"""Tests a model field to ensure it behaves correctly in both directions. """Tests a model field to ensure it behaves correctly in both directions.