Skip to content

Commit

Permalink
fixes #62502 user.present incapable of removing optional groups
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasmhughes authored and Megan Wilhite committed Sep 6, 2022
1 parent bb92db9 commit 81ff432
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/62502.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix user.present to allow removing groups using optional_groups parameter and enforcing idempotent group membership.
2 changes: 1 addition & 1 deletion salt/states/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _changes(
return False

change = {}
if groups is None:
if not remove_groups and groups is None:
groups = lusr["groups"]
wanted_groups = sorted(set((groups or []) + (optional_groups or [])))
if uid and lusr["uid"] != uid:
Expand Down
48 changes: 48 additions & 0 deletions tests/pytests/functional/states/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,51 @@ def test_user_present_existing(states, username):
assert ret.changes["profile"] == win_profile
assert "description" in ret.changes
assert ret.changes["description"] == win_description


def test_user_present_change_groups(modules, states, username, group_1, group_2):
ret = states.user.present(
name=username,
groups=[group_1.name, group_2.name],
)
assert ret.result is True

user_info = modules.user.info(username)
assert user_info
assert user_info["groups"] == [group_1.name, group_2.name]

# run again and remove group_2
ret = states.user.present(
name=username,
groups=[group_1.name],
)
assert ret.result is True

user_info = modules.user.info(username)
assert user_info
assert user_info["groups"] == [group_1.name]


def test_user_present_change_optional_groups(
modules, states, username, group_1, group_2
):
ret = states.user.present(
name=username,
optional_groups=[group_1.name, group_2.name],
)
assert ret.result is True

user_info = modules.user.info(username)
assert user_info
assert user_info["optional_groups"] == [group_1.name, group_2.name]

# run again and remove group_2
ret = states.user.present(
name=username,
optional_groups=[group_1.name],
)
assert ret.result is True

user_info = modules.user.info(username)
assert user_info
assert user_info["optional_groups"] == [group_1.name]
8 changes: 5 additions & 3 deletions tests/pytests/unit/states/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ def test_present_uid_gid_change():
# get the before/after for the changes dict, and one last time to
# confirm that no changes still need to be made.
mock_info = MagicMock(side_effect=[before, before, after, after])
mock_group_to_gid = MagicMock(side_effect=["foo", "othergroup"])
mock_gid_to_group = MagicMock(side_effect=[5000, 5000, 5001, 5001])
mock_group_to_gid = MagicMock(side_effect=[5000, 5001])
mock_gid_to_group = MagicMock(
side_effect=["othergroup", "foo", "othergroup", "othergroup"]
)
dunder_salt = {
"user.info": mock_info,
"user.chuid": Mock(),
Expand All @@ -197,7 +199,7 @@ def test_present_uid_gid_change():
)
assert ret == {
"comment": "Updated user foo",
"changes": {"gid": 5001, "uid": 5001, "groups": ["othergroup"]},
"changes": {"gid": 5001, "uid": 5001, "groups": []},
"name": "foo",
"result": True,
}
Expand Down

0 comments on commit 81ff432

Please sign in to comment.