Unit testing: calling APIClient.force_authenticate()
with user=None
does not work as intended
#8184
Unanswered
willbeaufoy
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
If you call
APIClient.force_authenticate()
with atoken
param but without auser
param,self.handler._force_token
is set to the provided token, but thenself.logout()
is called, which immediately setsself.handler._force_token
toNone
again. Surely this is not intended? The docstring and the docs say you can use either a user or a token or both, but in reality you cannot just use a token.I discovered this while writing a unit test for an endpoint with access authorised by the OAuth client credentials method (provided by django-oauth-toolkit), where requests have a token but no user. Therefore I tried to authenticate then call my endpoint like this:
But my test fails as I get a 401 Unauthorized response.
However if I comment out the line in
APIClient.logout()
that setsself.handler._force_token
toNone
, the request makes it through to my endpoint successfully and my test passes.The PR that changed the
logout()
method to setself.handler._force_user
andself.handler._force_token
toNone
was done for an unrelated reason. Perhaps at the time it was overlooked that this broke the case I am trying to test above?If you agree that this current apparently broken behaviour is a bug, then I can do a PR to fix it.
Beta Was this translation helpful? Give feedback.
All reactions