Ejemplo n.º 1
0
    def test_logout_when_logged_out(self):
        """
        Test to make sure that logout() works when
        the user is not logged in.
        """
        request = HttpRequest()

        SessionMiddleware().process_request(request)
        AuthMiddleware().process_request(request)
        self.assertTrue(hasattr(request, 'auth_user'), 'Request has no auth_user attribute')
        self.assertTrue(request.auth_user.is_anonymous(), 'User "%s" is authenticated' % request.auth_user)

        old_anon_user = request.auth_user

        logout(request)

        session_key = getattr(settings, 'NEWAUTH_SESSION_KEY', DEFAULT_SESSION_KEY)
        session_data = request.session.get(session_key) or {}
        self.assertEquals(session_data.get('uid'), None)
        self.assertEquals(session_data.get('bn'), None)
        self.assertTrue(hasattr(request, 'auth_user'), 'Request has no auth_user attribute')
        self.assertTrue(request.auth_user.is_anonymous(), 'User "%s" is authenticated' % request.auth_user)
        self.assertTrue(request.auth_user is old_anon_user, 'Anonymous User object was changed')
        self.assertTrue(isinstance(request.auth_user, BasicAnonymousUser), 'User "%s" is wrong AnonymousUser class "%s"' % (
            request.auth_user,
            request.auth_user.__class__,
        ))
Ejemplo n.º 2
0
    def test_logout_when_logged_in(self):
        """
        Test to make sure that logout() works when
        the user is logged in.
        """
        from newauth.tests.testapp.models import TestUser3, TestAnonymousUser3
        
        request = HttpRequest()
        user = authenticate(user_id=1, _backend_name='testapp3')
        self.assertTrue(user.is_authenticated(), "%s is not authenticated" % user)

        SessionMiddleware().process_request(request)
        AuthMiddleware().process_request(request)
        self.assertTrue(hasattr(request, 'auth_user'), 'Request has no auth_user attribute')
        self.assertTrue(request.auth_user.is_anonymous(), 'User "%s" is authenticated' % request.auth_user)

        login(request, user, backend_name='testapp3')
        
        session_key = getattr(settings, 'NEWAUTH_SESSION_KEY', DEFAULT_SESSION_KEY)
        session_data = request.session.get(session_key) or {}
        self.assertEquals(session_data.get('uid'), 1)
        self.assertEquals(session_data.get('bn'), 'testapp3')
        self.assertTrue(request.auth_user.is_authenticated(), "%s is not authenticated" % request.auth_user)
        self.assertTrue(isinstance(request.auth_user, TestUser3), 'User "%s" is wrong User class "%s"' % (
            request.auth_user,
            request.auth_user.__class__,
        ))
        self.assertTrue(hasattr(request.auth_user, '_backend'), 'User "%s" has no _backend attribute')
        self.assertTrue(hasattr(request.auth_user, '_backend_name'), 'User "%s" has no _backend_name attribute')

        logout(request)

        session_data = request.session.get(session_key) or {}
        self.assertEquals(session_data.get('uid'), None)
        self.assertEquals(session_data.get('bn'), None)
        self.assertTrue(hasattr(request, 'auth_user'), 'Request has no auth_user attribute')
        self.assertTrue(request.auth_user.is_anonymous(), 'User "%s" is authenticated' % request.auth_user)
        self.assertTrue(isinstance(request.auth_user, TestAnonymousUser3), 'User "%s" is wrong AnonymousUser class "%s"' % (
            request.auth_user,
            request.auth_user.__class__,
        ))