From: Iker Pedrosa Date: Tue, 4 Mar 2025 11:31:39 +0000 (+0100) Subject: tests/: test useradd expiration date X-Git-Tag: 4.18.0-rc1~48 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=01cbda33f617d548cdc4fed2ed29d920e55785a8;p=thirdparty%2Fshadow.git tests/: test useradd expiration date Signed-off-by: Iker Pedrosa Reviewed-by: Dan Lavu --- diff --git a/tests/system/tests/test_useradd.py b/tests/system/tests/test_useradd.py index e80f87668..14c714419 100644 --- a/tests/system/tests/test_useradd.py +++ b/tests/system/tests/test_useradd.py @@ -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"