From c784f2cbf559af5c5d9aa39eac459f7b19c48415 Mon Sep 17 00:00:00 2001 From: Simon Pichugin Date: Mon, 19 Oct 2015 17:18:26 +0200 Subject: [PATCH] Ticket 48303 - Fix lib389 broken tests - effective_rights_test Description: Remove SER_DEPLOYED_DIR assignment, because now it is in __init__.py by default. Remove "Created on" block, because git contains this information. Add Red Hat copyright block. Add a logging. Add a docstring to the test. Refactore code to the pytest compatibility. https://fedorahosted.org/389/ticket/48303 Review by: ? --- tests/effective_rights_test.py | 134 +++++++++++++++++++++++++---------------- 1 file changed, 81 insertions(+), 53 deletions(-) diff --git a/tests/effective_rights_test.py b/tests/effective_rights_test.py index a02ce7c..23da6bc 100644 --- a/tests/effective_rights_test.py +++ b/tests/effective_rights_test.py @@ -1,63 +1,91 @@ -''' -Created on Aug 1, 2015 - -@author: William Brown -''' +# --- BEGIN COPYRIGHT BLOCK --- +# Copyright (C) 2015 Red Hat, Inc. +# All rights reserved. +# +# License: GPL (version 3 or any later version). +# See LICENSE for details. +# --- END COPYRIGHT BLOCK --- +# from lib389._constants import * from lib389 import DirSrv,Entry +import pytest +import logging + +logging.getLogger(__name__).setLevel(logging.DEBUG) +log = logging.getLogger(__name__) INSTANCE_PORT = 54321 INSTANCE_SERVERID = 'effectiverightsds' -INSTANCE_PREFIX = None - -class Test_effective_rights(): - def setUp(self): - instance = DirSrv(verbose=False) - instance.log.debug("Instance allocated") - args = {SER_HOST: LOCALHOST, - SER_PORT: INSTANCE_PORT, - SER_DEPLOYED_DIR: INSTANCE_PREFIX, - SER_SERVERID_PROP: INSTANCE_SERVERID - } - instance.allocate(args) - if instance.exists(): - instance.delete() - instance.create() +TEST_USER = 'uid=test,%s' % DEFAULT_SUFFIX +TEST_GROUP = 'cn=testgroup,%s' % DEFAULT_SUFFIX + + +class TopologyInstance(object): + def __init__(self, instance): instance.open() self.instance = instance - def tearDown(self): - if self.instance.exists(): - self.instance.delete() - - def add_user(self): - # Create a user entry - uentry = Entry('uid=test,%s' % DEFAULT_SUFFIX) - uentry.setValues('objectclass', 'top', 'extensibleobject') - uentry.setValues('uid', 'test') - self.instance.add_s(uentry) - #self.instance.log.debug("Created user entry as:" ,uentry.dn) - - def add_group(self): - # Create a group for the user to have some rights to - gentry = Entry('cn=testgroup,%s' % DEFAULT_SUFFIX) - gentry.setValues('objectclass', 'top', 'extensibleobject') - gentry.setValues('cn', 'testgroup') - self.instance.add_s(gentry) - - def test_effective_rights(self): - # Run an effective rights search - result = self.instance.get_effective_rights('uid=test,%s' % DEFAULT_SUFFIX, filterstr='(cn=testgroup)', attrlist=['cn']) - - rights = result[0] - assert rights.getValue('attributeLevelRights') == 'cn:rsc' - assert rights.getValue('entryLevelRights') == 'v' -if __name__ == "__main__": - test = Test_effective_rights() - test.setUp() - test.add_user() - test.add_group() - test.test_effective_rights() - test.tearDown() +@pytest.fixture(scope="module") +def topology(request): + instance = DirSrv(verbose=False) + instance.log.debug("Instance allocated") + args = {SER_HOST: LOCALHOST, + SER_PORT: INSTANCE_PORT, + SER_SERVERID_PROP: INSTANCE_SERVERID} + instance.allocate(args) + if instance.exists(): + instance.delete() + instance.create() + instance.open() + + def fin(): + if instance.exists(): + instance.delete() + request.addfinalizer(fin) + + return TopologyInstance(instance) + +@pytest.fixture(scope="module") +def add_user(topology): + """Create a user entry""" + + log.info('Create a user entry: %s' % TEST_USER) + uentry = Entry(TEST_USER) + uentry.setValues('objectclass', 'top', 'extensibleobject') + uentry.setValues('uid', 'test') + topology.instance.add_s(uentry) + #topology.instance.log.debug("Created user entry as:" ,uentry.dn) + + +@pytest.fixture(scope="module") +def add_group(topology): + """Create a group for the user to have some rights to""" + + log.info('Create a group entry: %s' % TEST_GROUP) + gentry = Entry(TEST_GROUP) + gentry.setValues('objectclass', 'top', 'extensibleobject') + gentry.setValues('cn', 'testgroup') + topology.instance.add_s(gentry) + + +def test_effective_rights(topology, add_user, add_group): + """Run an effective rights search + and compare actual results with expected + """ + + log.info('Search for effective rights with get_effective_rights() function') + result = topology.instance.get_effective_rights(TEST_USER, + filterstr='(cn=testgroup)', + attrlist=['cn']) + + rights = result[0] + log.info('Assert that "attributeLevelRights: cn:rsc" and "entryLevelRights: v"') + assert rights.getValue('attributeLevelRights') == 'cn:rsc' + assert rights.getValue('entryLevelRights') == 'v' + + +if __name__ == "__main__": + CURRENT_FILE = os.path.realpath(__file__) + pytest.main("-s -v %s" % CURRENT_FILE) -- 2.4.3