I made an endpoint called /cars. A person can create cars with a frontend, but devices read cars using an SDK, which has an API Key. This way, 2 rent-a-car companies can use the API without getting the cars mixed-up. Each app has its own API Key and its own person managing the contents.
This is being implemented with django restframework 3.x and django-oauth-toolkit.
I'm writing a test for a human retrieving cars, and another for a device.
This is failing:
def test_get_list(self): # devices have a django user (AUTH_USER_MODEL ---onetoone--- Device) self.client.force_authenticate(user=self.user_device) self._get_list() self.client.force_authenticate(user=None) force_authentication sets request.auth to None. However, with postman or httpie, request.auth contains the Application object.
The queryset is:
def get_queryset(self): if hasattr(self.request.user, 'device'): # get the cars created by the owner of the API Key return self.request.auth.application.user.cars.all() return self.request.user.cars.all() # get my cars - Does this approach in the queryset make sense?
- Am I testing it in the wrong way?
- Why is request.auth empty? Is force_authentication using BasicAuthentication?