Skip to content

Conversation

@Gnonpi
Copy link

@Gnonpi Gnonpi commented Oct 31, 2025

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

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • I have attached screenshots in both light and dark modes for any UI changes.

@github-actions github-actions bot added the no ticket Based on PR title, no linked Trac ticket label Oct 31, 2025
Copy link

@github-actions github-actions bot left a 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.
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.
Copy link

@colleenDunlap colleenDunlap Oct 31, 2025

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(

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"?

call_command("changepassword", username="J\xfalia", stdout=self.stdout)

@mock.patch.object(
changepassword.Command, "_get_stdin", return_value=("not qwerty", "not qwerty")

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"

Copy link

@colleenDunlap colleenDunlap left a 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

User.objects.create_user(username="J\xfalia", password="qwerty")
call_command("changepassword", username="J\xfalia", stdout=self.stdout)

@mock.patch.object(

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?

@Gnonpi
Copy link
Author

Gnonpi commented Nov 3, 2025

Thanks for the quick review @colleenDunlap !
I've addressed your points in a few commits:

  • docs/topics/auth/default.txt:
    add example of parameter in action with the pipe itself
  • tests/test_auth/test_management.py:
    assert starting password is not new value
  • tests/test_auth/test_management.py:
    mock sys.stdin.readline()
  • tests/test_auth/test_management.py:
    add one failing test case

I have a doubt about how I handled exceptions when getting the value from sys.stdin.
I've wrapped the call in a try-except block catching Exception in order to always raise a CommandError. While simple and ensuring we don't show password values, I was wondering whether we should log something to stderr.

@jacobtylerwalls jacobtylerwalls changed the title Ticket 21392 Fixed #21392 --- Added a stdin option in ChangePassword command. Nov 4, 2025
@github-actions github-actions bot removed the no ticket Based on PR title, no linked Trac ticket label Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants