]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
tests/: test useradd expiration date
authorIker Pedrosa <ipedrosa@redhat.com>
Tue, 4 Mar 2025 11:31:39 +0000 (12:31 +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_useradd.py

index e80f87668d77b73c59cf815058ecca930a8d3b8c..14c714419225020d7f8bf85c89c859ef7a6b2138 100644 (file)
@@ -7,6 +7,7 @@ from __future__ import annotations
 import pytest
 
 from framework.misc import days_since_epoch
+from pytest_mh.conn import ProcessError
 from framework.roles.shadow import Shadow
 from framework.topology import KnownTopology
 
@@ -101,3 +102,94 @@ def test_useradd__recreate_deleted_user(shadow: Shadow):
     assert result.gid == 1000, "Incorrect GID"
 
     assert shadow.fs.exists("/home/tuser"), "Home folder should be found"
+
+
+@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_useradd__set_expire_date_with_valid_date(shadow: Shadow, expiration_date: str, expected_date: int | None):
+    """
+    :title: Create account with valid expiration date
+    :setup:
+        1. Create user account with valid expiration date
+    :steps:
+        1. Check user exists and expiration date
+    :expectedresults:
+        1. User is found and expiration date is valid
+    :customerscenario: False
+    """
+    shadow.useradd(f"tuser1 -e {expiration_date}")
+
+    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_useradd__set_expire_date_with_invalid_date(shadow: Shadow, expiration_date: str):
+    """
+    :title: Create account with invalid expiration date
+    :steps:
+        1. Create user account with invalid account expiration date
+        2. Check user exists
+    :expectedresults:
+        1. The process fails and the expiration date isn't changed
+        2. User isn't found
+    :customerscenario: False
+    """
+    with pytest.raises(ProcessError):
+        shadow.useradd(f"tuser1 -e {expiration_date}")
+
+    result = shadow.tools.getent.shadow("tuser1")
+    assert result is None, "User shouldn't be found"
+
+
+@pytest.mark.topology(KnownTopology.Shadow)
+@pytest.mark.parametrize(
+    "expiration_date",
+    [
+        "-1",
+        "''",
+    ],
+)
+def test_useradd__set_expire_date_with_empty_date(shadow: Shadow, expiration_date: str):
+    """
+    :title: Create account with empty expiration date
+    :setup:
+        1. Create user account with empty expiration date
+    :steps:
+        1. Check user exists and expiration date
+    :expectedresults:
+        1. User is found and expiration date is empty
+    :customerscenario: False
+    """
+    shadow.useradd(f"tuser1 -e {expiration_date}")
+
+    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"