[Zope3-checkins] CVS: Zope3/src/zope/app/pluggableauth/tests -
	__init__.py:1.1 authsetup.py:1.1 test_pluggableauth.py:1.1
    Stephan Richter 
    srichter at cosmos.phy.tufts.edu
       
    Wed Mar 10 12:56:38 EST 2004
    
    
  
Update of /cvs-repository/Zope3/src/zope/app/pluggableauth/tests
In directory cvs.zope.org:/tmp/cvs-serv16551/src/zope/app/pluggableauth/tests
Added Files:
	__init__.py authsetup.py test_pluggableauth.py 
Log Message:
Moved pluggable authentication service to zope.app.pluggableauth. Added module
aliases (tested) so that old services survive the change.
=== Added File Zope3/src/zope/app/pluggableauth/tests/__init__.py ===
""" duh """
=== Added File Zope3/src/zope/app/pluggableauth/tests/authsetup.py ===
##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################
"""Setup local Pluggable Authentication Service for tests
This setup class can be used, if a set of local principals are required for a
test.
$Id: authsetup.py,v 1.1 2004/03/10 17:56:37 srichter Exp $
"""
import base64
from zope.publisher.browser import TestRequest as Request
from zope.app.tests import ztapi, setup
from zope.app.services.tests import placefulsetup
from zope.publisher.interfaces.http import IHTTPCredentials
from zope.app.security.interfaces import ILoginPassword
from zope.app.security.basicauthadapter import BasicAuthAdapter
from zope.app.pluggableauth import \
     PrincipalAuthenticationView, PluggableAuthenticationService, \
     BTreePrincipalSource, SimplePrincipal
from zope.app.pluggableauth.interfaces import IPrincipalSource
class AuthSetup(placefulsetup.PlacefulSetup):
    def setUp(self):
        sm = placefulsetup.PlacefulSetup.setUp(self, site=True)
        ztapi.provideAdapter(IHTTPCredentials, ILoginPassword, BasicAuthAdapter)
        ztapi.browserView(IPrincipalSource, "login",
                          (PrincipalAuthenticationView,))
        auth = setup.addService(sm, "PluggableAuthService",
                                PluggableAuthenticationService())
        one = BTreePrincipalSource()
        two = BTreePrincipalSource()
        self._one = one
        self._two = two
        auth.addPrincipalSource('one', one)
        auth.addPrincipalSource('two', two)
        self._auth = auth
        self.createUsers()
    def createUsers(self):
        self._srichter = SimplePrincipal('srichter', 'hello',
                                         'Stephan', 'Richter')
        self._srichter.id = 'srichter'
        self._jim = SimplePrincipal('jim', 'hello2',
                                    'Jim', 'Fulton')
        self._jim.id = 'jim'
        self._stevea = SimplePrincipal('stevea', 'hello3',
                                       'Steve', 'Alenxander')
        self._stevea.id = 'stevea'
        self._one['srichter'] = self._srichter
        self._one['jim'] = self._jim
        self._two['stevea'] = self._stevea
    def getRequest(self, uid=None, passwd=None):
        if uid is None:
            return Request()
        if passwd is None:
            passwd = ''
        dict =  {
            'HTTP_AUTHORIZATION':
            "Basic %s" % base64.encodestring('%s:%s' % (uid, passwd))
         }
        return Request(**dict)
