Skip to content

add test for force_authenticate about cache of user attr #4102

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

Closed
wants to merge 2 commits into from
Closed

add test for force_authenticate about cache of user attr #4102

wants to merge 2 commits into from

Conversation

lexdene
Copy link

@lexdene lexdene commented May 6, 2016

First, the best way to know whether an OneToOneField field exists is using hasattr.
Second, force_authenticate will cache the user object.

So using force_authenticate will cause a bug as I wrote in the test.
But using client.login will not cause that bug.

@tomchristie
Copy link
Member

I cannot see any way we could possible resolve this.

Yes, force_authenticate sets the user on all incoming requests to a given instance, and yes if you mutate that instance, (or eg peform a reverse lookup that's cached) then then yes that'll persist between calls. I assume that Django's force_login has the same issue.

We might want to document this as a limitation, but I don't believe there's any way we can resolve it.

Does that assessment sound correct?

@lexdene
Copy link
Author

lexdene commented Aug 23, 2016

I assume that Django's force_login has the same issue.

I found that Django's force_login works correctly.

@tomchristie
Copy link
Member

Wondering if #5066 resolves this? Is #5016 a duplicate?

@lexdene
Copy link
Author

lexdene commented May 2, 2017

Wondering if #5066 resolves this?

No.
Using deepcopy makes user object stay in the status when calling force_authenticate.
In my code, the user attribute is changed when Hat.objects.create.

Is #5016 a duplicate?

I think so. How does the author of #5016 think?

@rpkilby
Copy link
Member

rpkilby commented May 3, 2017

I found that Django's force_login works correctly.

Are you sure? A brief look at the code here and here indicates that the user object would be cached similarly to force_authenticate().

@lexdene - could you rebase the PR? This will re-trigger the travis build which seems to be missing for the second commit with the force_login test.

@lexdene
Copy link
Author

lexdene commented May 4, 2017

@rpkilby
Already rebased.

As travis said:

  • test_get_my_hat_with_force_authenticate always failed.
  • test_get_my_hat_with_django_force_login always passed except when Django==1.8 because force_login was new in Django 1.9.
  • test_get_my_hat_with_login always passed.

@carltongibson
Copy link
Collaborator

OK, I have a query here:

Those three things are not consistent.

I'll look again/deeper later. Input/clarification welcome! 🙂

@carltongibson
Copy link
Collaborator

Milestoning to keep on my radar.

@lexdene
Copy link
Author

lexdene commented Sep 22, 2017

@carltongibson
need rebase again?

@carltongibson
Copy link
Collaborator

@lexdene Currently yes, that would help.

@lexdene
Copy link
Author

lexdene commented Sep 22, 2017

@carltongibson
Already rebased. And the result is same with what I commented on 4 May:

test_get_my_hat_with_force_authenticate always failed.
test_get_my_hat_with_django_force_login always passed except when Django==1.8 because force_login was new in Django 1.9.
test_get_my_hat_with_login always passed.

carltongibson added a commit to carltongibson/django-rest-framework that referenced this pull request Sep 25, 2017
…in case you’re reusing the same in-memory user whilst updating it in the DB.

Closes encode#5016, closes encode#5066, closes encode#4102
@carltongibson
Copy link
Collaborator

Closing this in favour of #5445. You need to call user.refresh_from_db() between tests.

carltongibson added a commit that referenced this pull request Sep 25, 2017
…in case you’re reusing the same in-memory user whilst updating it in the DB.

Closes #5016, closes #5066, closes #4102
@lexdene lexdene deleted the cache_user_when_force_authenticate branch April 9, 2019 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants