From eea369e73cb5759dae882915022a8a53f4cbf76a Mon Sep 17 00:00:00 2001 From: Chaemin-Lim Date: Mon, 19 May 2025 16:12:06 +0900 Subject: [PATCH] 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 --- openstackclient/identity/v3/user.py | 2 +- .../tests/unit/identity/v3/test_user.py | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/openstackclient/identity/v3/user.py b/openstackclient/identity/v3/user.py index 5be69bc652..14273e8f65 100644 --- a/openstackclient/identity/v3/user.py +++ b/openstackclient/identity/v3/user.py @@ -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 " diff --git a/openstackclient/tests/unit/identity/v3/test_user.py b/openstackclient/tests/unit/identity/v3/test_user.py index d9e16f174d..10207141c2 100644 --- a/openstackclient/tests/unit/identity/v3/test_user.py +++ b/openstackclient/tests/unit/identity/v3/test_user.py @@ -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',