From 355231234ddeb75095b761abcb15300979838bf3 Mon Sep 17 00:00:00 2001
From: Akshay Adhikari <aadhikar@redhat.com>
Date: Mar 29 2018 08:50:10 +0000
Subject: Issue 49585 - Add py3 support to password test suite


Description: Added py3 support by explicitly changing strings to bytes.

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

Reviewed by: spichugi

---

diff --git a/dirsrvtests/tests/suites/password/pwdPolicy_attribute_test.py b/dirsrvtests/tests/suites/password/pwdPolicy_attribute_test.py
index b3ef61d..64c82e4 100644
--- a/dirsrvtests/tests/suites/password/pwdPolicy_attribute_test.py
+++ b/dirsrvtests/tests/suites/password/pwdPolicy_attribute_test.py
@@ -67,7 +67,7 @@ def password_policy(topology_st, test_user):
     try:
         topology_st.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE,
                                                      'nsslapd-pwpolicy-local',
-                                                     'on')])
+                                                     b'on')])
     except ldap.LDAPError as e:
         log.error('Failed to set fine-grained policy: error {}'.format(
             e.message['desc']))
@@ -88,7 +88,7 @@ def password_policy(topology_st, test_user):
     try:
         topology_st.standalone.modify_s(OU_PEOPLE, [(ldap.MOD_REPLACE,
                                                      'pwdpolicysubentry',
-                                                     PW_POLICY_CONT_PEOPLE)])
+                                                     ensure_bytes(PW_POLICY_CONT_PEOPLE))])
     except ldap.LDAPError as e:
         log.error('Failed to pwdpolicysubentry pw policy ' \
                   'policy for {}: error {}'.format(OU_PEOPLE,
@@ -110,7 +110,7 @@ def password_policy(topology_st, test_user):
     try:
         topology_st.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_REPLACE,
                                                         'pwdpolicysubentry',
-                                                        PW_POLICY_CONT_USER)])
+                                                        ensure_bytes(PW_POLICY_CONT_USER))])
     except ldap.LDAPError as e:
         log.error('Failed to pwdpolicysubentry pw policy ' \
                   'policy for {}: error {}'.format(TEST_USER_DN,
@@ -150,7 +150,7 @@ def test_change_pwd(topology_st, test_user, password_policy,
     try:
         topology_st.standalone.modify_s(PW_POLICY_CONT_PEOPLE, [(ldap.MOD_REPLACE,
                                                                  'passwordChange',
-                                                                 subtree_pwchange)])
+                                                                 ensure_bytes(subtree_pwchange))])
     except ldap.LDAPError as e:
         log.error('Failed to set passwordChange ' \
                   'policy for {}: error {}'.format(PW_POLICY_CONT_PEOPLE,
@@ -162,7 +162,7 @@ def test_change_pwd(topology_st, test_user, password_policy,
     try:
         topology_st.standalone.modify_s(PW_POLICY_CONT_USER, [(ldap.MOD_REPLACE,
                                                                'passwordChange',
-                                                               user_pwchange)])
+                                                               ensure_bytes(user_pwchange))])
     except ldap.LDAPError as e:
         log.error('Failed to set passwordChange ' \
                   'policy for {}: error {}'.format(PW_POLICY_CONT_USER,
@@ -177,11 +177,11 @@ def test_change_pwd(topology_st, test_user, password_policy,
             with pytest.raises(exception):
                 topology_st.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_REPLACE,
                                                                 'userPassword',
-                                                                'new_pass')])
+                                                                b'new_pass')])
         else:
             topology_st.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_REPLACE,
                                                             'userPassword',
-                                                            'new_pass')])
+                                                            b'new_pass')])
     except ldap.LDAPError as e:
         log.error('Failed to change userpassword for {}: error {}'.format(
             TEST_USER_DN, e.message['info']))
@@ -191,7 +191,7 @@ def test_change_pwd(topology_st, test_user, password_policy,
         topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
         topology_st.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_REPLACE,
                                                         'userPassword',
-                                                        TEST_USER_PWD)])
+                                                        ensure_bytes(TEST_USER_PWD))])
 
 
 def test_pwd_min_age(topology_st, test_user, password_policy):
