zsoldosp / django-currentuser

Conveniently store reference to request user on thread/db level.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting and setting the current user while testing

ShmuelTreiger opened this issue · comments

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.

+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

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

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