django-ratelimit-backend
django-ratelimit-backend copied to clipboard
Don't raise UserWarning 'No request passed to the backend' during tests
What would be the accepted method to stop this from happening during mocked auth unit tests?
- Wrap tests with ignore UserWarning?
- Add setting to turn UserWarning on/off?
Thanks
This warning happens for us whenever we call self.client.login()
during tests. Workaround:
from django.contrib.auth.models import AnonymousUser
from django.urls import reverse
from django.test import RequestFactory, TestCase
class SomethingTests(TestCase):
def setUp(self):
factory = RequestFactory()
self.login_request = factory.get(reverse('login'))
self.login_request.user = AnonymousUser
super(TestCase, self).setUp()
def test_method(self):
self.client.login(username='user', password='pass', request=self.login_request)
I ended up using this snippet in my testing.py settings
import warnings
# https://github.com/brutasse/django-ratelimit-backend/issues/41
warnings.filterwarnings('ignore', message="No request passed to the backend", category=UserWarning)