freeipa/tests/test_xmlrpc/test_hbacsvcgroup_plugin.py
John Dennis 2673782aec ticket 1600 - convert unittests to use DN objects
We have a larger goal of replacing all DN creation via string
formatting/concatenation with DN object operations because string
operations are not a safe way to form a DN nor to compare a DN. This
work needs to be broken into smaller chunks for easier review and
testing.

Addressing the unit tests first makes sense because we don't want to
be modifying both the core code and the tests used to verify the core
code simultaneously. If we modify the unittests first with existing
core code and no regressions are found then we can move on to
modifying parts of the core code with the belief the unittests can
validate the changes in the core code. Also by doing the unittests
first we also help to validate the DN objects are working correctly
(although they do have an extensive unittest).

The fundamental changes are:

* replace string substitution & concatenation with DN object
  constructor

* when comparing dn's the comparision is done after promotion
  to a DN object, then two DN objects are compared

* when a list of string dn's are to be compared a new list is
  formed where each string dn is replaced by a DN object

* because the unittest framework accepts a complex data structure of
  expected values where dn's are represeted as strings the unittest
  needs to express the expected value of a dn as a callable object
  (e.g. a lambda expression) which promotes the dn string to a DN
  object in order to do the comparision.
2011-08-16 23:52:26 -04:00

252 lines
7.7 KiB
Python

