zsoldosp / django-currentuser

Conveniently store reference to request user on thread/db level.
BSD 3-Clause "New" or "Revised" License
159 stars 41 forks source link

Getting and setting the current user while testing #56

Open ShmuelTreiger opened 2 years ago

ShmuelTreiger commented 2 years ago

New to using this project, thanks for creating and maintaining it!

While creating tests for myself, I find myself wanting to set the current user. I've finally had success using the internal _set_current_user() method. It would be great if there were an external facing testing toolset to make this easier/more accessible. Happy to make a go at it, if there's interest.

ivan-price-acted commented 2 years ago

+1 @ShmuelTreiger may i suggest a context manager if you decide to implement this, eg.

with current_user('foobar'):
    objects.save()

we have noted that the client.login(user) in pytest 'works' however, so what you describe is only necessary for testing stuff outside of an api request.

-i

ShmuelTreiger commented 2 years ago

Unfortunately, I've moved on from using this project, don't think I'm likely make a patch any time soon. Sorry.

aidanlister commented 4 months ago

Django-CRUM has a testing middleware for inspiration in case anyone adopts this package: https://django-crum.readthedocs.io/en/latest/