Using Basic HTTP access authentication in Django testing framework

前端 未结 6 1453
栀梦
栀梦 2020-12-08 01:41

For some of my Django views I\'ve created a decorator that performs Basic HTTP access authentication. However, while writing test cases in Django, it took me a while to wor

6条回答
  •  攒了一身酷
    2020-12-08 02:38

    Another way to do it is to bypass the Django Client() and use Requests instead.

    class MyTest(TestCase):
        def setUp(self):
            AUTH = requests.auth.HTTPBasicAuth("username", "password")
    
        def some_test(self):
            resp = requests.get(BASE_URL + 'endpoint/', auth=AUTH)
            self.assertEqual(resp.status_code, 200)
    

提交回复
热议问题