]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Tests: Add a new user with custom primary group and supplementary groups
authoraborah-sudo <aborah@redhat.com>
Wed, 8 Apr 2026 10:22:48 +0000 (15:52 +0530)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Fri, 10 Apr 2026 08:55:43 +0000 (10:55 +0200)
This is the transformation to Python of the test located in
`tests/usertools/01/16_useradd_add_user_to_multiple_groups.test`
which checks that `useradd` can add a new user with specific group

tests/system/tests/test_useradd.py

index d50d14828a1ea997eb8fb7a0d7e9d0dbea6eb358..2b9288d07ea7b8e3326ac57a0ae17c57902e10fc 100644 (file)
@@ -485,3 +485,45 @@ def test_useradd__successful_large_uid(shadow: Shadow, uid_value: int):
     assert group_entry is not None, "Group test1 should be found"
     assert group_entry.name == "test1", "Incorrect group name"
 
+
+@pytest.mark.topology(KnownTopology.Shadow)
+def test_useradd_custom_primary_and_supplementary_groups(shadow: Shadow):
+    """
+    :title: Add a new user with custom primary group and supplementary groups
+    :steps:
+        1. Create required groups
+        2. Create user with primary group and supplementary groups
+        3. Check passwd entry
+        4. Verify primary group
+        5. Verify user is a member of all supplementary groups
+    :expectedresults:
+        1. Groups are created successfully
+        2. User is created successfully
+        3. Passwd entry exists with correct attributes
+        4. User has primary group
+        5. User is added to supplementary groups as a member
+    :customerscenario: False
+    """
+    username = "testuser1"
+    groups_to_create = ["testgroup", "testgroup1", "testgroup2", "testgroup3"]
+    for group in groups_to_create:
+        shadow.groupadd(group)
+
+    shadow.useradd(f"{username} -g testgroup -G testgroup1,testgroup2,testgroup3")
+
+    passwd_entry = shadow.tools.getent.passwd(username)
+    assert passwd_entry is not None, f"User {username} should be found in passwd"
+    assert passwd_entry.name == username, "Incorrect username"
+    assert passwd_entry.home == f"/home/{username}", "Incorrect home directory"
+
+    testgroup_entry = shadow.tools.getent.group("testgroup")
+    assert testgroup_entry is not None, "Group testgroup should exist"
+    assert (
+        passwd_entry.gid == testgroup_entry.gid
+    ), f"User's primary GID ({passwd_entry.gid}) should match testgroup GID ({testgroup_entry.gid})"
+
+    supplementary_groups = ["testgroup1", "testgroup2", "testgroup3"]
+    for group_name in supplementary_groups:
+        group_entry = shadow.tools.getent.group(group_name)
+        assert group_entry is not None, f"Group {group_name} should exist"
+        assert username in group_entry.members, f"User {username} should be a member of {group_name} group"