Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions salt/modules/win_useradd.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,13 +805,17 @@ def info(name):
items = {}
server = None

domain_name, user_name = salt.utils.win_runas.split_username(name)
user_name, domain_name = salt.utils.win_runas.split_username(name)
log.debug("user_name: %s", user_name)
log.debug("domain_name: %s", domain_name)

if domain_name:
if domain_name != ".":
try:
server = win32net.NetGetAnyDCName(None, domain_name)
log.debug("Found DC: %s", server)
except win32net.error:
# Restore username to original
log.debug("DC not found. Using username: %s", str(name))
user_name = str(name)

try:
Expand Down
3 changes: 2 additions & 1 deletion tests/pytests/functional/modules/test_win_useradd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def username_str(user):
try:
user.delete(_username, purge=True, force=True)
except Exception: # pylint: disable=broad-except
# The point here is just system cleanup. It can fail if no account was created
# The point here is just system cleanup. It can fail if no account
# was created
pass


Expand Down
75 changes: 75 additions & 0 deletions tests/pytests/unit/modules/test_win_useradd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import logging

import pytest
import win32net
from saltfactories.utils import random_string

import salt.modules.cmdmod as cmdmod
import salt.modules.win_useradd as win_useradd
from tests.support.mock import MagicMock, patch

pytestmark = [
pytest.mark.windows_whitelisted,
pytest.mark.skip_unless_on_windows,
]


@pytest.fixture
def configure_loader_modules():
return {
win_useradd: {
"__salt__": {
"cmd.run_all": cmdmod.run_all,
},
}
}


@pytest.fixture
def username():
_username = random_string("test-account-", uppercase=False)
try:
yield _username
finally:
try:
win_useradd.delete(_username, purge=True, force=True)
except Exception: # pylint: disable=broad-except
# The point here is just system cleanup. It can fail if no account
# was created
pass


@pytest.fixture
def account(username):
with pytest.helpers.create_account(username=username) as account:
win_useradd.addgroup(account.username, "Users")
yield account


def test_info(account, caplog):
with caplog.at_level(logging.DEBUG):
win_useradd.info(account.username)
assert f"user_name: {account.username}" in caplog.text
assert "domain_name: ." in caplog.text


def test_info_domain(account, caplog):
domain = "mydomain"
dc = "myDC"
with caplog.at_level(logging.DEBUG), patch(
"win32net.NetGetAnyDCName", MagicMock(return_value=dc)
):
account.username = f"{domain}\\{account.username}"
win_useradd.info(account.username)
assert f"Found DC: {dc}" in caplog.text


def test_info_error(account, caplog):
domain = "mydomain"
dc = "myDC"
with caplog.at_level(logging.DEBUG), patch(
"win32net.NetGetAnyDCName", MagicMock(side_effect=win32net.error)
):
account.username = f"{domain}\\{account.username}"
win_useradd.info(account.username)
assert f"DC not found. Using username: {account.username}" in caplog.text
Loading