Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions krill/krill/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import os
import sys
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand Down Expand Up @@ -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/
Expand Down
43 changes: 43 additions & 0 deletions krill/krill/tests/test_password_hasher.py
Original file line number Diff line number Diff line change
@@ -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)
Loading