Ejemplo n.º 1
0
 def test_removeNonexistentSession(self):
     """
     L{PersistentSessionWrapper.removeSessionWithKey} does nothing if the
     session does not exist.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     resource.removeSessionWithKey(b'key')
Ejemplo n.º 2
0
 def test_removeNonexistentSession(self):
     """
     L{PersistentSessionWrapper.removeSessionWithKey} does nothing if the
     session does not exist.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     resource.removeSessionWithKey(b'key')
Ejemplo n.º 3
0
 def test_retrieveNonexistentSession(self):
     """
     L{PersistentSessionWrapper.authenticatedUserForKey} returns C{None} if
     a session does not exist.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     user = resource.authenticatedUserForKey(b'doesnotexist')
     self.assertIdentical(user, None)
Ejemplo n.º 4
0
 def test_retrieveNonexistentSession(self):
     """
     L{PersistentSessionWrapper.authenticatedUserForKey} returns C{None} if
     a session does not exist.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     user = resource.authenticatedUserForKey(b'doesnotexist')
     self.assertIdentical(user, None)
Ejemplo n.º 5
0
 def test_retrieveSession(self):
     """
     L{PersistentSessionWrapper.authenticatedUserForKey} returns the user to
     whom a session belongs.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     resource.createSessionForKey(b'key', b'username@domain')
     user = resource.authenticatedUserForKey(b'key')
     self.assertEqual(user, b'username@domain')
Ejemplo n.º 6
0
 def _cookieTest(self, host, cookie, **kw):
     """
     Assert that a L{PersistentSessionWrapper} created with the given
     keyword arguments returns C{cookie} from its C{cookieDomainForRequest}
     method when passed a request with C{host} as the value for its I{Host}
     header.
     """
     request = FakeRequest(headers={'host': host})
     resource = PersistentSessionWrapper(None, None, **kw)
     self.assertEqual(resource.cookieDomainForRequest(request), cookie)
Ejemplo n.º 7
0
    def test_savorSessionCookie(self):
        """
        L{PersistentSessionWrapper.savorSessionCookie} adds a cookie with a
        large maximum age and a request-appropriate domain to the request.
        """
        request = FakeRequest(headers={"host": "example.com"})
        resource = PersistentSessionWrapper(None, None, domains=["example.org", "example.com"])

        resource.savorSessionCookie(request)
        self.assertEqual(request.cookies, {resource.cookieKey: request.getSession().uid})
Ejemplo n.º 8
0
 def test_retrieveSession(self):
     """
     L{PersistentSessionWrapper.authenticatedUserForKey} returns the user to
     whom a session belongs.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     resource.createSessionForKey(b'key', b'username@domain')
     user = resource.authenticatedUserForKey(b'key')
     self.assertEqual(user, b'username@domain')
Ejemplo n.º 9
0
 def _cookieTest(self, host, cookie, **kw):
     """
     Assert that a L{PersistentSessionWrapper} created with the given
     keyword arguments returns C{cookie} from its C{cookieDomainForRequest}
     method when passed a request with C{host} as the value for its I{Host}
     header.
     """
     request = FakeRequest(headers={"host": host})
     resource = PersistentSessionWrapper(None, None, **kw)
     self.assertEqual(resource.cookieDomainForRequest(request), cookie)
Ejemplo n.º 10
0
 def test_removeSession(self):
     """
     L{PersistentSessionWrapper.removeSessionWithKey} removes an existing
     session with the given key.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     resource.createSessionForKey(b'key', b'username@domain')
     self.assertEqual(store.query(PersistentSession).count(), 1)
     resource.removeSessionWithKey(b'key')
     self.assertEqual(store.query(PersistentSession).count(), 0)
Ejemplo n.º 11
0
 def test_createSession(self):
     """
     L{PersistentSessionWrapper.createSessionForKey} creates a persistent
     session in the database for the given session ID.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     resource.createSessionForKey(b'key', b'username@domain')
     session = store.findUnique(PersistentSession)
     self.assertEqual(session.sessionKey, b'key')
     self.assertEqual(session.authenticatedAs, b'username@domain')
