]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
tests/: test chage expiration date
authorIker Pedrosa <ipedrosa@redhat.com>
Tue, 4 Mar 2025 11:40:20 +0000 (12:40 +0100)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Mon, 2 Jun 2025 08:16:47 +0000 (10:16 +0200)
Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
Reviewed-by: Dan Lavu <dlavu@redhat.com>
tests/system/tests/test_chage.py [new file with mode: 0644]

diff --git a/tests/system/tests/test_chage.py b/tests/system/tests/test_chage.py
new file mode 100644 (file)
index 0000000..cece67b
--- /dev/null
@@ -0,0 +1,127 @@
+"""
+Test chage
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from pytest_mh.conn import ProcessError
+from framework.roles.shadow import Shadow
+from framework.topology import KnownTopology
+
+
+@pytest.mark.topology(KnownTopology.Shadow)
+@pytest.mark.parametrize(
+    "expiration_date, expected_date",
+    [
+        ("0", 0),  # 1970-01-01
+        ("1", 1),  # 1970-01-02
+        ("20089", 20089),  # 2025-01-01
+        ("1000000", 1000000),  # This will happen in a very long time
+        ("1970-01-01", 0),
+        ("1970-01-02", 1),
+        ("2025-01-01", 20089),
+    ],
+)
+def test_chage__set_expire_date_with_valid_date(shadow: Shadow, expiration_date: str, expected_date: int | None):
+    """
+    :title: Set valid account expiration date
+    :setup:
+        1. Create user
+    :steps:
+        1. Set valid account expiration date
+        2. Check user exists and expiration date
+    :expectedresults:
+        1. The expiration date is correctly set
+        2. User is found and expiration date is valid
+    :customerscenario: False
+    """
+    shadow.useradd("tuser1")
+
+    shadow.chage(f"-E {expiration_date} tuser1")
+
+    result = shadow.tools.getent.shadow("tuser1")
+    assert result is not None, "User should be found"
+    assert result.name == "tuser1", "Incorrect username"
+    assert result.expiration_date == expected_date, "Incorrect expiration date"
+
+
+@pytest.mark.topology(KnownTopology.Shadow)
+@pytest.mark.parametrize(
+    "expiration_date",
+    [
+        "-2",  # Dates can't be in negative numbers
+        "-1000",  # Dates can't be in negative numbers
+        "2025-18-18",  # That month and day don't exist
+        "1969-01-01",  # This is before 1970-01-01
+        "2025-13-01",  # That month doesn't exist
+        "2025-01-32",  # That day doesn't exist
+        "today",
+        "tomorrow",
+    ],
+)
+def test_chage__set_expire_date_with_invalid_date(shadow: Shadow, expiration_date: str):
+    """
+    :title: Set invalid account expiration date
+    :setup:
+        1. Create user
+    :steps:
+        1. Set invalid account expiration date
+        2. Check user exists and expiration date
+    :expectedresults:
+        1. The process fails and the expiration date isn't changed
+        2. User is found and expiration date is empty
+    :customerscenario: False
+    """
+    shadow.useradd("tuser1")
+
+    with pytest.raises(ProcessError):
+        shadow.chage(f"-E {expiration_date} tuser1")
+
+    result = shadow.tools.getent.shadow("tuser1")
+    assert result is not None, "User should be found"
+    assert result.name == "tuser1", "Incorrect username"
+    assert result.expiration_date is None, "Expiration date should be empty"
+
+
+@pytest.mark.topology(KnownTopology.Shadow)
+@pytest.mark.parametrize(
+    "expiration_date",
+    [
+        "-1",
+        "''",
+    ],
+)
+def test_chage__set_expire_date_with_empty_date(shadow: Shadow, expiration_date: str):
+    """
+    :title: Set empty account expiration date
+    :setup:
+        1. Create user
+    :steps:
+        1. Set account expiration date
+        2. Check user exists and expiration date
+        3. Empty account expiration date
+        4. Check user exists and expiration date
+    :expectedresults:
+        1. The expiration date is correctly set
+        2. User is found and expiration date is valid
+        3. The expiration date is correctly emptied
+        4. User is found and expiration date is empty
+    :customerscenario: False
+    """
+    shadow.useradd("tuser1")
+
+    shadow.chage("-E 10 tuser1")
+
+    result = shadow.tools.getent.shadow("tuser1")
+    assert result is not None, "User should be found"
+    assert result.name == "tuser1", "Incorrect username"
+    assert result.expiration_date == 10, "Incorrect expiration date"
+
+    shadow.chage(f"-E {expiration_date} tuser1")
+
+    result = shadow.tools.getent.shadow("tuser1")
+    assert result is not None, "User should be found"
+    assert result.name == "tuser1", "Incorrect username"
+    assert result.expiration_date is None, "Expiration date should be empty"