5144c6
From 9479a393a71fe1de7d62ca2b50a7d3d8698d4ba1 Mon Sep 17 00:00:00 2001
5144c6
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fcami@redhat.com>
5144c6
Date: Tue, 4 Aug 2020 11:05:31 +0200
5144c6
Subject: [PATCH] ipatests: tasks.py: fix ipa-epn invocation
5144c6
MIME-Version: 1.0
5144c6
Content-Type: text/plain; charset=UTF-8
5144c6
Content-Transfer-Encoding: 8bit
5144c6
5144c6
tasks.py::ipa_epn would previously fail to invoke ipa-epn with
5144c6
from_nbdays=0.
5144c6
5144c6
Related: https://pagure.io/freeipa/issue/8449
5144c6
Signed-off-by: François Cami <fcami@redhat.com>
5144c6
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
5144c6
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
5144c6
---
5144c6
 ipatests/pytest_ipa/integration/tasks.py | 4 ++--
5144c6
 1 file changed, 2 insertions(+), 2 deletions(-)
5144c6
5144c6
diff --git a/ipatests/pytest_ipa/integration/tasks.py b/ipatests/pytest_ipa/integration/tasks.py
5144c6
index a3f7cc838..c0a592750 100755
5144c6
--- a/ipatests/pytest_ipa/integration/tasks.py
5144c6
+++ b/ipatests/pytest_ipa/integration/tasks.py
5144c6
@@ -1470,9 +1470,9 @@ def ipa_epn(
5144c6
         cmd.append("--dry-run")
5144c6
     if mailtest:
5144c6
         cmd.append("--mail-test")
5144c6
-    if from_nbdays:
5144c6
+    if from_nbdays is not None:
5144c6
         cmd.extend(("--from-nbdays", str(from_nbdays)))
5144c6
-    if to_nbdays:
5144c6
+    if to_nbdays is not None:
5144c6
         cmd.extend(("--to-nbdays", str(to_nbdays)))
5144c6
     return host.run_command(cmd, raiseonerr=raiseonerr)
5144c6
 
5144c6
-- 
5144c6
2.26.2
5144c6
5144c6
From 3b8fdd87760cfb8ec739c67298f012cf0bd3ac39 Mon Sep 17 00:00:00 2001
5144c6
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fcami@redhat.com>
5144c6
Date: Wed, 5 Aug 2020 10:02:31 +0200
5144c6
Subject: [PATCH] ipatests: test_epn: test_EPN_nbdays enhancements
5144c6
MIME-Version: 1.0
5144c6
Content-Type: text/plain; charset=UTF-8
5144c6
Content-Transfer-Encoding: 8bit
5144c6
5144c6
Enhance test_EPN_nbdays so that it checks:
5144c6
* that no emails get sent when using --dry-run
5144c6
* that --from-nbdays implies --dry-run
5144c6
* that --to-nbdays requires --from-nbdays
5144c6
* illegal inputs for nbdays:
5144c6
** from-nbdays > to-nbdays
5144c6
** non-numerical input
5144c6
** decimal input
5144c6
5144c6
Fixes: https://pagure.io/freeipa/issue/8449
5144c6
Signed-off-by: François Cami <fcami@redhat.com>
5144c6
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
5144c6
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
5144c6
---
5144c6
 ipatests/test_integration/test_epn.py | 130 +++++++++++++++++++++++---
5144c6
 1 file changed, 117 insertions(+), 13 deletions(-)
5144c6
5144c6
diff --git a/ipatests/test_integration/test_epn.py b/ipatests/test_integration/test_epn.py
5144c6
index f4c123c6d..18f73c722 100644
5144c6
--- a/ipatests/test_integration/test_epn.py
5144c6
+++ b/ipatests/test_integration/test_epn.py
5144c6
@@ -15,6 +15,13 @@
5144c6
 # You should have received a copy of the GNU General Public License
5144c6
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
5144c6
 
5144c6
+######
5144c6
+# This test suite will _expectedly_ fail if run at the end of the UTC day
5144c6
+# because users would be created during day N and then EPN output checked
5144c6
+# during day N+1. This is expected and should be ignored as it does not
5144c6
+# reflect a product bug. -- fcami
5144c6
+######
5144c6
+
5144c6
 from __future__ import print_function, absolute_import
5144c6
 
5144c6
 import base64
5144c6
@@ -178,12 +185,14 @@ class TestEPN(IntegrationTest):
5144c6
         from_nbdays=None,
5144c6
         to_nbdays=None,
5144c6
         raiseonerr=True,
5144c6
+        validatejson=True
5144c6
     ):