@@ -228,7 +228,7 @@ def test_pwd_min_age(topology_st, test_user, password_policy):
     try:
         topology_st.standalone.modify_s(PW_POLICY_CONT_PEOPLE, [(ldap.MOD_REPLACE,
                                                                  'passwordminage',
-                                                                 num_seconds)])
+                                                                 ensure_bytes(num_seconds))])
     except ldap.LDAPError as e:
         log.error('Failed to set passwordminage ' \
                   'policy for {}: error {}'.format(PW_POLICY_CONT_PEOPLE,
@@ -239,7 +239,7 @@ def test_pwd_min_age(topology_st, test_user, password_policy):
     try:
         topology_st.standalone.modify_s(PW_POLICY_CONT_USER, [(ldap.MOD_REPLACE,
                                                                'passwordminage',
-                                                               num_seconds)])
+                                                               ensure_bytes(num_seconds))])
     except ldap.LDAPError as e:
         log.error('Failed to set passwordminage ' \
                   'policy for {}: error {}'.format(PW_POLICY_CONT_USER,
@@ -250,7 +250,7 @@ def test_pwd_min_age(topology_st, test_user, password_policy):
     try:
         topology_st.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE,
                                                      'passwordminage',
-                                                     num_seconds)])
+                                                     ensure_bytes(num_seconds))])
     except ldap.LDAPError as e:
         log.error('Failed to set passwordminage ' \
                   'policy for {}: error {}'.format(DN_CONFIG,
@@ -263,7 +263,7 @@ def test_pwd_min_age(topology_st, test_user, password_policy):
         topology_st.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PWD)
         topology_st.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_REPLACE,
                                                         'userPassword',
-                                                        'new_pass')])
+                                                        b'new_pass')])
     except ldap.LDAPError as e:
         log.error('Failed to change userpassword for {}: error {}'.format(
             TEST_USER_DN, e.message['info']))
@@ -275,7 +275,7 @@ def test_pwd_min_age(topology_st, test_user, password_policy):
     with pytest.raises(ldap.CONSTRAINT_VIOLATION):
         topology_st.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_REPLACE,
                                                         'userPassword',
-                                                        'new_new_pass')])
+                                                        b'new_new_pass')])
 
     log.info('Wait {} second'.format(int(num_seconds) + 2))
     time.sleep(int(num_seconds) + 2)
@@ -285,7 +285,7 @@ def test_pwd_min_age(topology_st, test_user, password_policy):
         topology_st.standalone.simple_bind_s(TEST_USER_DN, 'new_pass')
         topology_st.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_REPLACE,
                                                         'userPassword',
-                                                        TEST_USER_PWD)])
+                                                        ensure_bytes(TEST_USER_PWD))])
     except ldap.LDAPError as e:
         log.error('Failed to change userpassword for {}: error {}'.format(
             TEST_USER_DN, e.message['info']))
@@ -295,7 +295,7 @@ def test_pwd_min_age(topology_st, test_user, password_policy):
         topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
         topology_st.standalone.modify_s(TEST_USER_DN, [(ldap.MOD_REPLACE,
                                                         'userPassword',
-                                                        TEST_USER_PWD)])
+                                                        ensure_bytes(TEST_USER_PWD))])
 
 
 if __name__ == '__main__':
diff --git a/dirsrvtests/tests/suites/password/pwdPolicy_inherit_global_test.py b/dirsrvtests/tests/suites/password/pwdPolicy_inherit_global_test.py
index 823cfda..93d6db5 100644
--- a/dirsrvtests/tests/suites/password/pwdPolicy_inherit_global_test.py
+++ b/dirsrvtests/tests/suites/password/pwdPolicy_inherit_global_test.py
@@ -12,9 +12,11 @@ import time
 
 import ldap
 import pytest
+from lib389.utils import *
 from lib389 import Entry
 from lib389._constants import *
 from lib389.topologies import topology_st
+from lib389.idm.organisationalunit import OrganisationalUnits
 
 logging.getLogger(__name__).setLevel(logging.INFO)
 log = logging.getLogger(__name__)
@@ -50,8 +52,9 @@ def test_user(topology_st, request):
                                              'userPassword': PASSWORD})))
         log.info('Adding an aci for the bind user')
         BN_ACI = '(targetattr="*")(version 3.0; acl "pwp test"; allow (all) userdn="ldap:///%s";)' % BN
-        topology_st.standalone.modify_s(OU_PEOPLE, [(ldap.MOD_ADD, 'aci', BN_ACI)])
-
+        ous = OrganisationalUnits(topology_st.standalone, DEFAULT_SUFFIX)
+        ou_people = ous.get('people')
+        ou_people.add('aci', BN_ACI)
     except ldap.LDAPError as e:
         log.error('Failed to add user (%s): error (%s)' % (BN,
                                                            e.message['desc']))
@@ -60,7 +63,9 @@ def test_user(topology_st, request):
     def fin():
         log.info('Deleting user {}'.format(BN))
         topology_st.standalone.delete_s(BN)
-        topology_st.standalone.modify_s(OU_PEOPLE, [(ldap.MOD_DELETE, 'aci', BN_ACI)])
+        ous = OrganisationalUnits(topology_st.standalone, DEFAULT_SUFFIX)
+        ou_people = ous.get('people')
+        ou_people.remove('aci', BN_ACI)
 
     request.addfinalizer(fin)
 
@@ -76,9 +81,7 @@ def password_policy(topology_st, test_user):
 
     log.info('Enable fine-grained policy')
     try:
