올바른 템플릿을 사용하고 있는지 테스트하고 싶은데 login_required
데코레이터가있는 페이지가 있습니다. stackoverflow에서 단위 테스트에 대한 인증 방법을 찾았지만 나를 위해 어떤 이유로 작동하지 않습니다.unittest에서 인증 할 수 없음
from django.test import TestCase
from django.test import Client
import base64
class TestUsingCorrectTemplates(TestCase):
def test_correct_classroom_template_used(self):
auth_headers = {'HTTP_AUTHORIZATION': 'Basic '+base64.b64encode('[email protected]:admin')}
c = Client()
response = c.get('/classroom/', **auth_headers)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response,'classroom.html')
또한 오픈 ID/AllAuth로 처리 권한 부여를 언급 좋아하고 더 /login
페이지, 사용자가 로그인 따르는 시작 페이지 /
내용 변수 response
의에서이없는 것 :
Vary: Cookie
X-Frame-Options: SAMEORIGIN
Content-Type: text/html; charset=utf-8
Location: http://testserver/?next=/classroom/
테스트 오류 :
self.assertEqual(response.status_code, 200)
AssertionError: 302 != 200
내가 뭘 잘못하고 있니?
테스트를 실행할 때 인증 할 사용자가 없습니다. 먼저 데이터베이스에 추가해야합니다. Mock을 사용하여 요청과 액세스 토큰을 조롱해야 할 수도 있습니다. – Brandon
그리고'login_required'를 사용한다면 테스트에서 HTTP 인증을 위조하는 것이 전혀 도움이되지 않습니다. –
어떻게 테스트를 통과 할 수 있습니까? – micgeronimo