1
0
mirror of https://github.com/django/django.git synced 2024-12-30 13:05:45 +00:00
django/tests/messages_tests/test_api.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.1 KiB
Python
Raw Normal View History

from django.contrib import messages
from django.test import RequestFactory, SimpleTestCase
class DummyStorage:
"""
dummy message-store to test the api methods
"""
def __init__(self):
self.store = []
def add(self, level, message, extra_tags=""):
self.store.append(message)
2017-01-05 14:23:39 +00:00
class ApiTests(SimpleTestCase):
rf = RequestFactory()
def setUp(self):
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):
2017-01-05 14:23:39 +00:00
msg = "add_message() argument must be an HttpRequest object, not 'NoneType'."
self.request._messages = self.storage
2017-01-05 14:23:39 +00:00
with self.assertRaisesMessage(TypeError, msg):
messages.add_message(None, messages.DEBUG, "some message")
self.assertEqual(self.storage.store, [])
def test_middleware_missing(self):
2017-01-05 14:23:39 +00:00
msg = (
"You cannot add messages without installing "
"django.contrib.messages.middleware.MessageMiddleware"
)
2017-01-05 14:23:39 +00:00
with self.assertRaisesMessage(messages.MessageFailure, msg):
messages.add_message(self.request, messages.DEBUG, "some message")
self.assertEqual(self.storage.store, [])
def test_middleware_missing_silently(self):
2017-01-05 14:23:39 +00:00
messages.add_message(
self.request, messages.DEBUG, "some message", fail_silently=True
)
self.assertEqual(self.storage.store, [])
class CustomRequest:
def __init__(self, request):
self._request = request
def __getattribute__(self, attr):
try:
return super().__getattribute__(attr)
except AttributeError:
return getattr(self._request, attr)
class CustomRequestApiTests(ApiTests):
"""
add_message() should use ducktyping to allow request wrappers such as the
one in Django REST framework.
"""
def setUp(self):
super().setUp()
self.request = CustomRequest(self.request)