2016-09-27 30 views
0

안녕하세요, 내 애플 리케이션을위한 테스트 케이스를 작성하려고합니다. URL = '/ api/project /'. 나는 인증과 함께 get, post 및 put 메소드를 가능하게했다. 하지만 여전히 나는 내가 다음 명령을 사용하여 테스트 케이스를 실행하고 여기에 작품을 잘하지만 POST 요청 GET, POST 요청tastypie for api_client에 대한 401 오류

class EntryResourceTest(ResourceTestCaseMixin, TestCase): 
    class Meta: 
     queryset = Project.objects.all() 
     resource_name = 'project' 
     allowed_methods = ['get', 'post', 'put'] 
     authentication = Authentication() 
     authorization = Authorization() 

    def setUp(self): 
     super(EntryResourceTest, self).setUp() 
     # Create a user. 
     self.username = 'daniel' 
     self.password = 'pass' 
     self.user = User.objects.create_user(self.username, '[email protected]', self.password) 

    def login(self): 
     return self.api_client.client.login(
     username=self.username, password=self.password) 

    def get_credentials(self): 
    return self.create_basic(username=self.username, password=self.password) 

    def test_post_list(self): 
     self.login() 
     req_get = self.api_client.get('/api/project/', format='json', authentication=self.get_credentials())  # -> get works and i get 200 status code 
     req_post = self.api_client.post('/api/project/', format='json', data=self.post_data, authentication=self.get_credentials()) 

401 오류가 발생합니다. 그리고) 그것은 내가 self.login에서 정의한 기본 로그인을 (사용하기 때문에 내가 인증 매개 변수를 전달하지 않는 경우에도 요청이 작동 얻을

django-admin test myapp.api.project.tests   

답변

0

사용 BasicAuthentication 대신 Authentication.

self.create_basic(...)BasicAuthentication에 대한 헤더를 만듭니다.

def create_basic(self, username, password): 
    """ 
    Creates & returns the HTTP ``Authorization`` header for use with BASIC 
    Auth. 
    """