dfunckt / django-rules

Awesome Django authorization, without the database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error from TestCase - AttributeError: 'ObjectPermissionBackend' object has no attribute 'get_user'

hale01 opened this issue · comments

Hello

django == 1.9 but perhaps relevant for 2.0

If write in TestCase self.client.force_login(user) and self.client.get(url) we receive error

...
File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 22, in <lambda>
    request.user = SimpleLazyObject(lambda: get_user(request))
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 10, in get_user
    request._cached_user = auth.get_user(request)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 174, in get_user
    user = backend.get_user(user_id)
AttributeError: 'ObjectPermissionBackend' object has no attribute 'get_user'

ObjectPermissionBackend has no attribute 'get_user', but this attribute called in django.contrib.auth.__init__.get_user

def get_user(request):
    """
    Return the user model instance associated with the given request session.
    If no user is retrieved, return an instance of `AnonymousUser`.
    """
    from .models import AnonymousUser
    user = None
    try:
        user_id = _get_user_session_key(request)
        backend_path = request.session[BACKEND_SESSION_KEY]
    except KeyError:
        pass
    else:
        if backend_path in settings.AUTHENTICATION_BACKENDS:
            backend = load_backend(backend_path)
            user = backend.get_user(user_id)
            # Verify the session
            if hasattr(user, 'get_session_auth_hash'):
                session_hash = request.session.get(HASH_SESSION_KEY)
                session_hash_verified = session_hash and constant_time_compare(
                    session_hash,
                    user.get_session_auth_hash()
                )
                if not session_hash_verified:
                    request.session.flush()
                    user = None

    return user or AnonymousUser()

This code actual for 1.9 and 2.0

Thanks

@hale01 can you try the workaround mentioned here and see if it works? https://github.com/dfunckt/django-rules/tree/1.x#testing