Skip to content

Commit a6bbe4f

Browse files
committed
Fix users reset-password in FAB CLI (#63830)
1 parent 6710866 commit a6bbe4f

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

providers/fab/src/airflow/providers/fab/auth_manager/cli_commands/user_command.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,30 @@ def users_create(args):
8585

8686

8787
def _find_user(args):
88+
with get_application_builder() as appbuilder:
89+
return _find_user_with_appbuilder(args, appbuilder)
90+
91+
92+
def _find_user_with_appbuilder(args, appbuilder):
8893
if not args.username and not args.email:
8994
raise SystemExit("Missing args: must supply one of --username or --email")
9095

9196
if args.username and args.email:
9297
raise SystemExit("Conflicting args: must supply either --username or --email, but not both")
9398

94-
with get_application_builder() as appbuilder:
95-
user = appbuilder.sm.find_user(username=args.username, email=args.email)
96-
if not user:
97-
raise SystemExit(f'User "{args.username or args.email}" does not exist')
99+
user = appbuilder.sm.find_user(username=args.username, email=args.email)
100+
if not user:
101+
raise SystemExit(f'User "{args.username or args.email}" does not exist')
98102
return user
99103

100104

101105
@cli_utils.action_cli
102106
@providers_configuration_loaded
103107
def user_reset_password(args):
104108
"""Reset user password from DB."""
105-
user = _find_user(args)
106109
password = _create_password(args)
107110
with get_application_builder() as appbuilder:
111+
user = _find_user_with_appbuilder(args, appbuilder)
108112
if appbuilder.sm.reset_password(user.id, password):
109113
print(f'User "{user.username}" password reset successfully')
110114
else:

providers/fab/tests/unit/fab/auth_manager/cli_commands/test_user_command.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from contextlib import redirect_stdout
2424
from importlib import reload
2525
from io import StringIO
26+
from unittest import mock
2627

2728
import pytest
2829

@@ -541,3 +542,37 @@ def test_cli_reset_user_password_with_email(self):
541542
with redirect_stdout(StringIO()) as stdout:
542543
user_command.user_reset_password(args)
543544
assert 'User "test3" password reset successfully' in stdout.getvalue()
545+
546+
def test_cli_reset_user_password_uses_single_appbuilder_context(self):
547+
args = self.parser.parse_args(
548+
[
549+
"users",
550+
"create",
551+
"--username",
552+
"test4",
553+
"--lastname",
554+
"doe",
555+
"--firstname",
556+
"jane",
557+
"--email",
558+
"jane@example.com",
559+
"--role",
560+
"Viewer",
561+
"--password",
562+
"TempPass123!",
563+
]
564+
)
565+
user_command.users_create(args)
566+
567+
args = self.parser.parse_args(
568+
["users", "reset-password", "--username", "test4", "--password", "ResetPass456!"]
569+
)
570+
571+
with mock.patch.object(
572+
user_command, "get_application_builder", wraps=user_command.get_application_builder
573+
) as mocked_get_application_builder:
574+
with redirect_stdout(StringIO()) as stdout:
575+
user_command.user_reset_password(args)
576+
577+
assert mocked_get_application_builder.call_count == 1
578+
assert 'User "test4" password reset successfully' in stdout.getvalue()

0 commit comments

Comments
 (0)