diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 632ac45..50ad77e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -91,6 +91,10 @@ Feature suggestions are welcome! Please create an issue with: ```bash cd krill && uv run python manage.py test ``` +- `manage.py test` swaps in a fast password hasher (see `PASSWORD_HASHERS` in + `krill/settings.py`). Password hashing is deliberately slow and was nearly all of the + suite's run time. Only the `test` command is affected; the running app keeps Django's + default hashers. If a test must exercise real hashing, use `override_settings`. - The test suite also runs on every pull request through GitHub Actions (`.github/workflows/tests.yml`). Pull requests from forks wait for a maintainer to approve the run before it starts. diff --git a/krill/krill/settings.py b/krill/krill/settings.py index 74d3052..47590ea 100644 --- a/krill/krill/settings.py +++ b/krill/krill/settings.py @@ -11,6 +11,7 @@ """ import os +import sys from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -133,6 +134,12 @@ }, ] +# Password hashing is deliberately slow (PBKDF2, about 90 ms a hash). Tests create users constantly, and +# hashing was nearly all of the suite's run time (112 s; 3.5 s with a fast hasher). So `manage.py test` +# uses MD5; every other command, including the running app, keeps Django's default hashers. +if len(sys.argv) > 1 and sys.argv[1] == 'test': + PASSWORD_HASHERS = ['django.contrib.auth.hashers.MD5PasswordHasher'] + # Internationalization # https://docs.djangoproject.com/en/5.0/topics/i18n/ diff --git a/krill/krill/tests/test_password_hasher.py b/krill/krill/tests/test_password_hasher.py new file mode 100644 index 0000000..84b4d68 --- /dev/null +++ b/krill/krill/tests/test_password_hasher.py @@ -0,0 +1,43 @@ +import subprocess +import sys + +from django.conf import settings +from django.contrib.auth import get_user_model +from django.test import TestCase, override_settings + +MD5 = 'django.contrib.auth.hashers.MD5PasswordHasher' + + +class TestPasswordHasherTest(TestCase): + """`manage.py test` uses a fast password hasher; nothing else does (see krill/settings.py).""" + + def setUp(self): + if sys.argv[1:2] != ['test']: + self.skipTest('the fast hasher is only switched on by manage.py test') + + def test_the_test_command_uses_the_fast_hasher(self): + self.assertEqual(settings.PASSWORD_HASHERS, [MD5]) + + def test_users_made_in_tests_get_a_fast_hash_and_can_still_log_in(self): + user = get_user_model().objects.create_user(username='hash-check', password='s3cret-pass') + self.assertTrue(user.password.startswith('md5$')) + self.assertTrue(self.client.login(username='hash-check', password='s3cret-pass')) + self.assertFalse(self.client.login(username='hash-check', password='wrong')) + + @override_settings(PASSWORD_HASHERS=['django.contrib.auth.hashers.PBKDF2PasswordHasher']) + def test_a_test_can_still_ask_for_real_hashing(self): + user = get_user_model().objects.create_user(username='real-hash', password='s3cret-pass') + self.assertTrue(user.password.startswith('pbkdf2_sha256$')) + + +class OtherCommandsKeepTheRealHasherTest(TestCase): + def test_other_commands_use_djangos_default_hashers(self): + result = subprocess.run( + [sys.executable, 'manage.py', 'shell', '-c', + 'from django.conf import settings; print("HASHERS", settings.PASSWORD_HASHERS)'], + cwd=settings.BASE_DIR, capture_output=True, text=True, timeout=120, + ) + self.assertEqual(result.returncode, 0, result.stderr) + line = next(l for l in result.stdout.splitlines() if l.startswith('HASHERS')) + self.assertIn('PBKDF2PasswordHasher', line) + self.assertNotIn('MD5PasswordHasher', line)