当我期望响应为 400 或 401 并且它总是返回200 响应时,我一直在使用版本Django
和测试用例处理API。什么会导致这个问题?Tastypie
django-tastypie==0.9.13-beta
bad request
http ok
用户资源
class UserAuthentication(ApiKeyAuthentication):
def __init__(self, no_auth, full_auth, require_active=True):
super(Authentication, self).__init__()
self.no_auth = no_auth
self.full_auth = full_auth
def is_authenticated(self, request, **kwargs):
if request.method not in self.no_auth:
return super(UserAuthentication, self).is_authenticated(request, **kwargs)
return True
class UsersResource(MyModelResource):
class Meta:
queryset = User.objects.all()
allowed_methods = ['post']
detail_allowed_methods = ['put']
fields = ['id', 'first_name', 'last_name', 'email']
validation = UserValidation()
always_return_data = True
authorization = UserAuthorization()
authentication = UserAuthentication(no_auth=['GET', 'POST'], full_auth=['PUT'])
小部分测试
def test_login_unsuccessful_wrong_email(self):
post_data = {
"user": {
"email": 'wrong@email.com', # so email is wrong
"password": self.user_password
}
}
response = self.api_client.post('/api/v1/login/', data=post_data)
self.assertHttpBadRequest(response) # At this place I expect 400 status code
测试失败回溯
FAIL: test_login_unsuccessful_wrong_email (api.v1.tests.UsersLoginResourceTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sultan/.virtualenvs/myenv/myproject/api/v1/tests.py", line 458, in test_login_unsuccessful_wrong_email
self.assertHttpBadRequest(response)
File "/Users/sultan/.virtualenvs/myenv/lib/python2.7/site-packages/django_tastypie-0.9.13_beta-py2.7.egg/tastypie/test.py", line 346, in assertHttpBadRequest
return self.assertEqual(resp.status_code, 400)
AssertionError: 200 != 400
谢谢。