私は API を使用Django
しTastypie
てバージョンに取り組んできました。また、400 または 401 の応答を期待し、常に200 応答を返す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
ありがとう。