-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Fixed #21392 --- Added a stdin option in ChangePassword command. #20030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello! Thank you for your contribution 💪
As it's your first contribution be sure to check out the patch review checklist.
If you're fixing a ticket from Trac make sure to set the "Has patch" flag and include a link to this PR in the ticket!
If you have any design or process questions then you can ask in the Django forum.
Welcome aboard ⛵️!
Added the possibility to pass a new password value to the command auth.ChangePassword via stdin for integration or automation purposes. A new option was added to select reading using getpass or stdin. Thanks Paris Kasidiaris for bringing up the idea.
docs/topics/auth/default.txt
Outdated
| do not supply a user, the command will attempt to change the password | ||
| whose username matches the current system user. | ||
| whose username matches the current system user. An option named ``--stdin`` | ||
| is available to allow to pass the new password via a pipe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: include a example of the paramater in action e.g.
python manage.py changepassword admin --std-in <new_password>
or echo "new_password" | python manage.py changepassword admin --std-in
| Executing the changepassword command with the --stdin option | ||
| should should joe's password. | ||
| """ | ||
| call_command( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe assert that the starting password is not "not qwerty"?
tests/auth_tests/test_management.py
Outdated
| call_command("changepassword", username="J\xfalia", stdout=self.stdout) | ||
|
|
||
| @mock.patch.object( | ||
| changepassword.Command, "_get_stdin", return_value=("not qwerty", "not qwerty") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it'd be a little bit more effective of a test to not mock _get_stdin but rather sys.stdin.readline(), in case one day someone comes along and is like "why are we returning stdin_content twice in _get_stdin(self): that's silly we only need one"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far looks really good! Couple of requests on unit tests to 1) add another test case for failing and 2) mock sys.stdin.readline() instead of _get_stdin so we're still testing the function you wrote
tests/auth_tests/test_management.py
Outdated
| User.objects.create_user(username="J\xfalia", password="qwerty") | ||
| call_command("changepassword", username="J\xfalia", stdout=self.stdout) | ||
|
|
||
| @mock.patch.object( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also get a test case for when sys.stdin.readline() fails to make sure it fails gracefully?
Instead of patching the method `_get_stdin` in the Command, we directly patch `sys.stdin.readline` so that it is closer to actual behaviour. We also check that starting password is the one we expect.
Added a try-except block to catch any issue while getting stdin value, instead raise a CommandError.
|
Thanks for the quick review @colleenDunlap !
I have a doubt about how I handled exceptions when getting the value from |
Trac ticket number
ticket-21392
Branch description
Added the possibility to pass a new password value to the command auth.ChangePassword via stdin for integration or automation purposes. A new option was added to select reading using getpass or stdin. Thanks Paris Kasidiaris for bringing up the idea.
Checklist
mainbranch.