From bc3ea14c855d8ece62f7cccb27a2f8135ec866e1 Mon Sep 17 00:00:00 2001
From: Anuj Borah <aborah@redhat.com>
Date: Feb 21 2019 02:19:35 +0000
Subject: Issue: 50227 - Making an cosClassicDefinition type in src/lib389/lib389/cos.py


Making an cosClassicDefinition type in src/lib389/lib389/cos.py

https://pagure.io/389-ds-base/issue/50227

Reviewed by: William Brown

---

diff --git a/dirsrvtests/tests/suites/cos/cos_test.py b/dirsrvtests/tests/suites/cos/cos_test.py
new file mode 100644
index 0000000..98bed4d
--- /dev/null
+++ b/dirsrvtests/tests/suites/cos/cos_test.py
@@ -0,0 +1,83 @@
+# --- BEGIN COPYRIGHT BLOCK ---
+# Copyright (C) 2019 Red Hat, Inc.
+# All rights reserved.
+#
+# License: GPL (version 3 or any later version).
+# See LICENSE for details.
+# --- END COPYRIGHT BLOCK ---
+
+import pytest, os, ldap
+from lib389.cos import  CosClassicDefinition, CosClassicDefinitions, CosTemplate
+from lib389._constants import DEFAULT_SUFFIX
+from lib389.topologies import topology_st as topo
+from lib389.idm.nsrole import nsFilterRoles
+from lib389.idm.nscontainer import nsContainer
+from lib389.idm.user import UserAccount
+
+
+def test_positive(topo):
+    """
+        :id: ba6d5e9c-786b-11e8-860d-8c16451d917b
+        :setup: server
+        :steps:
+            1. Add filter role entry
+            2. Add ns container
+            3. Add cos template
+            4. Add CosClassic Definition
+            5. Cos entries should be added and searchable
+            6. employeeType attribute should be there in user entry as per the cos plugin property
+        :expectedresults:
+            1. Operation should success
+            2. Operation should success
+            3. Operation should success
+            4. Operation should success
+            5. Operation should success
+            6. Operation should success
+    """
+    # Adding ns filter role
+    roles = nsFilterRoles(topo.standalone, DEFAULT_SUFFIX)
+    roles.create(properties={'cn': 'FILTERROLEENGROLE',
+                             'nsRoleFilter': 'cn=eng*'})
+    # adding ns container
+    nsContainer(topo.standalone,'cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,{}'.format(DEFAULT_SUFFIX))\
+        .create(properties={'cn': 'cosTemplates'})
+
+    # creating cos template
+    properties = {'employeeType': 'EngType',
+                  'cn': '"cn=filterRoleEngRole,dc=example,dc=com",cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,dc=example,dc=com'
+                  }
+    CosTemplate(topo.standalone, 'cn="cn=filterRoleEngRole,dc=example,dc=com",cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,{}'.format(DEFAULT_SUFFIX))\
+        .create(properties=properties)
+
+    # creating CosClassicDefinition
+    properties = {'cosTemplateDn': 'cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,{}'.format(DEFAULT_SUFFIX),
+                  'cosAttribute': 'employeeType',
+                  'cosSpecifier': 'nsrole',
+                  'cn': 'cosClassicGenerateEmployeeTypeUsingnsrole'}
+    CosClassicDefinition(topo.standalone,'cn=cosClassicGenerateEmployeeTypeUsingnsrole,{}'.format(DEFAULT_SUFFIX))\
+        .create(properties=properties)
+
+    # Adding User entry
+    properties = {
+        'uid': 'enguser1',
+        'cn': 'enguser1',
+        'sn': 'user',
+        'uidNumber': '1000',
+        'gidNumber': '2000',
+        'homeDirectory': '/home/' + 'enguser1'
+    }
+    user = UserAccount(topo.standalone, 'cn=enguser1,{}'.format(DEFAULT_SUFFIX))
+    user.create(properties=properties)
+
+    # Asserting Cos should be added and searchable
+    cosdef = CosClassicDefinitions(topo.standalone, DEFAULT_SUFFIX).get('cosClassicGenerateEmployeeTypeUsingnsrole')
+    assert cosdef.dn == 'cn=cosClassicGenerateEmployeeTypeUsingnsrole,dc=example,dc=com'
+    assert cosdef.get_attr_val_utf8('cn') == 'cosClassicGenerateEmployeeTypeUsingnsrole'
+
+    #  CoS definition entry's cosSpecifier attribute specifies the employeeType attribute
+    assert user.present('employeeType')
+
+
+if __name__ == "__main__":
+    CURRENT_FILE = os.path.realpath(__file__)
+    pytest.main("-s -v %s" % CURRENT_FILE)
diff --git a/src/lib389/lib389/cos.py b/src/lib389/lib389/cos.py
index 0e15941..3f79087 100644
--- a/src/lib389/lib389/cos.py
+++ b/src/lib389/lib389/cos.py
@@ -156,3 +156,44 @@ class CosPointerDefinitions(DSLdapObjects):
             self._basedn = '{},{}'.format(ensure_str(rdn), ensure_str(basedn))
 
 
+class CosClassicDefinition(DSLdapObject):
+    """A Cos CosClassicDefinition associating a dn syntax type as a link
+        attr to a template type.
+
+        :param instance: DirSrv instance
+        :type instance: DirSrv
+        :param dn: The dn of the template
+        :type dn: str
+        """
+    def __init__(self, instance, dn=None):
+        super(CosClassicDefinition, self).__init__(instance, dn)
+        self._rdn_attribute = 'cn'
+        self._must_attributes = ['cn']
+        self._create_objectclasses = [
+            'top',
+            'cossuperdefinition',
+            'cosClassicDefinition',
+        ]
+        self._protected = False
+
+
+class CosClassicDefinitions(DSLdapObjects):
+    """The set of cos CosClassicDefinition  that exist.
+    :param instance: A dirsrv instance
+    :type instance: DirSrv
+    :param basedn: The basedn of the templates
+    :type basedn: str
+    :param rdn: The rdn of the templates
+    :type rdn: str
+    """
+
+    def __init__(self, instance, basedn):
+        super(CosClassicDefinitions, self).__init__(instance)
+        self._objectclasses = [
+            'top',
+            'cossuperdefinition',
+            'cosClassicDefinition',
+        ]
+        self._filterattrs = ['cn']
+        self._childobject = CosClassicDefinition
+        self._basedn = basedn

