django-ratelimit-backend icon indicating copy to clipboard operation
django-ratelimit-backend copied to clipboard

Don't raise UserWarning 'No request passed to the backend' during tests

Open ciarancourtney opened this issue 6 years ago • 2 comments

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

ciarancourtney avatar Jan 11 '18 15:01 ciarancourtney

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)

scjody avatar Mar 14 '18 21:03 scjody

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)

ciarancourtney avatar Mar 15 '18 08:03 ciarancourtney