[Zope3-checkins] CVS: Zope3/src/zope/app/services/pluggableauth/tests - __init__.py:1.1.2.1 test_pluggableauth.py:1.1.2.1

Chris McDonough chrism@zope.com
Sun, 22 Jun 2003 21:08:24 -0400


Update of /cvs-repository/Zope3/src/zope/app/services/pluggableauth/tests
In directory cvs.zope.org:/tmp/cvs-serv27350/src/zope/app/services/pluggableauth/tests

Added Files:
      Tag: pluggable_authentication_service-branch
	__init__.py test_pluggableauth.py 
Log Message:
Provide an authentication view for pluggable auth service.

Write some tests for pluggable auth service.


=== Added File Zope3/src/zope/app/services/pluggableauth/tests/__init__.py ===
""" duh """


=== Added File Zope3/src/zope/app/services/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.
#
##############################################################################
"""
$Id: test_pluggableauth.py,v 1.1.2.1 2003/06/23 01:08:23 chrism Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.app.services.auth import AuthenticationService
from zope.app.services.auth import User
from zope.app.services.servicenames import Adapters, Authentication
from zope.app.services.tests import placefulsetup

from zope.exceptions import NotFoundError
from zope.publisher.interfaces.http import IHTTPCredentials
from zope.app.services.service import ServiceConfiguration
from zope.app.traversing import getPath, traverse
from zope.app.interfaces.services.configuration import Active
from zope.app.container.tests.test_icontainer import BaseTestIContainer
from zope.publisher.interfaces.browser import IBrowserPresentation
from zope.exceptions import NotFoundError

from zope.app.services.pluggableauth import BTreePrincipalSource, \
     SimplePrincipal, PluggableAuthenticationService, \
     PrincipalAuthenticationView

from zope.publisher.browser import TestRequest as Request

import base64


class AuthServiceTest(placefulsetup.PlacefulSetup, TestCase):

    def setUp(self):
        from zope.component.view import viewService
        from zope.app.interfaces.services.pluggableauth import IPrincipalSource
        placefulsetup.PlacefulSetup.setUp(self)
        from zope.component import getService
        from zope.app.security.basicauthadapter import BasicAuthAdapter
        from zope.app.interfaces.security import ILoginPassword
        getService(None, Adapters).provideAdapter(
            IHTTPCredentials, ILoginPassword, BasicAuthAdapter)

        viewService.provideView(IPrincipalSource, "login",
                                IBrowserPresentation,
                                (PrincipalAuthenticationView,))

        self.buildFolders()
        sm = placefulsetup.createServiceManager(self.rootFolder)
        placefulsetup.addService(sm, "Authentication",
                                 PluggableAuthenticationService())

        folder = self.rootFolder

        if not folder.hasServiceManager():
            self.createServiceManager(folder)

        default = traverse(folder, '++etc++site/default')
        key = default.setObject("AuthenticationService",
                                PluggableAuthenticationService())
        auth = traverse(default, key)

        path = getPath(auth)

        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.setObject('slinkp', self._slinkp)
        self._one.setObject('chrism', self._chrism)
        self._two.setObject('slinkp2', self._slinkp2)
        self._two.setObject('chrism2', self._chrism2)


    def testGetPrincipal(self):
        auth = self._auth
        self.assertEqual(self._slinkp,
            auth.getPrincipal((auth.earmark, 'one', 'slinkp')))

    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)
        
    def testAuthenticate(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 testGetPrincipal(self):
##         auth = self._auth
##         self.assertEqual(auth['srichter'], auth.getPrincipal('srichter'))
##         self.assertRaises(NotFoundError, auth.getPrincipal, 'srichter2')

    def testGetPrincipals(self):
        auth = self._auth
        self.failUnless(self._slinkp in auth.getPrincipals('slinkp'))
        self.failUnless(self._slinkp2 in auth.getPrincipals('slinkp'))

    def _fail(self):
        self._auth.getPrincipal((self._auth.earmark, None, None))
        
    def testGetPrincipalId(self):
        auth = self._auth
        self.assertRaises(NotFoundError, self._fail)
        p = auth.getPrincipal((auth.earmark, "one", self._slinkp.getId()))
        self.assertEqual(p, self._slinkp)
                         
def test_suite():
    t1 = makeSuite(AuthServiceTest)
    return TestSuite((t1,))

if __name__=='__main__':
    main(defaultTest='test_suite')