5144c6
         result = tasks.ipa_epn(host, raiseonerr=raiseonerr, dry_run=dry_run,
5144c6
                                from_nbdays=from_nbdays,
5144c6
                                to_nbdays=to_nbdays)
5144c6
-        json.dumps(json.loads(result.stdout_text), ensure_ascii=False)
5144c6
-        return (result.stdout_text, result.stderr_text)
5144c6
+        if validatejson:
5144c6
+            json.dumps(json.loads(result.stdout_text), ensure_ascii=False)
5144c6
+        return (result.stdout_text, result.stderr_text, result.returncode)
5144c6
 
5144c6
     @classmethod
5144c6
     def install(cls, mh):
5144c6
@@ -244,12 +253,12 @@ class TestEPN(IntegrationTest):
5144c6
         ''')
5144c6
         self.master.put_file_contents('/etc/ipa/epn.conf', epn_conf)
5144c6
         # check EPN on client (LDAP+GSSAPI)
5144c6
-        (stdout_text, unused) = self._check_epn_output(
5144c6
+        (stdout_text, unused, _unused) = self._check_epn_output(
5144c6
             self.clients[0], dry_run=True
5144c6
         )
5144c6
         assert len(json.loads(stdout_text)) == 0
5144c6
         # check EPN on master (LDAPI)
5144c6
-        (stdout_text, unused) = self._check_epn_output(
5144c6
+        (stdout_text, unused, _unused) = self._check_epn_output(
5144c6
             self.master, dry_run=True
5144c6
         )
5144c6
         assert len(json.loads(stdout_text)) == 0
5144c6
@@ -292,10 +301,10 @@ class TestEPN(IntegrationTest):
5144c6
                 ),
5144c6
             ],
5144c6
         )
5144c6
-        (stdout_text_client, unused) = self._check_epn_output(
5144c6
+        (stdout_text_client, unused, _unused) = self._check_epn_output(
5144c6
             self.clients[0], dry_run=True
5144c6
         )
5144c6
-        (stdout_text_master, unused) = self._check_epn_output(
5144c6
+        (stdout_text_master, unused, _unused) = self._check_epn_output(
5144c6
             self.master, dry_run=True
5144c6
         )
5144c6
         assert stdout_text_master == stdout_text_client
5144c6
@@ -331,10 +340,10 @@ class TestEPN(IntegrationTest):
5144c6
                 password=None,
5144c6
             )
5144c6
 
5144c6
-        (stdout_text_client, unused) = self._check_epn_output(
5144c6
+        (stdout_text_client, unused, _unused) = self._check_epn_output(
5144c6
             self.clients[0], dry_run=True
5144c6
         )
5144c6
-        (stdout_text_master, unused) = self._check_epn_output(
5144c6
+        (stdout_text_master, unused, _unused) = self._check_epn_output(
5144c6
             self.master, dry_run=True
5144c6
         )
5144c6
         assert stdout_text_master == stdout_text_client
5144c6
@@ -344,22 +353,117 @@ class TestEPN(IntegrationTest):
5144c6
         expected_users = ["user1", "user3", "user7", "user14", "user28"]
5144c6
         assert sorted(user_lst) == sorted(expected_users)
5144c6
 
5144c6
-    def test_EPN_nbdays(self):
5144c6
+    def test_EPN_nbdays_0(self, cleanupmail):
5144c6
         """Test the to/from nbdays options (implies --dry-run)
5144c6
 
5144c6
            We have a set of users installed with varying expiration
5144c6
            dates. Confirm that to/from nbdays finds them.
5144c6
+
5144c6
+           Make sure --dry-run does not accidentally send emails.
5144c6
         """
5144c6
 
5144c6
-        # Compare the notify_ttls values
5144c6
+        # Use the notify_ttls values with a 1-day sliding window
5144c6
         for i in self.notify_ttls:
5144c6
             user_list = []
5144c6
-            (stdout_text_client, unused) = self._check_epn_output(
5144c6
-                self.clients[0], from_nbdays=i, to_nbdays=i + 1, dry_run=True)
5144c6
+            (stdout_text_client, unused, _unused) = self._check_epn_output(
5144c6
+                self.clients[0], from_nbdays=i, to_nbdays=i + 1, dry_run=True
5144c6
+            )
5144c6
             for user in json.loads(stdout_text_client):
5144c6
                 user_list.append(user["uid"])
5144c6
             assert len(user_list) == 1
5144c6
-            assert user_list[0] == "user%d" % i
5144c6
+            userid = "user{id}".format(id=i)
5144c6
+            assert user_list[0] == userid
5144c6
+
5144c6
+            # Check that the user list is expected for any given notify_ttls.
5144c6
+            (stdout_text_client, unused, _unused) = self._check_epn_output(
5144c6
+                self.clients[0], to_nbdays=i
5144c6
+            )
5144c6
+            user_list = [user["uid"] for user in json.loads(stdout_text_client)]
5144c6
+            assert len(user_list) == 1
5144c6
+            assert user_list[0] == "user{id}".format(id=i - 1)
5144c6
+
5144c6
+            # make sure no emails were sent
5144c6
+            result = self.clients[0].run_command(['ls', '-lha', '/var/mail/'])
5144c6
+            assert userid not in result.stdout_text
5144c6
+
5144c6
+    def test_EPN_nbdays_1(self, cleanupmail):
5144c6
+        """Test that for a given range, we find the users in that range"""
5144c6
+
5144c6
+        # Use hardcoded date ranges for now
5144c6
+        for date_range in [(0, 5), (7, 15), (1, 20)]:
5144c6
+            expected_user_list = ["user{i}".format(i=i)
5144c6
+                                  for i in range(date_range[0], date_range[1])]
5144c6
+            (stdout_text_client, unused, _unused) = self._check_epn_output(
5144c6
+                self.clients[0],
5144c6
+                from_nbdays=date_range[0],
5144c6
+                to_nbdays=date_range[1]
5144c6
+            )
5144c6
+            user_list = [user["uid"] for user in json.loads(stdout_text_client)]
5144c6
+            for user in expected_user_list:
5144c6
+                assert user in user_list
5144c6
+            for user in user_list:
5144c6
+                assert user in expected_user_list
5144c6
+
5144c6
+    # Test the to/from nbdays options behavior with illegal input
5144c6
+
5144c6
+    def test_EPN_nbdays_input_0(self):
5144c6
+        """Make sure that --to-nbdays implies --dry-run ;
5144c6
+           therefore check that the output is valid JSON and contains the
5144c6
+           expected user.
5144c6
+        """
5144c6
+
5144c6
+        (stdout_text_client, unused, _unused) = self._check_epn_output(
5144c6
+            self.clients[0], to_nbdays=5, dry_run=False
5144c6
+        )
5144c6
+        assert len(json.loads(stdout_text_client)) == 1
5144c6
+        assert json.loads(stdout_text_client)[0]["uid"] == "user4"
5144c6
+
5144c6
+    def test_EPN_nbdays_input_1(self):
5144c6
+        """Make sure that --from-nbdays cannot be used without --to-nbdays"""
5144c6
+
5144c6
+        (unused, stderr_text_client, rc) = \
5144c6
+            self._check_epn_output(
5144c6
+            self.clients[0], from_nbdays=3,
5144c6
+            raiseonerr=False, validatejson=False
5144c6
+        )
5144c6
+        assert "You cannot specify --from-nbdays without --to-nbdays" \
5144c6
+            in stderr_text_client
5144c6
+        assert rc > 0
5144c6
+
5144c6
+    @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True)
5144c6
+    def test_EPN_nbdays_input_2(self):
5144c6
+        """alpha input"""
5144c6
+
5144c6
+        (unused, stderr, rc) = self._check_epn_output(
5144c6
+            self.clients[0], to_nbdays="abc",
5144c6
+            raiseonerr=False, validatejson=False
5144c6
+        )
5144c6
+        assert "error: --to-nbdays must be an integer." in stderr
5144c6
+        assert rc > 0
5144c6
+
5144c6
+    @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True)
5144c6
+    def test_EPN_nbdays_input_3(self):
5144c6
+        """from_nbdays > to_nbdays"""
5144c6
+
5144c6
+        (unused, stderr, rc) = self._check_epn_output(
5144c6
+            self.clients[0], from_nbdays=9, to_nbdays=7,
5144c6
+            raiseonerr=False, validatejson=False
5144c6
+        )
5144c6
+        assert "error: --from-nbdays must be smaller than --to-nbdays." in \
5144c6
+            stderr
5144c6
+        assert rc > 0
5144c6
+
5144c6
+    @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True)
5144c6
+    def test_EPN_nbdays_input_4(self):
5144c6
+        """decimal input"""
5144c6
+
5144c6
+        (unused, stderr, rc) = self._check_epn_output(
5144c6
+            self.clients[0], to_nbdays=7.3,
5144c6
+            raiseonerr=False, validatejson=False
5144c6
+        )
5144c6
+        logger.info(stderr)
5144c6
+        assert rc > 0
5144c6
+        assert "error: --to-nbdays must be an integer." in stderr
5144c6
 
5144c6
     # From here the tests build on one another:
5144c6
     #  1) add auth
5144c6
-- 
5144c6
2.26.2
5144c6
5144c6
From b4266023e04729db12de2f7e0de4da9e1d00db38 Mon Sep 17 00:00:00 2001
5144c6
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fcami@redhat.com>
5144c6
Date: Fri, 7 Aug 2020 19:08:39 +0200
5144c6
Subject: [PATCH] ipatests: test_epn: update error messages
5144c6
MIME-Version: 1.0
5144c6
Content-Type: text/plain; charset=UTF-8
5144c6
Content-Transfer-Encoding: 8bit
5144c6
5144c6
Update error messages in the test.
5144c6
5144c6
Fixes: https://pagure.io/freeipa/issue/8449
5144c6
Signed-off-by: François Cami <fcami@redhat.com>
5144c6
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
5144c6
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
5144c6
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
5144c6
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
5144c6
---
5144c6
 ipatests/test_integration/test_epn.py | 4 ++--
5144c6
 1 file changed, 2 insertions(+), 2 deletions(-)
5144c6
5144c6
diff --git a/ipatests/test_integration/test_epn.py b/ipatests/test_integration/test_epn.py
5144c6
index e03521193..af662140a 100644
5144c6
--- a/ipatests/test_integration/test_epn.py
5144c6
+++ b/ipatests/test_integration/test_epn.py
5144c6
@@ -458,7 +458,7 @@ class TestEPN(IntegrationTest):
5144c6
             self.clients[0], to_nbdays="abc",
5144c6
             raiseonerr=False, validatejson=False
5144c6
         )
5144c6
-        assert "error: --to-nbdays must be an integer." in stderr
5144c6
+        assert "error: --to-nbdays must be a positive integer." in stderr
5144c6
         assert rc > 0
5144c6
 
5144c6
     @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True)
5144c6
@@ -483,7 +483,7 @@ class TestEPN(IntegrationTest):
5144c6
         )
5144c6
         logger.info(stderr)
5144c6
         assert rc > 0
5144c6
-        assert "error: --to-nbdays must be an integer." in stderr
5144c6
+        assert "error: --to-nbdays must be a positive integer." in stderr
5144c6
 
5144c6
     # From here the tests build on one another:
5144c6
     #  1) add auth
5144c6
-- 
5144c6
2.26.2
5144c6
5144c6
From 2809084a44e3b174fa48a611e79f04358e1d6dca Mon Sep 17 00:00:00 2001
5144c6
From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= <fcami@redhat.com>
5144c6
Date: Wed, 5 Aug 2020 09:05:31 +0200
5144c6
Subject: [PATCH] IPA-EPN: enhance input validation
5144c6
MIME-Version: 1.0
5144c6
Content-Type: text/plain; charset=UTF-8
5144c6
Content-Transfer-Encoding: 8bit
5144c6
5144c6
Enhance input validation:
5144c6
* make sure --from-nbdays and --to-nbdays are integer
5144c6
* make sure --from-nbdays < --to-nbdays
5144c6
5144c6
Fixes: https://pagure.io/freeipa/issue/8444
5144c6
Signed-off-by: François Cami <fcami@redhat.com>
5144c6
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
5144c6
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
5144c6
Reviewed-By: Rob Crittenden <rcritten@redhat.com>
5144c6
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
5144c6
---
5144c6
 ipaclient/install/ipa_epn.py          | 28 +++++++++++++++++++++++++--
5144c6
 ipatests/test_integration/test_epn.py |  3 ---
5144c6
 2 files changed, 26 insertions(+), 5 deletions(-)
5144c6
5144c6
diff --git a/ipaclient/install/ipa_epn.py b/ipaclient/install/ipa_epn.py
5144c6
index 82d7b3f57..88c926e88 100644
5144c6
--- a/ipaclient/install/ipa_epn.py
5144c6
+++ b/ipaclient/install/ipa_epn.py
5144c6
@@ -246,9 +246,33 @@ class EPN(admintool.AdminTool):
5144c6
 
5144c6
     def validate_options(self):
5144c6
         super(EPN, self).validate_options(needs_root=True)
5144c6
-        if self.options.to_nbdays:
5144c6
+        if self.options.to_nbdays is not None:
5144c6
+            try:
5144c6
+                if int(self.options.to_nbdays) < 0:
5144c6
+                    raise RuntimeError('Input is negative.')
5144c6
+            except Exception as e:
5144c6
+                self.option_parser.error(
5144c6
+                    "--to-nbdays must be a positive integer. "
5144c6
+                    "{error}".format(error=e)
5144c6
+                )
5144c6
             self.options.dry_run = True
5144c6
-        if self.options.from_nbdays and not self.options.to_nbdays:
5144c6
+        if self.options.from_nbdays is not None:
5144c6
+            try:
5144c6
+                if int(self.options.from_nbdays) < 0:
5144c6
+                    raise RuntimeError('Input is negative.')
5144c6
+            except Exception as e:
5144c6
+                self.option_parser.error(
5144c6
+                    "--from-nbdays must be a positive integer. "
5144c6
+                    "{error}".format(error=e)
5144c6
+                )
5144c6
+        if self.options.from_nbdays is not None and \
5144c6
+                self.options.to_nbdays is not None:
5144c6
+            if int(self.options.from_nbdays) >= int(self.options.to_nbdays):
5144c6
+                self.option_parser.error(
5144c6
+                    "--from-nbdays must be smaller than --to-nbdays."
5144c6
+                )
5144c6
+        if self.options.from_nbdays is not None and \
5144c6
+                self.options.to_nbdays is None:
5144c6
             self.option_parser.error(
5144c6
                 "You cannot specify --from-nbdays without --to-nbdays"
5144c6
             )
5144c6
diff --git a/ipatests/test_integration/test_epn.py b/ipatests/test_integration/test_epn.py
5144c6
index af662140a..fc26888cb 100644
5144c6
--- a/ipatests/test_integration/test_epn.py
5144c6
+++ b/ipatests/test_integration/test_epn.py
5144c6
@@ -450,7 +450,6 @@ class TestEPN(IntegrationTest):
5144c6
             in stderr_text_client
5144c6
         assert rc > 0
5144c6
 
5144c6
-    @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True)
5144c6
     def test_EPN_nbdays_input_2(self):
5144c6
         """alpha input"""
5144c6
 
5144c6
@@ -461,7 +460,6 @@ class TestEPN(IntegrationTest):
5144c6
         assert "error: --to-nbdays must be a positive integer." in stderr
5144c6
         assert rc > 0
5144c6
 
5144c6
-    @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True)
5144c6
     def test_EPN_nbdays_input_3(self):
5144c6
         """from_nbdays > to_nbdays"""
5144c6
 
5144c6
@@ -473,7 +471,6 @@ class TestEPN(IntegrationTest):
5144c6
             stderr
5144c6
         assert rc > 0
5144c6
 
5144c6
-    @pytest.mark.xfail(reason='freeipa ticket 8444', strict=True)
5144c6
     def test_EPN_nbdays_input_4(self):
5144c6
         """decimal input"""
5144c6
 
5144c6
-- 
5144c6
2.26.2
5144c6