swsnu / swppfall2019

31 stars 23 forks source link

[HW3]Token in Test #182

Open ttoru96 opened 4 years ago

ttoru96 commented 4 years ago

스켈레톤 코드에서 views.token이 다음과 같이 구현되어 있는데요,

1   def token(request):
2     if request.method == 'GET':
3          return HttpResponse(status=204)
4     else:
5         return HttpResponseNotAllowed(['GET'])

line 5를 테스트하려고 다음과 같이 url('/api/token/')에 정의되지 않은 리퀘스트를 보내도 커버리지 리포트를 출력해보면 line 5가 커버되지 않는다고 뜹니다.

response = client.post('/api/token/')

제가 잘못된 방식으로 테스팅하고 있는 건지 피드백 주시면 감사하겠습니다.

ktaebum commented 4 years ago

self.assertEqual(response.status_code, 405)와 같이 결과에 대한 확인도 하셨나요?

이것의 문제는 아닌 것 같군요

ktaebum commented 4 years ago

아니면 prefix가 test_로 설정이 안 되어 있어서 test가 안 되고 있을수도 있습니다

ttoru96 commented 4 years ago
def test_token(self):
    response = client.post('/api/token/')
    self.assertEqual(response.status_code, 405)

와 같이 별도의 test 함수로 빼주었더니 커버리지는 정상적으로 나왔습니다. (원래는 다른 assert 문과 함께 있었습니다) 다만, HttpResponseNotAllowed 를 리턴하도록 했는데도 불구하고 response 의 status_code는 403으로 찍혀나오는데, 원래 스켈레톤 코드의 line 5를

return HttpResponse(status=403) 

와 같이 바꿔서 해결해도 될까요..?

ktaebum commented 4 years ago

앗 아니요 기존 코드의 status_code 반환 값은 건드리시면 안 됩니다 아마 저 client가 현재 enforce_csrf_checkTrue로 되어 있지 않나요?? 그래서 post를 보내는데 csrf token이 없어서 403이 뜨는 것 같습니다

ttoru96 commented 4 years ago

말씀해주신 피드백 참고해서 다음과 같이 client 세팅하고 테스트 했더니 405가 제대로 떴습니다

client = Client(enforce_csrf_checks = False)

감사합니다