[Django]-Can not authorize in unittest

3πŸ‘

βœ…

The HTTP code 302 means that your server is sending a redirect response. You should tell your client to follow redirects so that you deal with the actual login page. You can change your get call like this:

response = c.get('/classroom/', follow=True, **auth_headers)

If you want to check the intermediate redirection steps you can inspect response.redirect_chain. It is all documented here.

πŸ‘€Louis

1πŸ‘

Did you try creating a user and calling the login method of your Client instance?

import base64

from django.test import TestCase


class TestUsingCorrectTemplates(TestCase):
    def setUp(self):
        # Create your user here
        # self.user = ...

    def test_correct_classroom_template_used(self):
        self.client.login('admin@dot.com', 'admin')
        response = self.client.get('/classroom/')  # XXX: You should use url reversal here.
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'classroom.html')

Leave a comment