Using Basic HTTP access authentication in Django testing framework

前端 未结 6 1452
栀梦
栀梦 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:44

    In your Django TestCase you can update the client defaults to contain your HTTP basic auth credentials.

    import base64
    from django.test import TestCase
    
    class TestMyStuff(TestCase):
    
        def setUp(self):
            credentials = base64.b64encode('username:password')
            self.client.defaults['HTTP_AUTHORIZATION'] = 'Basic ' + credentials
    

提交回复
热议问题