I want to write a Django test that looks at all the urls in one particular app, and makes sure that hitting them without being authenticated redirects me to the login page.
Something like:
from django.test import TestCase
from django.urls import reverse
from my_app.urls import urlpatterns
class AuthTest(TestCase):
def _test_url(self, url):
response = self.client.get(url)
self.assertRedirects(response, expected_url=reverse("login_app:login")+ "?next=" + url)
def test_auth(self):
for url in urlpatterns.SOMETHING:
with self.subTest(url=url):
self._test_url(url)
Is there a way to get the actual list of urls in each urlpattern for my app?
And, related question: for those URLs that require a pk, is there a way to automatically use pk=1
? My fixtures will make sure that pk=1
exists for all those urls.