[Zope3-checkins] CVS: Zope3/src/zope/app/utilities/tests - test_session.py:1.1.2.1

Stuart Bishop zen at shangri-la.dropbear.id.au
Sat Feb 7 22:19:05 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/utilities/tests
In directory cvs.zope.org:/tmp/cvs-serv15955/src/zope/app/utilities/tests

Added Files:
      Tag: ozzope-session-branch
	test_session.py 
Log Message:
Non-service session design and initial Browser Id Manager Utility 
implementation.


=== Added File Zope3/src/zope/app/utilities/tests/test_session.py ===
# -*- coding: ascii -*-
##############################################################################
#
# 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.
#
##############################################################################
'''
$Id: test_session.py,v 1.1.2.1 2004/02/08 03:19:04 Zen Exp $
'''

__rcs_id__  = '$Id: test_session.py,v 1.1.2.1 2004/02/08 03:19:04 Zen Exp $'
__version__ = '$Revision: 1.1.2.1 $'[11:-2]

import unittest, doctest
from zope.app.tests import placelesssetup
from zope.app.tests import ztapi

from zope.app.interfaces.utilities.session import IBrowserId, IBrowserIdManager
from zope.app.utilities.session import CookieBrowserIdManager
from zope.app.utilities.session import getBrowserId
from zope.publisher.http import HTTPRequest

def test_CookieBrowserIdManager():
    """
    CookieBrowserIdManager.generateUniqueId should generate a unique
    IBrowserId each time it is called

    >>> csu = CookieBrowserIdManager()
    >>> id1 = csu.generateUniqueId()
    >>> id2 = csu.generateUniqueId()
    >>> id1 != id2
    True
    >>> IBrowserId.isImplementedBy(id1)
    True
    >>> IBrowserId.isImplementedBy(id2)
    True

    CookieBrowserIdManager.getRequestId pulls the IBrowserId from an
    IHTTPRequest, or returns None if there isn't one stored in it.
    Because cookies cannnot be trusted, we confirm that they are not forged,
    returning None if we have a corrupt or forged browser id.
    
    >>> request = HTTPRequest(None, None, {}, None)
    >>> csu.getRequestId(request) is None
    True
    >>> csu.setRequestId(request, id1)
    >>> csu.getRequestId(request) == id1
    True
    >>> csu.setRequestId(request, 'invalid_id')
    >>> csu.getRequestId(request) is None
    True

    Make sure that the same browser id is extracted from a cookie in
    request (sent from the browser) and a cookie in request.response
    (set during this transaction)

    >>> request2 = HTTPRequest(None, None, {}, None)
    >>> request2._cookies = request.response._cookies
    >>> csu.getRequestId(request) == csu.getRequestId(request2)
    True

    CookieBrowserIdManager.getBrowserId pulls the IBrowserId from an
    IHTTPRequest, or generates a new one and returns it after storing
    it in the request.

    >>> id3 = csu.getBrowserId(request)
    >>> id4 = csu.getBrowserId(request)
    >>> str(id3) == str(id4)
    True
    >>> id3 == id4
    True

    Confirm the path of the cookie is correct. The value being tested
    for here will eventually change - it should be the URL to the
    site containing the CookieBrowserIdManager

    >>> cookie = request.response.getCookie(csu.namespace)
    >>> cookie['path'] == request.getApplicationURL(path_only=True)
    True

    XXX: Confirm the expiry time of the cookie is correct.
    XXX: >>> expires = strptime(cookie['expires'])

    """

def test_getBrowserId():
    """
    >>> placelesssetup.setUp()
    >>> ztapi.provideUtility(IBrowserIdManager, CookieBrowserIdManager())
    >>> request = HTTPRequest(None, None, {}, None)
    >>> id1 = getBrowserId(request)
    >>> id2 = getBrowserId(request)
    >>> id1 == id2
    True
    >>> IBrowserId.isImplementedBy(id1)
    True
    >>> IBrowserId.isImplementedBy(id2)
    True
    >>> placelesssetup.tearDown()
    """

def test_suite():
    return unittest.TestSuite((
        doctest.DocTestSuite(),
        ))

if __name__ == '__main__':
    unittest.main()

# vim: set filetype=python ts=4 sw=4 et si






More information about the Zope3-Checkins mailing list