I'm trying to Test non-login user redirect to login url. Is there a way to pass get parameter (example 'next') to reverse_lazy? I'm using class-based views.
class SecretIndexViewTest(TestCase):
url = reverse_lazy('secret_index')
login_url = reverse_lazy('login')
client = Client()
def test_http_status_code_302(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, self.login_url,)
above fails with
Response redirected to '/login?next=/secret', expected '/login'Expected '/login?next=/secret' to equal '/login'.
tried
class SecretIndexViewTest(TestCase):
url = reverse_lazy('secret_index')
login_url = reverse_lazy('login', kwargs={'next': url)
client = Client()
def test_http_status_code_302(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, self.login_url,)
result is no reverse match
django.urls.exceptions.NoReverseMatch: Reverse for 'login' with keyword arguments '{'next': '/secret'}' not found. 1 pattern(s) tried: ['/login$']
changing kwargs to args will result the same error.
I know I can handle it with below, but I'm looking for a better solution.
self.assertRedirects(response, self.login_url + f"?next={self.url}",)
or
login_url = reverse_lazy('login') + f"?next={url}"