Ejemplo n.º 12
0
 def test_createSession(self):
     """
     L{PersistentSessionWrapper.createSessionForKey} creates a persistent
     session in the database for the given session ID.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     resource.createSessionForKey(b'key', b'username@domain')
     session = store.findUnique(PersistentSession)
     self.assertEqual(session.sessionKey, b'key')
     self.assertEqual(session.authenticatedAs, b'username@domain')
Ejemplo n.º 13
0
    def test_savorSessionCookie(self):
        """
        L{PersistentSessionWrapper.savorSessionCookie} adds a cookie with a
        large maximum age and a request-appropriate domain to the request.
        """
        request = FakeRequest(headers={'host': 'example.com'})
        resource = PersistentSessionWrapper(
            None, None, domains=['example.org', 'example.com'])

        resource.savorSessionCookie(request)
        self.assertEqual(request.cookies,
                         {resource.cookieKey: request.getSession().uid})
Ejemplo n.º 14
0
    def test_cleanOnStart(self):
        """
        L{PersistentSessionWrapper} immediately cleans expired sessions on
        instantiation.
        """
        store = Store()
        resource = PersistentSessionWrapper(store, None)
        resource.createSessionForKey(b'key', b'username@domain')
        ps = store.findUnique(PersistentSession)
        ps.lastUsed -= timedelta(seconds=PERSISTENT_SESSION_LIFETIME + 1)

        PersistentSessionWrapper(store, None)
        self.assertEqual(store.query(PersistentSession).count(), 0)
Ejemplo n.º 15
0
    def test_logoutRemovesSession(self):
        """
        Logging out explicitly removes your persistent session.
        """
        store = Store()
        resource = PersistentSessionWrapper(store, None)
        session = GuardSession(resource, b'uid')

        resource.createSessionForKey(session.uid, b'username@domain')
        self.assertEqual(store.query(PersistentSession).count(), 1)

        resource.explicitLogout(session)
        self.assertEqual(store.query(PersistentSession).count(), 0)
Ejemplo n.º 16
0
    def test_cleanOnStart(self):
        """
        L{PersistentSessionWrapper} immediately cleans expired sessions on
        instantiation.
        """
        store = Store()
        resource = PersistentSessionWrapper(store, None)
        resource.createSessionForKey(b'key', b'username@domain')
        ps = store.findUnique(PersistentSession)
        ps.lastUsed -= timedelta(seconds=PERSISTENT_SESSION_LIFETIME + 1)

        PersistentSessionWrapper(store, None)
        self.assertEqual(store.query(PersistentSession).count(), 0)
Ejemplo n.º 17
0
    def test_sessionCleanup(self):
        """
        Expired sessions are cleaned up every C{sessionCleanFrequency} seconds.
        """
        clock = Clock()
        store = Store()
        portal = Portal(_TrivialRealm())
        portal.registerChecker(AllowAnonymousAccess(), IAnonymous)
        request = FakeRequest(headers={'host': 'example.com'})
        resource = PersistentSessionWrapper(
            store, portal, domains=['example.org', 'example.com'], clock=clock)
        session = GuardSession(resource, b'uid')

        # Create two sessions
        resource.createSessionForKey(b'key1', b'username@domain')
        resource.createSessionForKey(b'key2', b'username@domain')
        self.assertEqual(store.query(PersistentSession).count(), 2)

        # Session shouldn't be cleaned yet
        resource.login(request, session, Anonymous(), ())
        self.assertEqual(store.query(PersistentSession).count(), 2)

        # First session is expired and it's time for a clean
        ps = store.findUnique(PersistentSession,
                              PersistentSession.sessionKey == b'key1')
        ps.lastUsed -= timedelta(seconds=PERSISTENT_SESSION_LIFETIME + 1)
        clock.advance(SESSION_CLEAN_FREQUENCY + 1)
        resource.login(request, session, Anonymous(), ())
        self.assertEqual(
            list(store.query(PersistentSession).getColumn('sessionKey')),
            [b'key2'])

        # Now we expire the second session
        ps2 = store.findUnique(PersistentSession,
                               PersistentSession.sessionKey == b'key2')
        ps2.lastUsed -= timedelta(seconds=PERSISTENT_SESSION_LIFETIME + 1)
        clock.advance(SESSION_CLEAN_FREQUENCY + 1)
        resource.login(request, session, Anonymous(), ())
        self.assertEqual(store.query(PersistentSession).count(), 0)
Ejemplo n.º 18
0
 def getFactory(self):
     """
     Create an L{AxiomSite} which supports authenticated and anonymous
     access.
     """
     checkers = [self.loginSystem, AllowAnonymousAccess()]
     guardedRoot = PersistentSessionWrapper(
         self.store,
         Portal(self.loginSystem, checkers),
         domains=[self.hostname])
     unguardedRoot = UnguardedWrapper(self.store, guardedRoot)
     securingRoot = SecuringWrapper(self, unguardedRoot)
     logPath = None
     if self.httpLog is not None:
         logPath = self.httpLog.path
     return AxiomSite(self.store, securingRoot, logPath=logPath)
Ejemplo n.º 19
0
    def test_sessionCleanup(self):
        """
        Expired sessions are cleaned up every C{sessionCleanFrequency} seconds.
        """
        clock = Clock()
        store = Store()
        portal = Portal(_TrivialRealm())
        portal.registerChecker(AllowAnonymousAccess(), IAnonymous)
        request = FakeRequest(headers={'host': 'example.com'})
        resource = PersistentSessionWrapper(
            store, portal, domains=['example.org', 'example.com'], clock=clock)
        session = GuardSession(resource, b'uid')

        # Create two sessions
        resource.createSessionForKey(b'key1', b'username@domain')
        resource.createSessionForKey(b'key2', b'username@domain')
        self.assertEqual(store.query(PersistentSession).count(), 2)

        # Session shouldn't be cleaned yet
        resource.login(request, session, Anonymous(), ())
        self.assertEqual(store.query(PersistentSession).count(), 2)

        # First session is expired and it's time for a clean
        ps = store.findUnique(
            PersistentSession, PersistentSession.sessionKey == b'key1')
        ps.lastUsed -= timedelta(seconds=PERSISTENT_SESSION_LIFETIME + 1)
        clock.advance(SESSION_CLEAN_FREQUENCY + 1)
        resource.login(request, session, Anonymous(), ())
        self.assertEqual(
            list(store.query(PersistentSession).getColumn('sessionKey')),
            [b'key2'])

        # Now we expire the second session
        ps2 = store.findUnique(
            PersistentSession, PersistentSession.sessionKey == b'key2')
        ps2.lastUsed -= timedelta(seconds=PERSISTENT_SESSION_LIFETIME + 1)
        clock.advance(SESSION_CLEAN_FREQUENCY + 1)
        resource.login(request, session, Anonymous(), ())
        self.assertEqual(store.query(PersistentSession).count(), 0)
Ejemplo n.º 20
0
 def test_removeSession(self):
     """
     L{PersistentSessionWrapper.removeSessionWithKey} removes an existing
     session with the given key.
     """
     store = Store()
     resource = PersistentSessionWrapper(store, None)
     resource.createSessionForKey(b'key', b'username@domain')
     self.assertEqual(store.query(PersistentSession).count(), 1)
     resource.removeSessionWithKey(b'key')
     self.assertEqual(store.query(PersistentSession).count(), 0)
Ejemplo n.º 21
0
    def test_logoutRemovesSession(self):
        """
        Logging out explicitly removes your persistent session.
        """
        store = Store()
        resource = PersistentSessionWrapper(store, None)
        session = GuardSession(resource, b'uid')

        resource.createSessionForKey(session.uid, b'username@domain')
        self.assertEqual(store.query(PersistentSession).count(), 1)

        resource.explicitLogout(session)
        self.assertEqual(store.query(PersistentSession).count(), 0)