Skip to content

Commit

Permalink
Fix user.shadow module
Browse files Browse the repository at this point in the history
Fixes an issue with the user.shadow module where it would fail
retrieving values that weren't set yet... last login for example.
Also adds some tests
  • Loading branch information
twangboy committed Feb 2, 2024
1 parent 2c02887 commit 8ac138e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
41 changes: 23 additions & 18 deletions salt/modules/mac_shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,12 @@ def get_account_created(name):
salt '*' shadow.get_account_created admin
"""
ret = _get_account_policy_data_value(name, "creationTime")

unix_timestamp = salt.utils.mac_utils.parse_return(ret)

date_text = _convert_to_datetime(unix_timestamp)
try:
ret = _get_account_policy_data_value(name, "creationTime")
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
date_text = _convert_to_datetime(unix_timestamp)
except CommandExecutionError:
date_text = "Creation date not defined"

return date_text

Expand All @@ -228,11 +229,12 @@ def get_last_change(name):
salt '*' shadow.get_last_change admin
"""
ret = _get_account_policy_data_value(name, "passwordLastSetTime")

unix_timestamp = salt.utils.mac_utils.parse_return(ret)

date_text = _convert_to_datetime(unix_timestamp)
try:
ret = _get_account_policy_data_value(name, "passwordLastSetTime")
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
date_text = _convert_to_datetime(unix_timestamp)
except CommandExecutionError:
date_text = "Password last set date/time undefined"

return date_text

Expand All @@ -254,9 +256,11 @@ def get_login_failed_count(name):
salt '*' shadow.get_login_failed_count admin
"""
ret = _get_account_policy_data_value(name, "failedLoginCount")

return salt.utils.mac_utils.parse_return(ret)
try:
ret = _get_account_policy_data_value(name, "failedLoginCount")
return salt.utils.mac_utils.parse_return(ret)
except CommandExecutionError:
return "Failed login count undefined"


def get_login_failed_last(name):
Expand All @@ -277,11 +281,12 @@ def get_login_failed_last(name):
salt '*' shadow.get_login_failed_last admin
"""
ret = _get_account_policy_data_value(name, "failedLoginTimestamp")

unix_timestamp = salt.utils.mac_utils.parse_return(ret)

date_text = _convert_to_datetime(unix_timestamp)
try:
ret = _get_account_policy_data_value(name, "failedLoginTimestamp")
unix_timestamp = salt.utils.mac_utils.parse_return(ret)
date_text = _convert_to_datetime(unix_timestamp)
except CommandExecutionError:
date_text = "Last failed login undefined"

return date_text

Expand Down
69 changes: 69 additions & 0 deletions tests/pytests/unit/modules/test_mac_shadow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
Unit Tests for the mac_desktop execution module.
"""

import pytest

import salt.modules.mac_shadow as mac_shadow
from salt.exceptions import CommandExecutionError
from tests.support.mock import patch

pytestmark = [
pytest.mark.skip_unless_on_darwin,
]


def test_get_account_created():
with patch("_get_account_policy_data_value", return_value=0):
result = mac_shadow.get_account_created("junk")
expected = "1969-12-31 17:00:00"
assert result == expected


def test_get_account_created_error():
with patch("_get_account_policy_data_value", side_effect=CommandExecutionError):
result = mac_shadow.get_account_created("junk")
expected = "Creation date not defined"
assert result == expected


def test_get_last_change():
with patch("_get_account_policy_data_value", return_value=0):
result = mac_shadow.get_last_change("junk")
expected = "1969-12-31 17:00:00"
assert result == expected


def test_get_last_change_error():
with patch("_get_account_policy_data_value", side_effect=CommandExecutionError):
result = mac_shadow.get_last_change("junk")
expected = "Password last set date/time undefined"
assert result == expected


def test_login_failed_count():
with patch("_get_account_policy_data_value", return_value=0):
result = mac_shadow.get_login_failed_count("junk")
expected = "0"
assert result == expected


def test_get_login_failed_count_error():
with patch("_get_account_policy_data_value", side_effect=CommandExecutionError):
result = mac_shadow.get_login_failed_count("junk")
expected = "Failed login count undefined"
assert result == expected


def test_login_failed_last():
with patch("_get_account_policy_data_value", return_value=0):
result = mac_shadow.get_login_failed_last("junk")
expected = "1969-12-31 17:00:00"
assert result == expected


def test_get_login_failed_last_error():
with patch("_get_account_policy_data_value", side_effect=CommandExecutionError):
result = mac_shadow.get_login_failed_last("junk")
expected = "Last failed login undefined"
assert result == expected

0 comments on commit 8ac138e

Please sign in to comment.