-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IntegrityError when running django tests #55
Comments
Hi there I believe i ran into a variant of this as well, the function
it seems that during the setup of subsequent tests the 'current user' is still set, and the call to Our fix is to clear the current user after the https://github.com/PaesslerAG/django-currentuser/blob/master/django_currentuser/middleware.py#L34 e.g. Is there a reason why this is not cleared in the code already ? otherwise thanks for the module :) -ivan |
@ivan-price-acted did you modify the Having the exact same issue when running tests. Somebody else has suggested a fixture here #47 |
@alfonsrv indeed we run a version of the middleware where just after the line: https://github.com/PaesslerAG/django-currentuser/blob/master/django_currentuser/middleware.py#L34 , i.e. once we have the response, we set the user to None. We've not observed any bad side effects, and it avoids having to use special fixtures for the tests. To be mega explicit:
I'd be interested to know what dangers may befall us, for now it works. -i |
Yeah well it feels like it should be the default way to me too that the user is unset after requests. To be thread-safe and avoid the possibility of leaking information. There already seems to be a PR regarding the topic, but it's stalled #49 - tests... |
ah ok i hadn't found that PR. looking at the test that fails for me it looks like the test is not good, it is asserting the 'current user' after the request is finished. https://github.com/PaesslerAG/django-currentuser/blob/master/tests/testapp/tests.py#L46 the assert should be inside the handling for the view, and client.login() could probably be used to avoid the call to the login endpoint which maybe adds complexity for not much gain. |
In my tests, I create an object that has an
CurrentUserField
. Up until recently everything worked, but now it fails with anSQLdb.IntegrityError
after upgrading topython 3.9
,django 3.2
anddjango-currentuser 0.5.3
.I was able to resolve this by explicitly overriding the user in the
setUp()
method of my test class as follows:_set_current_user(my_user)
. Is that the recommended approach? If so, we should document it somewhere.Stacktrace:
The text was updated successfully, but these errors were encountered: