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