Monday, 5 July 2021

Get current user in Django not working in tests

I have a couple of views in my Django app that perform and action and record who did the action in the DB, something like:

def my_view(request):
    # do some stuff here first
    current_user = MyCustomUserObject.objects.filter(django_user_id=request.user.id).first()
    model_i_did_something_to_above.last_modified_by = current_user
    model.save()

And this actually works fine if I run the server and call it via postman. However, when I do a Unit test:

from datetime import datetime

class MyTests(TestCase):

    def setUp(self):
        now = datetime.utcnow()
        django_user = User.objects.create_user(username='myusername',
                                           email='test@test.com',
                                           password='abcd')
        
        self.user = MyCustomUserObject(user_name='myusername', email_address='test@test.com', created_datetime=now,
                    last_modified_datetime=now, is_admin=True, django_user=django_user)
        self.user.save()
        self.tokens = json.loads(self.client.post(reverse('authenticate'),
                                                  data={'username': 'myusername',
                                                        'password': 'abcd'},
                                                  content_type='application/json').content)

    def test_stuff(self):
        self.client.delete(reverse('nameoftheurl'),
                               {data: 'stuff'},
                               content_type='application/json',
                               **{'HTTP_AUTHORIZATION': self.tokens['access']})

And it reaches the view, it says that request.user.id is None. Why does this happen? Is there a way around this?

EDIT

The authenticate view goes like this:

from rest_framework_simplejwt.views import TokenObtainPairView

urlpatterns = [
    path('authenticate/', TokenObtainPairView.as_view(), name='authenticate'),
    ]

I also updated the test example above to show how the user is created.



from Get current user in Django not working in tests

No comments:

Post a Comment