Search code examples
djangomockingassertiondjango-testing

Django AssertRedirects throws Assertion error 301 != 302


My Django view conditionally redirects to either error page or the target page on invoking 'get'. I am using redirect(reverse()) for the redirection logic. When I run this application, the url redirections are working as expected. For writing test cases, I mocked the function that returns this redirect(reverse()). I confirmed that the mock response is being returned when the method is called. But the response in the test case either shows http404ResponseNotFound or an assertion Error 301 != 302. How to properly test for redirection?

If I use self.assertRedirects(response, '/view/step/123', fetch_redirect_response=False), it throws Assertion error: 301 != 302

If I use self.assertRedirects(response, '/view/step/123', status_code=301, fetch_redirect_response=False), it throws Assertion error: 404 != 200

Class MyView:
   def foo(self):
    try:
      # some logic
      return redirect(reverse('someview', kwargs={'key': '12345'}))
    except(ExceptionClass1, ExceptionClass2) as err:
      return errorView.as_view()(err)

   def get(self):
      # some logic
      if (some_condition)
          return self.foo() # Test case is for this line.
      return something_else
Class MyViewTests:
   @mock.patch(MyView.foo)
   def test_1(self, mock_foo):
       mock_foo.return_value = redirect(reverse('step', kwargs={'key': '123'}))
       client = Client()
       response = client.get('/myview', follow=True)
       self.assertRedirects(response, '/view/step/123', fetch_redirect_response=False)
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/mock/mock.py", line 1346, in patched
    return func(*newargs, **newkeywargs)
  File "/usr/src/app/django/tests/test_api.py", line 488, in test_1
    self.assertRedirects(response, '/view/step/123', fetch_redirect_response=False)
  File "/usr/local/lib/python3.8/site-packages/django/test/testcases.py", line 274, in assertRedirects
    self.assertEqual(
AssertionError: 301 != 302 : Initial response didn't redirect as expected: Response code was 301 (expected 302)


Solution

  • This has been resolved. The issue is that all the subsequent requests are also being followed through because of follow=True which is necessary if I want to assert the redirection. So, 'assertRedirects' asserts the status code against the final redirect as target_status_code. And 200 seems to be the default. And in my case, I don't need to test the behavior of subsequent redirects. So, I changed the code to self.assertRedirects(response, '/view/step/123', status_code=301, target_status_code=404)