Code Monkey home page Code Monkey logo

Comments (4)

Bouke avatar Bouke commented on June 14, 2024

I'm not that familiar with the test client, however I find it strange that request.user would not be available. It should be set by the AuthenticationMiddleware. Are you sure you've setup all middleware correctly?

from django-user-sessions.

danielquinn avatar danielquinn commented on June 14, 2024

It does, but in a typical Django setup, AuthenticationMiddleware comes later in the stack than SessionMiddleware. I agree that it's strange that request.user isn't available, but I'm just working with defaults here and the test client isn't working, hence this issue report :-(

from django-user-sessions.

Bouke avatar Bouke commented on June 14, 2024

After snooping around in the code of Django's auth.login() code, I found what you're referring to:

def login(request, user, backend=None):
    # (...)
    if hasattr(request, 'user'):
        request.user = user
    rotate_token(request)
    user_logged_in.send(sender=user.__class__, request=request, user=user)

So the signal user_logged_in will be passed the request that was passed to the login method. Since this package's (and also upstream's) test client doesn't set request.user, this property is also not available from your signal handler. The test client's login method doesn't use any middleware, so that's why AuthenticationMiddleware doesn't have any effect.

Could you test the fix that you propose in your initial comment? I think it should work fine.

from django-user-sessions.

danielquinn avatar danielquinn commented on June 14, 2024

It does. I just subclassed user_sessions.utils.tests.Client and overrided login() to change:

request = HttpRequest()

to

request = HttpRequest()
request.user = None

And everything works as expected.

Sorry, I'd submit a proper pull request, but I'm neck-deep in a legacy project I'm trying to bring up to speed, so this quick hack was all that I had time for. I just thought that upstream would like the heads-up.

from django-user-sessions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.