# Authors:
# Rob Crittenden <rcritten@redhat.com>
#
# Copyright (C) 2010 Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Test the `ipalib.plugins.hbacsvcgroup` module.
"""
from ipalib import api, errors
from tests.test_xmlrpc.xmlrpc_test import Declarative, fuzzy_uuid
from tests.test_xmlrpc import objectclasses
from ipalib.dn import *
hbacsvcgroup1 = u'testhbacsvcgroup1'
dn1 = DN(('cn',hbacsvcgroup1),('cn','hbacservicegroups'),('cn','hbac'),
api.env.basedn)
hbacsvc1 = u'sshd'
hbacsvc_dn1 = DN(('cn',hbacsvc1),('cn','hbacservices'),('cn','hbac'),
api.env.basedn)
class test_hbacsvcgroup(Declarative):
cleanup_commands = [
('hbacsvcgroup_del', [hbacsvcgroup1], {}),
('hbacsvc_del', [hbacsvc1], {}),
]
tests=[
dict(
desc='Try to retrieve non-existent %r' % hbacsvcgroup1,
command=('hbacsvcgroup_show', [hbacsvcgroup1], {}),
expected=errors.NotFound(reason='no such entry'),
),
dict(
desc='Try to update non-existent %r' % hbacsvcgroup1,
command=('hbacsvcgroup_mod', [hbacsvcgroup1],
dict(description=u'Updated hbacsvcgroup 1')
),
expected=errors.NotFound(reason='no such entry'),
),
dict(
desc='Try to delete non-existent %r' % hbacsvcgroup1,
command=('hbacsvcgroup_del', [hbacsvcgroup1], {}),
expected=errors.NotFound(reason='no such entry'),
),
dict(
desc='Create %r' % hbacsvcgroup1,
command=('hbacsvcgroup_add', [hbacsvcgroup1],
dict(description=u'Test hbacsvcgroup 1')
),
expected=dict(
value=hbacsvcgroup1,
summary=u'Added HBAC service group "testhbacsvcgroup1"',
result=dict(
dn=lambda x: DN(x) == dn1,
cn=[hbacsvcgroup1],
objectclass=objectclasses.hbacsvcgroup,
description=[u'Test hbacsvcgroup 1'],
ipauniqueid=[fuzzy_uuid],
),
),
),
dict(
desc='Try to create duplicate %r' % hbacsvcgroup1,
command=('hbacsvcgroup_add', [hbacsvcgroup1],
dict(description=u'Test hbacsvcgroup 1')
),
expected=errors.DuplicateEntry(),
),
dict(
desc='Create service %r' % hbacsvc1,
command=('hbacsvc_add', [hbacsvc1],
dict(
description=u'Test service 1',
),
),
expected=dict(
value=hbacsvc1,
summary=u'Added HBAC service "%s"' % hbacsvc1,
result=dict(
dn=lambda x: DN(x) == hbacsvc_dn1,
cn=[hbacsvc1],
description=[u'Test service 1'],
objectclass=objectclasses.hbacsvc,
ipauniqueid=[fuzzy_uuid],
),
),
),
dict(
desc=u'Add service %r to %r' % (hbacsvc1, hbacsvcgroup1),
command=(
'hbacsvcgroup_add_member', [hbacsvcgroup1], dict(hbacsvc=hbacsvc1)
),
expected=dict(
completed=1,
failed=dict(
member=dict(
hbacsvc=tuple(),
),
),
result={
'dn': lambda x: DN(x) == dn1,
'cn': [hbacsvcgroup1],
'description': [u'Test hbacsvcgroup 1'],
'member_hbacsvc': [hbacsvc1],
},
),
),
dict(
desc='Retrieve %r' % hbacsvcgroup1,
command=('hbacsvcgroup_show', [hbacsvcgroup1], {}),
expected=dict(
value=hbacsvcgroup1,
summary=None,
result={
'dn': lambda x: DN(x) == dn1,
'member_hbacsvc': [hbacsvc1],
'cn': [hbacsvcgroup1],
'description': [u'Test hbacsvcgroup 1'],
},
),
),
dict(
desc='Search for %r' % hbacsvcgroup1,
command=('hbacsvcgroup_find', [], dict(cn=hbacsvcgroup1)),
expected=dict(
count=1,
truncated=False,
summary=u'1 HBAC service group matched',
result=[
{
'dn': lambda x: DN(x) == dn1,
'member_hbacsvc': [hbacsvc1],
'cn': [hbacsvcgroup1],
'description': [u'Test hbacsvcgroup 1'],
},
],
),
),
dict(
desc='Update %r' % hbacsvcgroup1,
command=('hbacsvcgroup_mod', [hbacsvcgroup1],
dict(description=u'Updated hbacsvcgroup 1')
),
expected=dict(
value=hbacsvcgroup1,
summary=u'Modified HBAC service group "testhbacsvcgroup1"',
result=dict(
cn=[hbacsvcgroup1],
description=[u'Updated hbacsvcgroup 1'],
member_hbacsvc=[hbacsvc1],
),
),
),
dict(
desc='Retrieve %r to verify update' % hbacsvcgroup1,
command=('hbacsvcgroup_show', [hbacsvcgroup1], {}),
expected=dict(
value=hbacsvcgroup1,
summary=None,
result={
'dn': lambda x: DN(x) == dn1,
'member_hbacsvc': [hbacsvc1],
'cn': [hbacsvcgroup1],
'description': [u'Updated hbacsvcgroup 1'],
},
),
),
dict(
desc='Remove service %r from %r' % (hbacsvc1, hbacsvcgroup1),
command=('hbacsvcgroup_remove_member', [hbacsvcgroup1],
dict(hbacsvc=hbacsvc1)
),
expected=dict(
failed=dict(
member=dict(
hbacsvc=tuple(),
),
),
completed=1,
result={
'dn': lambda x: DN(x) == dn1,
'cn': [hbacsvcgroup1],
'description': [u'Updated hbacsvcgroup 1'],
},
),
),
dict(
desc='Delete %r' % hbacsvcgroup1,
command=('hbacsvcgroup_del', [hbacsvcgroup1], {}),
expected=dict(
value=hbacsvcgroup1,
summary=u'Deleted HBAC service group "testhbacsvcgroup1"',
result=dict(failed=u''),
),
),
dict(
desc='Delete service %r' % hbacsvc1,
command=('hbacsvc_del', [hbacsvc1], {}),
expected=dict(
value=hbacsvc1,
summary=u'Deleted HBAC service "%s"' % hbacsvc1,
result=dict(failed=u''),
),
)
]