Fix incorrect warning with --password-prompt option

When creating a user with the --password-prompt option, a warning is
incorrectly displayed stating that no password was supplied, even though
a password was entered. This occurs because the code checks parsed_args.password
instead of the password variable that actually stores the prompted password.

This patch fixes the issue by checking the 'password' variable
instead of 'parsed_args.password' in the warning condition. A test
case has been added to verify that no warning is displayed when
using --password-prompt and entering a password.

Closes-Bug: #2091836
Change-Id: Ib3ddc7e400ee7988f605c00db534bccc3617d982
This commit is contained in:
Chaemin-Lim
2025-05-19 16:12:06 +09:00
parent 79de137152
commit eea369e73c
2 changed files with 48 additions and 1 deletions
openstackclient
identity
tests
unit
identity

@@ -289,7 +289,7 @@ class CreateUser(command.ShowOne):
elif parsed_args.password_prompt:
password = utils.get_password(self.app.stdin)
if not parsed_args.password:
if not password:
LOG.warning(
_(
"No password was supplied, authentication will fail "

@@ -163,6 +163,53 @@ class TestUserCreate(identity_fakes.TestIdentityv3):
self.assertEqual(self.columns, columns)
self.assertEqual(self.datalist, data)
def test_user_create_password_prompt_no_warning(self):
arglist = [
'--password-prompt',
self.user.name,
]
verifylist = [
('password', None),
('password_prompt', True),
('enable', False),
('disable', False),
('name', self.user.name),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
import logging
# Mock the password prompt
mocker = mock.Mock()
mocker.return_value = 'abc123'
# Use assertLogs to verify no warnings are logged
logger = 'openstackclient.identity.v3.user'
with mock.patch("osc_lib.utils.get_password", mocker):
with self.assertLogs(logger, level='WARNING') as log_ctx:
logging.getLogger(logger).warning(
"Dummy warning for test setup"
)
columns, data = self.cmd.take_action(parsed_args)
self.assertEqual(1, len(log_ctx.records))
self.assertIn(
"Dummy warning for test setup", log_ctx.output[0]
)
self.assertNotIn(
"No password was supplied", ''.join(log_ctx.output)
)
# Set expected values
kwargs = {
'name': self.user.name,
'is_enabled': True,
'password': 'abc123',
}
self.identity_sdk_client.create_user.assert_called_with(**kwargs)
self.assertEqual(self.columns, columns)
self.assertEqual(self.datalist, data)
def test_user_create_email(self):
arglist = [
'--email',