Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't my Django unittests know that MessageMiddleware is installed?

I'm working on a Django project and am writing unittests for it. However, in a test, when I try and log a user in, I get this error:

MessageFailure: You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware

Logging in on the actual site works fine -- and a login message is displayed using the MessageMiddleware.

In my tests, if I do this:

from django.conf import settings
print settings.MIDDLEWARE_CLASSES

Then it outputs this:

('django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')

Which appears to show the MessageMiddleware is installed when tests are run.

Is there an obvious step I'm missing?

UPDATE

After suggestions below, it does look like it's a settings thing.

I currently have settings/__init__.py like this:

try:
    from settings.development import *
except ImportError:
    pass

and settings/defaults.py containing most of the standard settings (including MIDDLEWARE_CLASSES). And then settings.development.py overrides some of those defaults like this:

from defaults import *

DEBUG = True
# etc

It looks like my dev site itself works fine, using the development settings. But although the tests seem to load the settings OK (both defaults and development) settings.DEBUG is set to False. I don't know why, or whether that's the cause of the problem.

like image 628
Phil Gyford Avatar asked Aug 13 '12 16:08

Phil Gyford


3 Answers

Django 1.4 has a expected behavior when you create the request with RequestFactory that can trigger this error.

To resolve this issue, create your request with RequestFactory and do this:

from django.contrib.messages.storage.fallback import FallbackStorage
setattr(request, 'session', 'session')
messages = FallbackStorage(request)
setattr(request, '_messages', messages)

Works for me!

like image 159
Tarsis Azevedo Avatar answered Oct 16 '22 02:10

Tarsis Azevedo


A way to solve this quite elegant is to mock the messages module using mock

Say you have a class based view named FooView in app named myapp

from django.contrib import messages
from django.views.generic import TemplateView

class FooView(TemplateView):
    def post(self, request, *args, **kwargs):
        ...
        messages.add_message(request, messages.SUCCESS, '\o/ Profit \o/')
        ...

You now can test it with

def test_successful_post(self):
    mock_messages = patch('myapp.views.FooView.messages').start()
    mock_messages.SUCCESS = success = 'super duper'
    request = self.rf.post('/', {})
    view = FooView.as_view()
    response = view(request)
    msg = _(u'\o/ Profit \o/')
    mock_messages.add_message.assert_called_with(request, success, msg)
like image 41
Stephan Hoyer Avatar answered Oct 16 '22 03:10

Stephan Hoyer


In my case (django 1.8) this problem occurs in when unit-test calls signal handler for user_logged_in signal, looks like messages app has not been called, i.e. request._messages is not yet set. This fails:

from django.contrib.auth.signals import user_logged_in
...

@receiver(user_logged_in)
def user_logged_in_handler(sender, user, request, **kwargs):

    ...
    messages.warning(request, "user has logged in")

the same call to messages.warning in normal view function (that is called after) works without any issues.

A workaround I based on one of the suggestions from https://code.djangoproject.com/ticket/17971, use fail_silently argument only in signal handler function, i.e. this solved my problem:

messages.warning(request, "user has logged in",
                 fail_silently=True )
like image 1
Robert Lujo Avatar answered Oct 16 '22 01:10

Robert Lujo