中驗證我試圖測試一個*未經認證/未經授權用戶訪問在Django的API。在API中我有以下代碼如何獲得的Django單元測試
class myAPIView(TimezoneAwareMixin, RetrieveUpdateAPIView):
model = mymodel
serializer_class = mymodelSerializer
permission_classes = (IsAuthenticated, IsCompanyActive, HasRole)
get_roles = ('mymodel-view',)
def get_queryset(self):
"""
Allow only users within a company to see only their objects
"""
return mymodel.objects.filter(company=self.request.user.active_company)
然後在我的測試中我繼承的TestCase並嘗試以下
client = client_class() # no headers so its anonymous
client.get(url)
,但我得到的測試
AttributeError: 'AnonymousUser' object has no attribute 'attribute_name'
IM期待在接下來的一個403 Forbidden或一個401 Unauthorized
代替追溯
我怎樣才能得到正確的響應
聽起來像你的測試工作,它顯示你有一個錯誤。 – 2015-02-11 10:24:24