-        topology_st.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE,
-                                                     'nsslapd-pwpolicy-local',
-                                                     'on')])
+        topology_st.standalone.config.set('nsslapd-pwpolicy-local', 'on')
     except ldap.LDAPError as e:
         log.error('Failed to set fine-grained policy: error {}'.format(
             e.message['desc']))
@@ -97,9 +100,9 @@ def password_policy(topology_st, test_user):
 
     log.info('Add pwdpolicysubentry attribute to {}'.format(OU_PEOPLE))
     try:
-        topology_st.standalone.modify_s(OU_PEOPLE, [(ldap.MOD_REPLACE,
-                                                     'pwdpolicysubentry',
-                                                     PWP_CONTAINER_PEOPLE)])
+        ous = OrganisationalUnits(topology_st.standalone, DEFAULT_SUFFIX)
+        ou_people = ous.get('people')
+        ou_people.set('pwdpolicysubentry', PWP_CONTAINER_PEOPLE)
     except ldap.LDAPError as e:
         log.error('Failed to pwdpolicysubentry pw policy ' \
                   'policy for {}: error {}'.format(OU_PEOPLE,
@@ -108,11 +111,11 @@ def password_policy(topology_st, test_user):
 
     log.info("Set the default settings for the policy container.")
     topology_st.standalone.modify_s(PWP_CONTAINER_PEOPLE,
-                                    [(ldap.MOD_REPLACE, 'passwordMustChange', 'off'),
-                                     (ldap.MOD_REPLACE, 'passwordExp', 'off'),
-                                     (ldap.MOD_REPLACE, 'passwordMinAge', '0'),
-                                     (ldap.MOD_REPLACE, 'passwordChange', 'off'),
-                                     (ldap.MOD_REPLACE, 'passwordStorageScheme', 'ssha')])
+                                    [(ldap.MOD_REPLACE, 'passwordMustChange', b'off'),
+                                     (ldap.MOD_REPLACE, 'passwordExp', b'off'),
+                                     (ldap.MOD_REPLACE, 'passwordMinAge', b'0'),
+                                     (ldap.MOD_REPLACE, 'passwordChange', b'off'),
+                                     (ldap.MOD_REPLACE, 'passwordStorageScheme', b'ssha')])
 
     check_attr_val(topology_st, CONFIG_DN, ATTR_INHERIT_GLOBAL, 'off')
     check_attr_val(topology_st, CONFIG_DN, ATTR_CHECK_SYNTAX, 'off')
@@ -126,7 +129,7 @@ def check_attr_val(topology_st, dn, attr, expected):
         assert centry[0], 'Failed to get %s' % dn
 
         val = centry[0].getValue(attr)
-        assert val == expected, 'Default value of %s is not %s, but %s' % (
+        assert str(val, 'utf-8') == expected, 'Default value of %s is not %s, but %s' % (
             attr, expected, val)
 
         log.info('Default value of %s is %s' % (attr, expected))
@@ -160,10 +163,8 @@ def test_entry_has_no_restrictions(topology_st, password_policy, test_user,
 
     log.info('Set {} to {}'.format(ATTR_INHERIT_GLOBAL, inherit_value))
     log.info('Set {} to {}'.format(ATTR_CHECK_SYNTAX, checksyntax_value))
-    topology_st.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE,
-                                                 ATTR_INHERIT_GLOBAL, inherit_value)])
-    topology_st.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE,
-                                                 ATTR_CHECK_SYNTAX, checksyntax_value)])
+    topology_st.standalone.config.set(ATTR_INHERIT_GLOBAL, inherit_value)
+    topology_st.standalone.config.set(ATTR_CHECK_SYNTAX, checksyntax_value)
 
     # Wait a second for cn=config to apply
     time.sleep(1)
@@ -229,12 +230,10 @@ def test_entry_has_restrictions(topology_st, password_policy, test_user, contain
 
     log.info('Set {} to {}'.format(ATTR_INHERIT_GLOBAL, 'on'))
     log.info('Set {} to {}'.format(ATTR_CHECK_SYNTAX, 'on'))
-    topology_st.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE,
-                                                 ATTR_INHERIT_GLOBAL, 'on')])
-    topology_st.standalone.modify_s(CONFIG_DN, [(ldap.MOD_REPLACE,
-                                                 ATTR_CHECK_SYNTAX, 'on')])
+    topology_st.standalone.config.set(ATTR_INHERIT_GLOBAL, 'on')
+    topology_st.standalone.config.set(ATTR_CHECK_SYNTAX, 'on')
     topology_st.standalone.modify_s(container, [(ldap.MOD_REPLACE,
-                                                 'passwordMinLength', '9')])
+                                                 'passwordMinLength', b'9')])
 
     # Wait a second for cn=config to apply
     time.sleep(1)

