From c60b6886cf0537c9525bf5c139ad54ccdf709aaf Mon Sep 17 00:00:00 2001
From: Simon Pichugin <spichugi@redhat.com>
Date: Wed, 7 Oct 2015 14:10:41 +0200
Subject: [PATCH] Ticket 48303 - Fix lib389 broken tests - aci_parse_test

Description: Fix the imports to the correct ones.
             Add Red Hat copyright block.
             Remove "Created on" block, because git contains
             this information.
             Refactore code to the pytest compatibility.

https://fedorahosted.org/389/ticket/48303

Review: ?
---
 tests/aci_parse_test.py | 148 ++++++++++++++++++++++++++++--------------------
 1 file changed, 87 insertions(+), 61 deletions(-)

diff --git a/tests/aci_parse_test.py b/tests/aci_parse_test.py
index 72b716b..b8e80c5 100644
--- a/tests/aci_parse_test.py
+++ b/tests/aci_parse_test.py
@@ -1,70 +1,96 @@
-'''
-Created on Aug 3, 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._aci import Aci
-from lib389 import DirSrv,Entry
+from lib389.aci import Aci
+from lib389 import DirSrv, Entry
 import ldap
+import pytest
 
 INSTANCE_PORT     = 54321
 INSTANCE_SERVERID = 'aciparseds'
 #INSTANCE_PREFIX   = None
 
-class Test_schema():
-    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()
-        instance.open()
-        self.instance = instance
-
-    def tearDown(self):
-        if self.instance.exists():
-            self.instance.delete()
-
-    def create_complex_acis(self):
-        gentry = Entry('cn=testgroup,%s' % DEFAULT_SUFFIX)
-        gentry.setValues('objectclass', 'top', 'extensibleobject')
-        gentry.setValues('cn', 'testgroup')
-        gentry.setValues('aci',
-            """ (targetfilter="(ou=groups)")(targetattr = "uniqueMember || member")(version 3.0; acl "Allow test aci"; allow (read, search, write) (userdn="ldap:///dc=example,dc=com??sub?(ou=engineering)" and userdn="ldap:///dc=example,dc=com??sub?(manager=uid=wbrown,ou=managers,dc=example,dc=com) || ldap:///dc=example,dc=com??sub?(manager=uid=tbrown,ou=managers,dc=example,dc=com)" );) """,
-            )
-        self.instance.add_s(gentry)
-
-    def test_aci(self):
-        acis = self.instance.aci.list('cn=testgroup,%s' % DEFAULT_SUFFIX)
-        assert len(acis) == 1
-        aci = acis[0]
-        assert aci.acidata == {
-            'allow': [{'values': ['read', 'search', 'write']}], 
-            'target': [], 'targetattr': [{'values': ['uniqueMember', 'member'], 'equal': True}],
-            'targattrfilters': [],
-            'deny': [],
-            'acl': [{'values': ['Allow test aci']}],
-            'deny_raw_bindrules': [],
-            'targetattrfilters': [],
-            'allow_raw_bindrules': [{'values': ['userdn="ldap:///dc=example,dc=com??sub?(ou=engineering)" and userdn="ldap:///dc=example,dc=com??sub?(manager=uid=wbrown,ou=managers,dc=example,dc=com) || ldap:///dc=example,dc=com??sub?(manager=uid=tbrown,ou=managers,dc=example,dc=com)" ']}],
-            'targetfilter': [{'values': ['(ou=groups)'], 'equal': True}],
-            'targetscope': [],
-            'version 3.0;': [],
-            'rawaci': ' (targetfilter="(ou=groups)")(targetattr = "uniqueMember || member")(version 3.0; acl "Allow test aci"; allow (read, search, write) (userdn="ldap:///dc=example,dc=com??sub?(ou=engineering)" and userdn="ldap:///dc=example,dc=com??sub?(manager=uid=wbrown,ou=managers,dc=example,dc=com) || ldap:///dc=example,dc=com??sub?(manager=uid=tbrown,ou=managers,dc=example,dc=com)" );) '
-        }
-        assert aci.getRawAci() == """(targetfilter ="(ou=groups)")(targetattr ="uniqueMember || member")(version 3.0; acl "Allow test aci";allow (read, search, write)(userdn="ldap:///dc=example,dc=com??sub?(ou=engineering)" and userdn="ldap:///dc=example,dc=com??sub?(manager=uid=wbrown,ou=managers,dc=example,dc=com) || ldap:///dc=example,dc=com??sub?(manager=uid=tbrown,ou=managers,dc=example,dc=com)" );)"""
+class TopologyStandalone(object):
+    def __init__(self, standalone):
+        standalone.open()
+        self.standalone = standalone
 
-if __name__ == "__main__":
-    test = Test_schema()
-    test.setUp()
-    test.create_complex_acis()
-    test.test_aci()
-    test.tearDown()
 
+@pytest.fixture(scope="module")
+def topology(request):
+    standalone = DirSrv(verbose=False)
+    standalone.log.debug("Instance allocated")
+    args = {SER_HOST:          LOCALHOST,
+            SER_PORT:          INSTANCE_PORT,
+            #SER_DEPLOYED_DIR:  INSTANCE_PREFIX,
+            SER_SERVERID_PROP: INSTANCE_SERVERID}
+    standalone.allocate(args)
+    if standalone.exists():
+        standalone.delete()
+    standalone.create()
+    standalone.open()
+
+    def fin():
+        standalone.delete()
+    request.addfinalizer(fin)
+
+    return TopologyStandalone(standalone)
+
+
+@pytest.fixture(scope="module")
+def complex_aci(topology):
+    ACI_TARGET = '(targetfilter ="(ou=groups)")(targetattr ="uniqueMember || member")'
+    ACI_ALLOW = '(version 3.0; acl "Allow test aci";allow (read, search, write)'
+    ACI_SUBJECT = (
+         '(userdn="ldap:///dc=example,dc=com??sub?(ou=engineering)" and'
+         ' userdn="ldap:///dc=example,dc=com??sub?(manager=uid=wbrown,ou=managers,dc=example,dc=com)'
+         ' || ldap:///dc=example,dc=com??sub?(manager=uid=tbrown,ou=managers,dc=example,dc=com)" );)'
+          )
+    ACI_BODY = ACI_TARGET + ACI_ALLOW + ACI_SUBJECT
+
+    gentry = Entry('cn=testgroup,%s' % DEFAULT_SUFFIX)
+    gentry.setValues('objectclass', 'top', 'extensibleobject')
+    gentry.setValues('cn', 'testgroup')
+    gentry.setValues('aci', ACI_BODY)
+
+    topology.standalone.add_s(gentry)
+
+    return ACI_BODY
+
+
+def test_aci(topology, complex_aci):
+    """Checks content of previously added aci"""
+
+    acis = topology.standalone.aci.list('cn=testgroup,%s' % DEFAULT_SUFFIX)
+    assert len(acis) == 1
+    aci = acis[0]
+    assert aci.acidata == {
+        'allow': [{'values': ['read', 'search', 'write']}],
+        'target': [], 'targetattr': [{'values': ['uniqueMember', 'member'], 'equal': True}],
+        'targattrfilters': [],
+        'deny': [],
+        'acl': [{'values': ['Allow test aci']}],
+        'deny_raw_bindrules': [],
+        'targetattrfilters': [],
+        'allow_raw_bindrules': [{'values': [(
+            'userdn="ldap:///dc=example,dc=com??sub?(ou=engineering)" and'
+            ' userdn="ldap:///dc=example,dc=com??sub?(manager=uid=wbrown,ou=managers,dc=example,dc=com)'
+            ' || ldap:///dc=example,dc=com??sub?(manager=uid=tbrown,ou=managers,dc=example,dc=com)" '
+             )]}],
+        'targetfilter': [{'values': ['(ou=groups)'], 'equal': True}],
+        'targetscope': [],
+        'version 3.0;': [],
+        'rawaci': complex_aci
+    }
+    assert aci.getRawAci() == complex_aci
+
+
+if __name__ == "__main__":
+    CURRENT_FILE = os.path.realpath(__file__)
+    pytest.main("-s -vv %s" % CURRENT_FILE)
-- 
2.4.3

