def test_hmac_autoupdate(self):
        """Auto-update HMAC key if hash in DB is outdated."""
        # Get HMAC key IDs to compare
        old_key_id = max(settings.HMAC_KEYS.keys())
        new_key_id = '2020-01-01'

        # Add a new HMAC key
        new_keys = settings.HMAC_KEYS.copy()
        new_keys[new_key_id] = 'a_new_key'
        with patch.object(settings._wrapped, 'HMAC_KEYS', new_keys):
            # Make sure the database has the old key ID.
            john = User.objects.get(username='john')
            eq_(john.password.rsplit('$', 1)[1], old_key_id)

            # Log in.
            assert authenticate(username='john', password='123456')

            # Make sure the DB now has a new password hash.
            john = User.objects.get(username='john')
            eq_(john.password.rsplit('$', 1)[1], new_key_id)