Skip to content

Use a fast password hasher when running the tests - #217

Merged
olsonjp merged 1 commit into
mainfrom
fast-test-password-hasher
Nov 11, 2026
Merged

olsonjp merged 1 commit into
mainfrom
fast-test-password-hasher

Conversation

@olsonjp

@olsonjp olsonjp commented Nov 11, 2026

Copy link
Copy Markdown
Owner

The CI test job takes about 5 minutes for a fairly small app. Almost all of that is password hashing.

What I measured

Time
Last passing CI run, per step setup about 7 s, `Run tests` 4 m 50 s
Suite locally (869 tests), default hasher 112 s
Same suite locally, fast hasher 3.5 s, all passing

Django hashes passwords with PBKDF2 on purpose; one hash costs about 89 ms here. The tests create users constantly (the slowest tests, all about 0.46 s, are the ones that make four or five users in setup), so hashing was nearly the whole run time. Splitting the suite across runners, parallel test processes and dependency caching would each have saved little, because setup is only about 7 s and the tests are CPU-bound on hashing.

What changes

  • `PASSWORD_HASHERS = [MD5PasswordHasher]` in `krill/settings.py`, only when the command is `manage.py test`. Every other command, including gunicorn and `runserver`, keeps Django's default hashers. The Makefile, the CI workflow and the Docker image all call `manage.py test`, so all three get it.
  • Tests are not weakened beyond this: a test that wants real hashing can use `override_settings` (one test does this to show it works), and login, password change and the rest behave the same, because they work with any hasher. All existing tests pass unchanged.
  • A note in `CONTRIBUTING.md` explains it.

Tests

`krill/tests/test_password_hasher.py` (4 tests): the test command uses the fast hasher; users made in tests get an `md5$` hash and can still log in (and a wrong password is refused); a test can still ask for `pbkdf2_sha256`; and a separate `manage.py shell` process still reports the default hashers with PBKDF2 and no MD5, which guards against the fast hasher leaking into the app. The first three skip themselves if the tests are run some other way, such as through pytest, where the check would not apply. Full suite: 873 tests, OK, in about 3.6 s locally.

How to check it worked

Compare this PR's CI `Run tests` step against the 4 m 50 s above. My estimate for CI is roughly 10 s, but I have not measured it there.

🤖 Generated with Claude Code

The suite spent nearly all of its time hashing passwords: 112 s locally,
3.5 s with MD5. Only the test command is affected.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@olsonjp
olsonjp merged commit 78a05f7 into main Nov 11, 2026
1 check passed
@olsonjp
olsonjp deleted the fast-test-password-hasher branch November 11, 2026 18:19
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.

1 participant