[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/OFS/Services/SessionService/tests - testCookieSessionService.py:1.1 __init__.py:1.1

Itamar Shtull-Trauring zope@itamarst.org
Wed, 4 Dec 2002 16:46:17 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Services/SessionService/tests
In directory cvs.zope.org:/tmp/cvs-serv16834/SessionService/tests

Added Files:
	testCookieSessionService.py __init__.py 
Log Message:
initial session interfaces and simplistic session service implementation


=== Added File Zope3/lib/python/Zope/App/OFS/Services/SessionService/tests/testCookieSessionService.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################

from unittest import TestCase, TestLoader, TextTestRunner
from Zope.App.OFS.Services.ServiceManager.tests.PlacefulSetup \
    import PlacefulSetup
from Zope.ComponentArchitecture import getServiceManager, getService
from Zope.Server.HTTP.http_date import parse_http_date

from Zope.App.OFS.Services.SessionService.ISessions import \
     ISessionService, ISessionDataManager
from Zope.App.OFS.Services.SessionService.CookieSessionService import \
     CookieSessionService

import time


class DummyDataManager:

    __implements__ = ISessionDataManager

    def __init__(self):
        self.data = {}

    def getDataObject(self, sid):
        return self.data.setdefault(sid, {})

    def deleteData(self, sid):
        del self.data[sid]


class FakeRequest:

    def __init__(self):
        self.sets = 0
        self.cookies = {}
        self.response = self

    def setCookie(self, k, v, **kw):
        self.sets += 1
        self.cookies[k] = v
        if not abs(parse_http_date(kw["expires"]) - int(time.time()) - 1800) < 1:
            raise AssertionError

         
class SessionServiceTestCaseMixin(PlacefulSetup):

    serviceFactory = None
    
    def setUp(self):
        PlacefulSetup.setUp(self)
        self.buildFolders()
        root_sm = getServiceManager(None)
        svc = self.serviceFactory()
        root_sm.defineService("SessionService", ISessionService)
        root_sm.provideService("SessionService", svc)
        self.svc = getService(self.rootFolder, "SessionService")

    def testRegister(self):
        d = DummyDataManager()
        d2 = DummyDataManager()
        self.svc.registerDataManager("foo", d)
        self.assertRaises(ValueError, self.svc.registerDataManager, "foo", d2)
        self.assertEquals(self.svc.getDataManager("foo"), d)
        self.svc.unregisterDataManager("foo")
        self.assertRaises(KeyError, self.svc.getDataManager, "foo")
        self.svc.registerDataManager("foo", d2)

    def testCookie(self):
        req = FakeRequest()
        sid = self.svc.generateUniqueId()
        self.svc.setRequestId(req, sid)
        self.assertEquals(self.svc.getRequestId(req), sid)

    def testGetSession(self):
        req = FakeRequest()
        sid = self.svc.getSessionId(req)
        self.assertEquals(req.sets, 1)
        self.assertEquals(self.svc.getRequestId(req), sid)
        self.assertEquals(self.svc.getSessionId(req), sid)
        # make sure cookie was also set during 2nd getSessionId
        self.assertEquals(req.sets, 2)

    def testLookupAndInvalidate(self):
        dm = DummyDataManager()
        svc = self.svc
        svc.registerDataManager("dm", dm)
        req = FakeRequest()
        from Zope.App.OFS.Services.SessionService import getSessionDataObject
        d = getSessionDataObject(self.rootFolder, req, "dm")
        d["a"] = "b"
        self.assert_(d is dm.getDataObject(svc.getSessionId(req)))
        self.assertEquals("b", dm.getDataObject(svc.getSessionId(req))["a"])
        svc.invalidate(svc.getSessionId(req))
        d2 = getSessionDataObject(self.rootFolder, req, "dm")
        self.assertEquals(d2, {})

        
class CookieServiceTestCase(SessionServiceTestCaseMixin, TestCase):

    serviceFactory = CookieSessionService


def test_suite():
    loader=TestLoader()
    return loader.loadTestsFromTestCase(CookieServiceTestCase)

if __name__=='__main__':
    TextTestRunner().run(test_suite())


=== Added File Zope3/lib/python/Zope/App/OFS/Services/SessionService/tests/__init__.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################