=== Added File Zope3/src/zope/app/pluggableauth/tests/test_pluggableauth.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.
#
##############################################################################
"""Pluggable Auth Tests
$Id: test_pluggableauth.py,v 1.1 2004/03/10 17:56:37 srichter Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from zope.testing.doctestunit import DocTestSuite
from zope.interface.verify import verifyObject
from zope.app import zapi
from zope.app.tests import ztapi
from zope.app.services.tests import placefulsetup
from zope.exceptions import NotFoundError
from zope.publisher.interfaces.http import IHTTPCredentials
from zope.app.tests import setup
from zope.exceptions import NotFoundError
from zope.app.pluggableauth import BTreePrincipalSource, \
     SimplePrincipal, PluggableAuthenticationService, \
     PrincipalAuthenticationView
from zope.app.pluggableauth.interfaces import IPrincipalSource
from zope.app.pluggableauth.interfaces import IUserSchemafied
from zope.app.security.interfaces import IPrincipal, ILoginPassword
from zope.app.security.basicauthadapter import BasicAuthAdapter
from zope.publisher.browser import TestRequest as Request
from zope.app.tests.placelesssetup import setUp, tearDown
import base64
class Setup(placefulsetup.PlacefulSetup, TestCase):
    def setUp(self):
        sm = placefulsetup.PlacefulSetup.setUp(self, site=True)
        ztapi.provideAdapter(IHTTPCredentials, ILoginPassword, BasicAuthAdapter)
        ztapi.browserView(IPrincipalSource, "login",
                          (PrincipalAuthenticationView,))
        auth = setup.addService(sm, "TestPluggableAuthenticationService",
                                PluggableAuthenticationService())
        one = BTreePrincipalSource()
        two = BTreePrincipalSource()
        self._one = one
        self._two = two
        auth.addPrincipalSource('one', one)
        auth.addPrincipalSource('two', two)
        self._auth = auth
        self.createUsers()
    def createUsers(self):
        
        self._slinkp = SimplePrincipal('slinkp', '123')
        self._slinkp2 = SimplePrincipal('slinkp2', '123')
        self._chrism = SimplePrincipal('chrism', '123')
        self._chrism2 = SimplePrincipal('chrism2', '123')
        self._one['slinkp'] = self._slinkp
        self._one['chrism'] = self._chrism
        self._two['slinkp2'] = self._slinkp2
        self._two['chrism2'] = self._chrism2
    def getRequest(self, uid=None, passwd=None):
        if uid is None:
            return Request()
        if passwd is None:
            passwd = ''
        dict =  {
            'HTTP_AUTHORIZATION':
            "Basic %s" % base64.encodestring('%s:%s' % (uid, passwd))
         }
        return Request(**dict)
class AuthServiceTest(Setup):
    def testAuthServiceAuthenticate(self):
        auth = self._auth
        req = self.getRequest('slinkp', '123')
        pid = auth.authenticate(req).getLogin()
        self.assertEquals(pid, 'slinkp')
        req = self.getRequest('slinkp', 'hello2')
        p = auth.authenticate(req)
        self.assertEquals(p, None)
        req = self.getRequest('doesnotexit', 'hello')
        principal = auth.authenticate(req)
        self.assertEquals(principal, None)
    def testUnauthenticatedPrincipal(self):
        auth = self._auth
        self.assertEqual(None, auth.unauthenticatedPrincipal())
    def testUnauthorized(self):
        auth = self._auth
        req = self.getRequest('nobody', 'nopass')
        self.assertEqual(None, auth.unauthorized((None, None, None), req))
    def _fail_NoSourceId(self):
        self._auth.getPrincipal((self._auth.earmark, None, None))
    def _fail_BadIdType(self):
        self._auth.getPrincipal((self._auth.earmark, None, None))
    def _fail_BadIdLength(self):
        self._auth.getPrincipal((self._auth.earmark, None, None))
    def testAuthServiceGetPrincipal(self):
        auth = self._auth
        id = self._slinkp.id
        self.assertEqual(self._slinkp, auth.getPrincipal(id))
        self.assertRaises(NotFoundError, self._fail_NoSourceId)
        self.assertRaises(NotFoundError, self._fail_BadIdType)
        self.assertRaises(NotFoundError, self._fail_BadIdLength)
    def testGetPrincipals(self):
        auth = self._auth
        self.failUnless(self._slinkp in auth.getPrincipals('slinkp'))
        self.failUnless(self._slinkp2 in auth.getPrincipals('slinkp'))
    def testPrincipalInterface(self):
        verifyObject(IUserSchemafied, self._slinkp)
        verifyObject(IPrincipal, self._slinkp)
class BTreePrincipalSourceTest(Setup):
    def test_authenticate(self):
        one = self._one
        self.assertEqual(None, one.authenticate('bogus', 'bogus'))
        self.assertEqual(self._slinkp, one.authenticate('slinkp', '123'))
        self.assertEqual(None, one.authenticate('slinkp', 'not really'))
    def test_getPrincipal(self):
        one = self._one
        p = self._slinkp
        self.assertEqual(p, one.getPrincipal(p.id))
class PrincipalAuthenticationViewTest(Setup):
    def test_authenticate(self):
        request = self.getRequest('chrism', '123')
        view = PrincipalAuthenticationView(self._one, request)
        self.assertEqual(self._chrism, view.authenticate())
def test_suite():
    t1 = makeSuite(AuthServiceTest)
    t2 = DocTestSuite('zope.app.pluggableauth',
                      setUp=setUp, tearDown=tearDown)
    t3 = makeSuite(BTreePrincipalSourceTest)
    t4 = makeSuite(PrincipalAuthenticationViewTest)
    return TestSuite((t1, t2, t3, t4))
if __name__=='__main__':
    main(defaultTest='test_suite')
    
    
More information about the Zope3-Checkins
mailing list