revsys / django-test-plus

Useful additions to Django's default TestCase
https://django-test-plus.readthedocs.io
BSD 3-Clause "New" or "Revised" License
612 stars 62 forks source link

assertLoginRequired fails when used with the pytest tp fixture #126

Closed mblayman closed 3 years ago

mblayman commented 4 years ago

While attempting to replace a TestCase-derived test class with the tp fixture, I hit an error with assertLoginRequired. The problem appears to be that assertLoginRequired uses assertRedirects, but that method isn't available on the TestCase class that is defined by the pytest plugin module.

I'm not sure how doable it is to pull that method out of the Django TestCase class. Another alternative would be implement an alternate version of assertRedirects if the Django version has too many hooks to other things.

Here's my local traceback for fuller context:

    def test_unauthenticated_access(self, tp):
>       tp.assertLoginRequired("students:index")

homeschool/students/tests/test_views.py:24: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <test_plus.plugin.TestCase object at 0x1121bbc50>, url = 'students:index', args = (), kwargs = {}
response = <HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="/accounts/login/?next=/students/">, reversed_url = '/students/'
login_url = '/accounts/login/', expected_url = '/accounts/login/?next=/students/'

    def assertLoginRequired(self, url, *args, **kwargs):
        """ Ensure login is required to GET this URL """
        response = self.get(url, *args, **kwargs)
        reversed_url = reverse(url, args=args, kwargs=kwargs)
        login_url = str(resolve_url(settings.LOGIN_URL))
        expected_url = "{0}?next={1}".format(login_url, reversed_url)
>       self.assertRedirects(response, expected_url)
E       AttributeError: 'TestCase' object has no attribute 'assertRedirects'
fcurella commented 3 years ago

Closed by #160