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