Use "@override_settings" instead: https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.utils.override_settings
You should then be able to remove the special-case handling in the middleware.