From 1e970584178c44d561d54dc8bf0c3837b89bad5a Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Sat, 23 Nov 2013 15:35:24 +0100 Subject: [PATCH] Fixes #21412 -- Better error message for messages.add_message Problem were users calling messages.debug/info/* with a wrong argument and getting the error "You cannot add messages without installing MessageMiddleware" Thanks to trac-user merb for the report. --- django/contrib/messages/api.py | 5 +++ django/contrib/messages/tests/test_api.py | 55 +++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 django/contrib/messages/tests/test_api.py diff --git a/django/contrib/messages/api.py b/django/contrib/messages/api.py index 8e08655aa0..248e0f0e64 100644 --- a/django/contrib/messages/api.py +++ b/django/contrib/messages/api.py @@ -1,10 +1,12 @@ from django.contrib.messages import constants from django.contrib.messages.storage import default_storage +from django.http import HttpRequest __all__ = ( 'add_message', 'get_messages', 'get_level', 'set_level', 'debug', 'info', 'success', 'warning', 'error', + 'MessageFailure', ) @@ -16,6 +18,9 @@ def add_message(request, level, message, extra_tags='', fail_silently=False): """ Attempts to add a message to the request using the 'messages' app. """ + if not isinstance(request, HttpRequest): + raise TypeError("add_message() argument must be an HttpRequest object, " + "not '%s'." % request.__class__.__name__) if hasattr(request, '_messages'): return request._messages.add(level, message, extra_tags) if not fail_silently: diff --git a/django/contrib/messages/tests/test_api.py b/django/contrib/messages/tests/test_api.py new file mode 100644 index 0000000000..335a200450 --- /dev/null +++ b/django/contrib/messages/tests/test_api.py @@ -0,0 +1,55 @@ +from django.test import TestCase, RequestFactory + +from django.contrib import messages + + +class DummyStorage(object): + """ + dummy message-store to test the api methods + """ + + def __init__(self): + self.store = [] + + def add(self, level, message, extra_tags=''): + self.store.append(message) + + +class ApiTest(TestCase): + def setUp(self): + self.rf = RequestFactory() + self.request = self.rf.request() + self.storage = DummyStorage() + + def test_ok(self): + msg = 'some message' + + self.request._messages = self.storage + messages.add_message(self.request, messages.DEBUG, msg) + self.assertIn(msg, self.storage.store) + + def test_request_is_none(self): + msg = 'some message' + + self.request._messages = self.storage + + with self.assertRaises(TypeError): + messages.add_message(None, messages.DEBUG, msg) + + self.assertEqual([], self.storage.store) + + def test_middleware_missing(self): + msg = 'some message' + + with self.assertRaises(messages.MessageFailure): + messages.add_message(self.request, messages.DEBUG, msg) + + self.assertEqual([], self.storage.store) + + def test_middleware_missing_silently(self): + msg = 'some message' + + messages.add_message(self.request, messages.DEBUG, msg, + fail_silently=True) + + self.assertEqual([], self.storage.store)